Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
functions.hpp
Go to the documentation of this file.
1#ifndef NIX_LIBS_NMEA_NMEATOOLS_HPP_
2#define NIX_LIBS_NMEA_NMEATOOLS_HPP_
3
6#include "core/defines.hpp"
7#include "core/parsers.hpp"
8
9#include <sstream>
10#include <vector>
11
12#define MINUTE_LENGTH 8
13#define KNOTS_TO_MPS 0.514444444
14
15namespace iv::comms::nmea0183
16{
22inline std::string nmeaChecksum(const std::string &completeMsg)
23{
24 uint16_t checkSum {0};
25
26 // Obtener el mensaje desde el $ hasta el ultimo * del CRC, sin incluir estos caracteres
27
28 // Compute the checkSum by XORing all the character values in the string.
29 for (std::string msgFiltered =
30 completeMsg.substr(completeMsg.find_first_not_of(iv::constants::comms::nmea::nmeaHeaderStart),
32 completeMsg.find_first_not_of(iv::constants::comms::nmea::nmeaHeaderStart));
33 const char c: msgFiltered)
34 {
35 checkSum ^= static_cast<uint8_t>(c);
36 }
37
38 // Convert it to hexadecimal (base-16, upper case, most significant nybble first).
39 std::stringstream stream;
40 stream << std::uppercase << std::hex << checkSum;
41
42 std::string hexCheckSum = stream.str();
43 if (hexCheckSum.length() < 2)
44 {
45 hexCheckSum = "0" + hexCheckSum;
46 }
47
48 return hexCheckSum;
49}
57[[nodiscard]]
58inline double degreesToDecimal(const int32_t degrees, const double minutes, const int32_t seconds = 0)
59{
60 return (degrees * 60) + minutes + static_cast<float>(seconds) / 3600.0f;
61}
62
63inline std::vector<std::string> splitStringBy(const std::string &sentence, const char delimiter)
64{
65 std::vector<std::string> returnVector;
66 std::stringstream ssSentence(sentence);
67 std::string element;
68
69 while (std::getline(ssSentence, element, delimiter))
70 {
71 returnVector.push_back(element);
72 }
73
74 return returnVector;
75}
76
83[[nodiscard]]
84inline bool getCoordinates(std::string array, double &decimalDegrees)
85{
86 bool isOk {false};
87
88 if (array.length() > MINUTE_LENGTH)
89 {
90 std::string degreeArray;
91 std::string minuteArray;
92
93 isOk = true;
94 degreeArray.assign(array.begin(), array.end() - MINUTE_LENGTH);
95 minuteArray.assign(array.end() - MINUTE_LENGTH, array.end());
96
97 const int32_t degrees = iv::utils::parseNumber<int32_t>(degreeArray).value_or(0);
98 const double minutes = iv::utils::parseNumber<double>(minuteArray).value_or(0);
99 decimalDegrees = degreesToDecimal(degrees, minutes);
100 }
101
102 return isOk;
103}
104
110[[nodiscard]] inline std::optional<bool> nmea0183FieldToBoolean(std::string_view data)
111{
112 if (data == "1")
113 {
114 return true;
115 }
116 if (data == "0")
117 {
118 return false;
119 }
120
121 IV_ASSERT_MSG("Invalid boolean value");
122
123 return std::nullopt;
124}
125
131[[nodiscard]] inline std::optional<uint16_t> nmea0183FieldToInteger(std::string_view data)
132{
134}
135
141[[nodiscard]] inline std::optional<float> nmea0183FieldToFloat(std::string_view data)
142{
144}
151[[nodiscard]] inline std::optional<std::string> nmea0183FieldToTime(std::string_view data)
152{
153 //Hacer alguna comprobación de validez cuando se vaya a usar esto
154 std::string_view hours = data.substr(0, 2);
155 std::string_view minutes = data.substr(2, 2);
156 std::string_view seconds = data.substr(4, 2);
157 return fmt::format("Hours: {}, Minutes: {}, Seconds: {}", hours, minutes, seconds);
158}
164[[nodiscard]] inline std::optional<iv::comms::nmea0183::Pole> nmeaField0183ToPole(std::string_view data)
165{
166 if (data == "T")
167 {
168 return Pole::True;
169 }
170 else if (data == "M")
171 {
172 return Pole::Magnetic;
173 }
174
175 IV_ASSERT_MSG("Invalid pole value");
176
177 return std::nullopt;
178}
179
186inline std::optional<iv::types::channelValue> processValue(std::string_view stringValue,
188{
189 std::optional<iv::types::channelValue> retValue;
190
191 switch (nmeaFieldType)
192 {
194 retValue = nmea0183FieldToBoolean(stringValue);
195 break;
196
198 retValue = nmea0183FieldToInteger(stringValue);
199 break;
200
202 retValue = nmea0183FieldToFloat(stringValue);
203 break;
204
206 // TODO: Implementar el parseo de string
207 retValue = stringValue.at(0);
208 break;
209
210 default:
211 throw iv::exception::NotImplemented("NMEA0183 Field Type not implemented", __FILE__, __LINE__);
212 break;
213 }
214 return retValue;
215}
216
217}// namespace iv::comms::nmea0183
218
219#endif//NIX_LIBS_NMEA_NMEATOOLS_HPP_
#define IV_ASSERT_MSG(msg,...)
Definition assert.hpp:152
Definition enums.hpp:5
eFieldType
Definition enums.hpp:328
bool getCoordinates(std::string array, double &decimalDegrees)
Converts a string with the format "D*MM.MMMM" to decimal degrees.
Definition functions.hpp:84
std::optional< float > nmea0183FieldToFloat(std::string_view data)
Converts a NMEA0183 field to a float.
Definition functions.hpp:141
double degreesToDecimal(const int32_t degrees, const double minutes, const int32_t seconds=0)
Converts degress, minutes and seconds to decimal geometric degrees.
Definition functions.hpp:58
std::optional< bool > nmea0183FieldToBoolean(std::string_view data)
Converts a NMEA0183 field to a boolean.
Definition functions.hpp:110
std::optional< std::string > nmea0183FieldToTime(std::string_view data)
Converts a NMEA0183 time field to a string representation of day time.
Definition functions.hpp:151
std::optional< uint16_t > nmea0183FieldToInteger(std::string_view data)
Converts a NMEA0183 field to an integer.
Definition functions.hpp:131
std::string nmeaChecksum(const std::string &completeMsg)
Compute the checksum of a message for NMEA0183.
Definition functions.hpp:22
std::optional< iv::comms::nmea0183::Pole > nmeaField0183ToPole(std::string_view data)
Converts a NMEA0183 field to a Pole.
Definition functions.hpp:164
std::optional< iv::types::channelValue > processValue(std::string_view stringValue, iv::comms::nmea0183::eFieldType nmeaFieldType)
parses the value of a NMEA0183 field according to it's type
Definition functions.hpp:186
std::vector< std::string > splitStringBy(const std::string &sentence, const char delimiter)
Definition functions.hpp:63
constexpr uint8_t nmeaChecksumDelimiter
Definition defines.hpp:118
constexpr uint8_t nmeaHeaderStart
Definition defines.hpp:116
std::optional< T > parseNumber(const Str &stringNumber)
Definition parsers.hpp:12
#define MINUTE_LENGTH
Definition functions.hpp:12