@@ -116,7 +116,7 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
116
116
releaseStatusLED ();
117
117
118
118
// Deinit the pin if it's already in use
119
- if (GetDigitalOutputPinsIdx (pin_name) != -1 )
119
+ if (GetPinIdx (pin_name) != -1 )
120
120
_dio_hardware->deinit (pin_name);
121
121
122
122
// Attempt to configure the pin
@@ -144,7 +144,7 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
144
144
@return The index of the digital output pin.
145
145
*/
146
146
/* **********************************************************************/
147
- int DigitalIOController::GetDigitalOutputPinsIdx (uint8_t pin_name) {
147
+ int DigitalIOController::GetPinIdx (uint8_t pin_name) {
148
148
for (int i = 0 ; i < _digital_io_pins.size (); i++) {
149
149
if (_digital_io_pins[i].pin_name == pin_name) {
150
150
return i;
@@ -169,7 +169,7 @@ bool DigitalIOController::WriteDigitalIOPin(pb_istream_t *stream) {
169
169
}
170
170
171
171
// Get the digital pin
172
- int pin_idx = GetDigitalOutputPinsIdx (
172
+ int pin_idx = GetPinIdx (
173
173
atoi (_dio_model->GetDigitalIOWriteMsg ()->pin_name + 1 ));
174
174
// Check if the pin was found and is a valid digital output pin
175
175
if (pin_idx == -1 ) {
@@ -221,6 +221,20 @@ bool DigitalIOController::IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time) {
221
221
return cur_time - pin->prv_pin_time > pin->pin_period ;
222
222
}
223
223
224
+ /* **********************************************************************/
225
+ /* !
226
+ @brief Print a pin's ID and value
227
+ @param pin
228
+ The specified pin.
229
+ */
230
+ /* **********************************************************************/
231
+ void DigitalIOController::PrintPinValue (DigitalIOPin *pin) {
232
+ WS_DEBUG_PRINT (" DIO Pin D" );
233
+ WS_DEBUG_PRINT (pin->pin_name );
234
+ WS_DEBUG_PRINT (" | value: " );
235
+ WS_DEBUG_PRINTLN (pin->prv_pin_value );
236
+ }
237
+
224
238
/* **********************************************************************/
225
239
/* !
226
240
@brief Check if a pin's timer has expired
@@ -231,18 +245,15 @@ bool DigitalIOController::IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time) {
231
245
/* **********************************************************************/
232
246
bool DigitalIOController::CheckTimerPin (DigitalIOPin *pin) {
233
247
ulong cur_time = millis ();
234
-
248
+ // Bail out if the pin's timer has not expired
235
249
if (!IsPinTimerExpired (pin, cur_time))
236
250
return false ;
237
251
238
252
// Fill in the pin's current time and value
239
253
pin->prv_pin_time = cur_time;
240
254
pin->pin_value = _dio_hardware->GetValue (pin->pin_name );
241
255
242
- WS_DEBUG_PRINT (" DIO Pin D" );
243
- WS_DEBUG_PRINT (pin->pin_name );
244
- WS_DEBUG_PRINT (" | value: " );
245
- WS_DEBUG_PRINTLN (pin->prv_pin_value );
256
+ PrintPinValue (pin);
246
257
return true ;
247
258
}
248
259
@@ -260,14 +271,10 @@ bool DigitalIOController::CheckEventPin(DigitalIOPin *pin) {
260
271
// Bail out if the pin value hasn't changed
261
272
if (pin->pin_value == pin->prv_pin_value )
262
273
return false ;
263
-
264
274
// Update the pin's previous value to the current value
265
275
pin->prv_pin_value = pin->pin_value ;
266
276
267
- WS_DEBUG_PRINT (" DIO Pin D" );
268
- WS_DEBUG_PRINT (pin->pin_name );
269
- WS_DEBUG_PRINT (" value changed to: " );
270
- WS_DEBUG_PRINTLN (pin->pin_value );
277
+ PrintPinValue (pin);
271
278
272
279
return true ;
273
280
}
@@ -309,7 +316,8 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
309
316
310
317
/* **********************************************************************/
311
318
/* !
312
- @brief Updates the digital_io_pins array.
319
+ @brief Iterates through the digital pins and updates their values
320
+ (if necessary) and publishes the event to the broker.
313
321
*/
314
322
/* **********************************************************************/
315
323
void DigitalIOController::Update () {
@@ -325,7 +333,6 @@ void DigitalIOController::Update() {
325
333
wippersnapper_digitalio_DigitalIODirection_DIGITAL_IO_DIRECTION_OUTPUT)
326
334
continue ;
327
335
328
- // TODO: Use Event sample mode first, its more common!!!
329
336
if (pin.sample_mode ==
330
337
wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_EVENT) {
331
338
// Check if the pin value has changed
0 commit comments