Pico GPS Teseo I2C
Loading...
Searching...
No Matches
teseo.cpp
Go to the documentation of this file.
1module;
2
3#include<algorithm>
4#include <cassert>
5#include <string>
6// std::pair
7#include <utility>
8#include <span>
9
10module teseo;
11
12namespace teseo {
13
14nmea_rr teseo::gll_("$PSTMNMEAREQUEST,100000,0\r\n", "GLL,");
15nmea_rr teseo::gsv_("$PSTMNMEAREQUEST,80000,0\r\n", "GSV,");
16nmea_rr teseo::gsa_("$PSTMNMEAREQUEST,4,0\r\n", "GSA,");
17nmea_rr teseo::gga_("$PSTMNMEAREQUEST,2,0\r\n", "GGA,");
18nmea_rr teseo::rmc_("$PSTMNMEAREQUEST,40,0\r\n", "RMC,");
19nmea_rr teseo::vtg_("$PSTMNMEAREQUEST,10,0\r\n", "VTG,");
20
21/*
22when the teseo is preset for i2c according to AN5203,
23init is not required, and you can cut 4s 10ms from the startup sequence
24https://www.st.com/resource/en/application_note/an5203-teseoliv3f--i2c-positioning-sensor--stmicroelectronics.pdf
25*/
27 assert(writer_.is_set());
28 assert(reader_.is_set());
29 assert(resetter_.is_set());
30
31 std::string s;
32 resetter_();
33
34 // stop the engine
35 write("$PSTMGPSSUSPEND\r\n");
36
37 // reset the UART message list
38 write("$PSTMCFGMSGL,0,1,0,0\r\n");
39 // reset the I2C message list
40 write("$PSTMCFGMSGL,3,1,0,0\r\n");
41 // disable the eco-ing message
42 write("$PSTMSETPAR,1227,1,2\r\n");
43
44 // restart the engine
45 write("$PSTMGPSRESTART\r\n");
46 do {
47 read(s);
48 }
49 while(((s.length()) && s.find("$PSTMGPSRESTART") == std::string::npos)); // command successful
50}
51
52bool teseo::parse_multiline_reply(std::span<std::string> strings, const std::string s, unsigned int& count, const nmea_rr& command) {
53 std::size_t message_count = strings.size();
54 std::size_t string_index = 0;
55 std::size_t new_string_index; // intentionally uninitialised
56 std::size_t vector_index; // intentionally uninitialised
57 bool valid = false;
58
59 for(vector_index = 0; vector_index < message_count; vector_index++) {
60 new_string_index = s.find("\r\n", string_index);
61 if (new_string_index == s.length() - 2) { // exhausted. This should be the status string
62 valid = s.substr(string_index, s.length() - string_index).starts_with(command.first.substr(0, command.first.length()-2));
63 break;
64 }
65 assert(vector_index < message_count);
66 strings[vector_index] = s.substr(string_index, (new_string_index + 2) - string_index); // include the separator
67 valid = strings[vector_index].length() >= 7 && strings[vector_index].substr(3, 4).starts_with(command.second);
68 if (!valid) {
69 vector_index = 0;
70 break;
71 }
72 string_index = new_string_index + 2; // skip the separator
73 }
74 count = vector_index; // report the number of retrieved data lines.
75 std::for_each(strings.begin() + count, strings.end(),
76 [](auto &discard) { discard = std::string(); }); // clean out unused positions
77 return valid;
78}
79
80void teseo::write(const std::string& s) {
81 assert(writer_.is_set());
82 writer_(s);
83}
84
85void teseo::read(std::string& s) {
86 assert(reader_.is_set());
87 reader_(s);
88}
89
90bool teseo::ask_nmea(const nmea_rr& command, std::string& s) {
91 bool retval; // intentionally not initialised
92 unsigned int count;
93 write(command.first);
94 read(s);
95 retval = parse_multiline_reply(single_line_parser_, s, count, command);
96 s = single_line_parser_[0];
97 return retval;
98}
99
100bool teseo::ask_nmea_multiple(const nmea_rr& command, std::span<std::string> strings, unsigned int& count) {
101 unsigned int retval; // intentionally not initialised
102 std::string s;
103 write(command.first);
104 read(s);
105 retval = parse_multiline_reply(strings, s, count, command);
106 return retval;
107}
108
109bool teseo::ask_gll(std::string& s) {
110 return ask_nmea(gll_, s);
111}
112
113bool teseo::ask_gsv(std::span<std::string> strings, unsigned int& count) {
114 return ask_nmea_multiple(gsv_, strings, count);
115}
116
117bool teseo::ask_gsa(std::span<std::string> strings, unsigned int& count) {
118 return ask_nmea_multiple(gsa_, strings, count);
119}
120bool teseo::ask_gga(std::string& s) {
121 return ask_nmea(gga_, s);
122}
123
124bool teseo::ask_rmc(std::string& s) {
125 return ask_nmea(rmc_, s);
126}
127
128bool teseo::ask_vtg(std::string& s) {
129 return ask_nmea(vtg_, s);
130}
131
132} // namespace teseo
bool ask_gga(std::string &s)
get GGA request to the Teseo and read reply
Definition teseo.cpp:120
bool ask_gsv(std::span< std::string > strings, unsigned int &count)
get GSV request to the Teseo and read reply
Definition teseo.cpp:113
bool ask_vtg(std::string &s)
get VTG request to the Teseo and read reply
Definition teseo.cpp:128
static bool parse_multiline_reply(std::span< std::string > strings, const std::string s, unsigned int &count, const nmea_rr &command)
utility to parse a multiline Teseo reply into separate strings
Definition teseo.cpp:52
bool ask_gsa(std::span< std::string > strings, unsigned int &count)
get GSA request to the Teseo and read reply
Definition teseo.cpp:117
bool ask_nmea(const nmea_rr &command, std::string &s)
send NMEA request to the Teseo and return reply
Definition teseo.cpp:90
bool ask_rmc(std::string &s)
get RMC request to the Teseo and read reply
Definition teseo.cpp:124
void read(std::string &s)
read data from the Teseo
Definition teseo.cpp:85
bool ask_gll(std::string &s)
get GLL request to the Teseo and read reply
Definition teseo.cpp:109
void initialize()
configure the Teseo for use as a position sensor (optional).
Definition teseo.cpp:26
void write(const std::string &s)
write command to the Teseo
Definition teseo.cpp:80
bool ask_nmea_multiple(const nmea_rr &command, std::span< std::string > strings, unsigned int &count)
send NMEA request to the Teseo and return multi line reply
Definition teseo.cpp:100
void read(::std::string &s)
void write(const ::std::string &s)
const std::pair< const std::string, const std::string > nmea_rr