@@ -80,12 +80,20 @@ bool GPSHardware::Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config) {
8080 }
8181 WS_DEBUG_PRINT (" [gps] Expected response: " );
8282 WS_DEBUG_PRINTLN (msg_resp);
83- if (_iface_type == GPS_IFACE_UART_HW) {
84- // Flush the RX/TX buffers before sending
83+ if (_iface_type == GPS_IFACE_UART_HW ||
84+ _iface_type == GPS_IFACE_UART_SW) {
85+ // Flush the RX/TX buffers before sending
86+ #ifdef HAS_SW_SERIAL
87+ _sw_serial->flush ();
88+ while (_sw_serial->available () > 0 ) {
89+ _sw_serial->read ();
90+ }
91+ #else
8592 _hw_serial->flush ();
8693 while (_hw_serial->available () > 0 ) {
8794 _hw_serial->read ();
8895 }
96+ #endif // HAS_SW_SERIAL
8997 } else if (_iface_type == GPS_IFACE_I2C) {
9098 WS_DEBUG_PRINT (" [gps] Flushing I2C buffers..." );
9199 I2cReadDiscard ();
@@ -128,6 +136,24 @@ bool GPSHardware::SetInterface(HardwareSerial *serial, uint32_t baudrate) {
128136 return true ;
129137}
130138
139+ #ifdef HAS_SW_SERIAL
140+ /* !
141+ * @brief Sets a UART software serial interface for the GPS controller.
142+ * @param serial
143+ * Pointer to a SoftwareSerial instance.
144+ * @returns True if the interface was set successfully, False otherwise.
145+ */
146+ bool GPSHardware::SetInterface (SoftwareSerial *serial, uint32_t baudrate) {
147+ if (serial == nullptr )
148+ return false ;
149+ // Configure the hardware serial interface
150+ _sw_serial = serial;
151+ _iface_type = GPS_IFACE_UART_SW;
152+ _baudrate = baudrate;
153+ return true ;
154+ }
155+ #endif // HAS_SW_SERIAL
156+
131157/* !
132158 * @brief Sets a TwoWire (I2C) interface for the GPS controller.
133159 * @param wire
@@ -259,33 +285,57 @@ bool GPSHardware::DetectMtkUart() {
259285 return false ;
260286 }
261287
262- // Clear the tx and rx buffers before sending the command
288+ // Clear the tx and rx buffers before sending the command
289+ #ifdef HAS_SW_SERIAL
290+ _sw_serial->flush ();
291+ while (_sw_serial->available () > 0 ) {
292+ _sw_serial->read ();
293+ }
294+ #else
263295 _hw_serial->flush ();
264296 while (_hw_serial->available () > 0 ) {
265297 _hw_serial->read ();
266298 }
299+ #endif
267300
268301 // Query MediaTek firmware version
269302 uint16_t timeout = MTK_QUERY_FW_TIMEOUT;
303+
304+ #ifdef HAS_SW_SERIAL
305+ while (_sw_serial->available () < MAX_NEMA_SENTENCE_LEN && timeout--) {
306+ delay (1 );
307+ }
308+ #else
270309 while (_hw_serial->available () < MAX_NEMA_SENTENCE_LEN && timeout--) {
271310 delay (1 );
272311 }
312+ #endif // HAS_SW_SERIAL
313+
273314 if (timeout == 0 )
274315 return false ;
275316
276317 // We found a response, let's verify that it's the expected PMTK_DK_RELEASE
277318 // command by reading out the NMEA sentence string into a buffer
278319 size_t buf_len = MAX_NEMA_SENTENCE_LEN * 4 ; // +3 for \r\n and null terminator
279320 char buffer[buf_len];
280- size_t available = _hw_serial->available ();
321+ size_t available;
322+ #ifdef HAS_SW_SERIAL
323+ available = _sw_serial->available ();
324+ #else
325+ available = _hw_serial->available ();
326+ #endif // HAS_SW_SERIAL
281327 size_t bytes_to_read = min (available, buf_len - 1 );
282328 // Print the two out
283329 WS_DEBUG_PRINT (" [gps] Reading MediaTek GPS response: " );
284330 WS_DEBUG_PRINT (available);
285331 WS_DEBUG_PRINT (" bytes, reading " );
286332 WS_DEBUG_PRINTLN (bytes_to_read);
287333 for (size_t i = 0 ; i < bytes_to_read; i++) {
334+ #ifdef HAS_SW_SERIAL
335+ buffer[i] = _sw_serial->read ();
336+ #else
288337 buffer[i] = _hw_serial->read ();
338+ #endif // HAS_SW_SERIAL
289339 }
290340 buffer[bytes_to_read] = ' \0 ' ;
291341 WS_DEBUG_PRINT (" [gps] MediaTek GPS response: " );
@@ -295,8 +345,12 @@ bool GPSHardware::DetectMtkUart() {
295345 return false ;
296346 }
297347
298- // Attempt to use Adafruit_GPS
348+ // Attempt to use Adafruit_GPS
349+ #ifdef HAS_SW_SERIAL
350+ _ada_gps = new Adafruit_GPS (_sw_serial);
351+ #else
299352 _ada_gps = new Adafruit_GPS (_hw_serial);
353+ #endif // HAS_SW_SERIAL
300354 if (!_ada_gps->begin (_baudrate)) {
301355 WS_DEBUG_PRINTLN (" [gps] ERROR: Failed to initialize Mediatek!" );
302356 return false ;
0 commit comments