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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
#include <SoftwareSerial.h> //#include <SPI.h> //#include <SD.h> #include <Adafruit_SSD1306.h> #define OLED_RESET 9 Adafruit_SSD1306 display(OLED_RESET); #if (SSD1306_LCDHEIGHT != 64) #error("Height incorrect, please fix Adafruit_SSD1306.h!"); #endif // Connect the GPS RX/TX to arduino pins 7 and 8 SoftwareSerial S_Serial = SoftwareSerial(7,8); int intpin = 2; volatile int state = LOW; const unsigned char UBX_HEADER[] = { 0xB5, 0x62 }; struct NAV_PVT { unsigned char cls; unsigned char id; unsigned short len; unsigned long iTOW; unsigned short year; byte month; byte day; byte hour; byte miniute; byte sec; unsigned char valid; unsigned long tAcc; long nano; byte fixType; unsigned char flag; unsigned char flag2; byte numSV; long lon; long lat; long height; long hMSL; unsigned long hAcc; unsigned long vAcc; long velN; long velE; long velD; long gSpeed; long headMot; unsigned long sAcc; unsigned long headAcc; unsigned short pDop; byte reserved1[6]; long headVeh; byte reserved2[4]; }; NAV_PVT pvt; void calcChecksum(unsigned char* CK) { memset(CK, 0, 2); for (int i = 0; i < (int)sizeof(NAV_PVT); i++) { CK[0] += ((unsigned char*)(&pvt))[i]; CK[1] += CK[0]; } } bool processGPS() { static int fpos = 0; static unsigned char checksum[2]; const int payloadSize = sizeof(NAV_PVT); while ( S_Serial.available() ) { byte c = S_Serial.read(); if ( fpos < 2 ) { //SYNC if ( c == UBX_HEADER[fpos] ) fpos++; else fpos = 0; } else { if ( (fpos-2) < payloadSize ) ((unsigned char*)(&pvt))[fpos-2] = c; fpos++; if ( fpos == (payloadSize+2) ) { calcChecksum(checksum); } else if ( fpos == (payloadSize+3) ) { if ( c != checksum[0] ) fpos = 0; } else if ( fpos == (payloadSize+4) ) { fpos = 0; if ( c == checksum[1] ) { return true; } } else if ( fpos > (payloadSize+4) ) { fpos = 0; } } } return false; } void pps() { state=!state; } void PrintScreen() { display.clearDisplay(); //Size 2 display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); float sik = (pvt.gSpeed / 100) / 3.6f; if (sik<100) display.print(" "); if (sik<10) display.print(" "); display.print(sik,1);//SpeedInKph display.setCursor(84,0); if (pvt.numSV<10) display.print(" "); display.print(pvt.numSV);//SV //Size 1 display.setTextSize(1); display.setCursor(60,8); display.print("km/h"); display.setCursor(63,0); display.print("CHL"); display.setCursor(108,8); display.print("SVs"); display.setCursor(108,0); switch (pvt.fixType){ case 0: display.print("N.F"); break; case 2: display.print("2D"); break; case 3: display.print("3D"); if ((byte)pvt.flag & 2) display.print("+"); break; default: display.print("INV"); break; } long temp; //Line 1 display.setCursor(0,16); if (pvt.lat >= 0) temp = pvt.lat; else temp = -pvt.lat; if (temp >= 1000000000) display.print("lat:"); else if (temp >= 100000000) display.print("lat: "); else display.print("lat: "); display.print(temp / 10000000);display.print("."); long temp2 = 1000000; temp = temp % 10000000; while ((temp / temp2 == 0)&&(temp2>1)) { display.print("0"); temp2 /= 10; } display.print(temp); if (pvt.lat >= 0) display.print("N"); else display.print("S"); temp = 10000; while (((pvt.pDop / temp) == 0)&&(temp>1)) { display.print(" "); temp /= 10; } display.println(pvt.pDop); //Line2 if (pvt.lon >= 0) temp = pvt.lon; else temp = -pvt.lon; if (temp >= 1000000000) display.print("lon:"); else if (temp >= 100000000) display.print("lon: "); else display.print("lon: "); display.print(temp / 10000000);display.print("."); temp2 = 1000000; temp = temp % 10000000; while ((temp / temp2 == 0)&&(temp2>1)) { display.print("0"); temp2 /= 10; } display.print(temp); if (pvt.lon >= 0) display.print("E"); else display.print("W"); display.println(" HEAD"); //Line3 display.print("hMSL:"); if (pvt.hMSL < 0) pvt.hMSL = -pvt.hMSL; temp = 10000000; while (((pvt.hMSL / temp) == 0)&&(temp>1000)) { display.print(" "); temp /= 10; } display.print((pvt.hMSL % 100000000) / 1000);display.print("."); if ((pvt.hMSL % 1000) < 100) display.print("0"); if ((pvt.hMSL % 1000) < 10) display.print("0"); display.print(pvt.hMSL % 1000); display.print("|"); temp = pvt.headMot / 100000; if (temp < 100) display.print(" "); if (temp < 10) display.print(" "); display.print(temp);display.print("."); temp2 = (pvt.headMot%100000)/1000; if (temp2<0) temp2 = -temp2; if (temp2<10) display.print("0"); display.println(temp2); //Line4 display.print("h,vAcc:"); if (pvt.hAcc >= 100000) display.print(" >100"); else { display.print(pvt.hAcc / 1000);display.print("."); if (pvt.hAcc % 1000 < 100) display.print("0"); if (pvt.hAcc % 1000 < 10) display.print("0"); display.print(pvt.hAcc % 1000); } display.print(","); if (pvt.vAcc >= 100000) display.print(" >100"); else { display.print(pvt.vAcc / 1000);display.print("."); if (pvt.vAcc%1000 < 100) display.print("0"); if (pvt.vAcc%1000 < 10) display.print("0"); display.print(pvt.vAcc%1000); } display.println("m"); //Line5 display.print("V:"); if (pvt.gSpeed < 1000000) display.print(" "); if (pvt.gSpeed < 100000) display.print(" "); if (pvt.gSpeed < 10000) display.print(" "); display.print((pvt.gSpeed % 10000000)/1000);display.print("."); if (pvt.gSpeed % 1000 < 100) display.print("0"); if (pvt.gSpeed % 1000 < 10) display.print("0"); display.print(pvt.gSpeed % 1000); display.print("m/s"); if (pvt.sAcc < 1000000) { if (pvt.sAcc < 100000) display.print(" "); if (pvt.sAcc < 10000) display.print(" "); display.print("~"); display.print(pvt.sAcc / 1000);display.print("."); if (pvt.sAcc % 1000 < 100) display.print("0"); if (pvt.sAcc % 1000 < 10) display.print("0"); display.println(pvt.sAcc % 1000); } else display.println(" ~>100"); //Line6 if ((pvt.year<10000)&&(pvt.year>=1000)) display.print(pvt.year); else display.print("????"); display.print("/"); if ((pvt.month<=12)&&(pvt.month>=1)) { if (pvt.month < 10) display.print("0"); display.print(pvt.month); } else display.print("??"); display.print("/"); if ((pvt.day<=31)&&(pvt.day>=1)) { if (pvt.day < 10) display.print("0"); display.print(pvt.day); } else display.print("??"); if (pvt.valid & 0x04) display.print("UTC"); else display.print("INV"); if ((pvt.hour<=23)&&(pvt.hour>=0)) { if (pvt.hour < 10) display.print("0"); display.print(pvt.hour); } else display.print("??"); display.print(":"); if ((pvt.miniute<=59)&&(pvt.miniute>=0)) { if (pvt.miniute < 10) display.print("0"); display.print(pvt.miniute); } else display.print("??"); display.print(":"); if ((pvt.sec<=60)&&(pvt.sec>=0)) { if (pvt.sec < 10) display.print("0"); display.print(pvt.sec); } else display.print("??"); display.display(); } void PrintSec() { pvt.sec = (pvt.sec + 1)%60; display.fillRect(114,56,12,8,BLACK); display.setCursor(114,56); if (pvt.sec<10) display.print("0"); display.println(pvt.sec); display.display(); state = LOW; } void setup() { //Serial.begin(9600); S_Serial.begin(9600); pinMode(intpin, INPUT); attachInterrupt(digitalPinToInterrupt(intpin), pps, RISING); // by default, we'll generate the high voltage from the 3.3v line internally! (neat!) display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64) // init done } void loop() { if (state == HIGH) PrintSec(); if ( processGPS() ) PrintScreen(); } |