![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
GPS test
main.cpp@0:f7f81e36e9d7, 2021-04-05 (annotated)
- Committer:
- cocorlow
- Date:
- Mon Apr 05 09:18:42 2021 +0000
- Revision:
- 0:f7f81e36e9d7
- Child:
- 1:020b393621e5
gps
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cocorlow | 0:f7f81e36e9d7 | 1 | #include "mbed.h" |
cocorlow | 0:f7f81e36e9d7 | 2 | #include "AsyncSerial.hpp" |
cocorlow | 0:f7f81e36e9d7 | 3 | #include <string.h> |
cocorlow | 0:f7f81e36e9d7 | 4 | //"include <strlib.h> |
cocorlow | 0:f7f81e36e9d7 | 5 | |
cocorlow | 0:f7f81e36e9d7 | 6 | Serial pc(USBTX, USBRX); |
cocorlow | 0:f7f81e36e9d7 | 7 | Serial gps(PC_12, PD_2); |
cocorlow | 0:f7f81e36e9d7 | 8 | |
cocorlow | 0:f7f81e36e9d7 | 9 | void GPS_Read(); |
cocorlow | 0:f7f81e36e9d7 | 10 | |
cocorlow | 0:f7f81e36e9d7 | 11 | const int uart_max = 1000; |
cocorlow | 0:f7f81e36e9d7 | 12 | int uart_start = 0; |
cocorlow | 0:f7f81e36e9d7 | 13 | int uart_index = 0; |
cocorlow | 0:f7f81e36e9d7 | 14 | char uart_buffer[uart_max]; |
cocorlow | 0:f7f81e36e9d7 | 15 | |
cocorlow | 0:f7f81e36e9d7 | 16 | void gps_receive() |
cocorlow | 0:f7f81e36e9d7 | 17 | { |
cocorlow | 0:f7f81e36e9d7 | 18 | while (gps.readable()) |
cocorlow | 0:f7f81e36e9d7 | 19 | { |
cocorlow | 0:f7f81e36e9d7 | 20 | char c; |
cocorlow | 0:f7f81e36e9d7 | 21 | c = gps.getc(); |
cocorlow | 0:f7f81e36e9d7 | 22 | uart_buffer[uart_index] = c; |
cocorlow | 0:f7f81e36e9d7 | 23 | if (c == '$') |
cocorlow | 0:f7f81e36e9d7 | 24 | { |
cocorlow | 0:f7f81e36e9d7 | 25 | uart_start = uart_index; |
cocorlow | 0:f7f81e36e9d7 | 26 | } |
cocorlow | 0:f7f81e36e9d7 | 27 | if (c == '\n') |
cocorlow | 0:f7f81e36e9d7 | 28 | { |
cocorlow | 0:f7f81e36e9d7 | 29 | GPS_Read(); |
cocorlow | 0:f7f81e36e9d7 | 30 | } |
cocorlow | 0:f7f81e36e9d7 | 31 | uart_index = (uart_index + 1) % uart_max; |
cocorlow | 0:f7f81e36e9d7 | 32 | } |
cocorlow | 0:f7f81e36e9d7 | 33 | } |
cocorlow | 0:f7f81e36e9d7 | 34 | |
cocorlow | 0:f7f81e36e9d7 | 35 | |
cocorlow | 0:f7f81e36e9d7 | 36 | void GPS_Read() |
cocorlow | 0:f7f81e36e9d7 | 37 | { |
cocorlow | 0:f7f81e36e9d7 | 38 | char line[256]; |
cocorlow | 0:f7f81e36e9d7 | 39 | for (int i = 0 ;i < 256; i++) |
cocorlow | 0:f7f81e36e9d7 | 40 | { |
cocorlow | 0:f7f81e36e9d7 | 41 | line[i] = 0; |
cocorlow | 0:f7f81e36e9d7 | 42 | } |
cocorlow | 0:f7f81e36e9d7 | 43 | bool flag = false; |
cocorlow | 0:f7f81e36e9d7 | 44 | int counts = 0; |
cocorlow | 0:f7f81e36e9d7 | 45 | for (int i = 0; i < 256; i++) |
cocorlow | 0:f7f81e36e9d7 | 46 | { |
cocorlow | 0:f7f81e36e9d7 | 47 | line[i] = uart_buffer[(uart_start + i) % uart_max]; |
cocorlow | 0:f7f81e36e9d7 | 48 | counts++; |
cocorlow | 0:f7f81e36e9d7 | 49 | if (line[i] == '\n') |
cocorlow | 0:f7f81e36e9d7 | 50 | { |
cocorlow | 0:f7f81e36e9d7 | 51 | flag = true; |
cocorlow | 0:f7f81e36e9d7 | 52 | break; |
cocorlow | 0:f7f81e36e9d7 | 53 | } |
cocorlow | 0:f7f81e36e9d7 | 54 | } |
cocorlow | 0:f7f81e36e9d7 | 55 | if (flag) |
cocorlow | 0:f7f81e36e9d7 | 56 | { |
cocorlow | 0:f7f81e36e9d7 | 57 | pc.printf("Counts:%d\r\n", counts); |
cocorlow | 0:f7f81e36e9d7 | 58 | pc.printf("%s\r\n", line); |
cocorlow | 0:f7f81e36e9d7 | 59 | } |
cocorlow | 0:f7f81e36e9d7 | 60 | } |
cocorlow | 0:f7f81e36e9d7 | 61 | |
cocorlow | 0:f7f81e36e9d7 | 62 | int main() |
cocorlow | 0:f7f81e36e9d7 | 63 | { |
cocorlow | 0:f7f81e36e9d7 | 64 | pc.baud(115200); |
cocorlow | 0:f7f81e36e9d7 | 65 | gps.baud(57600); |
cocorlow | 0:f7f81e36e9d7 | 66 | gps.attach(gps_receive, Serial::RxIrq); |
cocorlow | 0:f7f81e36e9d7 | 67 | while (1) |
cocorlow | 0:f7f81e36e9d7 | 68 | { |
cocorlow | 0:f7f81e36e9d7 | 69 | // GPS_Read(); |
cocorlow | 0:f7f81e36e9d7 | 70 | // pc.printf("A\n"); |
cocorlow | 0:f7f81e36e9d7 | 71 | |
cocorlow | 0:f7f81e36e9d7 | 72 | } |
cocorlow | 0:f7f81e36e9d7 | 73 | } |