@@ -69,6 +69,8 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
69
69
70
70
// Create a new UartDevice "driver" on the hardware layer (UARTHardware)
71
71
drvUartBase *uart_driver = nullptr ;
72
+ GPSController *drv_uart_gps = nullptr ;
73
+ bool is_gps_drv = false ;
72
74
wippersnapper_uart_UartDeviceConfig cfg_device = add_msg->cfg_device ;
73
75
switch (cfg_device.device_type ) {
74
76
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_UNSPECIFIED:
@@ -101,17 +103,11 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
101
103
return false ;
102
104
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS:
103
105
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 ());
114
109
WS_DEBUG_PRINTLN (" [uart] GPS driver initialized successfully!" );
110
+ is_gps_drv = true ; // mark as GPS driver
115
111
break ;
116
112
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI:
117
113
WS_DEBUG_PRINTLN (" [uart] Adding PM2.5 AQI device.." );
@@ -137,46 +133,54 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
137
133
}
138
134
139
135
// 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
-
146
136
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
+ }
151
149
} 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
+ }
161
161
}
162
162
163
163
// Are we in offline mode?
164
164
if (WsV2._sdCardV2 ->isModeOffline ())
165
165
return true ; // Don't publish to IO in offline mode
166
166
167
167
// Encode and publish out to Adafruit IO
168
+ WS_DEBUG_PRINTLN (" [uart] Encoding UartAdded message..." );
168
169
if (!_uart_model->EncodeUartAdded (uart_hardware->GetBusNumber (),
169
170
cfg_device.device_type ,
170
171
cfg_device.device_id , did_begin)) {
171
172
WS_DEBUG_PRINTLN (" [uart] ERROR: Failed to encode UartAdded message!" );
172
173
return false ;
173
174
}
175
+ WS_DEBUG_PRINTLN (" [uart] UartAdded message encoded successfully!" );
174
176
177
+ WS_DEBUG_PRINTLN (" [uart] Publishing UartAdded message to IO..." );
175
178
if (!WsV2.PublishSignal (wippersnapper_signal_DeviceToBroker_uart_added_tag,
176
179
_uart_model->GetUartAddedMsg ())) {
177
180
WS_DEBUG_PRINTLN (" [i2c] ERROR: Unable to publish UartAdded message to IO!" );
178
181
return false ;
179
182
}
183
+ WS_DEBUG_PRINTLN (" [uart] UartAdded message published successfully!" );
180
184
181
185
return true ;
182
186
}
@@ -243,6 +247,7 @@ bool UARTController::Handle_UartWrite(pb_istream_t *stream) {
243
247
void UARTController::update () {
244
248
if (_uart_drivers.empty ())
245
249
return ; // bail-out
250
+ WS_DEBUG_PRINTLN (" [uart] Polling UART drivers for events..." );
246
251
247
252
for (drvUartBase *drv : _uart_drivers) {
248
253
0 commit comments