Skip to content

Commit 0675abc

Browse files
committed
🚧 WIP, digitalio - Refactoring in update()
1 parent fcc6914 commit 0675abc

File tree

3 files changed

+80
-55
lines changed

3 files changed

+80
-55
lines changed

src/Wippersnapper_demo.ino.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmpp15xkt4i"
2+
#include <Arduino.h>
3+
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
4+
# 16 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
5+
#include "ws_manager.h"
6+
7+
8+
Wippersnapper_Manager manager;
9+
10+
11+
#define WS_DEBUG
12+
13+
14+
#define API_PIN 0
15+
void setup();
16+
void loop();
17+
#line 27 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
18+
void setup() {
19+
20+
manager.checkAPIVersion(API_PIN);
21+
manager.provision();
22+
23+
Serial.begin(115200);
24+
while(!Serial) {;}
25+
Serial.println("Adafruit Wippersnapper API Manager Demo");
26+
Serial.print("Running Wippersnapper API Version: ");
27+
Serial.println(manager.getAPIVersion());
28+
manager.connect();
29+
30+
}
31+
32+
void loop() {
33+
manager.run();
34+
}

src/components/digitalIO/controller.cpp

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,30 @@ bool DigitalIOController::CheckEventPin(DigitalIOPin *pin) {
190190
return true;
191191
}
192192

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+
}
196217

197218
void DigitalIOController::Update() {
198219
// Bail out if we have no digital pins to poll
@@ -207,63 +228,31 @@ void DigitalIOController::Update() {
207228
wippersnapper_digitalio_DigitalIODirection_DIGITAL_IO_DIRECTION_OUTPUT)
208229
continue;
209230

210-
// TODO: Use Event sample mode first, its more common
231+
// TODO: Use Event sample mode first, its more common!!!
211232
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
235241
} else if (
236242
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
264251
} else {
265252
// 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!");
267256
}
268257
}
269258
}

src/components/digitalIO/controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class DigitalIOController {
4040
bool WriteDigitalIOPin(pb_istream_t *stream);
4141

4242
void Update();
43+
bool EncodePublishPinEvent(uint8_t pin_name, bool pin_value);
44+
4345
bool CheckTimerPin(DigitalIOPin *pin);
4446
bool IsPinTimerExpired(DigitalIOPin *pin, ulong cur_time);
4547
bool CheckEventPin(DigitalIOPin *pin);

0 commit comments

Comments
 (0)