14
14
*/
15
15
#include " controller.h"
16
16
17
+ /* **********************************************************************/
18
+ /* !
19
+ @brief AnalogIO controller constructor
20
+ */
21
+ /* **********************************************************************/
17
22
AnalogIOController::AnalogIOController () {
18
23
_analogio_hardware = new AnalogIOHardware ();
19
24
_analogio_model = new AnalogIOModel ();
20
25
_analogio_hardware->SetResolution (16 ); // Default to 16-bit resolution
21
26
SetRefVoltage (3.3 ); // Default to 3.3V
22
27
}
23
28
24
- AnalogIOController::~AnalogIOController () {}
29
+ /* **********************************************************************/
30
+ /* !
31
+ @brief AnalogIO controller destructor
32
+ */
33
+ /* **********************************************************************/
34
+ AnalogIOController::~AnalogIOController () {
35
+ delete _analogio_hardware;
36
+ delete _analogio_model;
37
+ }
25
38
39
+ /* **********************************************************************/
40
+ /* !
41
+ @brief Set the reference voltage for the analog pins
42
+ @param voltage
43
+ The reference voltage.
44
+ */
45
+ /* **********************************************************************/
26
46
void AnalogIOController::SetRefVoltage (float voltage) {
27
47
// To set the reference voltage, we call into the hardware
28
48
_analogio_hardware->SetReferenceVoltage (voltage);
29
49
}
30
50
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
+ /* **********************************************************************/
31
58
void AnalogIOController::SetTotalAnalogPins (uint8_t total_pins) {
32
59
_total_analogio_pins = total_pins;
33
60
}
34
61
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
+ /* **********************************************************************/
35
71
bool AnalogIOController::Handle_AnalogIOAdd (pb_istream_t *stream) {
36
72
// Attempt to decode the incoming message into an AnalogIOAdd object
37
73
if (!_analogio_model->DecodeAnalogIOAdd (stream)) {
@@ -60,6 +96,15 @@ bool AnalogIOController::Handle_AnalogIOAdd(pb_istream_t *stream) {
60
96
return true ;
61
97
}
62
98
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
+ /* **************************************************************************/
63
108
bool AnalogIOController::Handle_AnalogIORemove (pb_istream_t *stream) {
64
109
// Attempt to decode the incoming message into an AnalogIORemove object
65
110
if (!_analogio_model->DecodeAnalogIORemove (stream)) {
@@ -84,10 +129,32 @@ bool AnalogIOController::Handle_AnalogIORemove(pb_istream_t *stream) {
84
129
return true ;
85
130
}
86
131
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
+ /* **************************************************************************/
87
142
bool AnalogIOController::IsPinTimerExpired (analogioPin *pin, ulong cur_time) {
88
143
return cur_time - pin->prv_period > pin->period ;
89
144
}
90
145
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
+ /* **************************************************************************/
91
158
bool AnalogIOController::EncodePublishPinEvent (
92
159
uint8_t pin, float value, wippersnapper_sensor_SensorType read_type) {
93
160
char c_pin_name[12 ];
@@ -121,16 +188,43 @@ bool AnalogIOController::EncodePublishPinEvent(
121
188
return true ;
122
189
}
123
190
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
+ /* **************************************************************************/
124
202
bool AnalogIOController::EncodePublishPinValue (uint8_t pin, uint16_t value) {
125
203
return EncodePublishPinEvent (pin, (float )value,
126
204
wippersnapper_sensor_SensorType_SENSOR_TYPE_RAW);
127
205
}
128
206
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
+ /* **************************************************************************/
129
218
bool AnalogIOController::EncodePublishPinVoltage (uint8_t pin, float value) {
130
219
return EncodePublishPinEvent (
131
220
pin, value, wippersnapper_sensor_SensorType_SENSOR_TYPE_VOLTAGE);
132
221
}
133
222
223
+ /* **************************************************************************/
224
+ /* !
225
+ @brief Update/polling loop for the AnalogIO controller.
226
+ */
227
+ /* **************************************************************************/
134
228
void AnalogIOController::update () {
135
229
// Bail-out if the vector is empty
136
230
if (_analogio_pins.empty ())
@@ -159,7 +253,7 @@ void AnalogIOController::update() {
159
253
// Encode and publish the voltage value to the broker
160
254
EncodePublishPinVoltage (pin.name , pin_value);
161
255
} else {
162
- WS_DEBUG_PRINTLN (" ERROR: Invalid read mode for analog pin!" );
256
+ WS_DEBUG_PRINTLN (" ERROR: Invalid read mode for analog pin!" );
163
257
}
164
258
}
165
259
}
0 commit comments