Skip to content

Commit 09bfdcf

Browse files
committed
GPS - refactor away from driver approoach causing circular dependency
1 parent 57a4396 commit 09bfdcf

File tree

4 files changed

+57
-132
lines changed

4 files changed

+57
-132
lines changed

src/Wippersnapper_demo.ino.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 1 "/var/folders/ff/dmzflvf52tq9kzvt6g8jglxw0000gn/T/tmp5as28qi5"
2+
#include <Arduino.h>
3+
# 1 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
4+
# 11 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
5+
#include "ws_adapters.h"
6+
ws_adapter_wifi wipper;
7+
8+
9+
#define WS_DEBUG
10+
void setup();
11+
void loop();
12+
#line 17 "/Users/brentrubell/Documents/Arduino/libraries/Adafruit_Wippersnapper_Arduino/src/Wippersnapper_demo.ino"
13+
void setup() {
14+
Serial.begin(115200);
15+
while (!Serial)
16+
delay(10);
17+
wipper.provision();
18+
wipper.connect();
19+
}
20+
21+
void loop() { wipper.run(); }

src/components/uart/controller.cpp

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
6969

7070
// Create a new UartDevice "driver" on the hardware layer (UARTHardware)
7171
drvUartBase *uart_driver = nullptr;
72+
GPSController *drv_uart_gps = nullptr;
73+
bool is_gps_drv = false;
7274
wippersnapper_uart_UartDeviceConfig cfg_device = add_msg->cfg_device;
7375
switch (cfg_device.device_type) {
7476
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_UNSPECIFIED:
@@ -101,17 +103,11 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
101103
return false;
102104
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS:
103105
WS_DEBUG_PRINTLN("[uart] GPS device type not implemented!");
104-
uart_driver = new drvUartGps(uart_hardware->GetHardwareSerial(),
105-
cfg_device.device_id, cfg_serial.uart_nbr);
106-
// todo: set events, set period, configure gps via messages functionality
107-
// etc
108-
if (!uart_driver->begin()) {
109-
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to initialize GPS driver!");
110-
delete uart_driver;
111-
delete uart_hardware;
112-
return false;
113-
}
106+
// Create a new GPS driver instance
107+
drv_uart_gps = new GPSController();
108+
drv_uart_gps->SetInterface(uart_hardware->GetHardwareSerial());
114109
WS_DEBUG_PRINTLN("[uart] GPS driver initialized successfully!");
110+
is_gps_drv = true; // mark as GPS driver
115111
break;
116112
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI:
117113
WS_DEBUG_PRINTLN("[uart] Adding PM2.5 AQI device..");
@@ -137,46 +133,54 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
137133
}
138134

139135
// Attempt to initialize the UART driver
140-
if (uart_driver == nullptr) {
141-
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to create UART driver!");
142-
delete uart_hardware; // cleanup
143-
return false;
144-
}
145-
146136
bool did_begin = false;
147-
did_begin = uart_driver->begin();
148-
if (!did_begin) {
149-
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to initialize UART driver!");
150-
delete uart_driver; // cleanup
137+
WS_DEBUG_PRINTLN("[uart] Initializing UART driver...");
138+
if (!is_gps_drv) {
139+
WS_DEBUG_PRINTLN("[uart] STD UART drv...");
140+
did_begin = uart_driver->begin();
141+
if (did_begin) {
142+
WS_DEBUG_PRINTLN("[uart] UART driver initialized successfully!");
143+
_uart_drivers.push_back(uart_driver);
144+
} else {
145+
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to initialize UART driver!");
146+
delete uart_driver; // cleanup
147+
return false;
148+
}
151149
} else {
152-
WS_DEBUG_PRINTLN("[uart] UART driver initialized successfully!");
153-
_uart_drivers.push_back(uart_driver);
154-
}
155-
156-
// Check if we have a valid driver
157-
if (uart_driver == nullptr) {
158-
WS_DEBUG_PRINTLN("[uart] ERROR: No UART driver was created!");
159-
delete uart_hardware; // cleanup
160-
return false;
150+
// If a GPS driver, initialize the GPS controller
151+
WS_DEBUG_PRINTLN("[uart] GPS drv...");
152+
did_begin = drv_uart_gps->begin();
153+
if (did_begin) {
154+
WS_DEBUG_PRINTLN("[uart] GPS controller initialized successfully!");
155+
_uart_drivers_gps.push_back(drv_uart_gps);
156+
} else {
157+
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to initialize GPS controller!");
158+
delete drv_uart_gps; // cleanup
159+
return false;
160+
}
161161
}
162162

163163
// Are we in offline mode?
164164
if (WsV2._sdCardV2->isModeOffline())
165165
return true; // Don't publish to IO in offline mode
166166

167167
// Encode and publish out to Adafruit IO
168+
WS_DEBUG_PRINTLN("[uart] Encoding UartAdded message...");
168169
if (!_uart_model->EncodeUartAdded(uart_hardware->GetBusNumber(),
169170
cfg_device.device_type,
170171
cfg_device.device_id, did_begin)) {
171172
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to encode UartAdded message!");
172173
return false;
173174
}
175+
WS_DEBUG_PRINTLN("[uart] UartAdded message encoded successfully!");
174176

177+
WS_DEBUG_PRINTLN("[uart] Publishing UartAdded message to IO...");
175178
if (!WsV2.PublishSignal(wippersnapper_signal_DeviceToBroker_uart_added_tag,
176179
_uart_model->GetUartAddedMsg())) {
177180
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish UartAdded message to IO!");
178181
return false;
179182
}
183+
WS_DEBUG_PRINTLN("[uart] UartAdded message published successfully!");
180184

181185
return true;
182186
}
@@ -243,6 +247,7 @@ bool UARTController::Handle_UartWrite(pb_istream_t *stream) {
243247
void UARTController::update() {
244248
if (_uart_drivers.empty())
245249
return; // bail-out
250+
WS_DEBUG_PRINTLN("[uart] Polling UART drivers for events...");
246251

247252
for (drvUartBase *drv : _uart_drivers) {
248253

src/components/uart/controller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
#include "drivers/drvUartBase.h"
2323
#include "drivers/drvUartPm25.h"
2424
#include "drivers/drvUartUs100.h"
25-
#include "drivers/drvUartGps.h"
2625

2726
class Wippersnapper_V2; ///< Forward declaration
28-
class GPSController;
2927
class UARTModel; ///< Forward declaration
3028
class UARTHardware; ///< Forward declaration
29+
class GPSController; ///< Forward declaration
3130

3231
/*!
3332
@brief Routes messages between the uart.proto API and the hardware.
@@ -49,6 +48,7 @@ class UARTController {
4948
_uart_ports; ///< Vector of UART hardware instances
5049
std::vector<drvUartBase *>
5150
_uart_drivers; ///< Vector of UART device drivers (eg: PM2.5, etc.)
51+
std::vector<GPSController *> _uart_drivers_gps; ///< Vector of GPS device drivers (eg: Adafruit GPS, etc.)
5252
};
5353
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
5454
#endif // WS_UART_CONTROLLER_H

src/components/uart/drivers/drvUartGps.h

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)