arduno uno GPS OLED 輸出
喵的咧 用U8g2來寫直接記憶體爆掉
完整程式碼如下
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
//#include <Arduino.h>
//#include <U8g2lib.h>
#include <U8x8lib.h>
//#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
//#endif
//#ifdef U8X8_HAVE_HW_I2C
//#include <Wire.h>
//#endif
U8X8_SSD1306_128X64_NONAME_SW_I2C u8x8(/* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // OLEDs without Reset of the Display
//U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
int RXPin = 2;
int TXPin = 3;
int GPSBaud = 9600;
SoftwareSerial gpsSerial(RXPin, TXPin);
TinyGPSPlus gps; // Create a TinyGPS++ object
void setup(void) {
u8x8.begin();
u8x8.setPowerSave(0);
Serial.begin(9600);
gpsSerial.begin(GPSBaud);
}
void monShow(float La,float Lo,float Al){
//u8g2.clearBuffer(); // clear the internal memory
u8x8.clear();
u8x8.setFont(u8x8_font_chroma48medium8_r); // choose a suitable font u8g2_font_ncenB08_tr u8g2_font_pcsenior_8u
u8x8.setCursor(0,10); // write something to the internal memory
u8x8.print("Latitude:");
u8x8.print(La);
u8x8.setCursor(0,20);
u8x8.print("Longitude:");
u8x8.print(Lo);
u8x8.setCursor(0,30);
u8x8.print("Altitude:");
u8x8.print(Al);
//u8g2.sendBuffer(); // transfer internal memory to the display
u8x8.refreshDisplay();
delay(1000);
}
void monShow_not_ava(){ // clear the internal memory
u8x8.clear();
u8x8.setFont(u8x8_font_chroma48medium8_r); // choose a suitable font u8g2_font_ncenB08_tr u8g2_font_pcsenior_8u
u8x8.setCursor(0,10); // write something to the internal memory
u8x8.print("Location: Not Available");
}
void displayInfo()
{
if (gps.location.isValid())
{
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
Serial.print("Altitude: ");
Serial.println(gps.altitude.meters());
monShow(gps.location.lat(),gps.location.lng(),gps.altitude.meters());
}
else
{
Serial.println("Location: Not Available");
monShow_not_ava();
}
delay(1000);
Serial.println();
}
void loop() {
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
displayInfo();
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
monShow_not_ava();
//while(true);
}
}
留言
張貼留言