GPS/GNSS UBX library for UART
Dependencies: Vector3
Diff: GPSUBX_UART.cpp
- Revision:
- 1:71f5168e48c8
- Parent:
- 0:cf7c726ec8a1
- Child:
- 2:6218fe8e54f4
--- a/GPSUBX_UART.cpp Mon Sep 13 16:04:40 2021 +0000 +++ b/GPSUBX_UART.cpp Fri Sep 17 15:06:47 2021 +0000 @@ -3,8 +3,8 @@ #include <math.h> #define M_PI 3.14159265358979f -GPSUBX_UART::GPSUBX_UART(PinName tx, PinName rx, int baud, int timezone = +9) - :serial(tx, rx, baud), TimeZone(timezone) +GPSUBX_UART::GPSUBX_UART(PinName tx, PinName rx, int baud, int timezone) + :serial(tx, rx, baud), TimeZone(timezone), receive_index(0) { } @@ -23,21 +23,97 @@ void GPSUBX_UART::Receive() { - volatile static int receive_start = 0; - volatile static int receive_index = 0; - volatile static int sentence_start = 0; - volatile static int sentence_length = 0; - volatile static int sentence_counter = 0; - - volatile static char m_class = 0x00; - volatile static char m_id = 0x00; +// volatile static int receive_start = 0; +// volatile static int receive_index = 0; +// volatile static int sentence_start = 0; +// volatile static int sentence_length = 0; +// volatile static int sentence_counter = 0; +// +// volatile static char m_class = 0x00; +// volatile static char m_id = 0x00; while (serial.readable()) { char c; c = serial.getc(); receive_buffer[receive_index] = c; -// pc.printf("%d\r\n", c); +//// pc.printf("%d\r\n", c); +// if (sentence_counter >= 2) +// { +// sentence_counter++; +// if (sentence_counter == 3) +// { +// m_class = c; +//// pc.printf("@%x\r\n", m_class); +// } +// else if (sentence_counter == 4) +// { +// m_id = c; +//// pc.printf("@@%x\r\n", m_id); +// } +// else if (sentence_counter == 5) +// { +// } +// else if (sentence_counter == 6) +// { +// int sss = (receive_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; +// sentence_length = (int)(c << 8 | receive_buffer[sss]); +//// printf("%d\r\n", sentence_length); +// } +// else if (sentence_counter >= sentence_length+8) +// { +// for (int i = 0; i < sentence_length+8; i++) +// { +// sentence_buffer[i] = receive_buffer[(sentence_start+i)%RECEIVE_SIZE]; +// } +//// for (int i = 0; i < sentence_length+8; i++) +//// { +//// pc.printf("%x ", sentence_buffer[i]); +//// } +//// pc.printf("\r\n"); +// char ca, cb; +// Checksum(sentence_buffer, sentence_length, &ca, &cb); +//// pc.printf("^%x %x\r\n", m_class, m_id); +// if (ca == sentence_buffer[sentence_length+6] && cb == sentence_buffer[sentence_length+7]) +// { +//// pc.printf("=%x %x\r\n", m_class, m_id); +// Decode(sentence_buffer, m_class, m_id); +// } +// sentence_start = 0; +// sentence_length = 0; +// sentence_counter = 0; +// m_class = 0x00; +// m_id = 0x00; +// } +// } +// +// int ss = (receive_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; +// if (c == 0x62 && receive_buffer[ss] == 0xb5) +// { +// sentence_start = ss; +// sentence_counter = 2; +// } + receive_index = (receive_index + 1) % RECEIVE_SIZE; + } +} + + +void GPSUBX_UART::Update() +{ +// volatile static int receive_start = 0; +// volatile static int receive_index = 0; + volatile static int sentence_start = 0; + volatile static int sentence_length = 0; + volatile static int sentence_counter = 0; + volatile static int read_index = 0; + + volatile static char m_class = 0x00; + volatile static char m_id = 0x00; + + while (read_index != receive_index) + { + char c; + c = receive_buffer[read_index]; if (sentence_counter >= 2) { sentence_counter++; @@ -56,7 +132,7 @@ } else if (sentence_counter == 6) { - int sss = (receive_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; + int sss = (read_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; sentence_length = (int)(c << 8 | receive_buffer[sss]); // printf("%d\r\n", sentence_length); } @@ -87,13 +163,13 @@ } } - int ss = (receive_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; + int ss = (read_index+RECEIVE_SIZE-1)%RECEIVE_SIZE; if (c == 0x62 && receive_buffer[ss] == 0xb5) { sentence_start = ss; sentence_counter = 2; } - receive_index = (receive_index + 1) % RECEIVE_SIZE; + read_index = (read_index + 1) % RECEIVE_SIZE; } } @@ -129,7 +205,7 @@ Year = timeutc.data.year; Month = timeutc.data.month; Day = timeutc.data.day; - Hours = timeutc.data.hour + TimeZone; + Hours = (int)timeutc.data.hour + TimeZone; if (Hours >= 24) { Hours -= 24;