mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 10:53:17 +00:00
GPS implementation (#216)
Implemented basic GPS support: - GPS HAL - GPS Service - GPS Settings app
This commit is contained in:
parent
2345ba6d13
commit
14e459e50f
@ -6,7 +6,7 @@
|
||||
// Power on
|
||||
#define TDECK_POWERON_GPIO GPIO_NUM_10
|
||||
|
||||
static bool tdeck_power_on() {
|
||||
static bool powerOn() {
|
||||
gpio_config_t device_power_signal_config = {
|
||||
.pin_bit_mask = BIT64(TDECK_POWERON_GPIO),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
@ -26,9 +26,9 @@ static bool tdeck_power_on() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tdeck_init_power() {
|
||||
bool tdeckInit() {
|
||||
ESP_LOGI(TAG, LOG_MESSAGE_POWER_ON_START);
|
||||
if (!tdeck_power_on()) {
|
||||
if (!powerOn()) {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_POWER_ON_FAILED);
|
||||
return false;
|
||||
}
|
||||
@ -9,10 +9,10 @@
|
||||
|
||||
#define TDECK_SPI_TRANSFER_SIZE_LIMIT (TDECK_LCD_HORIZONTAL_RESOLUTION * TDECK_LCD_SPI_TRANSFER_HEIGHT * (TDECK_LCD_BITS_PER_PIXEL / 8))
|
||||
|
||||
bool tdeck_init_power();
|
||||
bool tdeckInit();
|
||||
|
||||
extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
.initBoot = tdeck_init_power,
|
||||
.initBoot = tdeckInit,
|
||||
.createDisplay = createDisplay,
|
||||
.createKeyboard = createKeyboard,
|
||||
.sdcard = createTdeckSdCard(),
|
||||
@ -39,7 +39,7 @@ extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
tt::hal::i2c::Configuration {
|
||||
.name = "External",
|
||||
.port = I2C_NUM_1,
|
||||
.initMode = tt::hal::i2c::InitMode::ByTactility,
|
||||
.initMode = tt::hal::i2c::InitMode::Disabled,
|
||||
.canReinit = true,
|
||||
.hasMutableConfiguration = true,
|
||||
.config = (i2c_config_t) {
|
||||
@ -84,8 +84,8 @@ extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
.uart {
|
||||
tt::hal::uart::Configuration {
|
||||
.port = UART_NUM_1,
|
||||
.initMode = tt::hal::uart::InitMode::ByTactility,
|
||||
.canReinit = false,
|
||||
.initMode = tt::hal::uart::InitMode::Disabled, // Let GPS driver control this interface
|
||||
.canReinit = true,
|
||||
.hasMutableConfiguration = false,
|
||||
.rxPin = GPIO_NUM_44,
|
||||
.txPin = GPIO_NUM_43,
|
||||
@ -94,7 +94,7 @@ extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
.rxBufferSize = 1024,
|
||||
.txBufferSize = 1024,
|
||||
.config = {
|
||||
.baud_rate = 9600,
|
||||
.baud_rate = 38400,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
@ -107,5 +107,12 @@ extern const tt::hal::Configuration lilygo_tdeck = {
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
.gps = {
|
||||
tt::hal::gps::GpsDevice::Configuration {
|
||||
.name = "Internal",
|
||||
.uartPort = UART_NUM_1,
|
||||
.baudRate = 38400
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -28,6 +28,7 @@ if (DEFINED ENV{ESP_IDF_VERSION})
|
||||
"Libraries/elf_loader"
|
||||
"Libraries/lvgl"
|
||||
"Libraries/lv_screenshot"
|
||||
"Libraries/minmea"
|
||||
"Libraries/QRCode"
|
||||
)
|
||||
|
||||
@ -77,6 +78,7 @@ if (NOT DEFINED ENV{ESP_IDF_VERSION})
|
||||
add_subdirectory(Libraries/FreeRTOS-Kernel)
|
||||
add_subdirectory(Libraries/lv_screenshot)
|
||||
add_subdirectory(Libraries/QRCode)
|
||||
add_subdirectory(Libraries/minmea)
|
||||
target_compile_definitions(freertos_kernel PUBLIC "projCOVERAGE_TEST=0")
|
||||
target_include_directories(freertos_kernel
|
||||
PUBLIC Boards/Simulator/Source # for FreeRTOSConfig.h
|
||||
|
||||
@ -39,6 +39,12 @@ Website: https://github.com/nayarsystems/posix_tz_db
|
||||
|
||||
License: [MIT](https://github.com/nayarsystems/posix_tz_db/blob/master/LICENSE)
|
||||
|
||||
### Minmea
|
||||
|
||||
Website: https://github.com/kosma/minmea
|
||||
|
||||
License: [WTFPL](https://github.com/kosma/minmea/blob/master/LICENSE.grants), [LGPL 3.0](https://github.com/kosma/minmea/blob/master/LICENSE.LGPL-3.0), [MIT](https://github.com/kosma/minmea/blob/master/LICENSE.MIT)
|
||||
|
||||
### Other Components
|
||||
|
||||
See `/components` for the respective projects and their licenses.
|
||||
|
||||
23
Libraries/minmea/CMakeLists.txt
Normal file
23
Libraries/minmea/CMakeLists.txt
Normal file
@ -0,0 +1,23 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
if (DEFINED ENV{ESP_IDF_VERSION})
|
||||
idf_component_register(
|
||||
SRC_DIRS "Source/"
|
||||
INCLUDE_DIRS "Include/"
|
||||
)
|
||||
|
||||
else()
|
||||
file(GLOB SOURCES "Source/*.c*")
|
||||
file(GLOB HEADERS "Include/*.h*")
|
||||
|
||||
add_library(minmea STATIC)
|
||||
|
||||
target_sources(minmea PRIVATE ${SOURCES})
|
||||
include_directories(minmea "Include/")
|
||||
target_include_directories(minmea PUBLIC "Include/")
|
||||
|
||||
endif()
|
||||
13
Libraries/minmea/COPYING
Normal file
13
Libraries/minmea/COPYING
Normal file
@ -0,0 +1,13 @@
|
||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
Version 2, December 2004
|
||||
|
||||
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim or modified
|
||||
copies of this license document, and changing it is allowed as long
|
||||
as the name is changed.
|
||||
|
||||
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. You just DO WHAT THE FUCK YOU WANT TO.
|
||||
295
Libraries/minmea/Include/minmea.h
Normal file
295
Libraries/minmea/Include/minmea.h
Normal file
@ -0,0 +1,295 @@
|
||||
/*
|
||||
* Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
|
||||
* This program is free software. It comes without any warranty, to the extent
|
||||
* permitted by applicable law. You can redistribute it and/or modify it under
|
||||
* the terms of the Do What The Fuck You Want To Public License, Version 2, as
|
||||
* published by Sam Hocevar. See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#ifndef MINMEA_H
|
||||
#define MINMEA_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#ifdef MINMEA_INCLUDE_COMPAT
|
||||
#include <minmea_compat.h>
|
||||
#endif
|
||||
|
||||
#ifndef MINMEA_MAX_SENTENCE_LENGTH
|
||||
#define MINMEA_MAX_SENTENCE_LENGTH 80
|
||||
#endif
|
||||
|
||||
enum minmea_sentence_id {
|
||||
MINMEA_INVALID = -1,
|
||||
MINMEA_UNKNOWN = 0,
|
||||
MINMEA_SENTENCE_GBS,
|
||||
MINMEA_SENTENCE_GGA,
|
||||
MINMEA_SENTENCE_GLL,
|
||||
MINMEA_SENTENCE_GSA,
|
||||
MINMEA_SENTENCE_GST,
|
||||
MINMEA_SENTENCE_GSV,
|
||||
MINMEA_SENTENCE_RMC,
|
||||
MINMEA_SENTENCE_VTG,
|
||||
MINMEA_SENTENCE_ZDA,
|
||||
};
|
||||
|
||||
struct minmea_float {
|
||||
int_least32_t value;
|
||||
int_least32_t scale;
|
||||
};
|
||||
|
||||
struct minmea_date {
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
};
|
||||
|
||||
struct minmea_time {
|
||||
int hours;
|
||||
int minutes;
|
||||
int seconds;
|
||||
int microseconds;
|
||||
};
|
||||
|
||||
struct minmea_sentence_gbs {
|
||||
struct minmea_time time;
|
||||
struct minmea_float err_latitude;
|
||||
struct minmea_float err_longitude;
|
||||
struct minmea_float err_altitude;
|
||||
int svid;
|
||||
struct minmea_float prob;
|
||||
struct minmea_float bias;
|
||||
struct minmea_float stddev;
|
||||
};
|
||||
|
||||
struct minmea_sentence_rmc {
|
||||
struct minmea_time time;
|
||||
bool valid;
|
||||
struct minmea_float latitude;
|
||||
struct minmea_float longitude;
|
||||
struct minmea_float speed;
|
||||
struct minmea_float course;
|
||||
struct minmea_date date;
|
||||
struct minmea_float variation;
|
||||
};
|
||||
|
||||
struct minmea_sentence_gga {
|
||||
struct minmea_time time;
|
||||
struct minmea_float latitude;
|
||||
struct minmea_float longitude;
|
||||
int fix_quality;
|
||||
int satellites_tracked;
|
||||
struct minmea_float hdop;
|
||||
struct minmea_float altitude; char altitude_units;
|
||||
struct minmea_float height; char height_units;
|
||||
struct minmea_float dgps_age;
|
||||
};
|
||||
|
||||
enum minmea_gll_status {
|
||||
MINMEA_GLL_STATUS_DATA_VALID = 'A',
|
||||
MINMEA_GLL_STATUS_DATA_NOT_VALID = 'V',
|
||||
};
|
||||
|
||||
// FAA mode added to some fields in NMEA 2.3.
|
||||
enum minmea_faa_mode {
|
||||
MINMEA_FAA_MODE_AUTONOMOUS = 'A',
|
||||
MINMEA_FAA_MODE_DIFFERENTIAL = 'D',
|
||||
MINMEA_FAA_MODE_ESTIMATED = 'E',
|
||||
MINMEA_FAA_MODE_MANUAL = 'M',
|
||||
MINMEA_FAA_MODE_SIMULATED = 'S',
|
||||
MINMEA_FAA_MODE_NOT_VALID = 'N',
|
||||
MINMEA_FAA_MODE_PRECISE = 'P',
|
||||
};
|
||||
|
||||
struct minmea_sentence_gll {
|
||||
struct minmea_float latitude;
|
||||
struct minmea_float longitude;
|
||||
struct minmea_time time;
|
||||
char status;
|
||||
char mode;
|
||||
};
|
||||
|
||||
struct minmea_sentence_gst {
|
||||
struct minmea_time time;
|
||||
struct minmea_float rms_deviation;
|
||||
struct minmea_float semi_major_deviation;
|
||||
struct minmea_float semi_minor_deviation;
|
||||
struct minmea_float semi_major_orientation;
|
||||
struct minmea_float latitude_error_deviation;
|
||||
struct minmea_float longitude_error_deviation;
|
||||
struct minmea_float altitude_error_deviation;
|
||||
};
|
||||
|
||||
enum minmea_gsa_mode {
|
||||
MINMEA_GPGSA_MODE_AUTO = 'A',
|
||||
MINMEA_GPGSA_MODE_FORCED = 'M',
|
||||
};
|
||||
|
||||
enum minmea_gsa_fix_type {
|
||||
MINMEA_GPGSA_FIX_NONE = 1,
|
||||
MINMEA_GPGSA_FIX_2D = 2,
|
||||
MINMEA_GPGSA_FIX_3D = 3,
|
||||
};
|
||||
|
||||
struct minmea_sentence_gsa {
|
||||
char mode;
|
||||
int fix_type;
|
||||
int sats[12];
|
||||
struct minmea_float pdop;
|
||||
struct minmea_float hdop;
|
||||
struct minmea_float vdop;
|
||||
};
|
||||
|
||||
struct minmea_sat_info {
|
||||
int nr;
|
||||
int elevation;
|
||||
int azimuth;
|
||||
int snr;
|
||||
};
|
||||
|
||||
struct minmea_sentence_gsv {
|
||||
int total_msgs;
|
||||
int msg_nr;
|
||||
int total_sats;
|
||||
struct minmea_sat_info sats[4];
|
||||
};
|
||||
|
||||
struct minmea_sentence_vtg {
|
||||
struct minmea_float true_track_degrees;
|
||||
struct minmea_float magnetic_track_degrees;
|
||||
struct minmea_float speed_knots;
|
||||
struct minmea_float speed_kph;
|
||||
enum minmea_faa_mode faa_mode;
|
||||
};
|
||||
|
||||
struct minmea_sentence_zda {
|
||||
struct minmea_time time;
|
||||
struct minmea_date date;
|
||||
int hour_offset;
|
||||
int minute_offset;
|
||||
};
|
||||
|
||||
/**
|
||||
* Calculate raw sentence checksum. Does not check sentence integrity.
|
||||
*/
|
||||
uint8_t minmea_checksum(const char *sentence);
|
||||
|
||||
/**
|
||||
* Check sentence validity and checksum. Returns true for valid sentences.
|
||||
*/
|
||||
bool minmea_check(const char *sentence, bool strict);
|
||||
|
||||
/**
|
||||
* Determine talker identifier.
|
||||
*/
|
||||
bool minmea_talker_id(char talker[3], const char *sentence);
|
||||
|
||||
/**
|
||||
* Determine sentence identifier.
|
||||
*/
|
||||
enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
|
||||
|
||||
/**
|
||||
* Scanf-like processor for NMEA sentences. Supports the following formats:
|
||||
* c - single character (char *)
|
||||
* d - direction, returned as 1/-1, default 0 (int *)
|
||||
* f - fractional, returned as value + scale (struct minmea_float *)
|
||||
* i - decimal, default zero (int *)
|
||||
* s - string (char *)
|
||||
* t - talker identifier and type (char *)
|
||||
* D - date (struct minmea_date *)
|
||||
* T - time stamp (struct minmea_time *)
|
||||
* _ - ignore this field
|
||||
* ; - following fields are optional
|
||||
* Returns true on success. See library source code for details.
|
||||
*/
|
||||
bool minmea_scan(const char *sentence, const char *format, ...);
|
||||
|
||||
/*
|
||||
* Parse a specific type of sentence. Return true on success.
|
||||
*/
|
||||
bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence);
|
||||
bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence);
|
||||
bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence);
|
||||
bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence);
|
||||
bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence);
|
||||
bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence);
|
||||
bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence);
|
||||
bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence);
|
||||
bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence);
|
||||
|
||||
/**
|
||||
* Convert GPS UTC date/time representation to a UNIX calendar time.
|
||||
*/
|
||||
int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_);
|
||||
|
||||
/**
|
||||
* Convert GPS UTC date/time representation to a UNIX timestamp.
|
||||
*/
|
||||
int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_);
|
||||
|
||||
/**
|
||||
* Rescale a fixed-point value to a different scale. Rounds towards zero.
|
||||
*/
|
||||
static inline int_least32_t minmea_rescale(const struct minmea_float *f, int_least32_t new_scale)
|
||||
{
|
||||
if (f->scale == 0)
|
||||
return 0;
|
||||
if (f->scale == new_scale)
|
||||
return f->value;
|
||||
if (f->scale > new_scale)
|
||||
return (f->value + ((f->value > 0) - (f->value < 0)) * f->scale/new_scale/2) / (f->scale/new_scale);
|
||||
else
|
||||
return f->value * (new_scale/f->scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a fixed-point value to a floating-point value.
|
||||
* Returns NaN for "unknown" values.
|
||||
*/
|
||||
static inline float minmea_tofloat(const struct minmea_float *f)
|
||||
{
|
||||
if (f->scale == 0)
|
||||
return NAN;
|
||||
return (float) f->value / (float) f->scale;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a raw coordinate to a floating point DD.DDD... value.
|
||||
* Returns NaN for "unknown" values.
|
||||
*/
|
||||
static inline float minmea_tocoord(const struct minmea_float *f)
|
||||
{
|
||||
if (f->scale == 0)
|
||||
return NAN;
|
||||
if (f->scale > (INT_LEAST32_MAX / 100))
|
||||
return NAN;
|
||||
if (f->scale < (INT_LEAST32_MIN / 100))
|
||||
return NAN;
|
||||
int_least32_t degrees = f->value / (f->scale * 100);
|
||||
int_least32_t minutes = f->value % (f->scale * 100);
|
||||
return (float) degrees + (float) minutes / (60 * f->scale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether a character belongs to the set of characters allowed in a
|
||||
* sentence data field.
|
||||
*/
|
||||
static inline bool minmea_isfield(char c) {
|
||||
return isprint((unsigned char) c) && c != ',' && c != '*';
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MINMEA_H */
|
||||
|
||||
/* vim: set ts=4 sw=4 et: */
|
||||
165
Libraries/minmea/LICENSE.LGPL-3.0
Normal file
165
Libraries/minmea/LICENSE.LGPL-3.0
Normal file
@ -0,0 +1,165 @@
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
|
||||
This version of the GNU Lesser General Public License incorporates
|
||||
the terms and conditions of version 3 of the GNU General Public
|
||||
License, supplemented by the additional permissions listed below.
|
||||
|
||||
0. Additional Definitions.
|
||||
|
||||
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||
General Public License.
|
||||
|
||||
"The Library" refers to a covered work governed by this License,
|
||||
other than an Application or a Combined Work as defined below.
|
||||
|
||||
An "Application" is any work that makes use of an interface provided
|
||||
by the Library, but which is not otherwise based on the Library.
|
||||
Defining a subclass of a class defined by the Library is deemed a mode
|
||||
of using an interface provided by the Library.
|
||||
|
||||
A "Combined Work" is a work produced by combining or linking an
|
||||
Application with the Library. The particular version of the Library
|
||||
with which the Combined Work was made is also called the "Linked
|
||||
Version".
|
||||
|
||||
The "Minimal Corresponding Source" for a Combined Work means the
|
||||
Corresponding Source for the Combined Work, excluding any source code
|
||||
for portions of the Combined Work that, considered in isolation, are
|
||||
based on the Application, and not on the Linked Version.
|
||||
|
||||
The "Corresponding Application Code" for a Combined Work means the
|
||||
object code and/or source code for the Application, including any data
|
||||
and utility programs needed for reproducing the Combined Work from the
|
||||
Application, but excluding the System Libraries of the Combined Work.
|
||||
|
||||
1. Exception to Section 3 of the GNU GPL.
|
||||
|
||||
You may convey a covered work under sections 3 and 4 of this License
|
||||
without being bound by section 3 of the GNU GPL.
|
||||
|
||||
2. Conveying Modified Versions.
|
||||
|
||||
If you modify a copy of the Library, and, in your modifications, a
|
||||
facility refers to a function or data to be supplied by an Application
|
||||
that uses the facility (other than as an argument passed when the
|
||||
facility is invoked), then you may convey a copy of the modified
|
||||
version:
|
||||
|
||||
a) under this License, provided that you make a good faith effort to
|
||||
ensure that, in the event an Application does not supply the
|
||||
function or data, the facility still operates, and performs
|
||||
whatever part of its purpose remains meaningful, or
|
||||
|
||||
b) under the GNU GPL, with none of the additional permissions of
|
||||
this License applicable to that copy.
|
||||
|
||||
3. Object Code Incorporating Material from Library Header Files.
|
||||
|
||||
The object code form of an Application may incorporate material from
|
||||
a header file that is part of the Library. You may convey such object
|
||||
code under terms of your choice, provided that, if the incorporated
|
||||
material is not limited to numerical parameters, data structure
|
||||
layouts and accessors, or small macros, inline functions and templates
|
||||
(ten or fewer lines in length), you do both of the following:
|
||||
|
||||
a) Give prominent notice with each copy of the object code that the
|
||||
Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
4. Combined Works.
|
||||
|
||||
You may convey a Combined Work under terms of your choice that,
|
||||
taken together, effectively do not restrict modification of the
|
||||
portions of the Library contained in the Combined Work and reverse
|
||||
engineering for debugging such modifications, if you also do each of
|
||||
the following:
|
||||
|
||||
a) Give prominent notice with each copy of the Combined Work that
|
||||
the Library is used in it and that the Library and its use are
|
||||
covered by this License.
|
||||
|
||||
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||
document.
|
||||
|
||||
c) For a Combined Work that displays copyright notices during
|
||||
execution, include the copyright notice for the Library among
|
||||
these notices, as well as a reference directing the user to the
|
||||
copies of the GNU GPL and this license document.
|
||||
|
||||
d) Do one of the following:
|
||||
|
||||
0) Convey the Minimal Corresponding Source under the terms of this
|
||||
License, and the Corresponding Application Code in a form
|
||||
suitable for, and under terms that permit, the user to
|
||||
recombine or relink the Application with a modified version of
|
||||
the Linked Version to produce a modified Combined Work, in the
|
||||
manner specified by section 6 of the GNU GPL for conveying
|
||||
Corresponding Source.
|
||||
|
||||
1) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (a) uses at run time
|
||||
a copy of the Library already present on the user's computer
|
||||
system, and (b) will operate properly with a modified version
|
||||
of the Library that is interface-compatible with the Linked
|
||||
Version.
|
||||
|
||||
e) Provide Installation Information, but only if you would otherwise
|
||||
be required to provide such information under section 6 of the
|
||||
GNU GPL, and only to the extent that such information is
|
||||
necessary to install and execute a modified version of the
|
||||
Combined Work produced by recombining or relinking the
|
||||
Application with a modified version of the Linked Version. (If
|
||||
you use option 4d0, the Installation Information must accompany
|
||||
the Minimal Corresponding Source and Corresponding Application
|
||||
Code. If you use option 4d1, you must provide the Installation
|
||||
Information in the manner specified by section 6 of the GNU GPL
|
||||
for conveying Corresponding Source.)
|
||||
|
||||
5. Combined Libraries.
|
||||
|
||||
You may place library facilities that are a work based on the
|
||||
Library side by side in a single library together with other library
|
||||
facilities that are not Applications and are not covered by this
|
||||
License, and convey such a combined library under terms of your
|
||||
choice, if you do both of the following:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based
|
||||
on the Library, uncombined with any other library facilities,
|
||||
conveyed under the terms of this License.
|
||||
|
||||
b) Give prominent notice with the combined library that part of it
|
||||
is a work based on the Library, and explaining where to find the
|
||||
accompanying uncombined form of the same work.
|
||||
|
||||
6. Revised Versions of the GNU Lesser General Public License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions
|
||||
of the GNU Lesser General Public License from time to time. Such new
|
||||
versions will be similar in spirit to the present version, but may
|
||||
differ in detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Library as you received it specifies that a certain numbered version
|
||||
of the GNU Lesser General Public License "or any later version"
|
||||
applies to it, you have the option of following the terms and
|
||||
conditions either of that published version or of any later version
|
||||
published by the Free Software Foundation. If the Library as you
|
||||
received it does not specify a version number of the GNU Lesser
|
||||
General Public License, you may choose any version of the GNU Lesser
|
||||
General Public License ever published by the Free Software Foundation.
|
||||
|
||||
If the Library as you received it specifies that a proxy can decide
|
||||
whether future versions of the GNU Lesser General Public License shall
|
||||
apply, that proxy's public statement of acceptance of any version is
|
||||
permanent authorization for you to choose that version for the
|
||||
Library.
|
||||
9
Libraries/minmea/LICENSE.MIT
Normal file
9
Libraries/minmea/LICENSE.MIT
Normal file
@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 Kosma Moczek
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
7
Libraries/minmea/LICENSE.grants
Normal file
7
Libraries/minmea/LICENSE.grants
Normal file
@ -0,0 +1,7 @@
|
||||
At your option, if WTFPL is unacceptable for your legal department, I hereby
|
||||
explicitly grant you the right to use any the following licenses for minmea:
|
||||
|
||||
1. MIT License
|
||||
2. LGPL-3.0 License or later
|
||||
|
||||
See accompanying license files for full license text.
|
||||
175
Libraries/minmea/README.md
Normal file
175
Libraries/minmea/README.md
Normal file
@ -0,0 +1,175 @@
|
||||
# minmea, a lightweight GPS NMEA 0183 parser library
|
||||
|
||||
[](https://github.com/kosma/minmea/actions/workflows/c-cpp.yml)
|
||||
|
||||
Minmea is a minimalistic GPS parser library written in pure C intended for
|
||||
resource-constrained platforms, especially microcontrollers and other embedded
|
||||
systems.
|
||||
|
||||
## Features
|
||||
|
||||
* Written in ISO C99.
|
||||
* No dynamic memory allocation.
|
||||
* No floating point usage in the core library.
|
||||
* Supports both fixed and floating point values.
|
||||
* One source file and one header - can't get any simpler.
|
||||
* Tested under Linux, OS X, Windows and embedded ARM GCC.
|
||||
* Easily extendable to support new sentences.
|
||||
* Complete with a test suite and static analysis.
|
||||
|
||||
## Supported sentences
|
||||
|
||||
* ``GBS`` (Satellite Fault Detection)
|
||||
* ``GGA`` (Fix Data)
|
||||
* ``GLL`` (Geographic Position: Latitude/Longitude)
|
||||
* ``GSA`` (DOP and active satellites)
|
||||
* ``GST`` (Pseudorange Noise Statistics)
|
||||
* ``GSV`` (Satellites in view)
|
||||
* ``RMC`` (Recommended Minimum: position, velocity, time)
|
||||
* ``VTG`` (Track made good and Ground speed)
|
||||
* ``ZDA`` (Time & Date - UTC, day, month, year and local time zone)
|
||||
|
||||
Adding support for more sentences is trivial; see ``minmea.c`` source. Good documentation
|
||||
on NMEA is at https://gpsd.gitlab.io/gpsd/NMEA.html
|
||||
|
||||
## Compatibility
|
||||
|
||||
Minmea runs out-of-the-box under most Unix-compatible systems. Support for non-Unix systems
|
||||
(including native Windows builds under MSVC) is provided via compatibility headers:
|
||||
|
||||
1. Define `MINMEA_INCLUDE_COMPAT` in the build environment.
|
||||
2. Add appropriate compatibility header from under `compat/` directory as `minmea_compat.h`.
|
||||
|
||||
If your GPS receiver outputs very long sentences, consider increasing `MINMEA_MAX_SENTENCE_LENGTH`
|
||||
in your build environment.
|
||||
|
||||
## Fractional number format
|
||||
|
||||
Internally, minmea stores fractional numbers as pairs of two integers: ``{value, scale}``.
|
||||
For example, a value of ``"-123.456"`` would be parsed as ``{-123456, 1000}``. As this
|
||||
format is quite unwieldy, minmea provides the following convenience functions for converting
|
||||
to either fixed-point or floating-point format:
|
||||
|
||||
* ``minmea_rescale({-123456, 1000}, 10) => -1235``
|
||||
* ``minmea_float({-123456, 1000}) => -123.456``
|
||||
|
||||
The compound type ``struct minmea_float`` uses ``int_least32_t`` internally. Therefore,
|
||||
the coordinate precision is guaranteed to be at least ``[+-]DDDMM.MMMMM`` (five decimal digits)
|
||||
or ±2cm LSB at the equator. Note that GPS modules commonly only provide four decimal digits
|
||||
(``[+-]DDDMM.MMMM``), which equates to ±20cm (0.0001 minute is 0.0001/60 degrees and one degree
|
||||
is about 111km) at the equator.
|
||||
|
||||
## Coordinate format
|
||||
|
||||
NMEA uses the clunky ``DDMM.MMMM`` format which, honestly, is not good in the internet era.
|
||||
Internally, minmea stores it as a fractional number (see above); for practical uses,
|
||||
the value should be probably converted to the DD.DDDDD floating point format using the
|
||||
following function:
|
||||
|
||||
* ``minmea_tocoord({-375165, 100}) => -37.860832``
|
||||
|
||||
The library doesn't perform this conversion automatically for the following reasons:
|
||||
|
||||
* The conversion is not reversible.
|
||||
* It requires floating point support.
|
||||
* The user might want to perform this conversion later on or retain the original values.
|
||||
|
||||
## Example
|
||||
|
||||
```c
|
||||
char line[MINMEA_MAX_SENTENCE_LENGTH];
|
||||
while (fgets(line, sizeof(line), stdin) != NULL) {
|
||||
switch (minmea_sentence_id(line, false)) {
|
||||
case MINMEA_SENTENCE_RMC: {
|
||||
struct minmea_sentence_rmc frame;
|
||||
if (minmea_parse_rmc(&frame, line)) {
|
||||
printf("$RMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d\n",
|
||||
frame.latitude.value, frame.latitude.scale,
|
||||
frame.longitude.value, frame.longitude.scale,
|
||||
frame.speed.value, frame.speed.scale);
|
||||
printf("$RMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d\n",
|
||||
minmea_rescale(&frame.latitude, 1000),
|
||||
minmea_rescale(&frame.longitude, 1000),
|
||||
minmea_rescale(&frame.speed, 1000));
|
||||
printf("$RMC floating point degree coordinates and speed: (%f,%f) %f\n",
|
||||
minmea_tocoord(&frame.latitude),
|
||||
minmea_tocoord(&frame.longitude),
|
||||
minmea_tofloat(&frame.speed));
|
||||
}
|
||||
} break;
|
||||
|
||||
case MINMEA_SENTENCE_GGA: {
|
||||
struct minmea_sentence_gga frame;
|
||||
if (minmea_parse_gga(&frame, line)) {
|
||||
printf("$GGA: fix quality: %d\n", frame.fix_quality);
|
||||
}
|
||||
} break;
|
||||
|
||||
case MINMEA_SENTENCE_GSV: {
|
||||
struct minmea_sentence_gsv frame;
|
||||
if (minmea_parse_gsv(&frame, line)) {
|
||||
printf("$GSV: message %d of %d\n", frame.msg_nr, frame.total_msgs);
|
||||
printf("$GSV: satellites in view: %d\n", frame.total_sats);
|
||||
for (int i = 0; i < 4; i++)
|
||||
printf("$GSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm\n",
|
||||
frame.sats[i].nr,
|
||||
frame.sats[i].elevation,
|
||||
frame.sats[i].azimuth,
|
||||
frame.sats[i].snr);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Integration with your project
|
||||
|
||||
Simply add ``minmea.[ch]`` to your project, ``#include "minmea.h"`` and you're
|
||||
good to go.
|
||||
|
||||
## Running unit tests
|
||||
|
||||
Building and running the tests requires the following:
|
||||
|
||||
* CMake
|
||||
* Check Framework (https://libcheck.github.io/check/).
|
||||
* Clang Static Analyzer (https://clang-analyzer.llvm.org/).
|
||||
|
||||
If you have both in your ``$PATH``, running the tests should be as simple as:
|
||||
|
||||
```
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ../
|
||||
make
|
||||
make test
|
||||
```
|
||||
|
||||
## Limitations
|
||||
|
||||
* Only a handful of frames is supported right now.
|
||||
* There's no support for omitting parts of the library from building. As
|
||||
a workaround, use the ``-ffunction-sections -Wl,--gc-sections`` linker flags
|
||||
(or equivalent) to remove the unused functions (parsers) from the final image.
|
||||
* Some systems lack ``timegm``. On these systems, the recommended course of
|
||||
action is to build with ``-Dtimegm=mktime`` which will work correctly as long
|
||||
the system runs in the default ``UTC`` timezone.
|
||||
|
||||
## Contributing
|
||||
|
||||
1. Use the GitHub pull request system.
|
||||
2. Make sure to follow to existing style (naming, indentation, etc.)
|
||||
3. Write unit tests for any new functionality you add.
|
||||
4. Be aware you're submitting your work under the repository's license.
|
||||
|
||||
## Licensing
|
||||
|
||||
Minmea is open source software; see ``COPYING`` for amusement. Email me if the
|
||||
license bothers you and I'll happily re-license under anything else under the sun.
|
||||
|
||||
## Author
|
||||
|
||||
Minmea was written by Kosma Moczek <kosma@kosma.pl> and Patryk Szymczak
|
||||
<patryk.szymczak@gmail.com> at Cloud Your Car, with bugs fixed by countless
|
||||
good people.
|
||||
679
Libraries/minmea/Source/minmea.c
Normal file
679
Libraries/minmea/Source/minmea.c
Normal file
@ -0,0 +1,679 @@
|
||||
/*
|
||||
* Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
|
||||
* This program is free software. It comes without any warranty, to the extent
|
||||
* permitted by applicable law. You can redistribute it and/or modify it under
|
||||
* the terms of the Do What The Fuck You Want To Public License, Version 2, as
|
||||
* published by Sam Hocevar. See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "minmea.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define boolstr(s) ((s) ? "true" : "false")
|
||||
|
||||
static int hex2int(char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
return c - '0';
|
||||
if (c >= 'A' && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
if (c >= 'a' && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t minmea_checksum(const char *sentence)
|
||||
{
|
||||
// Support senteces with or without the starting dollar sign.
|
||||
if (*sentence == '$')
|
||||
sentence++;
|
||||
|
||||
uint8_t checksum = 0x00;
|
||||
|
||||
// The optional checksum is an XOR of all bytes between "$" and "*".
|
||||
while (*sentence && *sentence != '*')
|
||||
checksum ^= *sentence++;
|
||||
|
||||
return checksum;
|
||||
}
|
||||
|
||||
bool minmea_check(const char *sentence, bool strict)
|
||||
{
|
||||
uint8_t checksum = 0x00;
|
||||
|
||||
// A valid sentence starts with "$".
|
||||
if (*sentence++ != '$')
|
||||
return false;
|
||||
|
||||
// The optional checksum is an XOR of all bytes between "$" and "*".
|
||||
while (*sentence && *sentence != '*' && isprint((unsigned char) *sentence))
|
||||
checksum ^= *sentence++;
|
||||
|
||||
// If checksum is present...
|
||||
if (*sentence == '*') {
|
||||
// Extract checksum.
|
||||
sentence++;
|
||||
int upper = hex2int(*sentence++);
|
||||
if (upper == -1)
|
||||
return false;
|
||||
int lower = hex2int(*sentence++);
|
||||
if (lower == -1)
|
||||
return false;
|
||||
int expected = upper << 4 | lower;
|
||||
|
||||
// Check for checksum mismatch.
|
||||
if (checksum != expected)
|
||||
return false;
|
||||
} else if (strict) {
|
||||
// Discard non-checksummed frames in strict mode.
|
||||
return false;
|
||||
}
|
||||
|
||||
// The only stuff allowed at this point is a newline.
|
||||
while (*sentence == '\r' || *sentence == '\n') {
|
||||
sentence++;
|
||||
}
|
||||
|
||||
if (*sentence) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_scan(const char *sentence, const char *format, ...)
|
||||
{
|
||||
bool result = false;
|
||||
bool optional = false;
|
||||
|
||||
if (sentence == NULL)
|
||||
return false;
|
||||
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
|
||||
const char *field = sentence;
|
||||
#define next_field() \
|
||||
do { \
|
||||
/* Progress to the next field. */ \
|
||||
while (minmea_isfield(*sentence)) \
|
||||
sentence++; \
|
||||
/* Make sure there is a field there. */ \
|
||||
if (*sentence == ',') { \
|
||||
sentence++; \
|
||||
field = sentence; \
|
||||
} else { \
|
||||
field = NULL; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
while (*format) {
|
||||
char type = *format++;
|
||||
|
||||
if (type == ';') {
|
||||
// All further fields are optional.
|
||||
optional = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!field && !optional) {
|
||||
// Field requested but we ran out if input. Bail out.
|
||||
goto parse_error;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case 'c': { // Single character field (char).
|
||||
char value = '\0';
|
||||
|
||||
if (field && minmea_isfield(*field))
|
||||
value = *field;
|
||||
|
||||
*va_arg(ap, char *) = value;
|
||||
} break;
|
||||
|
||||
case 'd': { // Single character direction field (int).
|
||||
int value = 0;
|
||||
|
||||
if (field && minmea_isfield(*field)) {
|
||||
switch (*field) {
|
||||
case 'N':
|
||||
case 'E':
|
||||
value = 1;
|
||||
break;
|
||||
case 'S':
|
||||
case 'W':
|
||||
value = -1;
|
||||
break;
|
||||
default:
|
||||
goto parse_error;
|
||||
}
|
||||
}
|
||||
|
||||
*va_arg(ap, int *) = value;
|
||||
} break;
|
||||
|
||||
case 'f': { // Fractional value with scale (struct minmea_float).
|
||||
int sign = 0;
|
||||
int_least32_t value = -1;
|
||||
int_least32_t scale = 0;
|
||||
|
||||
if (field) {
|
||||
while (minmea_isfield(*field)) {
|
||||
if (*field == '+' && !sign && value == -1) {
|
||||
sign = 1;
|
||||
} else if (*field == '-' && !sign && value == -1) {
|
||||
sign = -1;
|
||||
} else if (isdigit((unsigned char) *field)) {
|
||||
int digit = *field - '0';
|
||||
if (value == -1)
|
||||
value = 0;
|
||||
if (value > (INT_LEAST32_MAX-digit) / 10) {
|
||||
/* we ran out of bits, what do we do? */
|
||||
if (scale) {
|
||||
/* truncate extra precision */
|
||||
break;
|
||||
} else {
|
||||
/* integer overflow. bail out. */
|
||||
goto parse_error;
|
||||
}
|
||||
}
|
||||
value = (10 * value) + digit;
|
||||
if (scale)
|
||||
scale *= 10;
|
||||
} else if (*field == '.' && scale == 0) {
|
||||
scale = 1;
|
||||
} else if (*field == ' ') {
|
||||
/* Allow spaces at the start of the field. Not NMEA
|
||||
* conformant, but some modules do this. */
|
||||
if (sign != 0 || value != -1 || scale != 0)
|
||||
goto parse_error;
|
||||
} else {
|
||||
goto parse_error;
|
||||
}
|
||||
field++;
|
||||
}
|
||||
}
|
||||
|
||||
if ((sign || scale) && value == -1)
|
||||
goto parse_error;
|
||||
|
||||
if (value == -1) {
|
||||
/* No digits were scanned. */
|
||||
value = 0;
|
||||
scale = 0;
|
||||
} else if (scale == 0) {
|
||||
/* No decimal point. */
|
||||
scale = 1;
|
||||
}
|
||||
if (sign)
|
||||
value *= sign;
|
||||
|
||||
*va_arg(ap, struct minmea_float *) = (struct minmea_float) {value, scale};
|
||||
} break;
|
||||
|
||||
case 'i': { // Integer value, default 0 (int).
|
||||
int value = 0;
|
||||
|
||||
if (field) {
|
||||
char *endptr;
|
||||
value = strtol(field, &endptr, 10);
|
||||
if (minmea_isfield(*endptr))
|
||||
goto parse_error;
|
||||
}
|
||||
|
||||
*va_arg(ap, int *) = value;
|
||||
} break;
|
||||
|
||||
case 's': { // String value (char *).
|
||||
char *buf = va_arg(ap, char *);
|
||||
|
||||
if (field) {
|
||||
while (minmea_isfield(*field))
|
||||
*buf++ = *field++;
|
||||
}
|
||||
|
||||
*buf = '\0';
|
||||
} break;
|
||||
|
||||
case 't': { // NMEA talker+sentence identifier (char *).
|
||||
// This field is always mandatory.
|
||||
if (!field)
|
||||
goto parse_error;
|
||||
|
||||
if (field[0] != '$')
|
||||
goto parse_error;
|
||||
for (int f=0; f<5; f++)
|
||||
if (!minmea_isfield(field[1+f]))
|
||||
goto parse_error;
|
||||
|
||||
char *buf = va_arg(ap, char *);
|
||||
memcpy(buf, field+1, 5);
|
||||
buf[5] = '\0';
|
||||
} break;
|
||||
|
||||
case 'D': { // Date (int, int, int), -1 if empty.
|
||||
struct minmea_date *date = va_arg(ap, struct minmea_date *);
|
||||
|
||||
int d = -1, m = -1, y = -1;
|
||||
|
||||
if (field && minmea_isfield(*field)) {
|
||||
// Always six digits.
|
||||
for (int f=0; f<6; f++)
|
||||
if (!isdigit((unsigned char) field[f]))
|
||||
goto parse_error;
|
||||
|
||||
char dArr[] = {field[0], field[1], '\0'};
|
||||
char mArr[] = {field[2], field[3], '\0'};
|
||||
char yArr[] = {field[4], field[5], '\0'};
|
||||
d = strtol(dArr, NULL, 10);
|
||||
m = strtol(mArr, NULL, 10);
|
||||
y = strtol(yArr, NULL, 10);
|
||||
}
|
||||
|
||||
date->day = d;
|
||||
date->month = m;
|
||||
date->year = y;
|
||||
} break;
|
||||
|
||||
case 'T': { // Time (int, int, int, int), -1 if empty.
|
||||
struct minmea_time *time_ = va_arg(ap, struct minmea_time *);
|
||||
|
||||
int h = -1, i = -1, s = -1, u = -1;
|
||||
|
||||
if (field && minmea_isfield(*field)) {
|
||||
// Minimum required: integer time.
|
||||
for (int f=0; f<6; f++)
|
||||
if (!isdigit((unsigned char) field[f]))
|
||||
goto parse_error;
|
||||
|
||||
char hArr[] = {field[0], field[1], '\0'};
|
||||
char iArr[] = {field[2], field[3], '\0'};
|
||||
char sArr[] = {field[4], field[5], '\0'};
|
||||
h = strtol(hArr, NULL, 10);
|
||||
i = strtol(iArr, NULL, 10);
|
||||
s = strtol(sArr, NULL, 10);
|
||||
field += 6;
|
||||
|
||||
// Extra: fractional time. Saved as microseconds.
|
||||
if (*field++ == '.') {
|
||||
uint32_t value = 0;
|
||||
uint32_t scale = 1000000LU;
|
||||
while (isdigit((unsigned char) *field) && scale > 1) {
|
||||
value = (value * 10) + (*field++ - '0');
|
||||
scale /= 10;
|
||||
}
|
||||
u = value * scale;
|
||||
} else {
|
||||
u = 0;
|
||||
}
|
||||
}
|
||||
|
||||
time_->hours = h;
|
||||
time_->minutes = i;
|
||||
time_->seconds = s;
|
||||
time_->microseconds = u;
|
||||
} break;
|
||||
|
||||
case '_': { // Ignore the field.
|
||||
} break;
|
||||
|
||||
default: { // Unknown.
|
||||
goto parse_error;
|
||||
}
|
||||
}
|
||||
|
||||
next_field();
|
||||
}
|
||||
|
||||
result = true;
|
||||
|
||||
parse_error:
|
||||
va_end(ap);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool minmea_talker_id(char talker[3], const char *sentence)
|
||||
{
|
||||
char type[6];
|
||||
if (!minmea_scan(sentence, "t", type))
|
||||
return false;
|
||||
|
||||
talker[0] = type[0];
|
||||
talker[1] = type[1];
|
||||
talker[2] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict)
|
||||
{
|
||||
if (!minmea_check(sentence, strict))
|
||||
return MINMEA_INVALID;
|
||||
|
||||
char type[6];
|
||||
if (!minmea_scan(sentence, "t", type))
|
||||
return MINMEA_INVALID;
|
||||
|
||||
if (!strcmp(type+2, "GBS"))
|
||||
return MINMEA_SENTENCE_GBS;
|
||||
if (!strcmp(type+2, "GGA"))
|
||||
return MINMEA_SENTENCE_GGA;
|
||||
if (!strcmp(type+2, "GLL"))
|
||||
return MINMEA_SENTENCE_GLL;
|
||||
if (!strcmp(type+2, "GSA"))
|
||||
return MINMEA_SENTENCE_GSA;
|
||||
if (!strcmp(type+2, "GST"))
|
||||
return MINMEA_SENTENCE_GST;
|
||||
if (!strcmp(type+2, "GSV"))
|
||||
return MINMEA_SENTENCE_GSV;
|
||||
if (!strcmp(type+2, "RMC"))
|
||||
return MINMEA_SENTENCE_RMC;
|
||||
if (!strcmp(type+2, "VTG"))
|
||||
return MINMEA_SENTENCE_VTG;
|
||||
if (!strcmp(type+2, "ZDA"))
|
||||
return MINMEA_SENTENCE_ZDA;
|
||||
|
||||
return MINMEA_UNKNOWN;
|
||||
}
|
||||
|
||||
bool minmea_parse_gbs(struct minmea_sentence_gbs *frame, const char *sentence)
|
||||
{
|
||||
// $GNGBS,170556.00,3.0,2.9,8.3,,,,*5C
|
||||
char type[6];
|
||||
if (!minmea_scan(sentence, "tTfffifff",
|
||||
type,
|
||||
&frame->time,
|
||||
&frame->err_latitude,
|
||||
&frame->err_longitude,
|
||||
&frame->err_altitude,
|
||||
&frame->svid,
|
||||
&frame->prob,
|
||||
&frame->bias,
|
||||
&frame->stddev
|
||||
))
|
||||
return false;
|
||||
if (strcmp(type+2, "GBS"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_rmc(struct minmea_sentence_rmc *frame, const char *sentence)
|
||||
{
|
||||
// $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62
|
||||
char type[6];
|
||||
char validity;
|
||||
int latitude_direction;
|
||||
int longitude_direction;
|
||||
int variation_direction;
|
||||
if (!minmea_scan(sentence, "tTcfdfdffDfd",
|
||||
type,
|
||||
&frame->time,
|
||||
&validity,
|
||||
&frame->latitude, &latitude_direction,
|
||||
&frame->longitude, &longitude_direction,
|
||||
&frame->speed,
|
||||
&frame->course,
|
||||
&frame->date,
|
||||
&frame->variation, &variation_direction))
|
||||
return false;
|
||||
if (strcmp(type+2, "RMC"))
|
||||
return false;
|
||||
|
||||
frame->valid = (validity == 'A');
|
||||
frame->latitude.value *= latitude_direction;
|
||||
frame->longitude.value *= longitude_direction;
|
||||
frame->variation.value *= variation_direction;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_gga(struct minmea_sentence_gga *frame, const char *sentence)
|
||||
{
|
||||
// $GPGGA,123519,4807.038,N,01131.000,E,1,08,0.9,545.4,M,46.9,M,,*47
|
||||
char type[6];
|
||||
int latitude_direction;
|
||||
int longitude_direction;
|
||||
|
||||
if (!minmea_scan(sentence, "tTfdfdiiffcfcf_",
|
||||
type,
|
||||
&frame->time,
|
||||
&frame->latitude, &latitude_direction,
|
||||
&frame->longitude, &longitude_direction,
|
||||
&frame->fix_quality,
|
||||
&frame->satellites_tracked,
|
||||
&frame->hdop,
|
||||
&frame->altitude, &frame->altitude_units,
|
||||
&frame->height, &frame->height_units,
|
||||
&frame->dgps_age))
|
||||
return false;
|
||||
if (strcmp(type+2, "GGA"))
|
||||
return false;
|
||||
|
||||
frame->latitude.value *= latitude_direction;
|
||||
frame->longitude.value *= longitude_direction;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_gsa(struct minmea_sentence_gsa *frame, const char *sentence)
|
||||
{
|
||||
// $GPGSA,A,3,04,05,,09,12,,,24,,,,,2.5,1.3,2.1*39
|
||||
char type[6];
|
||||
|
||||
if (!minmea_scan(sentence, "tciiiiiiiiiiiiifff",
|
||||
type,
|
||||
&frame->mode,
|
||||
&frame->fix_type,
|
||||
&frame->sats[0],
|
||||
&frame->sats[1],
|
||||
&frame->sats[2],
|
||||
&frame->sats[3],
|
||||
&frame->sats[4],
|
||||
&frame->sats[5],
|
||||
&frame->sats[6],
|
||||
&frame->sats[7],
|
||||
&frame->sats[8],
|
||||
&frame->sats[9],
|
||||
&frame->sats[10],
|
||||
&frame->sats[11],
|
||||
&frame->pdop,
|
||||
&frame->hdop,
|
||||
&frame->vdop))
|
||||
return false;
|
||||
if (strcmp(type+2, "GSA"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_gll(struct minmea_sentence_gll *frame, const char *sentence)
|
||||
{
|
||||
// $GPGLL,3723.2475,N,12158.3416,W,161229.487,A,A*41$;
|
||||
char type[6];
|
||||
int latitude_direction;
|
||||
int longitude_direction;
|
||||
|
||||
if (!minmea_scan(sentence, "tfdfdTc;c",
|
||||
type,
|
||||
&frame->latitude, &latitude_direction,
|
||||
&frame->longitude, &longitude_direction,
|
||||
&frame->time,
|
||||
&frame->status,
|
||||
&frame->mode))
|
||||
return false;
|
||||
if (strcmp(type+2, "GLL"))
|
||||
return false;
|
||||
|
||||
frame->latitude.value *= latitude_direction;
|
||||
frame->longitude.value *= longitude_direction;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_gst(struct minmea_sentence_gst *frame, const char *sentence)
|
||||
{
|
||||
// $GPGST,024603.00,3.2,6.6,4.7,47.3,5.8,5.6,22.0*58
|
||||
char type[6];
|
||||
|
||||
if (!minmea_scan(sentence, "tTfffffff",
|
||||
type,
|
||||
&frame->time,
|
||||
&frame->rms_deviation,
|
||||
&frame->semi_major_deviation,
|
||||
&frame->semi_minor_deviation,
|
||||
&frame->semi_major_orientation,
|
||||
&frame->latitude_error_deviation,
|
||||
&frame->longitude_error_deviation,
|
||||
&frame->altitude_error_deviation))
|
||||
return false;
|
||||
if (strcmp(type+2, "GST"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_gsv(struct minmea_sentence_gsv *frame, const char *sentence)
|
||||
{
|
||||
// $GPGSV,3,1,11,03,03,111,00,04,15,270,00,06,01,010,00,13,06,292,00*74
|
||||
// $GPGSV,3,3,11,22,42,067,42,24,14,311,43,27,05,244,00,,,,*4D
|
||||
// $GPGSV,4,2,11,08,51,203,30,09,45,215,28*75
|
||||
// $GPGSV,4,4,13,39,31,170,27*40
|
||||
// $GPGSV,4,4,13*7B
|
||||
char type[6];
|
||||
|
||||
if (!minmea_scan(sentence, "tiii;iiiiiiiiiiiiiiii",
|
||||
type,
|
||||
&frame->total_msgs,
|
||||
&frame->msg_nr,
|
||||
&frame->total_sats,
|
||||
&frame->sats[0].nr,
|
||||
&frame->sats[0].elevation,
|
||||
&frame->sats[0].azimuth,
|
||||
&frame->sats[0].snr,
|
||||
&frame->sats[1].nr,
|
||||
&frame->sats[1].elevation,
|
||||
&frame->sats[1].azimuth,
|
||||
&frame->sats[1].snr,
|
||||
&frame->sats[2].nr,
|
||||
&frame->sats[2].elevation,
|
||||
&frame->sats[2].azimuth,
|
||||
&frame->sats[2].snr,
|
||||
&frame->sats[3].nr,
|
||||
&frame->sats[3].elevation,
|
||||
&frame->sats[3].azimuth,
|
||||
&frame->sats[3].snr
|
||||
)) {
|
||||
return false;
|
||||
}
|
||||
if (strcmp(type+2, "GSV"))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_vtg(struct minmea_sentence_vtg *frame, const char *sentence)
|
||||
{
|
||||
// $GPVTG,054.7,T,034.4,M,005.5,N,010.2,K*48
|
||||
// $GPVTG,156.1,T,140.9,M,0.0,N,0.0,K*41
|
||||
// $GPVTG,096.5,T,083.5,M,0.0,N,0.0,K,D*22
|
||||
// $GPVTG,188.36,T,,M,0.820,N,1.519,K,A*3F
|
||||
char type[6];
|
||||
char c_true, c_magnetic, c_knots, c_kph, c_faa_mode;
|
||||
|
||||
if (!minmea_scan(sentence, "t;fcfcfcfcc",
|
||||
type,
|
||||
&frame->true_track_degrees,
|
||||
&c_true,
|
||||
&frame->magnetic_track_degrees,
|
||||
&c_magnetic,
|
||||
&frame->speed_knots,
|
||||
&c_knots,
|
||||
&frame->speed_kph,
|
||||
&c_kph,
|
||||
&c_faa_mode))
|
||||
return false;
|
||||
if (strcmp(type+2, "VTG"))
|
||||
return false;
|
||||
// values are only valid with the accompanying characters
|
||||
if (c_true != 'T')
|
||||
frame->true_track_degrees.scale = 0;
|
||||
if (c_magnetic != 'M')
|
||||
frame->magnetic_track_degrees.scale = 0;
|
||||
if (c_knots != 'N')
|
||||
frame->speed_knots.scale = 0;
|
||||
if (c_kph != 'K')
|
||||
frame->speed_kph.scale = 0;
|
||||
frame->faa_mode = (enum minmea_faa_mode)c_faa_mode;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool minmea_parse_zda(struct minmea_sentence_zda *frame, const char *sentence)
|
||||
{
|
||||
// $GPZDA,201530.00,04,07,2002,00,00*60
|
||||
char type[6];
|
||||
|
||||
if(!minmea_scan(sentence, "tTiiiii",
|
||||
type,
|
||||
&frame->time,
|
||||
&frame->date.day,
|
||||
&frame->date.month,
|
||||
&frame->date.year,
|
||||
&frame->hour_offset,
|
||||
&frame->minute_offset))
|
||||
return false;
|
||||
if (strcmp(type+2, "ZDA"))
|
||||
return false;
|
||||
|
||||
// check offsets
|
||||
if (abs(frame->hour_offset) > 13 ||
|
||||
frame->minute_offset > 59 ||
|
||||
frame->minute_offset < 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int minmea_getdatetime(struct tm *tm, const struct minmea_date *date, const struct minmea_time *time_)
|
||||
{
|
||||
if (date->year == -1 || time_->hours == -1)
|
||||
return -1;
|
||||
|
||||
memset(tm, 0, sizeof(*tm));
|
||||
if (date->year < 80) {
|
||||
tm->tm_year = 2000 + date->year - 1900; // 2000-2079
|
||||
} else if (date->year >= 1900) {
|
||||
tm->tm_year = date->year - 1900; // 4 digit year, use directly
|
||||
} else {
|
||||
tm->tm_year = date->year; // 1980-1999
|
||||
}
|
||||
tm->tm_mon = date->month - 1;
|
||||
tm->tm_mday = date->day;
|
||||
tm->tm_hour = time_->hours;
|
||||
tm->tm_min = time_->minutes;
|
||||
tm->tm_sec = time_->seconds;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int minmea_gettime(struct timespec *ts, const struct minmea_date *date, const struct minmea_time *time_)
|
||||
{
|
||||
struct tm tm;
|
||||
if (minmea_getdatetime(&tm, date, time_))
|
||||
return -1;
|
||||
|
||||
time_t timestamp = mktime(&tm); /* See README.md if your system lacks timegm(). */
|
||||
if (timestamp != (time_t)-1) {
|
||||
ts->tv_sec = timestamp;
|
||||
ts->tv_nsec = time_->microseconds * 1000;
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* vim: set ts=4 sw=4 et: */
|
||||
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace tt::app::gpssettings {
|
||||
|
||||
void start();
|
||||
|
||||
}
|
||||
@ -33,9 +33,10 @@ namespace app {
|
||||
namespace alertdialog { extern const AppManifest manifest; }
|
||||
namespace applist { extern const AppManifest manifest; }
|
||||
namespace boot { extern const AppManifest manifest; }
|
||||
namespace display { extern const AppManifest manifest; }
|
||||
namespace files { extern const AppManifest manifest; }
|
||||
namespace gpio { extern const AppManifest manifest; }
|
||||
namespace display { extern const AppManifest manifest; }
|
||||
namespace gpssettings { extern const AppManifest manifest; }
|
||||
namespace i2cscanner { extern const AppManifest manifest; }
|
||||
namespace i2csettings { extern const AppManifest manifest; }
|
||||
namespace imageviewer { extern const AppManifest manifest; }
|
||||
@ -72,6 +73,7 @@ static void registerSystemApps() {
|
||||
addApp(app::display::manifest);
|
||||
addApp(app::files::manifest);
|
||||
addApp(app::gpio::manifest);
|
||||
addApp(app::gpssettings::manifest);
|
||||
addApp(app::i2cscanner::manifest);
|
||||
addApp(app::i2csettings::manifest);
|
||||
addApp(app::imageviewer::manifest);
|
||||
|
||||
79
Tactility/Source/app/gpssettings/GpsSettings.cpp
Normal file
79
Tactility/Source/app/gpssettings/GpsSettings.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
#include "Tactility/app/AppManifest.h"
|
||||
#include "Tactility/lvgl/Toolbar.h"
|
||||
#include "Tactility/service/loader/Loader.h"
|
||||
#include <Tactility/service/gps/Gps.h>
|
||||
|
||||
#include <lvgl.h>
|
||||
|
||||
#define TAG "text_viewer"
|
||||
|
||||
namespace tt::app::gpssettings {
|
||||
|
||||
extern const AppManifest manifest;
|
||||
|
||||
class GpsSettingsApp final : public App {
|
||||
|
||||
private:
|
||||
|
||||
static void onGpsToggled(lv_event_t* event) {
|
||||
auto* widget = lv_event_get_target_obj(event);
|
||||
|
||||
bool wants_on = lv_obj_has_state(widget, LV_STATE_CHECKED);
|
||||
bool is_on = service::gps::isReceiving();
|
||||
|
||||
if (wants_on != is_on) {
|
||||
if (wants_on) {
|
||||
if (!service::gps::startReceiving()) {
|
||||
TT_LOG_E(TAG, "Failed to toggle GPS on");
|
||||
lv_obj_remove_state(widget, LV_STATE_CHECKED);
|
||||
}
|
||||
} else {
|
||||
service::gps::stopReceiving();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
void onShow(AppContext& app, lv_obj_t* parent) final {
|
||||
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
||||
|
||||
lvgl::toolbar_create(parent, app);
|
||||
|
||||
auto* main_wrapper = lv_obj_create(parent);
|
||||
lv_obj_set_flex_flow(main_wrapper, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_width(main_wrapper, LV_PCT(100));
|
||||
lv_obj_set_flex_grow(main_wrapper, 1);
|
||||
|
||||
auto* top_wrapper = lv_obj_create(main_wrapper);
|
||||
lv_obj_set_width(top_wrapper, LV_PCT(100));
|
||||
lv_obj_set_height(top_wrapper, LV_SIZE_CONTENT);
|
||||
lv_obj_set_style_pad_all(top_wrapper, 0, 0);
|
||||
lv_obj_set_style_border_width(top_wrapper, 0, 0);
|
||||
|
||||
auto* toggle_label = lv_label_create(top_wrapper);
|
||||
lv_label_set_text(toggle_label, "GPS receiver");
|
||||
|
||||
auto* toggle_switch = lv_switch_create(top_wrapper);
|
||||
lv_obj_align(toggle_switch, LV_ALIGN_TOP_RIGHT, 0, 0);
|
||||
|
||||
if (service::gps::isReceiving()) {
|
||||
lv_obj_add_state(toggle_switch, LV_STATE_CHECKED);
|
||||
}
|
||||
|
||||
lv_obj_add_event_cb(toggle_switch, onGpsToggled, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
extern const AppManifest manifest = {
|
||||
.id = "GpsSettings",
|
||||
.name = "GPS",
|
||||
.type = Type::Settings,
|
||||
.createApp = create<GpsSettingsApp>
|
||||
};
|
||||
|
||||
void start() {
|
||||
service::loader::startApp(manifest.id);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@ -52,6 +52,8 @@ uint32_t getTickFrequency();
|
||||
|
||||
TickType_t getTicks();
|
||||
|
||||
inline size_t getMillis() { return getTicks() / portTICK_PERIOD_MS; }
|
||||
|
||||
/** Delay execution
|
||||
* @warning don't call from ISR context
|
||||
* Also keep in mind delay is aliased to scheduler timer intervals.
|
||||
@ -66,6 +68,14 @@ void delayTicks(TickType_t ticks);
|
||||
*/
|
||||
bool delayUntilTick(TickType_t tick);
|
||||
|
||||
constexpr inline TickType_t secondsToTicks(uint32_t seconds) {
|
||||
return (TickType_t)seconds * 1000U / portTICK_PERIOD_MS;
|
||||
}
|
||||
|
||||
constexpr inline TickType_t minutesToTicks(uint32_t minutes) {
|
||||
return secondsToTicks(minutes * 60U);
|
||||
}
|
||||
|
||||
/** Convert milliseconds to ticks
|
||||
*
|
||||
* @param[in] milliSeconds time in milliseconds
|
||||
|
||||
@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
if (DEFINED ENV{ESP_IDF_VERSION})
|
||||
file(GLOB_RECURSE SOURCE_FILES Source/*.c*)
|
||||
|
||||
list(APPEND REQUIRES_LIST TactilityCore esp_wifi nvs_flash driver spiffs vfs fatfs lwip)
|
||||
list(APPEND REQUIRES_LIST TactilityCore minmea esp_wifi nvs_flash driver spiffs vfs fatfs lwip)
|
||||
if("${IDF_TARGET}" STREQUAL "esp32s3")
|
||||
list(APPEND REQUIRES_LIST esp_tinyusb)
|
||||
endif()
|
||||
@ -51,5 +51,6 @@ else()
|
||||
target_link_libraries(TactilityHeadless
|
||||
PUBLIC TactilityCore
|
||||
PUBLIC freertos_kernel
|
||||
PUBLIC minmea
|
||||
)
|
||||
endif()
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Tactility/hal/gps/GpsDevice.h"
|
||||
#include "Tactility/hal/sdcard/SdCardDevice.h"
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
#include "Tactility/hal/uart/Uart.h"
|
||||
@ -44,14 +45,17 @@ struct Configuration {
|
||||
/** An optional power interface for battery or other power delivery. */
|
||||
const CreatePower _Nullable power = nullptr;
|
||||
|
||||
/** A list of I2C interfaces */
|
||||
/** A list of I2C interface configurations */
|
||||
const std::vector<i2c::Configuration> i2c = {};
|
||||
|
||||
/** A list of SPI interfaces */
|
||||
/** A list of SPI interface configurations */
|
||||
const std::vector<spi::Configuration> spi = {};
|
||||
|
||||
/** A list of UART interfaces */
|
||||
/** A list of UART interface configurations */
|
||||
const std::vector<uart::Configuration> uart = {};
|
||||
|
||||
/** A list of GPS device configurations */
|
||||
const std::vector<gps::GpsDevice::Configuration> gps = {};
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -22,7 +22,8 @@ public:
|
||||
Touch,
|
||||
SdCard,
|
||||
Keyboard,
|
||||
Power
|
||||
Power,
|
||||
Gps
|
||||
};
|
||||
|
||||
typedef uint32_t Id;
|
||||
@ -36,7 +37,7 @@ public:
|
||||
Device();
|
||||
virtual ~Device() = default;
|
||||
|
||||
Id getId() const { return id; }
|
||||
inline Id getId() const { return id; }
|
||||
|
||||
/** The type of device. */
|
||||
virtual Type getType() const = 0;
|
||||
|
||||
99
TactilityHeadless/Include/Tactility/hal/gps/GpsDevice.h
Normal file
99
TactilityHeadless/Include/Tactility/hal/gps/GpsDevice.h
Normal file
@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Device.h"
|
||||
#include "../uart/Uart.h"
|
||||
#include "GpsDeviceInitDefault.h"
|
||||
#include "Satellites.h"
|
||||
|
||||
#include <Tactility/Mutex.h>
|
||||
#include <Tactility/Thread.h>
|
||||
|
||||
#include <minmea.h>
|
||||
#include <utility>
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
class GpsDevice : public Device {
|
||||
|
||||
public:
|
||||
|
||||
typedef int SatelliteSubscriptionId;
|
||||
typedef int LocationSubscriptionId;
|
||||
|
||||
struct Configuration {
|
||||
std::string name;
|
||||
uart_port_t uartPort;
|
||||
uint32_t baudRate;
|
||||
std::function<bool(uart_port_t)> initFunction = initGpsDefault;
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
struct SatelliteSubscription {
|
||||
SatelliteSubscriptionId id;
|
||||
std::shared_ptr<std::function<void(Device::Id id, const minmea_sat_info&)>> onData;
|
||||
};
|
||||
|
||||
struct LocationSubscription {
|
||||
LocationSubscriptionId id;
|
||||
std::shared_ptr<std::function<void(Device::Id id, const minmea_sentence_rmc&)>> onData;
|
||||
};
|
||||
|
||||
const Configuration configuration;
|
||||
Mutex mutex;
|
||||
std::unique_ptr<Thread> thread;
|
||||
bool threadInterrupted = false;
|
||||
std::vector<SatelliteSubscription> satelliteSubscriptions;
|
||||
std::vector<LocationSubscription> locationSubscriptions;
|
||||
SatelliteSubscriptionId lastSatelliteSubscriptionId = 0;
|
||||
LocationSubscriptionId lastLocationSubscriptionId = 0;
|
||||
|
||||
static int32_t threadMainStatic(void* parameter);
|
||||
int32_t threadMain();
|
||||
|
||||
bool isThreadInterrupted() const;
|
||||
|
||||
public:
|
||||
|
||||
explicit GpsDevice(Configuration configuration) : configuration(std::move(configuration)) {
|
||||
assert(this->configuration.initFunction != nullptr);
|
||||
}
|
||||
|
||||
~GpsDevice() override = default;
|
||||
|
||||
Type getType() const override { return Type::Gps; }
|
||||
|
||||
std::string getName() const override { return configuration.name; }
|
||||
std::string getDescription() const override { return ""; }
|
||||
|
||||
bool start();
|
||||
bool stop();
|
||||
|
||||
bool isStarted() const;
|
||||
|
||||
SatelliteSubscriptionId subscribeSatellites(const std::function<void(Device::Id deviceId, const minmea_sat_info&)>& onData) {
|
||||
satelliteSubscriptions.push_back({
|
||||
.id = ++lastSatelliteSubscriptionId,
|
||||
.onData = std::make_shared<std::function<void(Device::Id, const minmea_sat_info&)>>(onData)
|
||||
});
|
||||
return lastSatelliteSubscriptionId;
|
||||
}
|
||||
|
||||
void unsubscribeSatellites(SatelliteSubscriptionId subscriptionId) {
|
||||
std::erase_if(satelliteSubscriptions, [subscriptionId](auto& subscription) { return subscription.id == subscriptionId; });
|
||||
}
|
||||
|
||||
LocationSubscriptionId subscribeLocations(const std::function<void(Device::Id deviceId, const minmea_sentence_rmc&)>& onData) {
|
||||
locationSubscriptions.push_back({
|
||||
.id = ++lastLocationSubscriptionId,
|
||||
.onData = std::make_shared<std::function<void(Device::Id, const minmea_sentence_rmc&)>>(onData)
|
||||
});
|
||||
return lastLocationSubscriptionId;
|
||||
}
|
||||
|
||||
void unsubscribeLocations(SatelliteSubscriptionId subscriptionId) {
|
||||
std::erase_if(locationSubscriptions, [subscriptionId](auto& subscription) { return subscription.id == subscriptionId; });
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <Tactility/hal/uart/Uart.h>
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
bool initGpsDefault(uart_port_t port);
|
||||
|
||||
}
|
||||
59
TactilityHeadless/Include/Tactility/hal/gps/Satellites.h
Normal file
59
TactilityHeadless/Include/Tactility/hal/gps/Satellites.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <Tactility/RtosCompat.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#include <minmea.h>
|
||||
|
||||
#include <ranges>
|
||||
#include <memory>
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
/** Thread-safe storage of recent satellites */
|
||||
class SatelliteStorage {
|
||||
|
||||
public:
|
||||
|
||||
static constexpr size_t recordCount = 32;
|
||||
|
||||
private:
|
||||
|
||||
struct SatelliteRecord {
|
||||
minmea_sat_info data {
|
||||
.nr = 0,
|
||||
.elevation = 0,
|
||||
.azimuth = 0,
|
||||
.snr = 0
|
||||
};
|
||||
TickType_t lastUpdated = 0;
|
||||
bool inUse = false;
|
||||
};
|
||||
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
std::array<SatelliteRecord, recordCount> records;
|
||||
uint16_t recycleTimeSeconds;
|
||||
uint16_t recentTimeSeconds;
|
||||
|
||||
SatelliteRecord* findRecord(int number);
|
||||
|
||||
SatelliteRecord* findUnusedRecord();
|
||||
|
||||
SatelliteRecord* findRecordToRecycle();
|
||||
|
||||
/** Tries to find an existing record, otherwise return a free one, otherwise return the oldest active one */
|
||||
SatelliteRecord* findWithFallback(int number);
|
||||
|
||||
public:
|
||||
|
||||
explicit SatelliteStorage(
|
||||
uint16_t recycleTimeSeconds = 120,
|
||||
uint16_t recentTimeSeconds = 60
|
||||
) : recycleTimeSeconds(recycleTimeSeconds), recentTimeSeconds(recentTimeSeconds) {}
|
||||
|
||||
void notify(const minmea_sat_info& info);
|
||||
|
||||
void getRecords(const std::function<void(const minmea_sat_info&)>& onRecord) const;
|
||||
};
|
||||
|
||||
} // namespace tt::hal::gps
|
||||
@ -56,6 +56,19 @@ bool lock(uart_port_t port, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
bool unlock(uart_port_t port);
|
||||
|
||||
size_t read(uart_port_t port, uint8_t* buffer, size_t bufferSize, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
bool readByte(uart_port_t port, uint8_t* output, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
size_t write(uart_port_t port, const uint8_t* buffer, size_t bufferSize, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
bool writeString(uart_port_t port, const char* buffer, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
|
||||
size_t available(uart_port_t port, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
|
||||
bool setBaudRate(uart_port_t port, uint32_t baudRate, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
uint32_t getBaudRate(uart_port_t port);
|
||||
|
||||
void flush(uart_port_t port, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
void flushInput(uart_port_t port, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
|
||||
std::string readStringUntil(uart_port_t port, char untilChar, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
bool readUntil(uart_port_t port, uint8_t* buffer, size_t bufferSize, uint8_t untilByte, TickType_t timeout = 10 / portTICK_PERIOD_MS);
|
||||
|
||||
} // namespace tt::hal::uart
|
||||
|
||||
22
TactilityHeadless/Include/Tactility/service/gps/Gps.h
Normal file
22
TactilityHeadless/Include/Tactility/service/gps/Gps.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "Tactility/hal/gps/GpsDevice.h"
|
||||
|
||||
namespace tt::service::gps {
|
||||
|
||||
/** Register a hardware device to the GPS service. */
|
||||
void addGpsDevice(const std::shared_ptr<hal::gps::GpsDevice>& device);
|
||||
|
||||
/** Deregister a hardware device to the GPS service. */
|
||||
void removeGpsDevice(const std::shared_ptr<hal::gps::GpsDevice>& device);
|
||||
|
||||
/** @return true when GPS is set to receive updates from at least 1 device */
|
||||
bool startReceiving();
|
||||
|
||||
/** Turn GPS receiving off */
|
||||
void stopReceiving();
|
||||
|
||||
/** @return true when GPS receiver is on and 1 or more devices are active */
|
||||
bool isReceiving();
|
||||
|
||||
}
|
||||
10
TactilityHeadless/Include/Tactility/service/gps/GpsUtil.h
Normal file
10
TactilityHeadless/Include/Tactility/service/gps/GpsUtil.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <minmea.h>
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
/** @return true when the input float is valid (contains non-zero values) */
|
||||
inline bool isValid(const minmea_float& inFloat) { return inFloat.value != 0 && inFloat.scale != 0; }
|
||||
|
||||
}
|
||||
9
TactilityHeadless/Private/Tactility/hal/gps/GpsInit.h
Normal file
9
TactilityHeadless/Private/Tactility/hal/gps/GpsInit.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "Tactility/hal/gps/GpsDevice.h"
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
bool init(const std::vector<GpsDevice::Configuration>& configurations);
|
||||
|
||||
}
|
||||
44
TactilityHeadless/Private/Tactility/service/gps/GpsService.h
Normal file
44
TactilityHeadless/Private/Tactility/service/gps/GpsService.h
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include "Tactility/Mutex.h"
|
||||
#include "Tactility/service/Service.h"
|
||||
#include "Tactility/hal/gps/GpsDevice.h"
|
||||
|
||||
namespace tt::service::gps {
|
||||
|
||||
class GpsService final : public Service {
|
||||
|
||||
private:
|
||||
|
||||
struct GpsDeviceRecord {
|
||||
std::shared_ptr<hal::gps::GpsDevice> device = nullptr;
|
||||
hal::gps::GpsDevice::SatelliteSubscriptionId satelliteSubscriptionId = -1;
|
||||
hal::gps::GpsDevice::LocationSubscriptionId locationSubscriptionId = -1;
|
||||
};
|
||||
|
||||
Mutex mutex = Mutex(Mutex::Type::Recursive);
|
||||
std::vector<GpsDeviceRecord> deviceRecords;
|
||||
bool receiving = false;
|
||||
|
||||
bool startGpsDevice(GpsDeviceRecord& deviceRecord);
|
||||
static bool stopGpsDevice(GpsDeviceRecord& deviceRecord);
|
||||
|
||||
GpsDeviceRecord* _Nullable findGpsRecord(const std::shared_ptr<hal::gps::GpsDevice>& record);
|
||||
|
||||
void onSatelliteInfo(hal::Device::Id deviceId, const minmea_sat_info& info);
|
||||
void onRmcSentence(hal::Device::Id deviceId, const minmea_sentence_rmc& rmc);
|
||||
|
||||
public:
|
||||
|
||||
void onStart(tt::service::ServiceContext &serviceContext) final;
|
||||
void onStop(tt::service::ServiceContext &serviceContext) final;
|
||||
|
||||
void addGpsDevice(const std::shared_ptr<hal::gps::GpsDevice>& device);
|
||||
void removeGpsDevice(const std::shared_ptr<hal::gps::GpsDevice>& device);
|
||||
|
||||
bool startReceiving();
|
||||
void stopReceiving();
|
||||
bool isReceiving();
|
||||
};
|
||||
|
||||
} // tt::hal::gps
|
||||
@ -16,6 +16,7 @@ namespace tt {
|
||||
|
||||
#define TAG "tactility"
|
||||
|
||||
namespace service::gps { extern const ServiceManifest manifest; }
|
||||
namespace service::wifi { extern const ServiceManifest manifest; }
|
||||
namespace service::sdcard { extern const ServiceManifest manifest; }
|
||||
|
||||
@ -25,6 +26,7 @@ static const hal::Configuration* hardwareConfig = nullptr;
|
||||
|
||||
static void registerAndStartSystemServices() {
|
||||
TT_LOG_I(TAG, "Registering and starting system services");
|
||||
addService(service::gps::manifest);
|
||||
addService(service::sdcard::manifest);
|
||||
addService(service::wifi::manifest);
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include "Tactility/hal/Configuration.h"
|
||||
#include "Tactility/hal/Device.h"
|
||||
#include "Tactility/hal/gps/GpsInit.h"
|
||||
#include "Tactility/hal/i2c/I2c.h"
|
||||
#include "Tactility/hal/power/PowerDevice.h"
|
||||
#include "Tactility/hal/spi/Spi.h"
|
||||
@ -28,6 +29,8 @@ void init(const Configuration& configuration) {
|
||||
tt_check(uart::init(configuration.uart), "UART init failed");
|
||||
kernel::systemEventPublish(kernel::SystemEvent::BootInitUartEnd);
|
||||
|
||||
tt_check(gps::init(configuration.gps), "GPS init failed");
|
||||
|
||||
if (configuration.initBoot != nullptr) {
|
||||
TT_LOG_I(TAG, "Init power");
|
||||
tt_check(configuration.initBoot(), "Init power failed");
|
||||
|
||||
152
TactilityHeadless/Source/hal/gps/GpsDevice.cpp
Normal file
152
TactilityHeadless/Source/hal/gps/GpsDevice.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
#include "Tactility/hal/gps/GpsDevice.h"
|
||||
#include <minmea.h>
|
||||
#include <cstring>
|
||||
|
||||
#define TAG "gps"
|
||||
#define GPS_UART_BUFFER_SIZE 256
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
int32_t GpsDevice::threadMainStatic(void* parameter) {
|
||||
auto* gps_device = (GpsDevice*)parameter;
|
||||
return gps_device->threadMain();
|
||||
}
|
||||
|
||||
int32_t GpsDevice::threadMain() {
|
||||
uint8_t buffer[GPS_UART_BUFFER_SIZE];
|
||||
|
||||
if (!uart::setBaudRate(configuration.uartPort, (int)configuration.baudRate)) {
|
||||
TT_LOG_E(TAG, "Failed to set baud rate to %lu", configuration.baudRate);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!configuration.initFunction(configuration.uartPort)) {
|
||||
TT_LOG_E(TAG, "Failed to init");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Reference: https://gpsd.gitlab.io/gpsd/NMEA.html
|
||||
while (!isThreadInterrupted()) {
|
||||
if (uart::readUntil(configuration.uartPort, (uint8_t*)buffer, GPS_UART_BUFFER_SIZE, '\n', 100 / portTICK_PERIOD_MS) > 0) {
|
||||
TT_LOG_D(TAG, "RX: %s", buffer);
|
||||
switch (minmea_sentence_id((char*)buffer, false)) {
|
||||
case MINMEA_SENTENCE_RMC:
|
||||
minmea_sentence_rmc frame;
|
||||
if (minmea_parse_rmc(&frame, (char*)buffer)) {
|
||||
for (auto& subscription : locationSubscriptions) {
|
||||
(*subscription.onData)(getId(), frame);
|
||||
}
|
||||
TT_LOG_D(TAG, "RX RMC %f lat, %f lon, %f m/s", minmea_tocoord(&frame.latitude), minmea_tocoord(&frame.longitude), minmea_tofloat(&frame.speed));
|
||||
} else {
|
||||
TT_LOG_W(TAG, "RX RMC parse error: %s", buffer);
|
||||
}
|
||||
break;
|
||||
case MINMEA_SENTENCE_GGA:
|
||||
minmea_sentence_gga gga_frame;
|
||||
if (minmea_parse_gga(&gga_frame, (char*)buffer)) {
|
||||
TT_LOG_D(TAG, "RX GGA %f lat, %f lon", minmea_tocoord(&gga_frame.latitude), minmea_tocoord(&gga_frame.longitude));
|
||||
} else {
|
||||
TT_LOG_W(TAG, "RX GGA parse error: %s", buffer);
|
||||
}
|
||||
break;
|
||||
case MINMEA_SENTENCE_GSV:
|
||||
minmea_sentence_gsv gsv_frame;
|
||||
if (minmea_parse_gsv(&gsv_frame, (char*)buffer)) {
|
||||
for (auto& sat : gsv_frame.sats) {
|
||||
if (sat.nr != 0 && sat.elevation != 0 && sat.snr != 0) {
|
||||
for (auto& subscription : satelliteSubscriptions) {
|
||||
(*subscription.onData)(getId(), sat);
|
||||
}
|
||||
}
|
||||
TT_LOG_D(TAG, "Satellite: id %d, elevation %d, azimuth %d, snr %d", sat.nr, sat.elevation, sat.azimuth, sat.snr);
|
||||
}
|
||||
TT_LOG_D(TAG, "RX GGA %f lat, %f lon", minmea_tocoord(&gga_frame.latitude), minmea_tocoord(&gga_frame.longitude));
|
||||
} else {
|
||||
TT_LOG_W(TAG, "RX GGA parse error: %s", buffer);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool GpsDevice::start() {
|
||||
auto scoped_lockable = mutex.scoped();
|
||||
scoped_lockable->lock(portMAX_DELAY);
|
||||
|
||||
if (thread != nullptr && thread->getState() != Thread::State::Stopped) {
|
||||
TT_LOG_W(TAG, "Already started");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (uart::isStarted(configuration.uartPort)) {
|
||||
TT_LOG_E(TAG, "UART %d already in use", configuration.uartPort);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!uart::start(configuration.uartPort)) {
|
||||
TT_LOG_E(TAG, "UART %d failed to start", configuration.uartPort);
|
||||
return false;
|
||||
}
|
||||
|
||||
threadInterrupted = false;
|
||||
|
||||
thread = std::make_unique<Thread>(
|
||||
"gps",
|
||||
4096,
|
||||
threadMainStatic,
|
||||
this
|
||||
);
|
||||
thread->setPriority(tt::Thread::Priority::High);
|
||||
thread->start();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GpsDevice::stop() {
|
||||
auto scoped_lockable = mutex.scoped();
|
||||
scoped_lockable->lock();
|
||||
|
||||
if (thread != nullptr) {
|
||||
threadInterrupted = true;
|
||||
|
||||
// Detach thread, it will auto-delete when leaving the current scope
|
||||
auto old_thread = std::move(thread);
|
||||
|
||||
if (old_thread->getState() != Thread::State::Stopped) {
|
||||
// Unlock so thread can lock
|
||||
scoped_lockable->unlock();
|
||||
// Wait for thread to finish
|
||||
old_thread->join();
|
||||
// Re-lock to continue logic below
|
||||
scoped_lockable->lock();
|
||||
}
|
||||
}
|
||||
|
||||
if (uart::isStarted(configuration.uartPort)) {
|
||||
if (!uart::stop(configuration.uartPort)) {
|
||||
TT_LOG_E(TAG, "UART %d failed to stop", configuration.uartPort);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GpsDevice::isStarted() const {
|
||||
auto scoped_lockable = mutex.scoped();
|
||||
scoped_lockable->lock(portMAX_DELAY);
|
||||
return thread != nullptr && thread->getState() != Thread::State::Stopped;
|
||||
}
|
||||
|
||||
bool GpsDevice::isThreadInterrupted() const {
|
||||
auto scoped_lockable = mutex.scoped();
|
||||
scoped_lockable->lock(portMAX_DELAY);
|
||||
return threadInterrupted;
|
||||
}
|
||||
|
||||
} // namespace tt::hal::gps
|
||||
214
TactilityHeadless/Source/hal/gps/GpsDeviceInitDefault.cpp
Normal file
214
TactilityHeadless/Source/hal/gps/GpsDeviceInitDefault.cpp
Normal file
@ -0,0 +1,214 @@
|
||||
#include <Tactility/Log.h>
|
||||
#include <Tactility/hal/uart/Uart.h>
|
||||
#include <Tactility/kernel/Kernel.h>
|
||||
|
||||
#define TAG "gps"
|
||||
#define GPS_UART_BUFFER_SIZE 256
|
||||
|
||||
using namespace tt;
|
||||
using namespace tt::hal;
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
static int ackGps(uart_port_t port, uint8_t* buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedId) {
|
||||
uint16_t ubxFrameCounter = 0;
|
||||
uint32_t startTime = kernel::getTicks();
|
||||
uint16_t needRead;
|
||||
|
||||
while (kernel::getTicks() - startTime < 800) {
|
||||
while (uart::available(port)) {
|
||||
uint8_t c;
|
||||
uart::readByte(port, &c);
|
||||
switch (ubxFrameCounter) {
|
||||
case 0:
|
||||
if (c == 0xB5) {
|
||||
ubxFrameCounter++;
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
if (c == 0x62) {
|
||||
ubxFrameCounter++;
|
||||
} else {
|
||||
ubxFrameCounter = 0;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
if (c == requestedClass) {
|
||||
ubxFrameCounter++;
|
||||
} else {
|
||||
ubxFrameCounter = 0;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
if (c == requestedId) {
|
||||
ubxFrameCounter++;
|
||||
} else {
|
||||
ubxFrameCounter = 0;
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
needRead = c;
|
||||
ubxFrameCounter++;
|
||||
break;
|
||||
case 5:
|
||||
needRead |= (c << 8);
|
||||
ubxFrameCounter++;
|
||||
break;
|
||||
case 6:
|
||||
if (needRead >= size) {
|
||||
ubxFrameCounter = 0;
|
||||
break;
|
||||
}
|
||||
if (uart::read(port, buffer, needRead) != needRead) {
|
||||
ubxFrameCounter = 0;
|
||||
} else {
|
||||
return needRead;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool configureGps(uart_port_t port, uint8_t* buffer, size_t bufferSize) {
|
||||
// According to the reference implementation, L76K GPS uses 9600 baudrate, but the default in the developer device was 38400
|
||||
// https://github.com/Xinyuan-LilyGO/T-Deck/blob/master/examples/GPSShield/GPSShield.ino
|
||||
bool result = false;
|
||||
uint32_t startTimeout;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
if (!uart::writeString(port, "$PCAS03,0,0,0,0,0,0,0,0,0,0,,,0,0*02\r\n")) {
|
||||
TT_LOG_E(TAG, "Failed to write init string");
|
||||
}
|
||||
|
||||
kernel::delayMillis(5U);
|
||||
// Get version information
|
||||
startTimeout = kernel::getMillis() + 1000;
|
||||
|
||||
TT_LOG_I(TAG, "Manual flushing of input");
|
||||
#ifdef ESP_PLATFORM
|
||||
esp_rom_printf("Waiting...");
|
||||
#endif
|
||||
while (uart::available(port)) {
|
||||
#ifdef ESP_PLATFORM
|
||||
esp_rom_printf(".");
|
||||
#endif
|
||||
uart::readUntil(port, buffer, bufferSize, '\n');
|
||||
if (kernel::getMillis() > startTimeout) {
|
||||
TT_LOG_E(TAG, "NMEA timeout");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
#ifdef ESP_PLATFORM
|
||||
esp_rom_printf("\n");
|
||||
#endif
|
||||
uart::flush(port);
|
||||
kernel::delayMillis(200);
|
||||
|
||||
if (!uart::writeString(port, "$PCAS06,0*1B\r\n")) {
|
||||
TT_LOG_E(TAG, "Failed to write PCAS06");
|
||||
}
|
||||
|
||||
startTimeout = kernel::getMillis() + 500;
|
||||
while (!uart::available(port)) {
|
||||
if (kernel::getMillis() > startTimeout) {
|
||||
TT_LOG_E(TAG, "L76K timeout");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto ver = uart::readStringUntil(port, '\n');
|
||||
if (ver.starts_with("$GPTXT,01,01,02")) {
|
||||
TT_LOG_I(TAG, "L76K GNSS init success");
|
||||
result = true;
|
||||
break;
|
||||
}
|
||||
kernel::delayMillis(500);
|
||||
}
|
||||
// Initialize the L76K Chip, use GPS + GLONASS
|
||||
uart::writeString(port, "$PCAS04,5*1C\r\n");
|
||||
kernel::delayMillis(250);
|
||||
uart::writeString(port, "$PCAS03,1,1,1,1,1,1,1,1,1,1,,,0,0*26\r\n");
|
||||
kernel::delayMillis(250);
|
||||
|
||||
// Switch to Vehicle Mode, since SoftRF enables Aviation < 2g
|
||||
uart::writeString(port, "$PCAS11,3*1E\r\n");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool recoverGps(uart_port_t port, uint8_t* buffer, size_t bufferSize) {
|
||||
uint8_t cfg_clear1[] = {0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1C, 0xA2};
|
||||
uint8_t cfg_clear2[] = {0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x1B, 0xA1};
|
||||
uint8_t cfg_clear3[] = {0xB5, 0x62, 0x06, 0x09, 0x0D, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x03, 0x1D, 0xB3};
|
||||
|
||||
if (!uart::write(port, cfg_clear1, sizeof(cfg_clear1), 10)) {
|
||||
return false;
|
||||
TT_LOG_E(TAG, "Failed to send ack 1");
|
||||
}
|
||||
|
||||
if (ackGps(port, buffer, bufferSize, 0x05, 0x01)) {
|
||||
TT_LOG_I(TAG, "Ack 1");
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Ack 1 failed");
|
||||
}
|
||||
|
||||
if (!uart::write(port, cfg_clear2, sizeof(cfg_clear2))) {
|
||||
return false;
|
||||
TT_LOG_E(TAG, "Failed to send ack 2");
|
||||
}
|
||||
|
||||
if (ackGps(port, buffer, bufferSize, 0x05, 0x01)) {
|
||||
TT_LOG_I(TAG, "Ack 2");
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Ack 2 failed");
|
||||
}
|
||||
|
||||
if (!uart::write(port, cfg_clear3, sizeof(cfg_clear3))) {
|
||||
TT_LOG_E(TAG, "Failed to send ack 3");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ackGps(port, buffer, bufferSize, 0x05, 0x01)) {
|
||||
TT_LOG_I(TAG, "Ack 3");
|
||||
} else {
|
||||
TT_LOG_W(TAG, "Ack 3 failed");
|
||||
}
|
||||
|
||||
// UBX-CFG-RATE, Size 8, 'Navigation/measurement rate settings'
|
||||
uint8_t cfg_rate[] = {0xB5, 0x62, 0x06, 0x08, 0x00, 0x00, 0x0E, 0x30};
|
||||
uart::write(port, cfg_rate, sizeof(cfg_rate));
|
||||
if (ackGps(port, buffer, bufferSize, 0x06, 0x08)) {
|
||||
TT_LOG_I(TAG, "Ack completed");
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool initGpsDefault(uart_port_t port) {
|
||||
uint8_t buffer[GPS_UART_BUFFER_SIZE];
|
||||
if (!configureGps(port, buffer, GPS_UART_BUFFER_SIZE)) {
|
||||
if (!recoverGps(port, buffer, GPS_UART_BUFFER_SIZE)) {
|
||||
uint32_t initial_baud_rate = std::max(uart::getBaudRate(port), (uint32_t)9600U);
|
||||
uart::setBaudRate(port, 9600U);
|
||||
if (!recoverGps(port, buffer, GPS_UART_BUFFER_SIZE)) {
|
||||
TT_LOG_E(TAG, "Recovery repeatedly failed");
|
||||
return false;
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Recovery 2 complete");
|
||||
}
|
||||
uart::setBaudRate(port, initial_baud_rate);
|
||||
} else {
|
||||
TT_LOG_I(TAG, "Recovery 1 complete");
|
||||
}
|
||||
}
|
||||
|
||||
TT_LOG_I(TAG, "Init complete");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace tt::hal::gps
|
||||
15
TactilityHeadless/Source/hal/gps/GpsInit.cpp
Normal file
15
TactilityHeadless/Source/hal/gps/GpsInit.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include "Tactility/hal/gps/GpsDevice.h"
|
||||
#include "Tactility/service/Service.h"
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
bool init(const std::vector<GpsDevice::Configuration>& configurations) {
|
||||
for (auto& configuration : configurations) {
|
||||
auto device = std::make_shared<GpsDevice>(configuration);
|
||||
hal::registerDevice(std::move(device));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace tt::hal::gps
|
||||
114
TactilityHeadless/Source/hal/gps/Satellites.cpp
Normal file
114
TactilityHeadless/Source/hal/gps/Satellites.cpp
Normal file
@ -0,0 +1,114 @@
|
||||
#include "Tactility/hal/gps/Satellites.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#define TAG "satellites"
|
||||
|
||||
namespace tt::hal::gps {
|
||||
|
||||
constexpr inline bool hasTimeElapsed(TickType_t now, TickType_t timeInThePast, TickType_t expireTimeInTicks) {
|
||||
return (TickType_t)(now - timeInThePast) >= expireTimeInTicks;
|
||||
}
|
||||
|
||||
SatelliteStorage::SatelliteRecord* SatelliteStorage::findRecord(int number) {
|
||||
auto result = records | std::views::filter([number](auto& record) {
|
||||
return record.inUse && record.data.nr == number;
|
||||
});
|
||||
|
||||
if (!result.empty()) {
|
||||
return &result.front();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
SatelliteStorage::SatelliteRecord* SatelliteStorage::findUnusedRecord() {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
auto result = records | std::views::filter([](auto& record) {
|
||||
return !record.inUse;
|
||||
});
|
||||
|
||||
if (!result.empty()) {
|
||||
auto* record = &result.front();
|
||||
record->inUse = true;
|
||||
TT_LOG_D(TAG, "Found unused record");
|
||||
return record;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
SatelliteStorage::SatelliteRecord* SatelliteStorage::findRecordToRecycle() {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
int candidate_index = -1;
|
||||
auto candidate_age = portMAX_DELAY;
|
||||
TickType_t expire_duration = kernel::secondsToTicks(recycleTimeSeconds);
|
||||
TickType_t now = kernel::getTicks();
|
||||
for (int i = 0; i < records.size(); ++i) {
|
||||
// First try to find a record that is "old enough"
|
||||
if (hasTimeElapsed(now, records[i].lastUpdated, expire_duration)) {
|
||||
TT_LOG_D(TAG, "! [%d] %lu < %lu", i, records[i].lastUpdated, expire_duration);
|
||||
candidate_index = i;
|
||||
break;
|
||||
}
|
||||
|
||||
// Otherwise keep finding the oldest record
|
||||
if (records[i].inUse && records[i].lastUpdated < candidate_age) {
|
||||
candidate_index = i;
|
||||
candidate_age = records[i].lastUpdated;
|
||||
TT_LOG_D(TAG, "? [%d] %lu < %lu", i, records[i].lastUpdated, candidate_age);
|
||||
}
|
||||
}
|
||||
|
||||
assert(candidate_index != -1);
|
||||
|
||||
TT_LOG_D(TAG, "Recycled record %d", candidate_index);
|
||||
|
||||
return &records[candidate_index];
|
||||
}
|
||||
|
||||
SatelliteStorage::SatelliteRecord* SatelliteStorage::findWithFallback(int number) {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
if (auto* found_record = findRecord(number)) {
|
||||
return found_record;
|
||||
} else if (auto* unused_record = findUnusedRecord()) {
|
||||
return unused_record;
|
||||
} else {
|
||||
return findRecordToRecycle();
|
||||
}
|
||||
}
|
||||
|
||||
void SatelliteStorage::notify(const minmea_sat_info& data) {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
auto* record = findWithFallback(data.nr);
|
||||
if (record != nullptr) {
|
||||
record->inUse = true;
|
||||
record->lastUpdated = kernel::getTicks();
|
||||
record->data = data;
|
||||
TT_LOG_D(TAG, "Updated satellite %d: elevation %d, azimuth %d, snr %d", record->data.nr, record->data.elevation, record->data.elevation, record->data.snr);
|
||||
}
|
||||
}
|
||||
|
||||
void SatelliteStorage::getRecords(const std::function<void(const minmea_sat_info&)>& onRecord) const {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
TickType_t expire_duration = kernel::secondsToTicks(recentTimeSeconds);
|
||||
TickType_t now = kernel::getTicks();
|
||||
|
||||
for (auto& record: records) {
|
||||
if (record.inUse && !hasTimeElapsed(now, record.lastUpdated, expire_duration)) {
|
||||
onRecord(record.data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace tt::hal::gps
|
||||
@ -4,6 +4,8 @@
|
||||
#include <Tactility/LogMessages.h>
|
||||
#include <Tactility/Mutex.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#ifdef ESP_PLATFORM
|
||||
#include <esp_check.h>
|
||||
#endif
|
||||
@ -35,16 +37,16 @@ static const char* initModeToString(InitMode mode) {
|
||||
}
|
||||
|
||||
static void printInfo(const Data& data) {
|
||||
TT_LOG_V(TAG, "UART info for port %d", data.configuration.port);
|
||||
TT_LOG_V(TAG, " isStarted: %d", data.isStarted);
|
||||
TT_LOG_V(TAG, " isConfigured: %d", data.isConfigured);
|
||||
TT_LOG_V(TAG, " initMode: %s", initModeToString(data.configuration.initMode));
|
||||
TT_LOG_V(TAG, " canReinit: %d", data.configuration.canReinit);
|
||||
TT_LOG_V(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
|
||||
TT_LOG_V(TAG, " RX pin: %d", data.configuration.rxPin);
|
||||
TT_LOG_V(TAG, " TX pin: %d", data.configuration.txPin);
|
||||
TT_LOG_V(TAG, " RTS pin: %d", data.configuration.rtsPin);
|
||||
TT_LOG_V(TAG, " CTS pin: %d", data.configuration.ctsPin);
|
||||
TT_LOG_D(TAG, "UART info for port %d", data.configuration.port);
|
||||
TT_LOG_D(TAG, " isStarted: %d", data.isStarted);
|
||||
TT_LOG_D(TAG, " isConfigured: %d", data.isConfigured);
|
||||
TT_LOG_D(TAG, " initMode: %s", initModeToString(data.configuration.initMode));
|
||||
TT_LOG_D(TAG, " canReinit: %d", data.configuration.canReinit);
|
||||
TT_LOG_D(TAG, " hasMutableConfiguration: %d", data.configuration.hasMutableConfiguration);
|
||||
TT_LOG_D(TAG, " RX pin: %d", data.configuration.rxPin);
|
||||
TT_LOG_D(TAG, " TX pin: %d", data.configuration.txPin);
|
||||
TT_LOG_D(TAG, " RTS pin: %d", data.configuration.rtsPin);
|
||||
TT_LOG_D(TAG, " CTS pin: %d", data.configuration.ctsPin);
|
||||
}
|
||||
|
||||
bool init(const std::vector<uart::Configuration>& configurations) {
|
||||
@ -118,6 +120,7 @@ static bool startLocked(uart_port_t port) {
|
||||
#else
|
||||
intr_alloc_flags = 0;
|
||||
#endif
|
||||
|
||||
esp_err_t result = uart_param_config(config.port, &config.config);
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Starting: Failed to configure: %s", port, esp_err_to_name(result));
|
||||
@ -174,13 +177,11 @@ static bool stopLocked(uart_port_t port) {
|
||||
if (result != ESP_OK) {
|
||||
TT_LOG_E(TAG, "(%d) Stopping: Failed to delete driver: %s", port, esp_err_to_name(result));
|
||||
return false;
|
||||
} else {
|
||||
data.isStarted = false;
|
||||
}
|
||||
#else
|
||||
data.isStarted = true;
|
||||
#endif // ESP_PLATFORM
|
||||
|
||||
data.isStarted = false;
|
||||
|
||||
TT_LOG_I(TAG, "(%d) Stopped", port);
|
||||
return true;
|
||||
}
|
||||
@ -231,6 +232,10 @@ size_t read(uart_port_t port, uint8_t* buffer, size_t bufferSize, TickType_t tim
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool readByte(uart_port_t port, uint8_t* output, TickType_t timeout) {
|
||||
return read(port, output, 1, timeout) == 1;
|
||||
}
|
||||
|
||||
size_t write(uart_port_t port, const uint8_t* buffer, size_t bufferSize, TickType_t timeout) {
|
||||
#ifdef ESP_PLATFORM
|
||||
if (lock(port, timeout)) {
|
||||
@ -244,4 +249,114 @@ size_t write(uart_port_t port, const uint8_t* buffer, size_t bufferSize, TickTyp
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool writeString(uart_port_t port, const char* buffer, TickType_t timeout) {
|
||||
while (*buffer != 0) {
|
||||
if (write(port, (const uint8_t*)buffer, 1, timeout)) {
|
||||
buffer++;
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to write - breaking off");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t available(uart_port_t port, TickType_t timeout) {
|
||||
#ifdef ESP_PLATFORM
|
||||
size_t size = 0;
|
||||
if (lock(port, timeout)) {
|
||||
uart_get_buffered_data_len(port, &size);
|
||||
unlock(port);
|
||||
return size;
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
return 0;
|
||||
}
|
||||
|
||||
void flush(uart_port_t port, TickType_t timeout) {
|
||||
#ifdef ESP_PLATFORM
|
||||
size_t size = 0;
|
||||
if (lock(port, timeout)) {
|
||||
uart_flush(port);
|
||||
unlock(port);
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
}
|
||||
|
||||
void flushInput(uart_port_t port, TickType_t timeout) {
|
||||
#ifdef ESP_PLATFORM
|
||||
size_t size = 0;
|
||||
if (lock(port, timeout)) {
|
||||
uart_flush_input(port);
|
||||
unlock(port);
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
}
|
||||
|
||||
uint32_t getBaudRate(uart_port_t port) {
|
||||
#ifdef ESP_PLATFORM
|
||||
uint32_t baud_rate = 0;
|
||||
uart_get_baudrate(port, &baud_rate);
|
||||
return baud_rate;
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool setBaudRate(uart_port_t port, uint32_t baudRate, TickType_t timeout) {
|
||||
#ifdef ESP_PLATFORM
|
||||
if (lock(port, timeout)) {
|
||||
uart_set_baudrate(port, baudRate);
|
||||
unlock(port);
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED_FMT, "write()");
|
||||
}
|
||||
#endif // ESP_PLATFORM
|
||||
return true;
|
||||
}
|
||||
|
||||
bool readUntil(uart_port_t port, uint8_t* buffer, size_t bufferSize, uint8_t untilByte, TickType_t timeout) {
|
||||
bool success = false;
|
||||
size_t index = 0;
|
||||
size_t index_limit = bufferSize - 1;
|
||||
while (readByte(port, buffer, timeout) && index < index_limit) {
|
||||
if (*buffer == untilByte) {
|
||||
success = true;
|
||||
// We have the extra space because index < index_limit
|
||||
if (buffer++) {
|
||||
*buffer = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
buffer++;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
std::string readStringUntil(uart_port_t port, char untilChar, TickType_t timeout) {
|
||||
std::stringstream output;
|
||||
char buffer;
|
||||
bool success = false;
|
||||
while (readByte(port, (uint8_t*)&buffer, timeout)) {
|
||||
if (buffer == untilChar) {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
output << buffer;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
return output.str();
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace tt::hal::uart
|
||||
|
||||
38
TactilityHeadless/Source/service/gps/Gps.cpp
Normal file
38
TactilityHeadless/Source/service/gps/Gps.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
#include "Tactility/service/ServiceManifest.h"
|
||||
#include "Tactility/service/ServiceRegistry.h"
|
||||
#include "Tactility/service/gps/GpsService.h"
|
||||
|
||||
using tt::hal::gps::GpsDevice;
|
||||
|
||||
namespace tt::service::gps {
|
||||
|
||||
extern ServiceManifest manifest;
|
||||
|
||||
static std::shared_ptr<GpsService> findGpsService() {
|
||||
auto service = findServiceById(manifest.id);
|
||||
assert(service != nullptr);
|
||||
return std::static_pointer_cast<GpsService>(service);
|
||||
}
|
||||
|
||||
|
||||
void addGpsDevice(const std::shared_ptr<GpsDevice>& device) {
|
||||
return findGpsService()->addGpsDevice(device);
|
||||
}
|
||||
|
||||
void removeGpsDevice(const std::shared_ptr<GpsDevice>& device) {
|
||||
return findGpsService()->removeGpsDevice(device);
|
||||
}
|
||||
|
||||
bool startReceiving() {
|
||||
return findGpsService()->startReceiving();
|
||||
}
|
||||
|
||||
void stopReceiving() {
|
||||
findGpsService()->stopReceiving();
|
||||
}
|
||||
|
||||
bool isReceiving() {
|
||||
return findGpsService()->isReceiving();
|
||||
}
|
||||
|
||||
} // namespace tt::service::gps
|
||||
163
TactilityHeadless/Source/service/gps/GpsService.cpp
Normal file
163
TactilityHeadless/Source/service/gps/GpsService.cpp
Normal file
@ -0,0 +1,163 @@
|
||||
#include "Tactility/service/gps/GpsService.h"
|
||||
#include "Tactility/service/ServiceManifest.h"
|
||||
|
||||
#define TAG "gps_service"
|
||||
|
||||
using tt::hal::gps::GpsDevice;
|
||||
|
||||
namespace tt::service::gps {
|
||||
|
||||
GpsService::GpsDeviceRecord* _Nullable GpsService::findGpsRecord(const std::shared_ptr<GpsDevice>& device) {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
auto result = std::views::filter(deviceRecords, [&device](auto& record){
|
||||
return record.device.get() == device.get();
|
||||
});
|
||||
if (!result.empty()) {
|
||||
return &result.front();
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void GpsService::addGpsDevice(const std::shared_ptr<GpsDevice>& device) {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
GpsDeviceRecord record = { .device = device };
|
||||
|
||||
if (isReceiving()) {
|
||||
startGpsDevice(record);
|
||||
}
|
||||
|
||||
deviceRecords.push_back(record);
|
||||
}
|
||||
|
||||
void GpsService::removeGpsDevice(const std::shared_ptr<GpsDevice>& device) {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
GpsDeviceRecord* record = findGpsRecord(device);
|
||||
|
||||
if (isReceiving()) {
|
||||
stopGpsDevice(*record);
|
||||
}
|
||||
|
||||
std::erase_if(deviceRecords, [&device](auto& reference){
|
||||
return reference.device.get() == device.get();
|
||||
});
|
||||
}
|
||||
|
||||
void GpsService::onStart(tt::service::ServiceContext &serviceContext) {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
deviceRecords.clear();
|
||||
|
||||
auto devices = hal::findDevices<GpsDevice>(hal::Device::Type::Gps);
|
||||
for (auto& device : devices) {
|
||||
TT_LOG_I(TAG, "[device %lu] added", device->getId());
|
||||
addGpsDevice(device);
|
||||
}
|
||||
}
|
||||
|
||||
void GpsService::onStop(tt::service::ServiceContext &serviceContext) {
|
||||
if (isReceiving()) {
|
||||
stopReceiving();
|
||||
}
|
||||
}
|
||||
|
||||
bool GpsService::startGpsDevice(GpsDeviceRecord& record) {
|
||||
TT_LOG_I(TAG, "[device %lu] starting", record.device->getId());
|
||||
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
auto device = record.device;
|
||||
|
||||
if (!device->start()) {
|
||||
TT_LOG_E(TAG, "[device %lu] starting failed", record.device->getId());
|
||||
return false;
|
||||
}
|
||||
|
||||
record.satelliteSubscriptionId = device->subscribeSatellites([this](hal::Device::Id deviceId, auto& record) {
|
||||
onSatelliteInfo(deviceId, record);
|
||||
});
|
||||
|
||||
record.locationSubscriptionId = device->subscribeLocations([this](hal::Device::Id deviceId, auto& record) {
|
||||
onRmcSentence(deviceId, record);
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GpsService::stopGpsDevice(GpsDeviceRecord& record) {
|
||||
TT_LOG_I(TAG, "[device %lu] stopping", record.device->getId());
|
||||
|
||||
auto device = record.device;
|
||||
|
||||
if (!device->stop()) {
|
||||
TT_LOG_E(TAG, "[device %lu] stopping failed", record.device->getId());
|
||||
return false;
|
||||
}
|
||||
|
||||
device->unsubscribeSatellites(record.satelliteSubscriptionId);
|
||||
device->unsubscribeLocations(record.locationSubscriptionId);
|
||||
|
||||
record.satelliteSubscriptionId = -1;
|
||||
record.locationSubscriptionId = -1;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GpsService::startReceiving() {
|
||||
TT_LOG_I(TAG, "Start receiving");
|
||||
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
bool started_one_or_more = false;
|
||||
|
||||
for (auto& record : deviceRecords) {
|
||||
started_one_or_more |= startGpsDevice(record);
|
||||
}
|
||||
|
||||
receiving = started_one_or_more;
|
||||
|
||||
return receiving;
|
||||
}
|
||||
|
||||
void GpsService::stopReceiving() {
|
||||
TT_LOG_I(TAG, "Stop receiving");
|
||||
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
|
||||
for (auto& record : deviceRecords) {
|
||||
stopGpsDevice(record);
|
||||
}
|
||||
|
||||
receiving = false;
|
||||
}
|
||||
|
||||
bool GpsService::isReceiving() {
|
||||
auto lockable = mutex.scoped();
|
||||
lockable->lock();
|
||||
return receiving;
|
||||
}
|
||||
|
||||
void GpsService::onSatelliteInfo(hal::Device::Id deviceId, const minmea_sat_info& info) {
|
||||
TT_LOG_I(TAG, "[device %lu] satellite %d", deviceId, info.nr);
|
||||
}
|
||||
|
||||
void GpsService::onRmcSentence(hal::Device::Id deviceId, const minmea_sentence_rmc& rmc) {
|
||||
TT_LOG_I(TAG, "[device %lu] coordinates %f %f", deviceId, minmea_tofloat(&rmc.longitude), minmea_tofloat(&rmc.latitude));
|
||||
}
|
||||
|
||||
extern const ServiceManifest manifest = {
|
||||
.id = "Gps",
|
||||
.createService = create<GpsService>
|
||||
};
|
||||
|
||||
} // tt::hal::gps
|
||||
Loading…
x
Reference in New Issue
Block a user