@@ -190,9 +190,30 @@ bool DigitalIOController::CheckEventPin(DigitalIOPin *pin) {
190
190
return true ;
191
191
}
192
192
193
- // TODO: Maybe the partition increase is causing issues with the ESP32-S2
194
- // we are also using the larger partition sz with the S3 so it could cause
195
- // issues there too..
193
+ bool DigitalIOController::EncodePublishPinEvent (uint8_t pin_name,
194
+ bool pin_value) {
195
+ // Prefix pin_name with "D" to match the expected pin name format
196
+ char c_pin_name[12 ];
197
+ sprintf (c_pin_name, " D%d" , pin_name);
198
+
199
+ // Encode the DigitalIOEvent message
200
+ if (!_dio_model->EncodeDigitalIOEvent (c_pin_name, pin_value)) {
201
+ WS_DEBUG_PRINTLN (" ERROR: Unable to encode DigitalIOEvent message!" );
202
+ return false ;
203
+ }
204
+
205
+ // Publish the DigitalIOEvent message to the broker
206
+ if (!WsV2.PublishSignal (
207
+ wippersnapper_signal_DeviceToBroker_digitalio_event_tag,
208
+ _dio_model->GetDigitalIOEventMsg ())) {
209
+ WS_DEBUG_PRINTLN (" ERROR: Unable to publish digitalio event message, "
210
+ " moving onto the next pin!" );
211
+ return false ;
212
+ }
213
+ WS_DEBUG_PRINTLN (" Published DigitalIOEvent to broker!" )
214
+
215
+ return true ;
216
+ }
196
217
197
218
void DigitalIOController::Update () {
198
219
// Bail out if we have no digital pins to poll
@@ -207,63 +228,31 @@ void DigitalIOController::Update() {
207
228
wippersnapper_digitalio_DigitalIODirection_DIGITAL_IO_DIRECTION_OUTPUT)
208
229
continue ;
209
230
210
- // TODO: Use Event sample mode first, its more common
231
+ // TODO: Use Event sample mode first, its more common!!!
211
232
if (pin.sample_mode ==
212
- wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_TIMER) {
213
- if (!CheckTimerPin (&pin))
214
- continue ;
215
- // TODO: Move all the encode and publish code into a new func.
216
- char pin_name[12 ];
217
- sprintf (pin_name, " D%d" , pin.pin_name );
218
- // Encode the event and publish it to the broker
219
- WS_DEBUG_PRINT (" Encoding digitalio event for pin: " );
220
- WS_DEBUG_PRINTLN (pin_name);
221
- if (!_dio_model->EncodeDigitalIOEvent (pin_name, pin.pin_value )) {
222
- WS_DEBUG_PRINTLN (" ERROR: Unable to encode digitalio event message, "
223
- " moving onto the next pin!" );
224
- continue ;
225
- }
226
- WS_DEBUG_PRINTLN (" Encoded digitalio event message!" );
227
- WS_DEBUG_PRINTLN (" Publishing digitalio event message to broker..." );
228
- if (!WsV2.PublishSignal (
229
- wippersnapper_signal_DeviceToBroker_digitalio_event_tag,
230
- _dio_model->GetDigitalIOEventMsg ())) {
231
- WS_DEBUG_PRINTLN (" ERROR: Unable to publish digitalio event message, "
232
- " moving onto the next pin!" );
233
- continue ;
234
- }
233
+ wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_EVENT) {
234
+ // Check if the pin value has changed
235
+ if (!CheckEventPin (&pin))
236
+ continue ; // No change in pin value detected, move onto the next pin
237
+
238
+ // Encode and publish the event
239
+ if (!EncodePublishPinEvent (pin.pin_name , pin.pin_value ))
240
+ continue ; // Unable to encode and publish event, move onto the next pin
235
241
} else if (
236
242
pin.sample_mode ==
237
- wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_EVENT) {
238
- if (!CheckEventPin (&pin)) {
239
- WS_DEBUG_PRINTLN (" ERROR: Unable to check event pin, moving on.." );
240
- continue ;
241
- }
242
-
243
- // TODO: Move all the encode and publish code into a new func.
244
- char pin_name[12 ];
245
- sprintf (pin_name, " D%d" , pin.pin_name );
246
- // Encode the event and publish it to the broker
247
- WS_DEBUG_PRINT (" Encoding digitalio event for pin: " );
248
- WS_DEBUG_PRINTLN (pin_name);
249
- if (!_dio_model->EncodeDigitalIOEvent (pin_name, pin.pin_value )) {
250
- WS_DEBUG_PRINTLN (" ERROR: Unable to encode digitalio event message, "
251
- " moving onto the next pin!" );
252
- continue ;
253
- }
254
- WS_DEBUG_PRINTLN (" Encoded digitalio event message!" );
255
- WS_DEBUG_PRINTLN (" Publishing digitalio event message to broker..." );
256
- if (!WsV2.PublishSignal (
257
- wippersnapper_signal_DeviceToBroker_digitalio_event_tag,
258
- _dio_model->GetDigitalIOEventMsg ())) {
259
- WS_DEBUG_PRINTLN (" ERROR: Unable to publish digitalio event message, "
260
- " moving onto the next pin!" );
261
- continue ;
262
- }
263
- WS_DEBUG_PRINTLN (" Published digitalio event message!" );
243
+ wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_TIMER) {
244
+ // Check if the timer has expired
245
+ if (!CheckTimerPin (&pin))
246
+ continue ; // Timer has not expired yet, move onto the next pin
247
+
248
+ // Encode and publish the event
249
+ if (!EncodePublishPinEvent (pin.pin_name , pin.pin_value ))
250
+ continue ; // Failed to encode and publish event, move onto the next pin
264
251
} else {
265
252
// Invalid sample mode
266
- WS_DEBUG_PRINTLN (" ERROR: Invalid digital io pin sample mode!" );
253
+ WS_DEBUG_PRINT (" ERROR: DigitalIO Pin " );
254
+ WS_DEBUG_PRINT (pin.pin_name );
255
+ WS_DEBUG_PRINTLN (" contains an invalid sample mode!" );
267
256
}
268
257
}
269
258
}
0 commit comments