|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | #pragma once |
| 16 | + |
16 | 17 | #include <array> |
17 | | -#include <libhal-util/map.hpp> |
18 | | -#include <libhal-util/math.hpp> |
| 18 | +#include <span> |
| 19 | + |
19 | 20 | #include <libhal/adc.hpp> |
20 | | -#include <libhal/error.hpp> |
21 | 21 | #include <libhal/output_pin.hpp> |
22 | 22 | #include <libhal/steady_clock.hpp> |
23 | | -#include <span> |
24 | | - |
25 | | -using namespace hal::literals; |
26 | | -using namespace std::chrono_literals; |
27 | 23 |
|
28 | 24 | namespace hal { |
29 | 25 |
|
30 | | -// This function does not take into account setup and hold times. |
31 | | -hal::status set_mux_channel(uint32_t p_position, |
32 | | - std::span<hal::output_pin*> p_signal_pins) |
33 | | -{ |
34 | | - |
35 | | - for (int i = 0; i < static_cast<int>(p_signal_pins.size()); i++) { |
36 | | - |
37 | | - bool value = bool(p_position & (1 << i)); |
38 | | - HAL_CHECK(p_signal_pins[i]->level(value)); |
39 | | - } |
40 | | - return hal::success(); |
41 | | -} |
42 | | - |
43 | 26 | /** |
44 | | - * @brief A driver for Analog to Digital mulitplexers, creates adc pin objects |
45 | | - * for each mux output |
| 27 | + * @brief A driver for an ADC multiplexer that manages and reads ADC mux pins. |
| 28 | + * This driver is intended to be used with multiplexers that use digital |
| 29 | + * signals. An ADC multiplexer can be used to expand the number of input |
| 30 | + * channels of an ADC. |
46 | 31 | */ |
47 | 32 | class adc_multiplexer |
48 | 33 | { |
49 | 34 | public: |
50 | 35 | /** |
51 | | - * @brief An internal class of a hal ADC implimentation to represent a |
52 | | - * multiplexer pin |
| 36 | + * @brief Constructs a new adc_multiplexer object. |
| 37 | + * |
| 38 | + * @param p_signal_pins A span of the output signal pins used to determine the |
| 39 | + * channel on the mux. |
| 40 | + * @param p_source_pin The output adc pin of the multiplexer. |
| 41 | + * @param p_clock A steady clock used for delaying 500ns to give time to the |
| 42 | + * mux to have an updated signal. |
| 43 | + * @return The constructed adc_multiplexer. |
53 | 44 | */ |
54 | | - class adc_mux_pin : public hal::adc |
55 | | - { |
56 | | - public: |
57 | | - /** @brief An internal constructor to build an adc mux pin. |
58 | | - * @param p_mux_port the channel port of the pin on the mux itself. |
59 | | - * @param p_mux A pointer to the multiplexer. |
60 | | - * */ |
61 | | - adc_mux_pin(uint8_t p_mux_port, adc_multiplexer* p_mux) |
62 | | - : m_mux_port{ p_mux_port } |
63 | | - , m_mux{ p_mux } {}; // TODO add m_mux |
64 | | - |
65 | | - /** |
66 | | - * @brief Reads the pin. |
67 | | - */ |
68 | | - hal::result<read_t> driver_read() override |
69 | | - { |
70 | | - return m_mux->read_channel(m_mux_port); |
71 | | - } |
72 | | - |
73 | | - private: |
74 | | - const uint8_t m_mux_port; |
75 | | - adc_multiplexer* m_mux; |
76 | | - }; |
| 45 | + static adc_multiplexer create(std::span<hal::output_pin*> p_signal_pins, |
| 46 | + hal::adc& p_source_pin, |
| 47 | + hal::steady_clock& p_clock); |
77 | 48 |
|
78 | 49 | /** |
79 | | - * @param p_signal_pins A span of output pins to represent the signal pins of |
80 | | - * the mux, assuming the mux signals digitally. |
81 | | - * @param p_source_pin The adc source pin that reads the output of the mux. |
| 50 | + * @brief Reads a channel on the mux. |
| 51 | + * |
| 52 | + * @param p_mux_port The port to be read. If an out of bounds port number is |
| 53 | + * passed, an error-typed result is returned. |
| 54 | + * @return The hal::adc::read_t struct of the read value or an error if an |
| 55 | + * invalid port is given. |
82 | 56 | */ |
83 | | - adc_multiplexer(std::span<hal::output_pin*> p_signal_pins, |
84 | | - hal::adc* p_source_pin) |
85 | | - : m_signal_pins{ p_signal_pins } |
86 | | - , m_source_pin{ p_source_pin } {}; |
| 57 | + hal::result<hal::adc::read_t> read_channel(std::uint16_t p_mux_port); |
87 | 58 |
|
88 | 59 | /** |
89 | | - * @brief Returns a multiplexer ADC pin |
90 | | - * @param p_channel The channel number of the pin. |
| 60 | + * @brief Gets the highest capacity channel held by the ADC mux object. |
| 61 | + * This is caluclated based off of how many source pins are available. |
| 62 | + * |
| 63 | + * @return The maximum channel number for this mux (2^n states, where n is |
| 64 | + * number of source pins). |
91 | 65 | */ |
92 | | - hal::result<adc_mux_pin> pin_factory(uint8_t p_channel) |
93 | | - { |
94 | | - // TODO: Ask if we should keep track of channels in use, if a port is asked |
95 | | - // for again, disallow it. |
96 | | - const std::uint32_t max_size = 1 << m_signal_pins.size(); |
97 | | - if (p_channel >= max_size) { |
98 | | - return hal::new_error( |
99 | | - "Unable to add any more pins to this multiplexer.\n"); |
100 | | - } |
101 | | - |
102 | | - return adc_mux_pin(p_channel, this); |
103 | | - } |
| 66 | + int get_max_channel(); |
104 | 67 |
|
105 | 68 | private: |
106 | | - /** |
107 | | - * @brief Reads the channel |
108 | | - * |
109 | | - * @param p_mux_port |
110 | | - * @return hal::result<hal::adc::read_t> |
111 | | - */ |
112 | | - hal::result<hal::adc::read_t> read_channel(uint8_t p_mux_port) |
113 | | - { |
114 | | - set_mux_channel(p_mux_port, m_signal_pins); |
115 | | - return HAL_CHECK(m_source_pin->read()); |
116 | | - } |
| 69 | + adc_multiplexer(std::span<output_pin*> p_signal_pins, |
| 70 | + hal::adc& p_source_pin, |
| 71 | + hal::steady_clock& p_clock); |
117 | 72 |
|
118 | 73 | private: |
119 | | - std::span<hal::output_pin*> m_signal_pins; |
| 74 | + std::span<output_pin*> m_signal_pins; |
120 | 75 | hal::adc* m_source_pin; |
| 76 | + hal::steady_clock* m_clock; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief A class that represents a multiplexer pin for ADC. |
| 81 | + */ |
| 82 | +class adc_mux_pin : public hal::adc |
| 83 | +{ |
| 84 | + friend result<adc_mux_pin> make_adc(adc_multiplexer& p_multiplexer, |
| 85 | + std::uint8_t p_channel); |
| 86 | + |
| 87 | +private: |
| 88 | + adc_mux_pin(adc_multiplexer& p_mux, std::uint8_t p_mux_port); |
| 89 | + hal::result<read_t> driver_read() override; |
| 90 | + |
| 91 | + adc_multiplexer* m_mux; |
| 92 | + std::uint8_t m_mux_port; |
121 | 93 | }; |
122 | 94 |
|
| 95 | +/** |
| 96 | + * @brief Returns a multiplexer ADC pin. |
| 97 | + * |
| 98 | + * @param p_multiplexer The multiplexer object to manage each mux pin. |
| 99 | + * @param p_channel The channel number of the pin. |
| 100 | + * @return A newly constructed ADC multiplexer pin. |
| 101 | + */ |
| 102 | +result<adc_mux_pin> make_adc(adc_multiplexer& p_multiplexer, |
| 103 | + std::uint8_t p_channel); |
| 104 | + |
123 | 105 | } // namespace hal |
0 commit comments