Created
January 10, 2019 03:42
-
-
Save gdunstone/7e2ba86c5ca77b3e9135557433ebedf9 to your computer and use it in GitHub Desktop.
heltec GPS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier | |
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"` | |
#define SDA 4 | |
#define SCL 15 | |
#define RST 16 //RST must be set by software | |
#define DISPLAY_HEIGHT 64 | |
#define DISPLAY_WIDTH 128 | |
SSD1306 display(0x3c, SDA, SCL, RST); | |
float speed_kmh = 0.0; | |
int satellites_num = 0; | |
// The TinyGPS++ object | |
#include <TinyGPS++.h> | |
TinyGPSPlus gps; | |
// Adapted from Adafruit_SSD1306 | |
void drawRect(void) { | |
for (int16_t i=0; i<DISPLAY_HEIGHT/2; i+=2) { | |
display.drawRect(i, i, DISPLAY_WIDTH-2*i, DISPLAY_HEIGHT-2*i); | |
display.display(); | |
delay(20); | |
} | |
} | |
void setup() { | |
// gps init | |
Serial.begin(9600); | |
// display init | |
delay(200); | |
display.init(); | |
// display.flipScreenVertically(); | |
display.setFont(ArialMT_Plain_24); | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
drawRect(); | |
display.display(); | |
delay(100); | |
display.clear(); | |
display.drawString(64,15, "Hello!"); | |
display.display(); | |
delay(500); | |
display.clear(); | |
display.display(); | |
updateDisplay(); | |
} | |
bool onoff = false; | |
char * TimeToString(int h, int m, int s){ | |
static char str[12]; | |
sprintf(str, "%02d:%02d:%02d", h, m, s); | |
return str; | |
} | |
char * FormatSpeed(){ | |
static char str[3]; | |
dtostrf(speed_kmh, 1, 1, str); | |
if (speed_kmh < 10){ | |
sprintf(str, "0%s", str); | |
} | |
return str; | |
} | |
void updateDisplay(){ | |
display.clear(); | |
if (onoff) display.drawRect(1, 1, DISPLAY_WIDTH-2, DISPLAY_HEIGHT-2); | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
display.setFont(ArialMT_Plain_16); | |
display.drawString(64,3, "sats: " +String(satellites_num)); | |
display.drawString(64,42, TimeToString(gps.time.hour(), gps.time.minute(), gps.time.second())); | |
display.setFont(ArialMT_Plain_24); | |
display.drawString(64,15, FormatSpeed()+String("kph")); | |
display.display(); | |
delay(20); | |
} | |
void loop() { | |
while (Serial.available() > 0) { | |
if (gps.encode(Serial.read())){ | |
onoff = !onoff; | |
satellites_num = gps.satellites.value(); | |
speed_kmh = gps.speed.kmph(); | |
updateDisplay(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment