| 
									
										
										
										
											2020-02-06 15:39:21 +00:00
										 |  |  | #include "GPS.h"
 | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | #include "NodeDB.h"
 | 
					
						
							| 
									
										
										
										
											2020-10-07 23:46:20 +00:00
										 |  |  | #include "RTC.h"
 | 
					
						
							| 
									
										
										
										
											2022-05-07 10:31:21 +00:00
										 |  |  | #include "configuration.h"
 | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | #include "sleep.h"
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:39:21 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-10 18:43:14 +00:00
										 |  |  | // If we have a serial GPS port it will not be null
 | 
					
						
							| 
									
										
										
										
											2021-04-11 07:17:56 +00:00
										 |  |  | #ifdef GPS_SERIAL_NUM
 | 
					
						
							| 
									
										
										
										
											2020-05-04 18:15:05 +00:00
										 |  |  | HardwareSerial _serial_gps_real(GPS_SERIAL_NUM); | 
					
						
							| 
									
										
										
										
											2020-07-10 04:27:34 +00:00
										 |  |  | HardwareSerial *GPS::_serial_gps = &_serial_gps_real; | 
					
						
							| 
									
										
										
										
											2020-10-15 05:47:10 +00:00
										 |  |  | #elif defined(NRF52840_XXAA) || defined(NRF52833_XXAA)
 | 
					
						
							| 
									
										
										
										
											2020-07-10 04:27:34 +00:00
										 |  |  | // Assume NRF52840
 | 
					
						
							|  |  |  | HardwareSerial *GPS::_serial_gps = &Serial1; | 
					
						
							| 
									
										
										
										
											2020-04-15 03:22:27 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2020-07-10 04:27:34 +00:00
										 |  |  | HardwareSerial *GPS::_serial_gps = NULL; | 
					
						
							| 
									
										
										
										
											2020-04-15 03:22:27 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-02-20 04:02:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-04 18:15:05 +00:00
										 |  |  | GPS *gps; | 
					
						
							| 
									
										
										
										
											2020-03-14 01:44:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 07:21:54 +00:00
										 |  |  | /// Multiple GPS instances might use the same serial port (in sequence), but we can
 | 
					
						
							| 
									
										
										
										
											2020-11-07 01:15:28 +00:00
										 |  |  | /// only init that port once.
 | 
					
						
							|  |  |  | static bool didSerialInit; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  | bool GPS::getACK(uint8_t c, uint8_t i) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint8_t b; | 
					
						
							|  |  |  |     uint8_t ack = 0; | 
					
						
							|  |  |  |     const uint8_t ackP[2] = {c, i}; | 
					
						
							|  |  |  |     uint8_t buf[10] = {0xB5, 0x62, 0x05, 0x01, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00}; | 
					
						
							|  |  |  |     unsigned long startTime = millis(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     for (int j = 2; j < 6; j++) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         buf[8] += buf[j]; | 
					
						
							|  |  |  |         buf[9] += buf[8]; | 
					
						
							| 
									
										
										
										
											2022-05-17 17:54:29 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     for (int j = 0; j < 2; j++) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         buf[6 + j] = ackP[j]; | 
					
						
							|  |  |  |         buf[8] += buf[6 + j]; | 
					
						
							|  |  |  |         buf[9] += buf[8]; | 
					
						
							| 
									
										
										
										
											2022-05-17 17:54:29 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     while (1) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (ack > 9) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (millis() - startTime > 1000) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (_serial_gps->available()) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             b = _serial_gps->read(); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (b == buf[ack]) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 ack++; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 ack = 0; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-05-17 17:54:29 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |  * @brief | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |  * @note   New method, this method can wait for the specified class and message ID, and return the payload | 
					
						
							|  |  |  |  * @param  *buffer: The message buffer, if there is a response payload message, it will be returned through the buffer parameter | 
					
						
							|  |  |  |  * @param  size:    size of buffer | 
					
						
							|  |  |  |  * @param  requestedClass:  request class constant | 
					
						
							|  |  |  |  * @param  requestedID:     request message ID constant | 
					
						
							|  |  |  |  * @retval length of payload message | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int GPS::getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |     uint16_t ubxFrameCounter = 0; | 
					
						
							|  |  |  |     uint32_t startTime = millis(); | 
					
						
							|  |  |  |     uint16_t needRead; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     while (millis() - startTime < 800) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         while (_serial_gps->available()) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             int c = _serial_gps->read(); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             switch (ubxFrameCounter) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             case 0: | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 // ubxFrame 'μ'
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (c == 0xB5) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter++; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 1: | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 // ubxFrame 'b'
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (c == 0x62) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter++; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter = 0; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 2: | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 // Class
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (c == requestedClass) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter++; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter = 0; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 3: | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 // Message ID
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (c == requestedID) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter++; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter = 0; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 4: | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 // Payload lenght lsb
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 needRead = c; | 
					
						
							|  |  |  |                 ubxFrameCounter++; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 5: | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 // Payload lenght msb
 | 
					
						
							|  |  |  |                 needRead |= (c << 8); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 ubxFrameCounter++; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 6: | 
					
						
							|  |  |  |                 // Check for buffer overflow
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (needRead >= size) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter = 0; | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (_serial_gps->readBytes(buffer, needRead) != needRead) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     ubxFrameCounter = 0; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     // return payload lenght
 | 
					
						
							|  |  |  |                     return needRead; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-25 09:07:54 +00:00
										 |  |  | bool GPS::setupGPS() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (_serial_gps && !didSerialInit) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-11-07 01:15:28 +00:00
										 |  |  |         didSerialInit = true; | 
					
						
							| 
									
										
										
										
											2021-03-10 07:21:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-19 13:46:59 +00:00
										 |  |  | #ifdef ARCH_ESP32
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         // In esp32 framework, setRxBufferSize needs to be initialized before Serial
 | 
					
						
							|  |  |  |         _serial_gps->setRxBufferSize(2048); // the default is 256
 | 
					
						
							| 
									
										
										
										
											2022-09-06 07:58:33 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         // if the overrides are not dialled in, set them from the board definitions, if they exist
 | 
					
						
							| 
									
										
										
										
											2022-11-24 08:23:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if defined(GPS_RX_PIN)
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         if (!config.position.rx_gpio) | 
					
						
							|  |  |  |             config.position.rx_gpio = GPS_RX_PIN; | 
					
						
							| 
									
										
										
										
											2022-11-24 08:23:17 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | #if defined(GPS_TX_PIN)
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         if (!config.position.tx_gpio) | 
					
						
							|  |  |  |             config.position.tx_gpio = GPS_TX_PIN; | 
					
						
							| 
									
										
										
										
											2022-11-24 08:23:17 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 07:17:56 +00:00
										 |  |  | // ESP32 has a special set of parameters vs other arduino ports
 | 
					
						
							| 
									
										
										
										
											2022-11-24 08:23:17 +00:00
										 |  |  | #if defined(ARCH_ESP32)
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         if (config.position.rx_gpio) | 
					
						
							| 
									
										
										
										
											2022-11-24 08:23:17 +00:00
										 |  |  |             _serial_gps->begin(GPS_BAUDRATE, SERIAL_8N1, config.position.rx_gpio, config.position.tx_gpio); | 
					
						
							| 
									
										
										
										
											2020-10-25 09:07:54 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  |         _serial_gps->begin(GPS_BAUDRATE); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-09-06 07:58:33 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 02:52:03 +00:00
										 |  |  |         /*
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |          * T-Beam-S3-Core will be preset to use gps Probe here, and other boards will not be changed first | 
					
						
							|  |  |  |          */ | 
					
						
							|  |  |  |         gnssModel = probe(); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (gnssModel == GNSS_MODEL_MTK) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             /*
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |              * t-beam-s3-core uses the same L76K GNSS module as t-echo. | 
					
						
							|  |  |  |              * Unlike t-echo, L76K uses 9600 baud rate for communication by default. | 
					
						
							|  |  |  |              * */ | 
					
						
							|  |  |  |             // _serial_gps->begin(9600);    //The baud rate of 9600 has been initialized at the beginning of setupGPS, this line
 | 
					
						
							|  |  |  |             // is the redundant part delay(250);
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // Initialize the L76K Chip, use GPS + GLONASS
 | 
					
						
							|  |  |  |             _serial_gps->write("$PCAS04,5*1C\r\n"); | 
					
						
							|  |  |  |             delay(250); | 
					
						
							|  |  |  |             // only ask for RMC and GGA
 | 
					
						
							|  |  |  |             _serial_gps->write("$PCAS03,1,0,0,0,1,0,0,0,0,0,,,0,0*02\r\n"); | 
					
						
							|  |  |  |             delay(250); | 
					
						
							|  |  |  |             // Switch to Vehicle Mode, since SoftRF enables Aviation < 2g
 | 
					
						
							|  |  |  |             _serial_gps->write("$PCAS11,3*1E\r\n"); | 
					
						
							|  |  |  |             delay(250); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (gnssModel == GNSS_MODEL_UBLOX) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             /*
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 tips: NMEA Only should not be set here, otherwise initializing Ublox gnss module again after | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 setting will not output command messages in UART1, resulting in unrecognized module information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Set the UART port to output NMEA only
 | 
					
						
							|  |  |  |                 byte _message_nmea[] = {0xB5, 0x62, 0x06, 0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0xC0, 0x08, 0x00, 0x00, | 
					
						
							|  |  |  |                                         0x80, 0x25, 0x00, 0x00, 0x07, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x91, 0xAF}; | 
					
						
							|  |  |  |                 _serial_gps->write(_message_nmea, sizeof(_message_nmea)); | 
					
						
							|  |  |  |                 if (!getACK(0x06, 0x00)) { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                     LOG_WARN("Unable to enable NMEA Mode.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     return true; | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             // ublox-M10S can be compatible with UBLOX traditional protocol, so the following sentence settings are also valid
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // disable GGL
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             byte _message_GGL[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x01, | 
					
						
							|  |  |  |                                    0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x05, 0x3A}; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             _serial_gps->write(_message_GGL, sizeof(_message_GGL)); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (!getACK(0x06, 0x01)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                 LOG_WARN("Unable to disable NMEA GGL.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // disable GSA
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             byte _message_GSA[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x02, | 
					
						
							|  |  |  |                                    0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x06, 0x41}; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             _serial_gps->write(_message_GSA, sizeof(_message_GSA)); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (!getACK(0x06, 0x01)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                 LOG_WARN("Unable to disable NMEA GSA.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // disable GSV
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             byte _message_GSV[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x03, | 
					
						
							|  |  |  |                                    0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x07, 0x48}; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             _serial_gps->write(_message_GSV, sizeof(_message_GSV)); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (!getACK(0x06, 0x01)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                 LOG_WARN("Unable to disable NMEA GSV.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // disable VTG
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             byte _message_VTG[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x05, | 
					
						
							|  |  |  |                                    0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x09, 0x56}; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             _serial_gps->write(_message_VTG, sizeof(_message_VTG)); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (!getACK(0x06, 0x01)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                 LOG_WARN("Unable to disable NMEA VTG.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // enable RMC
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             byte _message_RMC[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x04, | 
					
						
							|  |  |  |                                    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x09, 0x54}; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             _serial_gps->write(_message_RMC, sizeof(_message_RMC)); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (!getACK(0x06, 0x01)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                 LOG_WARN("Unable to enable NMEA RMC.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // enable GGA
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             byte _message_GGA[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x00, | 
					
						
							|  |  |  |                                    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x05, 0x38}; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             _serial_gps->write(_message_GGA, sizeof(_message_GGA)); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (!getACK(0x06, 0x01)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                 LOG_WARN("Unable to enable NMEA GGA.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-25 09:07:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | bool GPS::setup() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-17 05:15:12 +00:00
										 |  |  |     // Master power for the GPS
 | 
					
						
							|  |  |  | #ifdef PIN_GPS_EN
 | 
					
						
							| 
									
										
										
										
											2022-04-02 10:52:50 +00:00
										 |  |  |     digitalWrite(PIN_GPS_EN, 1); | 
					
						
							| 
									
										
										
										
											2020-10-17 05:15:12 +00:00
										 |  |  |     pinMode(PIN_GPS_EN, OUTPUT); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  | #ifdef HAS_PMU
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (config.position.gps_enabled) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         setGPSPower(true); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-24 10:40:47 +00:00
										 |  |  | #ifdef PIN_GPS_RESET
 | 
					
						
							|  |  |  |     digitalWrite(PIN_GPS_RESET, 1); // assert for 10ms
 | 
					
						
							|  |  |  |     pinMode(PIN_GPS_RESET, OUTPUT); | 
					
						
							|  |  |  |     delay(10); | 
					
						
							|  |  |  |     digitalWrite(PIN_GPS_RESET, 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2020-10-05 07:29:26 +00:00
										 |  |  |     setAwake(true); // Wake GPS power before doing any init
 | 
					
						
							|  |  |  |     bool ok = setupGPS(); | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (ok) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-05 07:29:26 +00:00
										 |  |  |         notifySleepObserver.observe(¬ifySleep); | 
					
						
							| 
									
										
										
										
											2020-10-30 09:05:32 +00:00
										 |  |  |         notifyDeepSleepObserver.observe(¬ifyDeepSleep); | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  |         notifyGPSSleepObserver.observe(¬ifyGPSSleep); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-01-22 02:34:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (config.position.gps_enabled == false && config.position.fixed_position == false) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  |         setAwake(false); | 
					
						
							|  |  |  |         doGPSpowersave(false); | 
					
						
							| 
									
										
										
										
											2020-10-30 09:05:32 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-05 07:29:26 +00:00
										 |  |  |     return ok; | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 07:21:54 +00:00
										 |  |  | GPS::~GPS() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // we really should unregister our sleep observer
 | 
					
						
							| 
									
										
										
										
											2022-04-13 22:43:06 +00:00
										 |  |  |     notifySleepObserver.unobserve(¬ifySleep); | 
					
						
							|  |  |  |     notifyDeepSleepObserver.unobserve(¬ifyDeepSleep); | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  |     notifyGPSSleepObserver.observe(¬ifyGPSSleep); | 
					
						
							| 
									
										
										
										
											2021-03-10 07:21:54 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-07 10:31:21 +00:00
										 |  |  | bool GPS::hasLock() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return hasValidLocation; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-09-02 13:11:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 20:38:33 +00:00
										 |  |  | bool GPS::hasFlow() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return hasGPS; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 06:43:28 +00:00
										 |  |  | // Allow defining the polarity of the WAKE output.  default is active high
 | 
					
						
							|  |  |  | #ifndef GPS_WAKE_ACTIVE
 | 
					
						
							|  |  |  | #define GPS_WAKE_ACTIVE 1
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-13 05:59:06 +00:00
										 |  |  | void GPS::wake() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef PIN_GPS_WAKE
 | 
					
						
							| 
									
										
										
										
											2020-10-13 06:43:28 +00:00
										 |  |  |     digitalWrite(PIN_GPS_WAKE, GPS_WAKE_ACTIVE); | 
					
						
							| 
									
										
										
										
											2020-10-13 05:59:06 +00:00
										 |  |  |     pinMode(PIN_GPS_WAKE, OUTPUT); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-10 07:21:54 +00:00
										 |  |  | void GPS::sleep() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-13 05:59:06 +00:00
										 |  |  | #ifdef PIN_GPS_WAKE
 | 
					
						
							| 
									
										
										
										
											2020-10-13 06:43:28 +00:00
										 |  |  |     digitalWrite(PIN_GPS_WAKE, GPS_WAKE_ACTIVE ? 0 : 1); | 
					
						
							| 
									
										
										
										
											2020-10-13 05:59:06 +00:00
										 |  |  |     pinMode(PIN_GPS_WAKE, OUTPUT); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  | /// Record that we have a GPS
 | 
					
						
							|  |  |  | void GPS::setConnected() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (!hasGPS) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |         hasGPS = true; | 
					
						
							|  |  |  |         shouldPublish = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPS::setNumSatellites(uint8_t n) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (n != numSatellites) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |         numSatellites = n; | 
					
						
							|  |  |  |         shouldPublish = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 00:04:19 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Switch the GPS into a mode where we are actively looking for a lock, or alternatively switch GPS into a low power mode | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * calls sleep/wake | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | void GPS::setAwake(bool on) | 
					
						
							| 
									
										
										
										
											2020-09-29 00:04:19 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (!wakeAllowed && on) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-30 16:27:07 +00:00
										 |  |  |         LOG_WARN("Inhibiting because !wakeAllowed\n"); | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |         on = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (isAwake != on) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         LOG_DEBUG("WANT GPS=%d\n", on); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (on) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |             lastWakeStartMsec = millis(); | 
					
						
							| 
									
										
										
										
											2020-09-29 00:04:19 +00:00
										 |  |  |             wake(); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         } | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |             lastSleepStartMsec = millis(); | 
					
						
							| 
									
										
										
										
											2020-09-29 00:04:19 +00:00
										 |  |  |             sleep(); | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         isAwake = on; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | /** Get how long we should stay looking for each aquisition in msecs
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | uint32_t GPS::getWakeTime() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-05-21 20:38:33 +00:00
										 |  |  |     uint32_t t = config.position.gps_attempt_time; | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 21:34:56 +00:00
										 |  |  |     if (t == UINT32_MAX) | 
					
						
							|  |  |  |         return t; // already maxint
 | 
					
						
							| 
									
										
										
										
											2022-09-12 23:07:21 +00:00
										 |  |  |     return t * 1000; | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** Get how long we should sleep between aqusition attempts in msecs
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | uint32_t GPS::getSleepTime() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-05-21 20:38:33 +00:00
										 |  |  |     uint32_t t = config.position.gps_update_interval; | 
					
						
							| 
									
										
										
										
											2022-09-11 13:36:47 +00:00
										 |  |  |     bool gps_enabled = config.position.gps_enabled; | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |     // We'll not need the GPS thread to wake up again after first acq. with fixed position.
 | 
					
						
							|  |  |  |     if (!gps_enabled || config.position.fixed_position) | 
					
						
							| 
									
										
										
										
											2020-10-05 22:27:46 +00:00
										 |  |  |         t = UINT32_MAX; // Sleep forever now
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 21:34:56 +00:00
										 |  |  |     if (t == UINT32_MAX) | 
					
						
							|  |  |  |         return t; // already maxint
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-12 23:07:21 +00:00
										 |  |  |     return t * 1000; | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPS::publishUpdate() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (shouldPublish) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |         shouldPublish = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-24 12:38:35 +00:00
										 |  |  |         // In debug logs, identify position by @timestamp:stage (stage 2 = publish)
 | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         LOG_DEBUG("publishing pos@%x:2, hasVal=%d, GPSlock=%d\n", p.timestamp, hasValidLocation, hasLock()); | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |         // Notify any status instances that are observing us
 | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  |         const meshtastic::GPSStatus status = meshtastic::GPSStatus(hasValidLocation, isConnected(), isPowerSaving(), p); | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |         newStatus.notifyObservers(&status); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-10 01:57:57 +00:00
										 |  |  | int32_t GPS::runOnce() | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     // Repeaters have no need for GPS
 | 
					
						
							|  |  |  |     if (config.device.role == meshtastic_Config_DeviceConfig_Role_REPEATER) | 
					
						
							|  |  |  |         disable(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (whileIdle()) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |         // if we have received valid NMEA claim we are connected
 | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |         setConnected(); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ((config.position.gps_enabled == 1) && (gnssModel == GNSS_MODEL_UBLOX)) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             // reset the GPS on next bootup
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (devicestate.did_gps_reset && (millis() > 60000) && !hasFlow()) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                 LOG_DEBUG("GPS is not communicating, trying factory reset on next bootup.\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 devicestate.did_gps_reset = false; | 
					
						
							|  |  |  |                 nodeDB.saveDeviceStateToDisk(); | 
					
						
							| 
									
										
										
										
											2023-01-23 11:27:09 +00:00
										 |  |  |                 disable(); // Stop the GPS thread as it can do nothing useful until next reboot.
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // If we are overdue for an update, turn on the GPS and at least publish the current status
 | 
					
						
							|  |  |  |     uint32_t now = millis(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 21:34:56 +00:00
										 |  |  |     auto sleepTime = getSleepTime(); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (!isAwake && sleepTime != UINT32_MAX && (now - lastSleepStartMsec) > sleepTime) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |         // We now want to be awake - so wake up the GPS
 | 
					
						
							|  |  |  |         setAwake(true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // While we are awake
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (isAwake) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         // LOG_DEBUG("looking for location\n");
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if ((now - lastWhileActiveMsec) > 5000) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-10-05 22:07:30 +00:00
										 |  |  |             lastWhileActiveMsec = now; | 
					
						
							|  |  |  |             whileActive(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |         // If we've already set time from the GPS, no need to ask the GPS
 | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |         bool gotTime = (getRTCQuality() >= RTCQualityGPS); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (!gotTime && lookForTime()) | 
					
						
							|  |  |  |         { // Note: we count on this && short-circuiting and not resetting the RTC time
 | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |             gotTime = true; | 
					
						
							|  |  |  |             shouldPublish = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |         bool gotLoc = lookForLocation(); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (gotLoc && !hasValidLocation) | 
					
						
							|  |  |  |         { // declare that we have location ASAP
 | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |             LOG_DEBUG("hasValidLocation RISING EDGE\n"); | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |             hasValidLocation = true; | 
					
						
							|  |  |  |             shouldPublish = true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |         // We've been awake too long - force sleep
 | 
					
						
							| 
									
										
										
										
											2020-12-31 05:11:03 +00:00
										 |  |  |         now = millis(); | 
					
						
							| 
									
										
										
										
											2020-10-05 21:34:56 +00:00
										 |  |  |         auto wakeTime = getWakeTime(); | 
					
						
							|  |  |  |         bool tooLong = wakeTime != UINT32_MAX && (now - lastWakeStartMsec) > wakeTime; | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Once we get a location we no longer desperately want an update
 | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         // LOG_DEBUG("gotLoc %d, tooLong %d, gotTime %d\n", gotLoc, tooLong, gotTime);
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if ((gotLoc && gotTime) || tooLong) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (tooLong) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |                 // we didn't get a location during this ack window, therefore declare loss of lock
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (hasValidLocation) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |                     LOG_DEBUG("hasValidLocation FALLING EDGE (last read: %d)\n", gotLoc); | 
					
						
							| 
									
										
										
										
											2021-10-24 12:38:35 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2023-01-21 17:22:19 +00:00
										 |  |  |                 p = meshtastic_Position_init_default; | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |                 hasValidLocation = false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |             setAwake(false); | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  |             shouldPublish = true; // publish our update for this just finished acquisition window
 | 
					
						
							| 
									
										
										
										
											2020-10-05 06:43:44 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-10 01:20:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // If state has changed do a publish
 | 
					
						
							|  |  |  |     publishUpdate(); | 
					
						
							| 
									
										
										
										
											2020-10-10 01:57:57 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (!(fixeddelayCtr >= 20) && config.position.fixed_position && hasValidLocation) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         fixeddelayCtr++; | 
					
						
							|  |  |  |         // LOG_DEBUG("Our delay counter is %d\n", fixeddelayCtr);
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (fixeddelayCtr >= 20) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             doGPSpowersave(false); | 
					
						
							|  |  |  |             forceWake(false); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-10 01:57:57 +00:00
										 |  |  |     // 9600bps is approx 1 byte per msec, so considering our buffer size we never need to wake more often than 200ms
 | 
					
						
							|  |  |  |     // if not awake we can run super infrquently (once every 5 secs?) to see if we need to wake.
 | 
					
						
							| 
									
										
										
										
											2022-04-02 10:52:50 +00:00
										 |  |  |     return isAwake ? GPS_THREAD_INTERVAL : 5000; | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPS::forceWake(bool on) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (on) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         LOG_DEBUG("Allowing GPS lock\n"); | 
					
						
							| 
									
										
										
										
											2020-10-05 21:34:56 +00:00
										 |  |  |         // lastSleepStartMsec = 0; // Force an update ASAP
 | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |         wakeAllowed = true; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |         wakeAllowed = false; | 
					
						
							| 
									
										
										
										
											2020-10-07 22:23:53 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Note: if the gps was already awake, we DO NOT shut it down, because we want to allow it to complete its lock
 | 
					
						
							|  |  |  |         // attempt even if we are in light sleep.  Once the attempt succeeds (or times out) we'll then shut it down.
 | 
					
						
							|  |  |  |         // setAwake(false);
 | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs
 | 
					
						
							|  |  |  | int GPS::prepareSleep(void *unused) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-30 16:27:07 +00:00
										 |  |  |     LOG_INFO("GPS prepare sleep!\n"); | 
					
						
							| 
									
										
										
										
											2020-10-01 16:11:54 +00:00
										 |  |  |     forceWake(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-10-30 09:05:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs
 | 
					
						
							|  |  |  | int GPS::prepareDeepSleep(void *unused) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-12-30 16:27:07 +00:00
										 |  |  |     LOG_INFO("GPS deep sleep!\n"); | 
					
						
							| 
									
										
										
										
											2020-10-30 09:05:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // For deep sleep we also want abandon any lock attempts (because we want minimum power)
 | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  |     getSleepTime(); | 
					
						
							| 
									
										
										
										
											2020-10-30 09:05:32 +00:00
										 |  |  |     setAwake(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2021-03-15 02:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | GnssModel_t GPS::probe() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-10-05 12:59:07 +00:00
										 |  |  |     // return immediately if the model is set by the variant.h file
 | 
					
						
							|  |  |  | #ifdef GPS_UBLOX
 | 
					
						
							|  |  |  |     return GNSS_MODEL_UBLOX; | 
					
						
							|  |  |  | #elif defined(GPS_L76K)
 | 
					
						
							|  |  |  |     return GNSS_MODEL_MTK; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |     // we use autodetect, only T-BEAM S3 for now...
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |     uint8_t buffer[256]; | 
					
						
							|  |  |  |     /*
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |      * The GNSS module information variable is temporarily placed inside the function body, | 
					
						
							|  |  |  |      * if it needs to be used elsewhere, it can be moved to the outside | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     struct uBloxGnssModelInfo info; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     memset(&info, 0, sizeof(struct uBloxGnssModelInfo)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Close all NMEA sentences , Only valid for MTK platform
 | 
					
						
							|  |  |  |     _serial_gps->write("$PCAS03,0,0,0,0,0,0,0,0,0,0,,,0,0*02\r\n"); | 
					
						
							|  |  |  |     delay(20); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Get version information
 | 
					
						
							|  |  |  |     _serial_gps->write("$PCAS06,0*1B\r\n"); | 
					
						
							|  |  |  |     uint32_t startTimeout = millis() + 500; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     while (millis() < startTimeout) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (_serial_gps->available()) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             String ver = _serial_gps->readStringUntil('\r'); | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             // Get module info , If the correct header is returned,
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             // it can be determined that it is the MTK chip
 | 
					
						
							|  |  |  |             int index = ver.indexOf("$"); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |             if (index != -1) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 ver = ver.substring(index); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |                 if (ver.startsWith("$GPTXT,01,01,02")) | 
					
						
							|  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2022-12-30 16:27:07 +00:00
										 |  |  |                     LOG_INFO("L76K GNSS init succeeded, using L76K GNSS Module\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                     return GNSS_MODEL_MTK; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uint8_t cfg_rate[] = {0xB5, 0x62, 0x06, 0x08, 0x00, 0x00, 0x0E, 0x30}; | 
					
						
							|  |  |  |     _serial_gps->write(cfg_rate, sizeof(cfg_rate)); | 
					
						
							|  |  |  |     // Check that the returned response class and message ID are correct
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (!getAck(buffer, 256, 0x06, 0x08)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         LOG_WARN("Failed to find UBlox & MTK GNSS Module\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |         return GNSS_MODEL_UNKONW; | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     //  Get Ublox gnss module hardware and software info
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |     uint8_t cfg_get_hw[] = {0xB5, 0x62, 0x0A, 0x04, 0x00, 0x00, 0x0E, 0x34}; | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |     _serial_gps->write(cfg_get_hw, sizeof(cfg_get_hw)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     uint16_t len = getAck(buffer, 256, 0x0A, 0x04); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (len) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         uint16_t position = 0; | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         for (int i = 0; i < 30; i++) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             info.swVersion[i] = buffer[position]; | 
					
						
							|  |  |  |             position++; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         for (int i = 0; i < 10; i++) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             info.hwVersion[i] = buffer[position]; | 
					
						
							|  |  |  |             position++; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         while (len >= position + 30) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             for (int i = 0; i < 30; i++) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |                 info.extension[info.extensionNo][i] = buffer[position]; | 
					
						
							|  |  |  |                 position++; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             info.extensionNo++; | 
					
						
							|  |  |  |             if (info.extensionNo > 9) | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         LOG_DEBUG("Module Info : \n"); | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         LOG_DEBUG("Soft version: %s\n", info.swVersion); | 
					
						
							|  |  |  |         LOG_DEBUG("Hard version: %s\n", info.hwVersion); | 
					
						
							|  |  |  |         LOG_DEBUG("Extensions:%d\n", info.extensionNo); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         for (int i = 0; i < info.extensionNo; i++) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |             LOG_DEBUG("  %s\n", info.extension[i]); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         memset(buffer, 0, sizeof(buffer)); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         // tips: extensionNo field is 0 on some 6M GNSS modules
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         for (int i = 0; i < info.extensionNo; ++i) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             if (!strncmp(info.extension[i], "OD=", 3)) | 
					
						
							|  |  |  |             { | 
					
						
							| 
									
										
										
										
											2023-01-16 09:55:40 +00:00
										 |  |  |                 strncpy((char *)buffer, &(info.extension[i][3]), sizeof(buffer)); | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |                 LOG_DEBUG("GetModel:%s\n", (char *)buffer); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (strlen((char *)buffer)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-01-21 11:32:41 +00:00
										 |  |  |         LOG_INFO("UBlox GNSS init succeeded, using UBlox %s GNSS Module\n", buffer); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-30 16:27:07 +00:00
										 |  |  |         LOG_INFO("UBlox GNSS init succeeded, using UBlox GNSS Module\n"); | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return GNSS_MODEL_UBLOX; | 
					
						
							| 
									
										
										
										
											2022-10-05 12:59:07 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-09-15 18:43:04 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-31 12:11:47 +00:00
										 |  |  | #if HAS_GPS
 | 
					
						
							| 
									
										
										
										
											2021-03-15 02:00:20 +00:00
										 |  |  | #include "NMEAGPS.h"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-11 07:17:56 +00:00
										 |  |  | GPS *createGps() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-03-15 02:00:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-31 12:11:47 +00:00
										 |  |  | #if !HAS_GPS
 | 
					
						
							| 
									
										
										
										
											2021-03-15 02:00:20 +00:00
										 |  |  |     return nullptr; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     if (config.position.gps_enabled) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-09 14:12:37 +00:00
										 |  |  | #ifdef GPS_ALTITUDE_HAE
 | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         LOG_DEBUG("Using HAE altitude model\n"); | 
					
						
							| 
									
										
										
										
											2021-09-09 14:12:37 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2022-12-30 02:41:37 +00:00
										 |  |  |         LOG_DEBUG("Using MSL altitude model\n"); | 
					
						
							| 
									
										
										
										
											2021-09-09 14:12:37 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |         if (GPS::_serial_gps) | 
					
						
							|  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-03-24 17:22:31 +00:00
										 |  |  |             // Some boards might have only the TX line from the GPS connected, in that case, we can't configure it at all.  Just
 | 
					
						
							|  |  |  |             // assume NMEA at 9600 baud.
 | 
					
						
							|  |  |  |             GPS *new_gps = new NMEAGPS(); | 
					
						
							|  |  |  |             new_gps->setup(); | 
					
						
							|  |  |  |             return new_gps; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2023-01-28 12:38:13 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-12-13 22:23:58 +00:00
										 |  |  |         GPS *new_gps = new NMEAGPS(); | 
					
						
							|  |  |  |         new_gps->setup(); | 
					
						
							|  |  |  |         return new_gps; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-15 02:00:20 +00:00
										 |  |  |     return nullptr; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-03-24 17:22:31 +00:00
										 |  |  | } |