1414 */
1515#include " controller.h"
1616
17+ /* **********************************************************************/
18+ /* !
19+ @brief AnalogIO controller constructor
20+ */
21+ /* **********************************************************************/
1722AnalogIOController::AnalogIOController () {
1823 _analogio_hardware = new AnalogIOHardware ();
1924 _analogio_model = new AnalogIOModel ();
2025 _analogio_hardware->SetResolution (16 ); // Default to 16-bit resolution
2126 SetRefVoltage (3.3 ); // Default to 3.3V
2227}
2328
24- AnalogIOController::~AnalogIOController () {}
29+ /* **********************************************************************/
30+ /* !
31+ @brief AnalogIO controller destructor
32+ */
33+ /* **********************************************************************/
34+ AnalogIOController::~AnalogIOController () {
35+ delete _analogio_hardware;
36+ delete _analogio_model;
37+ }
2538
39+ /* **********************************************************************/
40+ /* !
41+ @brief Set the reference voltage for the analog pins
42+ @param voltage
43+ The reference voltage.
44+ */
45+ /* **********************************************************************/
2646void AnalogIOController::SetRefVoltage (float voltage) {
2747 // To set the reference voltage, we call into the hardware
2848 _analogio_hardware->SetReferenceVoltage (voltage);
2949}
3050
51+ /* **********************************************************************/
52+ /* !
53+ @brief Set the total number of analog pins present on the hardware.
54+ @param total_pins
55+ The hardware's total number of analog pins.
56+ */
57+ /* **********************************************************************/
3158void AnalogIOController::SetTotalAnalogPins (uint8_t total_pins) {
3259 _total_analogio_pins = total_pins;
3360}
3461
62+ /* **********************************************************************/
63+ /* !
64+ @brief Handles an AnalogIOAdd message from the broker and adds a
65+ new analog pin to the controller.
66+ @param stream
67+ The nanopb input stream.
68+ @return True if the pin was successfully added, False otherwise.
69+ */
70+ /* **********************************************************************/
3571bool AnalogIOController::Handle_AnalogIOAdd (pb_istream_t *stream) {
3672 // Attempt to decode the incoming message into an AnalogIOAdd object
3773 if (!_analogio_model->DecodeAnalogIOAdd (stream)) {
@@ -60,6 +96,15 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
6096 return true ;
6197}
6298
99+ /* **************************************************************************/
100+ /* !
101+ @brief Handles an AnalogIORemove message from the broker and removes
102+ the requested analog pin from the controller.
103+ @param stream
104+ The nanopb input stream.
105+ @return True if the pin was successfully removed, False otherwise.
106+ */
107+ /* **************************************************************************/
63108bool AnalogIOController::Handle_AnalogIORemove (pb_istream_t *stream) {
64109 // Attempt to decode the incoming message into an AnalogIORemove object
65110 if (!_analogio_model->DecodeAnalogIORemove (stream)) {
@@ -84,10 +129,32 @@ bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
84129 return true ;
85130}
86131
132+ /* **************************************************************************/
133+ /* !
134+ @brief Checks if a pin's periodic timer has expired.
135+ @param pin
136+ The requested pin to check.
137+ @param cur_time
138+ The current time (called from millis()).
139+ @return True if the pin's period has expired, False otherwise.
140+ */
141+ /* **************************************************************************/
87142bool AnalogIOController::IsPinTimerExpired (analogioPin *pin, ulong cur_time) {
88143 return cur_time - pin->prv_period > pin->period ;
89144}
90145
146+ /* **************************************************************************/
147+ /* !
148+ @brief Encodes and publishes an AnalogIOEvent message to the broker.
149+ @param pin
150+ The pin to encode and publish.
151+ @param value
152+ The pin's value.
153+ @param read_type
154+ The type of read to perform on the pin.
155+ @return True if the message was successfully encoded and published.
156+ */
157+ /* **************************************************************************/
91158bool AnalogIOController::EncodePublishPinEvent (
92159 uint8_t pin, float value, wippersnapper_sensor_SensorType read_type) {
93160 char c_pin_name[12 ];
@@ -121,16 +188,43 @@ bool AnalogIOController::EncodePublishPinEvent(
121188 return true ;
122189}
123190
191+ /* **************************************************************************/
192+ /* !
193+ @brief Encodes and publishes an AnalogIOEvent message to the broker.
194+ @param pin
195+ The requested pin.
196+ @param value
197+ The pin's value.
198+ @return True if the message was successfully encoded and published,
199+ False othewise.
200+ */
201+ /* **************************************************************************/
124202bool AnalogIOController::EncodePublishPinValue (uint8_t pin, uint16_t value) {
125203 return EncodePublishPinEvent (pin, (float )value,
126204 wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW);
127205}
128206
207+ /* **************************************************************************/
208+ /* !
209+ @brief Encodes and publishes an AnalogIOEvent message to the broker.
210+ @param pin
211+ The requested pin.
212+ @param value
213+ The pin's value, as a voltage.
214+ @return True if the message was successfully encoded and published,
215+ False othewise.
216+ */
217+ /* **************************************************************************/
129218bool AnalogIOController::EncodePublishPinVoltage (uint8_t pin, float value) {
130219 return EncodePublishPinEvent (
131220 pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE);
132221}
133222
223+ /* **************************************************************************/
224+ /* !
225+ @brief Update/polling loop for the AnalogIO controller.
226+ */
227+ /* **************************************************************************/
134228void AnalogIOController::update () {
135229 // Bail-out if the vector is empty
136230 if (_analogio_pins.empty ())
@@ -159,7 +253,7 @@ void AnalogIOController::update() {
159253 // Encode and publish the voltage value to the broker
160254 EncodePublishPinVoltage (pin.name , pin_value);
161255 } else {
162- WS_DEBUG_PRINTLN (" ERROR: Invalid read mode for analog pin!" );
256+ WS_DEBUG_PRINTLN (" ERROR: Invalid read mode for analog pin!" );
163257 }
164258 }
165259}
0 commit comments