1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <freertos/message_buffer.h>
MessageBufferHandle_t xMessageBuffer = NULL;
const size_t xMessageBufferSizeBytes = 100; xMessageBuffer = xMessageBufferCreate( xMessageBufferSizeBytes );
String randomGPS() { char gps[30];
static int counter = 100; counter++; switch (random(0, 3)) { case 0: sprintf(gps, "%d-%d-%d-%d", counter, random(100, 999), random(100, 999), random(100, 999)); break; case 1: sprintf(gps, "%d-%d-%d", counter, random(100, 999), random(100, 999)); break; case 2: sprintf(gps, "%d-%d", counter, random(100, 999)); break; default: break; }
return String(gps); }
void readGPS(void * pvParam) { size_t xBytesSent; String gpsInfo; while (1) { gpsInfo = randomGPS(); xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) &gpsInfo, sizeof( gpsInfo ), portMAX_DELAY );
if ( xBytesSent != sizeof( gpsInfo ) ) { Serial.println("危险: xMessageBufferSend 发送数据不完整"); } vTaskDelay(3000); } }
void gpsDecoder(String gpsinfo) { struct GPS { int counter; int longVal; int latVal; int AltVal; };
String s1, s2, s3, s4; int counter = 0; String gpsinfo2 = gpsinfo; do { counter++; int index = gpsinfo.indexOf('-'); if (gpsinfo.indexOf('-') < 0) gpsinfo = ""; gpsinfo = gpsinfo.substring(index + 1, gpsinfo.length()); } while (gpsinfo.length() > 0);
GPS gps;
switch (counter) { case 2: gps.counter = gpsinfo2.substring(0, 3).toInt(); gps.longVal = -1; gps.latVal = -1; gps.AltVal = gpsinfo2.substring(4, 7).toInt(); break; case 3: gps.counter = gpsinfo2.substring(0, 3).toInt(); gps.longVal = gpsinfo2.substring(4, 7).toInt(); gps.latVal = gpsinfo2.substring(8, 11).toInt(); gps.AltVal = -1; break; case 4: gps.counter = gpsinfo2.substring(0, 3).toInt(); gps.longVal = gpsinfo2.substring(4, 7).toInt(); gps.latVal = gpsinfo2.substring(8, 11).toInt(); gps.AltVal = gpsinfo2.substring(12, 15).toInt(); break; default: break; }
void showGPS(void * pvParam) { size_t xReceivedBytes; String gpsInfo; const size_t xMessageSizeMax = 100; lcd.init(); lcd.backlight(); lcd.setCursor(0, 0); lcd.print(" GPS INFO"); while (1) { xReceivedBytes = xMessageBufferReceive( xMessageBuffer, ( void * ) &gpsInfo, xMessageSizeMax, portMAX_DELAY );
if ( xReceivedBytes > 0 ) { gpsDecoder(gpsInfo); }
vTaskDelay(1000); } }
|