Skip to content

Commit b699c06

Browse files
committed
GPS - attempt a driver approach for managing instances
1 parent 51dea24 commit b699c06

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

src/components/gps/controller.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ bool GPSController::DetectMediatek() {
169169
buffer[bytes_to_read] = '\0';
170170
WS_DEBUG_PRINT("[gps] MediaTek GPS response: ");
171171
WS_DEBUG_PRINTLN(buffer);
172-
// Compare the first 7 characters to the expected PMTK705 string
173-
if (strncmp(buffer, CMD_MTK_QUERY_FW_RESP, 7) != 0) {
172+
// did we get the expected PMTK705 string?
173+
if (strncmp(buffer, CMD_MTK_QUERY_FW_RESP, 8) != 0) {
174174
return false;
175175
}
176176

src/components/uart/controller.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ bool UARTController::Handle_UartAdd(pb_istream_t *stream) {
7070
// Create a new UartDevice "driver" on the hardware layer (UARTHardware)
7171
drvUartBase *uart_driver = nullptr;
7272
wippersnapper_uart_UartDeviceConfig cfg_device = add_msg->cfg_device;
73-
GPSController gps; // TODO: This should be a member variable, not a local one
7473
switch (cfg_device.device_type) {
7574
case wippersnapper_uart_UartDeviceType_UART_DEVICE_TYPE_UNSPECIFIED:
7675
WS_DEBUG_PRINTLN("[uart] ERROR: Unspecified device type!");
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*!
2+
* @file drvUartUs100.h
3+
*
4+
* UART driver for a GPS component
5+
*
6+
* Adafruit invests time and resources providing this open source code,
7+
* please support Adafruit and open-source hardware by purchasing
8+
* products from Adafruit!
9+
*
10+
* Copyright (c) Brent Rubell 2025 for Adafruit Industries.
11+
*
12+
* MIT license, all text here must be included in any redistribution.
13+
*
14+
*/
15+
16+
#ifndef DRV_UART_GPS_H
17+
#define DRV_UART_GPS_H
18+
#include "drvUartBase.h"
19+
#include "../../gps/controller.h"
20+
21+
22+
/*!
23+
@brief Provides an interface for the US-100 Ultrasonic Distance Sensor over
24+
UART.
25+
*/
26+
class drvUartGps : public drvUartBase {
27+
28+
public:
29+
/*!
30+
@brief Instantiates a US-100 UART device.
31+
@param hw_serial
32+
Pointer to a HardwareSerial instance.
33+
@param driver_name
34+
The name of the driver.
35+
@param port_num
36+
The port number for the UART device corresponding to the Serial
37+
instance.
38+
*/
39+
drvUartGps(HardwareSerial *hw_serial, const char *driver_name,
40+
uint32_t port_num)
41+
: drvUartBase(hw_serial, driver_name, port_num) {
42+
// Handled by drvUartBase constructor
43+
}
44+
45+
#if HAS_SW_SERIAL
46+
/*!
47+
@brief Instantiates a US-100 UART device.
48+
@param sw_serial
49+
Pointer to a SoftwareSerial instance.
50+
@param driver_name
51+
The name of the driver.
52+
@param port_num
53+
The port number for the UART device corresponding to the Serial
54+
instance.
55+
*/
56+
drvUartGps(SoftwareSerial *sw_serial, const char *driver_name,
57+
uint32_t port_num)
58+
: drvUartBase(sw_serial, driver_name, port_num) {
59+
// Handled by drvUartBase constructor
60+
}
61+
#endif // HAS_SW_SERIAL
62+
63+
/*!
64+
@brief Destructor for a UART GPS.
65+
*/
66+
~drvUartGps() { /* TODO: Add back dtor */ }
67+
68+
/*!
69+
@brief Initializes a UART GPS device.
70+
@returns True if initialized successfully, False otherwise.
71+
*/
72+
bool begin() override {
73+
if (_hw_serial == nullptr)
74+
return false;
75+
76+
_gps = new GPSController();
77+
// TODO TUES: Note that _hw_serial isnt a pointer to the UARTHardware
78+
// and instead the raw hw serial. This is fine
79+
// but we'll have to refactor the GPS controller iface to take in
80+
// a hardwareserial ptr rather than a UARTHardware ptr.
81+
if (!_gps->SetInterface(_hw_serial)) {
82+
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to set GPS interface!");
83+
delete _gps; // cleanup
84+
_gps = nullptr;
85+
return false;
86+
}
87+
WS_DEBUG_PRINTLN("[uart] Initializing GPS device...");
88+
if (!_gps->begin()) {
89+
WS_DEBUG_PRINTLN("[uart] ERROR: Failed to initialize GPS device!");
90+
delete _gps; // cleanup
91+
_gps = nullptr;
92+
return false;
93+
}
94+
WS_DEBUG_PRINTLN("[uart] GPS device initialized successfully!");
95+
return true;
96+
}
97+
98+
99+
protected:
100+
GPSController *_gps;
101+
};
102+
#endif // DRV_UART_US100_H

0 commit comments

Comments
 (0)