Exercise 10 works now, #9 needs to be revised accordingly
This commit is contained in:
parent
3b15b0aeef
commit
0077381546
12 changed files with 1775 additions and 0 deletions
508
exercises/09_GPS_Time/src/main.cpp
Normal file
508
exercises/09_GPS_Time/src/main.cpp
Normal file
|
|
@ -0,0 +1,508 @@
|
|||
// 20260217 ChatGPT
|
||||
// $Id$
|
||||
// $HeadURL$
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <U8g2lib.h>
|
||||
|
||||
#include "StartupSdManager.h"
|
||||
#include "tbeam_supreme_adapter.h"
|
||||
|
||||
#ifndef OLED_SDA
|
||||
#define OLED_SDA 17
|
||||
#endif
|
||||
|
||||
#ifndef OLED_SCL
|
||||
#define OLED_SCL 18
|
||||
#endif
|
||||
|
||||
#ifndef OLED_ADDR
|
||||
#define OLED_ADDR 0x3C
|
||||
#endif
|
||||
|
||||
#ifndef RTC_I2C_ADDR
|
||||
#define RTC_I2C_ADDR 0x51
|
||||
#endif
|
||||
|
||||
#ifndef GPS_BAUD
|
||||
#define GPS_BAUD 9600
|
||||
#endif
|
||||
|
||||
static const uint32_t kSerialDelayMs = 5000;
|
||||
static const uint32_t kGpsStartupProbeMs = 20000;
|
||||
static const uint32_t kMinuteMs = 60000;
|
||||
|
||||
static XPowersLibInterface* g_pmu = nullptr;
|
||||
static StartupSdManager g_sd(Serial);
|
||||
static U8G2_SH1106_128X64_NONAME_F_HW_I2C g_oled(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
|
||||
static HardwareSerial g_gpsSerial(1);
|
||||
|
||||
static uint32_t g_logSeq = 0;
|
||||
static uint32_t g_lastMinuteReportMs = 0;
|
||||
|
||||
static bool g_prevHadSatellites = false;
|
||||
static bool g_prevHadValidUtc = false;
|
||||
static bool g_satellitesAcquiredAnnounced = false;
|
||||
static bool g_timeAcquiredAnnounced = false;
|
||||
|
||||
static char g_gpsLine[128];
|
||||
static size_t g_gpsLineLen = 0;
|
||||
|
||||
enum class GpsModuleKind : uint8_t {
|
||||
UNKNOWN = 0,
|
||||
L76K,
|
||||
QUECTEL_TODO
|
||||
};
|
||||
|
||||
struct RtcDateTime {
|
||||
uint16_t year;
|
||||
uint8_t month;
|
||||
uint8_t day;
|
||||
uint8_t hour;
|
||||
uint8_t minute;
|
||||
uint8_t second;
|
||||
};
|
||||
|
||||
struct GpsState {
|
||||
GpsModuleKind module = GpsModuleKind::UNKNOWN;
|
||||
bool sawAnySentence = false;
|
||||
|
||||
uint8_t satsUsed = 0;
|
||||
uint8_t satsInView = 0;
|
||||
|
||||
bool hasValidUtc = false;
|
||||
uint16_t utcYear = 0;
|
||||
uint8_t utcMonth = 0;
|
||||
uint8_t utcDay = 0;
|
||||
uint8_t utcHour = 0;
|
||||
uint8_t utcMinute = 0;
|
||||
uint8_t utcSecond = 0;
|
||||
};
|
||||
|
||||
static GpsState g_gps;
|
||||
|
||||
static void logf(const char* fmt, ...) {
|
||||
char msg[220];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
vsnprintf(msg, sizeof(msg), fmt, args);
|
||||
va_end(args);
|
||||
Serial.printf("[%10lu][%06lu] %s\r\n", (unsigned long)millis(), (unsigned long)g_logSeq++, msg);
|
||||
}
|
||||
|
||||
static void oledShowLines(const char* l1,
|
||||
const char* l2 = nullptr,
|
||||
const char* l3 = nullptr,
|
||||
const char* l4 = nullptr,
|
||||
const char* l5 = nullptr) {
|
||||
g_oled.clearBuffer();
|
||||
g_oled.setFont(u8g2_font_5x8_tf);
|
||||
if (l1) g_oled.drawUTF8(0, 12, l1);
|
||||
if (l2) g_oled.drawUTF8(0, 24, l2);
|
||||
if (l3) g_oled.drawUTF8(0, 36, l3);
|
||||
if (l4) g_oled.drawUTF8(0, 48, l4);
|
||||
if (l5) g_oled.drawUTF8(0, 60, l5);
|
||||
g_oled.sendBuffer();
|
||||
}
|
||||
|
||||
static uint8_t fromBcd(uint8_t b) {
|
||||
return ((b >> 4U) * 10U) + (b & 0x0FU);
|
||||
}
|
||||
|
||||
static bool rtcRead(RtcDateTime& out, bool& lowVoltageFlag) {
|
||||
Wire1.beginTransmission(RTC_I2C_ADDR);
|
||||
Wire1.write(0x02);
|
||||
if (Wire1.endTransmission(false) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint8_t need = 7;
|
||||
uint8_t got = Wire1.requestFrom((int)RTC_I2C_ADDR, (int)need);
|
||||
if (got != need) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t sec = Wire1.read();
|
||||
uint8_t min = Wire1.read();
|
||||
uint8_t hour = Wire1.read();
|
||||
uint8_t day = Wire1.read();
|
||||
(void)Wire1.read(); // weekday
|
||||
uint8_t month = Wire1.read();
|
||||
uint8_t year = Wire1.read();
|
||||
|
||||
lowVoltageFlag = (sec & 0x80U) != 0;
|
||||
out.second = fromBcd(sec & 0x7FU);
|
||||
out.minute = fromBcd(min & 0x7FU);
|
||||
out.hour = fromBcd(hour & 0x3FU);
|
||||
out.day = fromBcd(day & 0x3FU);
|
||||
out.month = fromBcd(month & 0x1FU);
|
||||
uint8_t yy = fromBcd(year);
|
||||
bool century = (month & 0x80U) != 0;
|
||||
out.year = century ? (1900U + yy) : (2000U + yy);
|
||||
return true;
|
||||
}
|
||||
|
||||
static String formatRtcNow() {
|
||||
RtcDateTime now{};
|
||||
bool lowV = false;
|
||||
if (!rtcRead(now, lowV)) {
|
||||
return "RTC: read failed";
|
||||
}
|
||||
|
||||
char buf[48];
|
||||
snprintf(buf,
|
||||
sizeof(buf),
|
||||
"RTC %04u-%02u-%02u %02u:%02u:%02u%s",
|
||||
(unsigned)now.year,
|
||||
(unsigned)now.month,
|
||||
(unsigned)now.day,
|
||||
(unsigned)now.hour,
|
||||
(unsigned)now.minute,
|
||||
(unsigned)now.second,
|
||||
lowV ? " !LOWV" : "");
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
static String gpsModuleToString(GpsModuleKind kind) {
|
||||
if (kind == GpsModuleKind::L76K) {
|
||||
return "L76K";
|
||||
}
|
||||
if (kind == GpsModuleKind::QUECTEL_TODO) {
|
||||
return "Quectel/TODO";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
static bool parseUInt2(const char* s, uint8_t& out) {
|
||||
if (!s || !isdigit((unsigned char)s[0]) || !isdigit((unsigned char)s[1])) {
|
||||
return false;
|
||||
}
|
||||
out = (uint8_t)((s[0] - '0') * 10 + (s[1] - '0'));
|
||||
return true;
|
||||
}
|
||||
|
||||
static void detectModuleFromText(const char* text) {
|
||||
if (!text || text[0] == '\0') {
|
||||
return;
|
||||
}
|
||||
|
||||
String t(text);
|
||||
t.toUpperCase();
|
||||
|
||||
if (t.indexOf("L76K") >= 0) {
|
||||
g_gps.module = GpsModuleKind::L76K;
|
||||
return;
|
||||
}
|
||||
|
||||
if (t.indexOf("QUECTEL") >= 0 || t.indexOf("2021-10-20") >= 0 || t.indexOf("2021/10/20") >= 0) {
|
||||
if (g_gps.module != GpsModuleKind::L76K) {
|
||||
g_gps.module = GpsModuleKind::QUECTEL_TODO;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void parseGga(char* fields[], int count) {
|
||||
if (count <= 7) {
|
||||
return;
|
||||
}
|
||||
int sats = atoi(fields[7]);
|
||||
if (sats >= 0 && sats <= 255) {
|
||||
g_gps.satsUsed = (uint8_t)sats;
|
||||
}
|
||||
}
|
||||
|
||||
static void parseGsv(char* fields[], int count) {
|
||||
if (count <= 3) {
|
||||
return;
|
||||
}
|
||||
int sats = atoi(fields[3]);
|
||||
if (sats >= 0 && sats <= 255) {
|
||||
g_gps.satsInView = (uint8_t)sats;
|
||||
}
|
||||
}
|
||||
|
||||
static void parseRmc(char* fields[], int count) {
|
||||
if (count <= 9) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char* utc = fields[1];
|
||||
const char* status = fields[2];
|
||||
const char* date = fields[9];
|
||||
|
||||
if (!status || status[0] != 'A') {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!utc || strlen(utc) < 6 || !date || strlen(date) < 6) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t hh = 0, mm = 0, ss = 0;
|
||||
uint8_t dd = 0, mo = 0, yy = 0;
|
||||
if (!parseUInt2(utc + 0, hh) || !parseUInt2(utc + 2, mm) || !parseUInt2(utc + 4, ss)) {
|
||||
return;
|
||||
}
|
||||
if (!parseUInt2(date + 0, dd) || !parseUInt2(date + 2, mo) || !parseUInt2(date + 4, yy)) {
|
||||
return;
|
||||
}
|
||||
|
||||
g_gps.utcHour = hh;
|
||||
g_gps.utcMinute = mm;
|
||||
g_gps.utcSecond = ss;
|
||||
g_gps.utcDay = dd;
|
||||
g_gps.utcMonth = mo;
|
||||
g_gps.utcYear = (uint16_t)(2000U + yy);
|
||||
g_gps.hasValidUtc = true;
|
||||
}
|
||||
|
||||
static void parseTxt(char* fields[], int count) {
|
||||
if (count <= 4) {
|
||||
return;
|
||||
}
|
||||
detectModuleFromText(fields[4]);
|
||||
}
|
||||
|
||||
static void processNmeaLine(char* line) {
|
||||
if (!line || line[0] != '$') {
|
||||
return;
|
||||
}
|
||||
g_gps.sawAnySentence = true;
|
||||
|
||||
char* star = strchr(line, '*');
|
||||
if (star) {
|
||||
*star = '\0';
|
||||
}
|
||||
|
||||
char* fields[24] = {0};
|
||||
int count = 0;
|
||||
char* saveptr = nullptr;
|
||||
char* tok = strtok_r(line, ",", &saveptr);
|
||||
while (tok && count < 24) {
|
||||
fields[count++] = tok;
|
||||
tok = strtok_r(nullptr, ",", &saveptr);
|
||||
}
|
||||
if (count <= 0 || !fields[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
const char* header = fields[0];
|
||||
size_t n = strlen(header);
|
||||
if (n < 6) {
|
||||
return;
|
||||
}
|
||||
const char* type = header + (n - 3);
|
||||
|
||||
if (strcmp(type, "GGA") == 0) {
|
||||
parseGga(fields, count);
|
||||
} else if (strcmp(type, "GSV") == 0) {
|
||||
parseGsv(fields, count);
|
||||
} else if (strcmp(type, "RMC") == 0) {
|
||||
parseRmc(fields, count);
|
||||
} else if (strcmp(type, "TXT") == 0) {
|
||||
parseTxt(fields, count);
|
||||
}
|
||||
}
|
||||
|
||||
static void pollGpsSerial() {
|
||||
while (g_gpsSerial.available() > 0) {
|
||||
char c = (char)g_gpsSerial.read();
|
||||
if (c == '\r') {
|
||||
continue;
|
||||
}
|
||||
if (c == '\n') {
|
||||
if (g_gpsLineLen > 0) {
|
||||
g_gpsLine[g_gpsLineLen] = '\0';
|
||||
processNmeaLine(g_gpsLine);
|
||||
g_gpsLineLen = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (g_gpsLineLen + 1 < sizeof(g_gpsLine)) {
|
||||
g_gpsLine[g_gpsLineLen++] = c;
|
||||
} else {
|
||||
g_gpsLineLen = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t bestSatelliteCount() {
|
||||
return (g_gps.satsUsed > g_gps.satsInView) ? g_gps.satsUsed : g_gps.satsInView;
|
||||
}
|
||||
|
||||
static bool isUnsupportedQuectelMode() {
|
||||
return g_gps.module == GpsModuleKind::QUECTEL_TODO;
|
||||
}
|
||||
|
||||
static void reportStatusToSerial() {
|
||||
uint8_t sats = bestSatelliteCount();
|
||||
logf("GPS module: %s", gpsModuleToString(g_gps.module).c_str());
|
||||
logf("GPS sentences seen: %s", g_gps.sawAnySentence ? "yes" : "no");
|
||||
logf("GPS satellites: used=%u in-view=%u best=%u",
|
||||
(unsigned)g_gps.satsUsed,
|
||||
(unsigned)g_gps.satsInView,
|
||||
(unsigned)sats);
|
||||
logf("GPS can provide time from satellites: %s", g_gps.hasValidUtc ? "YES" : "NO");
|
||||
}
|
||||
|
||||
static void maybeAnnounceGpsTransitions() {
|
||||
if (isUnsupportedQuectelMode()) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t sats = bestSatelliteCount();
|
||||
bool hasSats = sats > 0;
|
||||
bool hasUtc = g_gps.hasValidUtc;
|
||||
|
||||
if (!g_satellitesAcquiredAnnounced && !g_prevHadSatellites && hasSats) {
|
||||
String rtc = formatRtcNow();
|
||||
char l2[28];
|
||||
snprintf(l2, sizeof(l2), "Satellites: %u", (unsigned)sats);
|
||||
oledShowLines("GPS acquired", l2, "Satellite lock found", "Waiting for UTC...", rtc.c_str());
|
||||
logf("Transition: satellites acquired (%u)", (unsigned)sats);
|
||||
g_satellitesAcquiredAnnounced = true;
|
||||
}
|
||||
|
||||
if (!g_timeAcquiredAnnounced && !g_prevHadValidUtc && hasUtc) {
|
||||
char line2[40];
|
||||
char line3[28];
|
||||
snprintf(line2,
|
||||
sizeof(line2),
|
||||
"%04u-%02u-%02u %02u:%02u:%02u",
|
||||
(unsigned)g_gps.utcYear,
|
||||
(unsigned)g_gps.utcMonth,
|
||||
(unsigned)g_gps.utcDay,
|
||||
(unsigned)g_gps.utcHour,
|
||||
(unsigned)g_gps.utcMinute,
|
||||
(unsigned)g_gps.utcSecond);
|
||||
snprintf(line3, sizeof(line3), "Satellites: %u", (unsigned)sats);
|
||||
oledShowLines("GPS UTC acquired", line2, line3, "Source: L76K");
|
||||
logf("Transition: GPS UTC acquired: %s", line2);
|
||||
g_timeAcquiredAnnounced = true;
|
||||
}
|
||||
|
||||
g_prevHadSatellites = hasSats;
|
||||
g_prevHadValidUtc = hasUtc;
|
||||
}
|
||||
|
||||
static void drawMinuteStatus() {
|
||||
if (isUnsupportedQuectelMode()) {
|
||||
oledShowLines("GPS module mismatch", "Quectel detected", "L76K required", "TODO: implement", "Quectel support");
|
||||
logf("GPS module mismatch: Quectel detected but this exercise currently supports only L76K (TODO)");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t sats = bestSatelliteCount();
|
||||
if (g_gps.hasValidUtc) {
|
||||
char line2[40];
|
||||
char line3[28];
|
||||
snprintf(line2,
|
||||
sizeof(line2),
|
||||
"%04u-%02u-%02u %02u:%02u:%02u",
|
||||
(unsigned)g_gps.utcYear,
|
||||
(unsigned)g_gps.utcMonth,
|
||||
(unsigned)g_gps.utcDay,
|
||||
(unsigned)g_gps.utcHour,
|
||||
(unsigned)g_gps.utcMinute,
|
||||
(unsigned)g_gps.utcSecond);
|
||||
snprintf(line3, sizeof(line3), "Satellites: %u", (unsigned)sats);
|
||||
oledShowLines("GPS time (UTC)", line2, line3, "Source: L76K");
|
||||
logf("GPS time (UTC): %s satellites=%u", line2, (unsigned)sats);
|
||||
return;
|
||||
}
|
||||
|
||||
String rtc = formatRtcNow();
|
||||
if (sats > 0) {
|
||||
char line2[28];
|
||||
snprintf(line2, sizeof(line2), "Satellites: %u", (unsigned)sats);
|
||||
oledShowLines("GPS signal detected", line2, "GPS UTC not ready", "yet, using RTC", rtc.c_str());
|
||||
logf("Satellites detected (%u) but GPS UTC not ready. %s", (unsigned)sats, rtc.c_str());
|
||||
} else {
|
||||
oledShowLines("Unable to acquire", "satellites", "Take me outside so I", "can see satellites", rtc.c_str());
|
||||
logf("Unable to acquire satellites. %s", rtc.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(kSerialDelayMs);
|
||||
|
||||
Serial.println("\r\n==================================================");
|
||||
Serial.println("Exercise 09: GPS Time");
|
||||
Serial.println("==================================================");
|
||||
|
||||
if (!tbeam_supreme::initPmuForPeripherals(g_pmu, &Serial)) {
|
||||
logf("PMU init failed");
|
||||
}
|
||||
|
||||
Wire.begin(OLED_SDA, OLED_SCL);
|
||||
g_oled.setI2CAddress(OLED_ADDR << 1);
|
||||
g_oled.begin();
|
||||
oledShowLines("GPS Time exercise", "Booting...");
|
||||
|
||||
SdWatcherConfig sdCfg{};
|
||||
if (!g_sd.begin(sdCfg, nullptr)) {
|
||||
logf("SD startup manager begin() failed");
|
||||
}
|
||||
|
||||
#ifdef GPS_WAKEUP_PIN
|
||||
pinMode(GPS_WAKEUP_PIN, OUTPUT);
|
||||
digitalWrite(GPS_WAKEUP_PIN, HIGH);
|
||||
#endif
|
||||
|
||||
#ifdef GPS_1PPS_PIN
|
||||
pinMode(GPS_1PPS_PIN, INPUT);
|
||||
#endif
|
||||
|
||||
#ifdef GPS_RX_PIN
|
||||
#ifdef GPS_TX_PIN
|
||||
g_gpsSerial.begin(GPS_BAUD, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
|
||||
#else
|
||||
g_gpsSerial.begin(GPS_BAUD);
|
||||
#endif
|
||||
#else
|
||||
g_gpsSerial.begin(GPS_BAUD);
|
||||
#endif
|
||||
|
||||
logf("GPS UART started at %u baud", (unsigned)GPS_BAUD);
|
||||
|
||||
oledShowLines("GPS startup probe", "Checking satellites", "and GPS time...");
|
||||
|
||||
uint32_t probeStart = millis();
|
||||
uint32_t lastProbeUiMs = 0;
|
||||
while ((uint32_t)(millis() - probeStart) < kGpsStartupProbeMs) {
|
||||
pollGpsSerial();
|
||||
g_sd.update();
|
||||
|
||||
uint32_t now = millis();
|
||||
if ((uint32_t)(now - lastProbeUiMs) >= 1000) {
|
||||
lastProbeUiMs = now;
|
||||
char l3[28];
|
||||
char l4[30];
|
||||
snprintf(l3, sizeof(l3), "Sats: %u", (unsigned)bestSatelliteCount());
|
||||
snprintf(l4, sizeof(l4), "Module: %s", gpsModuleToString(g_gps.module).c_str());
|
||||
oledShowLines("GPS startup probe", "Checking satellites", l3, l4);
|
||||
}
|
||||
delay(10);
|
||||
}
|
||||
|
||||
reportStatusToSerial();
|
||||
g_prevHadSatellites = (bestSatelliteCount() > 0);
|
||||
g_prevHadValidUtc = g_gps.hasValidUtc;
|
||||
drawMinuteStatus();
|
||||
g_lastMinuteReportMs = millis();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
pollGpsSerial();
|
||||
g_sd.update();
|
||||
maybeAnnounceGpsTransitions();
|
||||
|
||||
uint32_t now = millis();
|
||||
if ((uint32_t)(now - g_lastMinuteReportMs) >= kMinuteMs) {
|
||||
g_lastMinuteReportMs = now;
|
||||
drawMinuteStatus();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue