84 lines
1.7 KiB
C
84 lines
1.7 KiB
C
/*
|
|
* cm_nmea_decoder.c
|
|
*
|
|
* Created on: 26 Jun 2025
|
|
* Author: Christian Lind Vie Madsen
|
|
*/
|
|
|
|
#include "cm_nmea_decoder.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#define MAX_NMEA_MSG_TYPES 2
|
|
const char *nmea_msg_types[MAX_NMEA_MSG_TYPES] = {"$GPGLL", "$GPGFF"};
|
|
typedef int (*nmeaDecoder_Evt_fpt)(cm_nmea_msg_t *inst, char *str);
|
|
|
|
static int findLenToChar(const char *str, const char character){
|
|
|
|
int i = 0;
|
|
|
|
while((str[i] != character) && (str[i] != '\0')) i++;
|
|
|
|
return i;
|
|
}
|
|
|
|
static int sGPGLL_decode(cm_nmea_msg_t *inst, char *str){
|
|
|
|
printf("%s \r\n ",str);
|
|
|
|
//Move pointer until we meet a "," or NULL!
|
|
while((str != NULL) && (*str != ',')) str++;
|
|
|
|
// Move past ','
|
|
if (*str == ',') str++;
|
|
|
|
// Find comma
|
|
int comma_idx = findLenToChar(str, '.');
|
|
|
|
// Replace with '\0'
|
|
str[comma_idx] = '\0';
|
|
|
|
// Decode first part of latitude
|
|
inst->gpgll_msg.coordinates.lat_deg = atoi(str);
|
|
|
|
printf("deg: %d, str: %s \r\n ",comma_idx,str);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
nmeaDecoder_Evt_fpt nmea_decodeFuncs[1] = {
|
|
(void *)&sGPGLL_decode,
|
|
};
|
|
|
|
static int sNMEA_getMsgType(char *str){
|
|
|
|
for(int i = 0; i < (MAX_NMEA_MSG_TYPES-1); i++){
|
|
|
|
if(strcmp(nmea_msg_types[i], str)) return i;
|
|
};
|
|
|
|
return -1;
|
|
}
|
|
|
|
int cm_nmea_characterDecode(char in){
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// This function returns the message type, so the user knows which message has been decoded!
|
|
int cm_nmea_stringDecode(cm_nmea_msg_t *inst, char *str){
|
|
|
|
// Find which type of message we need to decode.
|
|
int nmea_msg_idx = sNMEA_getMsgType(str);
|
|
|
|
// Check if we found a valid NMEA message.
|
|
if(nmea_msg_idx == -1) return nmea_msg_idx;
|
|
|
|
// Use the correct function to decode!
|
|
if(nmea_msg_idx >= 0) nmea_decodeFuncs[nmea_msg_idx](inst,str);
|
|
|
|
return nmea_msg_idx;
|
|
|
|
}
|