14
14
*/
15
15
#include " controller.h"
16
16
17
+ /* **********************************************************************/
18
+ /* !
19
+ @brief DigitalIOController constructor
20
+ */
21
+ /* **********************************************************************/
17
22
DigitalIOController::DigitalIOController () {
18
23
_dio_model = new DigitalIOModel ();
19
24
_dio_hardware = new DigitalIOHardware ();
@@ -22,11 +27,23 @@ DigitalIOController::DigitalIOController() {
22
27
SetMaxDigitalPins (0 );
23
28
}
24
29
30
+ /* **********************************************************************/
31
+ /* !
32
+ @brief DigitalIOController destructor
33
+ */
34
+ /* **********************************************************************/
25
35
DigitalIOController::~DigitalIOController () {
26
36
delete _dio_model;
27
37
delete _dio_hardware;
28
38
}
29
39
40
+ /* **********************************************************************/
41
+ /* !
42
+ @brief Set the maximum number of digital pins
43
+ @param max_digital_pins
44
+ The maximum number of digital pins
45
+ */
46
+ /* **********************************************************************/
30
47
void DigitalIOController::SetMaxDigitalPins (uint8_t max_digital_pins) {
31
48
_max_digital_pins = max_digital_pins;
32
49
}
@@ -39,6 +56,21 @@ bool DigitalIOController::IsStatusLEDPin(uint8_t pin_name) {
39
56
return false ;
40
57
}
41
58
59
+ /* **********************************************************************/
60
+ /* !
61
+ @brief Create a new digital pin and add it to the controller's vector
62
+ @param name
63
+ The pin's name.
64
+ @param direction
65
+ The pin's direction.
66
+ @param sample_mode
67
+ The pin's sample mode.
68
+ @param value
69
+ The pin's value.
70
+ @param period
71
+ The pin's period.
72
+ */
73
+ /* **********************************************************************/
42
74
void DigitalIOController::CreateDigitalIOPin (
43
75
uint8_t name, wippersnapper_digitalio_DigitalIODirection direction,
44
76
wippersnapper_digitalio_DigitalIOSampleMode sample_mode, bool value,
@@ -54,6 +86,14 @@ void DigitalIOController::CreateDigitalIOPin(
54
86
_digital_io_pins.push_back (new_pin);
55
87
}
56
88
89
+ /* **********************************************************************/
90
+ /* !
91
+ @brief Add a new digital pin to the controller
92
+ @param stream
93
+ The nanopb input stream.
94
+ @return True if the digital pin was successfully added.
95
+ */
96
+ /* **********************************************************************/
57
97
bool DigitalIOController::AddDigitalIOPin (pb_istream_t *stream) {
58
98
// Early-out if we have reached the maximum number of digital pins
59
99
if (_digital_io_pins.size () >= _max_digital_pins) {
@@ -96,6 +136,14 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
96
136
return true ;
97
137
}
98
138
139
+ /* **********************************************************************/
140
+ /* !
141
+ @brief Get the index of a digital output pin
142
+ @param pin_name
143
+ The pin's name.
144
+ @return The index of the digital output pin.
145
+ */
146
+ /* **********************************************************************/
99
147
int DigitalIOController::GetDigitalOutputPinsIdx (uint8_t pin_name) {
100
148
for (int i = 0 ; i < _digital_io_pins.size (); i++) {
101
149
if (_digital_io_pins[i].pin_name == pin_name) {
@@ -105,6 +153,14 @@ int DigitalIOController::GetDigitalOutputPinsIdx(uint8_t pin_name) {
105
153
return -1 ; // Pin not found
106
154
}
107
155
156
+ /* **********************************************************************/
157
+ /* !
158
+ @brief Write a digital pin
159
+ @param stream
160
+ The nanopb input stream.
161
+ @return True if the digital pin was successfully written.
162
+ */
163
+ /* **********************************************************************/
108
164
bool DigitalIOController::WriteDigitalIOPin (pb_istream_t *stream) {
109
165
// Attempt to decode the DigitalIOWrite message
110
166
if (!_dio_model->DecodeDigitalIOWrite (stream)) {
@@ -151,10 +207,28 @@ bool DigitalIOController::WriteDigitalIOPin(pb_istream_t *stream) {
151
207
return true ;
152
208
}
153
209
210
+ /* **********************************************************************/
211
+ /* !
212
+ @brief Check if a pin's timer has expired
213
+ @param pin
214
+ The pin to check.
215
+ @param cur_time
216
+ The current time.
217
+ @return True if the pin's timer has expired.
218
+ */
219
+ /* **********************************************************************/
154
220
bool DigitalIOController::IsPinTimerExpired (DigitalIOPin *pin, ulong cur_time) {
155
221
return cur_time - pin->prv_pin_time > pin->pin_period ;
156
222
}
157
223
224
+ /* **********************************************************************/
225
+ /* !
226
+ @brief Check if a pin's timer has expired
227
+ @param pin
228
+ The pin to check.
229
+ @return True if the pin's timer has expired.
230
+ */
231
+ /* **********************************************************************/
158
232
bool DigitalIOController::CheckTimerPin (DigitalIOPin *pin) {
159
233
ulong cur_time = millis ();
160
234
@@ -172,6 +246,14 @@ bool DigitalIOController::CheckTimerPin(DigitalIOPin *pin) {
172
246
return true ;
173
247
}
174
248
249
+ /* **********************************************************************/
250
+ /* !
251
+ @brief Check if a pin's value has changed
252
+ @param pin
253
+ The pin to check.
254
+ @return True if the pin's value has changed.
255
+ */
256
+ /* **********************************************************************/
175
257
bool DigitalIOController::CheckEventPin (DigitalIOPin *pin) {
176
258
// Get the pin's current value
177
259
pin->pin_value = _dio_hardware->GetValue (pin->pin_name );
@@ -190,6 +272,16 @@ bool DigitalIOController::CheckEventPin(DigitalIOPin *pin) {
190
272
return true ;
191
273
}
192
274
275
+ /* **********************************************************************/
276
+ /* !
277
+ @brief Encode and publish a pin event
278
+ @param pin_name
279
+ The pin's name.
280
+ @param pin_value
281
+ The pin's value.
282
+ @return True if the pin event was successfully encoded and published.
283
+ */
284
+ /* **********************************************************************/
193
285
bool DigitalIOController::EncodePublishPinEvent (uint8_t pin_name,
194
286
bool pin_value) {
195
287
// Prefix pin_name with "D" to match the expected pin name format
@@ -215,6 +307,11 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
215
307
return true ;
216
308
}
217
309
310
+ /* **********************************************************************/
311
+ /* !
312
+ @brief Updates the digital_io_pins array.
313
+ */
314
+ /* **********************************************************************/
218
315
void DigitalIOController::Update () {
219
316
// Bail out if we have no digital pins to poll
220
317
if (_digital_io_pins.empty ())
0 commit comments