Skip to content

Commit 57a4396

Browse files
committed
GPS -refactor to use hwserial pattern
1 parent b699c06 commit 57a4396

File tree

5 files changed

+32
-50
lines changed

5 files changed

+32
-50
lines changed

src/components/gps/controller.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,16 @@ GPSController::~GPSController() {
4343

4444
/*!
4545
* @brief Sets a UART hardware interface for the GPS controller.
46-
* @param uart_hardware Pointer to the UARTHardware instance.
46+
* @param serial
47+
* Pointer to a HardwareSerial instance.
4748
* @returns True if the interface was set successfully, False otherwise.
4849
*/
49-
bool GPSController::SetInterface(UARTHardware *uart_hardware) {
50-
if (uart_hardware == nullptr) {
51-
WS_DEBUG_PRINTLN("[gps] ERROR: Provided UART instance is undefined!");
50+
bool GPSController::SetInterface(HardwareSerial *serial) {
51+
if (serial == nullptr)
5252
return false;
53-
}
54-
_uart_hardware = uart_hardware;
55-
// Determine iface type
56-
if (_uart_hardware->isHardwareSerial()) {
57-
_iface_type = GPS_IFACE_UART_HW;
58-
} else if (_uart_hardware->isSoftwareSerial()) {
59-
_iface_type = GPS_IFACE_UART_SW;
60-
} else {
61-
WS_DEBUG_PRINTLN("[gps] ERROR: Unsupported UART interface type!");
62-
return false;
63-
}
53+
// Set the hardware serial interface
54+
_hw_serial = serial;
55+
_iface_type = GPS_IFACE_UART_HW;
6456
return true;
6557
}
6658

@@ -136,13 +128,11 @@ bool GPSController::DetectMediatek() {
136128
}
137129

138130
// Query MediaTek firmware version
139-
_uart_hardware->GetHardwareSerial()->flush();
140-
_uart_hardware->GetHardwareSerial()->println(CMD_MTK_QUERY_FW);
131+
_hw_serial->flush();
132+
_hw_serial->println(CMD_MTK_QUERY_FW);
141133
// Wait for response
142134
uint16_t timeout = 1000; // 1 second timeout
143-
while (_uart_hardware->GetHardwareSerial()->available() <
144-
MAX_NEMA_SENTENCE_LEN &&
145-
timeout--) {
135+
while (_hw_serial->available() < MAX_NEMA_SENTENCE_LEN && timeout--) {
146136
delay(1);
147137
}
148138

@@ -156,15 +146,15 @@ bool GPSController::DetectMediatek() {
156146
// command by reading out the NMEA sentence string into a buffer
157147
size_t buf_len = MAX_NEMA_SENTENCE_LEN + 3; // +3 for \r\n and null terminator
158148
char buffer[buf_len];
159-
size_t available = _uart_hardware->GetHardwareSerial()->available();
149+
size_t available = _hw_serial->available();
160150
size_t bytes_to_read = min(available, buf_len - 1);
161151
// Print the two out
162152
WS_DEBUG_PRINT("[gps] Reading MediaTek GPS response: ");
163153
WS_DEBUG_PRINT(available);
164154
WS_DEBUG_PRINT(" bytes, reading ");
165155
WS_DEBUG_PRINTLN(bytes_to_read);
166156
for (size_t i = 0; i < bytes_to_read; i++) {
167-
buffer[i] = _uart_hardware->GetHardwareSerial()->read();
157+
buffer[i] = _hw_serial->read();
168158
}
169159
buffer[bytes_to_read] = '\0';
170160
WS_DEBUG_PRINT("[gps] MediaTek GPS response: ");
@@ -178,8 +168,8 @@ bool GPSController::DetectMediatek() {
178168
if (_ada_gps != nullptr) {
179169
delete _ada_gps; // Clean up previous instance if it exists
180170
}
181-
_ada_gps = new Adafruit_GPS(_uart_hardware->GetHardwareSerial());
182-
if (!_ada_gps->begin(_uart_hardware->GetBaudRate())) {
171+
_ada_gps = new Adafruit_GPS(_hw_serial);
172+
if (!_ada_gps->begin(_hw_serial->baudRate())) {
183173
WS_DEBUG_PRINTLN("[gps] ERROR: Failed to initialize Mediatek!");
184174
return false;
185175
}

src/components/gps/controller.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class GPSController {
5151
public:
5252
GPSController();
5353
~GPSController();
54-
bool SetInterface(UARTHardware *uart_hardware);
54+
bool SetInterface(HardwareSerial *serial);
5555
// TODO: Add SetInterface(I2C *_i2c_hardware) for I2C support here!
5656
bool begin();
5757
bool QueryModuleType();
@@ -62,11 +62,11 @@ class GPSController {
6262
void update();
6363

6464
private:
65-
GPSModel *_gps_model; ///< GPS model
66-
UARTHardware *_uart_hardware = nullptr; ///< UART hardware instance for GPS
67-
GpsInterfaceType _iface_type; ///< Type of interface used by GPS
68-
GpsDriverType _driver_type; ///< Type of GPS driver used
69-
Adafruit_GPS *_ada_gps = nullptr; ///< Adafruit GPS instance
65+
GPSModel *_gps_model; ///< GPS model
66+
GpsInterfaceType _iface_type; ///< Type of interface used by GPS
67+
GpsDriverType _driver_type; ///< Type of GPS driver used
68+
HardwareSerial *_hw_serial = nullptr; ///< HardwareSerial instance for GPS;
69+
Adafruit_GPS *_ada_gps = nullptr; ///< Adafruit GPS instance
7070
};
7171
extern Wippersnapper_V2 WsV2; ///< Wippersnapper V2 instance
7272
#endif // WS_GPS_CONTROLLER_H

src/components/uart/controller.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,17 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
101101
return false;
102102
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_GPS:
103103
WS_DEBUG_PRINTLN("[uart] GPS device type not implemented!");
104-
// delete uart_hardware; // cleanup
105-
// return false;
106-
107-
WS_DEBUG_PRINTLN("[uart] Adding GPS device..");
108-
if (!gps.SetInterface(uart_hardware)) {
109-
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to set GPS interface!");
110-
delete uart_hardware; // cleanup
111-
return false;
112-
}
113-
WS_DEBUG_PRINTLN("[uart] Initializing GPS device...");
114-
if (!gps.begin()) {
115-
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to initialize GPS device!");
116-
delete uart_hardware; // cleanup
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;
117112
return false;
118113
}
119-
WS_DEBUG_PRINTLN("[uart] GPS device initialized successfully!");
120-
// TODO: Figure out how to add this to the drivers list so it can be
121-
// queried, also this is sitting on the stack instead of the heap, so it
122-
// will be destroyed when this function returns!!!
114+
WS_DEBUG_PRINTLN("[uart] GPS driver initialized successfully!");
123115
break;
124116
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_PM25AQI:
125117
WS_DEBUG_PRINTLN("[uart] Adding PM2.5 AQI device..");
@@ -144,13 +136,13 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
144136
return false;
145137
}
146138

147-
/*
148139
// Attempt to initialize the UART driver
149140
if (uart_driver == nullptr) {
150141
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to create UART driver!");
151142
delete uart_hardware; // cleanup
152143
return false;
153144
}
145+
154146
bool did_begin = false;
155147
did_begin = uart_driver->begin();
156148
if (!did_begin) {
@@ -185,7 +177,7 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
185177
WS_DEBUG_PRINTLN("[i2c] ERROR: Unable to publish UartAdded message to IO!");
186178
return false;
187179
}
188-
*/
180+
189181
return true;
190182
}
191183

src/components/uart/controller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "drivers/drvUartBase.h"
2323
#include "drivers/drvUartPm25.h"
2424
#include "drivers/drvUartUs100.h"
25+
#include "drivers/drvUartGps.h"
2526

2627
class Wippersnapper_V2; ///< Forward declaration
2728
class GPSController;

src/components/uart/drivers/drvUartGps.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class drvUartGps : public drvUartBase {
7272
bool begin() override {
7373
if (_hw_serial == nullptr)
7474
return false;
75-
7675
_gps = new GPSController();
7776
// TODO TUES: Note that _hw_serial isnt a pointer to the UARTHardware
7877
// and instead the raw hw serial. This is fine

0 commit comments

Comments
 (0)