@@ -116,7 +116,7 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
116116 releaseStatusLED ();
117117
118118 // Deinit the pin if it's already in use
119- if (GetDigitalOutputPinsIdx (pin_name) != -1 )
119+ if (GetPinIdx (pin_name) != -1 )
120120 _dio_hardware->deinit (pin_name);
121121
122122 // Attempt to configure the pin
@@ -144,7 +144,7 @@ bool DigitalIOController::AddDigitalIOPin(pb_istream_t *stream) {
144144 @return The index of the digital output pin.
145145*/
146146/* **********************************************************************/
147- int DigitalIOController::GetDigitalOutputPinsIdx (uint8_t pin_name) {
147+ int DigitalIOController::GetPinIdx (uint8_t pin_name) {
148148 for (int i = 0 ; i < _digital_io_pins.size (); i++) {
149149 if (_digital_io_pins[i].pin_name == pin_name) {
150150 return i;
@@ -169,7 +169,7 @@ bool DigitalIOController::WriteDigitalIOPin(pb_istream_t *stream) {
169169 }
170170
171171 // Get the digital pin
172- int pin_idx = GetDigitalOutputPinsIdx (
172+ int pin_idx = GetPinIdx (
173173 atoi (_dio_model->GetDigitalIOWriteMsg ()->pin_name + 1 ));
174174 // Check if the pin was found and is a valid digital output pin
175175 if (pin_idx == -1 ) {
@@ -221,6 +221,20 @@ bool DigitalIOController::IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time) {
221221 return cur_time - pin->prv_pin_time > pin->pin_period ;
222222}
223223
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+
224238/* **********************************************************************/
225239/* !
226240 @brief Check if a pin's timer has expired
@@ -231,18 +245,15 @@ bool DigitalIOController::IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time) {
231245/* **********************************************************************/
232246bool DigitalIOController::CheckTimerPin (DigitalIOPin *pin) {
233247 ulong cur_time = millis ();
234-
248+ // Bail out if the pin's timer has not expired
235249 if (!IsPinTimerExpired (pin, cur_time))
236250 return false ;
237251
238252 // Fill in the pin's current time and value
239253 pin->prv_pin_time = cur_time;
240254 pin->pin_value = _dio_hardware->GetValue (pin->pin_name );
241255
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);
246257 return true ;
247258}
248259
@@ -260,14 +271,10 @@ bool DigitalIOController::CheckEventPin(DigitalIOPin *pin) {
260271 // Bail out if the pin value hasn't changed
261272 if (pin->pin_value == pin->prv_pin_value )
262273 return false ;
263-
264274 // Update the pin's previous value to the current value
265275 pin->prv_pin_value = pin->pin_value ;
266276
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);
271278
272279 return true ;
273280}
@@ -309,7 +316,8 @@ bool DigitalIOController::EncodePublishPinEvent(uint8_t pin_name,
309316
310317/* **********************************************************************/
311318/* !
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.
313321*/
314322/* **********************************************************************/
315323void DigitalIOController::Update () {
@@ -325,7 +333,6 @@ void DigitalIOController::Update() {
325333 wippersnapper_digitalio_DigitalIODirection_DIGITAL_IO_DIRECTION_OUTPUT)
326334 continue ;
327335
328- // TODO: Use Event sample mode first, its more common!!!
329336 if (pin.sample_mode ==
330337 wippersnapper_digitalio_DigitalIOSampleMode_DIGITAL_IO_SAMPLE_MODE_EVENT) {
331338 // Check if the pin value has changed
0 commit comments