Files
cm_nmea_decoder/cm_nmea_decoder.c
Christian Lind Vie Madsen 59c08f05f8 some work done..
2025-06-26 16:11:40 +02:00

82 lines
1.6 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){
//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.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;
}