From ba6160d004be07d2ef031834e5ed9110ec160b61 Mon Sep 17 00:00:00 2001 From: John Poole Date: Fri, 17 Apr 2026 09:54:46 -0700 Subject: [PATCH] Not fully working, OLED displays, SD card mounts, web not working? --- .../lib/startup_sd/StartupSdManager.cpp | 361 +++++++++ .../lib/startup_sd/StartupSdManager.h | 92 +++ exercises/22_compass/platformio.ini | 1 + exercises/22_compass/src/main.cpp | 759 ++++++++++++++++++ 4 files changed, 1213 insertions(+) create mode 100644 exercises/22_compass/lib/startup_sd/StartupSdManager.cpp create mode 100644 exercises/22_compass/lib/startup_sd/StartupSdManager.h create mode 100644 exercises/22_compass/src/main.cpp diff --git a/exercises/22_compass/lib/startup_sd/StartupSdManager.cpp b/exercises/22_compass/lib/startup_sd/StartupSdManager.cpp new file mode 100644 index 0000000..085763d --- /dev/null +++ b/exercises/22_compass/lib/startup_sd/StartupSdManager.cpp @@ -0,0 +1,361 @@ +#include "StartupSdManager.h" + +#include +#include "driver/gpio.h" + +StartupSdManager::StartupSdManager(Print& serial) : serial_(serial) {} + +bool StartupSdManager::begin(const SdWatcherConfig& cfg, SdStatusCallback callback) { + cfg_ = cfg; + callback_ = callback; + + forceSpiDeselected(); + dumpSdPins("very-early"); + + if (!initPmuForSdPower()) { + return false; + } + + cycleSdRail(); + delay(cfg_.startupWarmupMs); + + bool warmMounted = false; + for (uint8_t i = 0; i < 3; ++i) { + if (mountPreferred(false)) { + warmMounted = true; + break; + } + delay(200); + } + + if (!warmMounted) { + logf("Watcher: startup preferred mount failed, retrying with extended settle"); + cycleSdRail(400, 1200); + delay(cfg_.startupWarmupMs + 1500); + warmMounted = mountCardFullScan(); + } + + if (warmMounted) { + setStateMounted(); + } else { + setStateAbsent(); + } + return true; +} + +void StartupSdManager::update() { + const uint32_t now = millis(); + const uint32_t pollInterval = + (watchState_ == SdWatchState::MOUNTED) ? cfg_.pollIntervalMountedMs : cfg_.pollIntervalAbsentMs; + + if ((uint32_t)(now - lastPollMs_) < pollInterval) { + return; + } + lastPollMs_ = now; + + if (watchState_ == SdWatchState::MOUNTED) { + if (verifyMountedCard()) { + presentVotes_ = 0; + absentVotes_ = 0; + return; + } + + if (mountPreferred(false) && verifyMountedCard()) { + presentVotes_ = 0; + absentVotes_ = 0; + return; + } + + absentVotes_++; + presentVotes_ = 0; + if (absentVotes_ >= cfg_.votesToAbsent) { + setStateAbsent(); + absentVotes_ = 0; + } + return; + } + + bool mounted = mountPreferred(false); + if (!mounted && (uint32_t)(now - lastFullScanMs_) >= cfg_.fullScanIntervalMs) { + lastFullScanMs_ = now; + if (cfg_.recoveryRailCycleOnFullScan) { + logf("Watcher: recovery rail cycle before full scan"); + cycleSdRail(cfg_.recoveryRailOffMs, cfg_.recoveryRailOnSettleMs); + delay(150); + } + logf("Watcher: preferred probe failed, running full scan"); + mounted = mountCardFullScan(); + } + + if (mounted) { + presentVotes_++; + absentVotes_ = 0; + if (presentVotes_ >= cfg_.votesToPresent) { + setStateMounted(); + presentVotes_ = 0; + } + } else { + absentVotes_++; + presentVotes_ = 0; + if (absentVotes_ >= cfg_.votesToAbsent) { + setStateAbsent(); + absentVotes_ = 0; + } + } +} + +bool StartupSdManager::consumeMountedEvent() { + bool out = mountedEventPending_; + mountedEventPending_ = false; + return out; +} + +bool StartupSdManager::consumeRemovedEvent() { + bool out = removedEventPending_; + removedEventPending_ = false; + return out; +} + +bool StartupSdManager::forceRemount() { + logf("Watcher: manual rescan requested"); + presentVotes_ = 0; + absentVotes_ = 0; + lastPollMs_ = 0; + lastFullScanMs_ = millis(); + + cycleSdRail(cfg_.recoveryRailOffMs, cfg_.recoveryRailOnSettleMs); + delay(cfg_.startupWarmupMs); + + if (mountCardFullScan()) { + setStateMounted(); + return true; + } + + setStateAbsent(); + return false; +} + +void StartupSdManager::logf(const char* fmt, ...) { + char msg[196]; + 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)logSeq_++, + msg); +} + +void StartupSdManager::notify(SdEvent event, const char* message) { + if (callback_ != nullptr) { + callback_(event, message); + } +} + +void StartupSdManager::forceSpiDeselected() { + pinMode(tbeam_supreme::sdCs(), OUTPUT); + digitalWrite(tbeam_supreme::sdCs(), HIGH); + pinMode(tbeam_supreme::imuCs(), OUTPUT); + digitalWrite(tbeam_supreme::imuCs(), HIGH); +} + +void StartupSdManager::dumpSdPins(const char* tag) { + if (!cfg_.enablePinDumps) { + (void)tag; + return; + } + + const gpio_num_t cs = (gpio_num_t)tbeam_supreme::sdCs(); + const gpio_num_t sck = (gpio_num_t)tbeam_supreme::sdSck(); + const gpio_num_t miso = (gpio_num_t)tbeam_supreme::sdMiso(); + const gpio_num_t mosi = (gpio_num_t)tbeam_supreme::sdMosi(); + logf("PINS(%s): CS=%d SCK=%d MISO=%d MOSI=%d", + tag, gpio_get_level(cs), gpio_get_level(sck), gpio_get_level(miso), gpio_get_level(mosi)); +} + +bool StartupSdManager::initPmuForSdPower() { + if (!tbeam_supreme::initPmuForPeripherals(pmu_, &serial_)) { + logf("ERROR: PMU init failed"); + return false; + } + return true; +} + +void StartupSdManager::cycleSdRail(uint32_t offMs, uint32_t onSettleMs) { + if (!cfg_.enableSdRailCycle) { + return; + } + if (!pmu_) { + logf("SD rail cycle skipped: pmu=null"); + return; + } + + forceSpiDeselected(); + pmu_->disablePowerOutput(XPOWERS_BLDO1); + delay(offMs); + pmu_->setPowerChannelVoltage(XPOWERS_BLDO1, 3300); + pmu_->enablePowerOutput(XPOWERS_BLDO1); + delay(onSettleMs); +} + +bool StartupSdManager::tryMountWithBus(SPIClass& bus, const char* busName, uint32_t hz, bool verbose) { + SD.end(); + bus.end(); + delay(10); + forceSpiDeselected(); + + bus.begin(tbeam_supreme::sdSck(), tbeam_supreme::sdMiso(), tbeam_supreme::sdMosi(), tbeam_supreme::sdCs()); + digitalWrite(tbeam_supreme::sdCs(), HIGH); + delay(2); + for (int i = 0; i < 10; i++) { + bus.transfer(0xFF); + } + delay(2); + + if (verbose) { + logf("SD: trying bus=%s freq=%lu Hz", busName, (unsigned long)hz); + } + + if (!SD.begin(tbeam_supreme::sdCs(), bus, hz)) { + if (verbose) { + logf("SD: mount failed (possible non-FAT format, power, or bus issue)"); + } + return false; + } + + if (SD.cardType() == CARD_NONE) { + SD.end(); + return false; + } + + sdSpi_ = &bus; + sdBusName_ = busName; + sdFreq_ = hz; + return true; +} + +bool StartupSdManager::mountPreferred(bool verbose) { + return tryMountWithBus(sdSpiH_, "HSPI", 400000, verbose); +} + +bool StartupSdManager::mountCardFullScan() { + const uint32_t freqs[] = {400000, 1000000, 4000000, 10000000}; + + for (uint8_t i = 0; i < (sizeof(freqs) / sizeof(freqs[0])); ++i) { + if (tryMountWithBus(sdSpiH_, "HSPI", freqs[i], true)) { + logf("SD: card detected and mounted"); + return true; + } + } + for (uint8_t i = 0; i < (sizeof(freqs) / sizeof(freqs[0])); ++i) { + if (tryMountWithBus(sdSpiF_, "FSPI", freqs[i], true)) { + logf("SD: card detected and mounted"); + return true; + } + } + + logf("SD: begin() failed on all bus/frequency attempts"); + return false; +} + +bool StartupSdManager::verifyMountedCard() { + File root = SD.open("/", FILE_READ); + if (!root) { + return false; + } + root.close(); + return true; +} + +const char* StartupSdManager::cardTypeToString(uint8_t type) { + switch (type) { + case CARD_MMC: return "MMC"; + case CARD_SD: return "SDSC"; + case CARD_SDHC: return "SDHC/SDXC"; + default: return "UNKNOWN"; + } +} + +void StartupSdManager::setStateMounted() { + if (watchState_ != SdWatchState::MOUNTED) { + mountedEventPending_ = true; + removedEventPending_ = false; + notify(SdEvent::CARD_MOUNTED, "mounted"); + } + watchState_ = SdWatchState::MOUNTED; +} + +void StartupSdManager::setStateAbsent() { + if (watchState_ != SdWatchState::ABSENT) { + removedEventPending_ = true; + mountedEventPending_ = false; + notify(SdEvent::CARD_REMOVED, "removed"); + } + watchState_ = SdWatchState::ABSENT; +} + +void StartupSdManager::printCardInfo() { + if (!isMounted()) { + logf("SD: no mounted card"); + return; + } + + logf("SD: bus=%s freq=%lu type=%s sizeMB=%llu usedMB=%llu", + sdBusName_, + (unsigned long)sdFreq_, + cardTypeToString(SD.cardType()), + (unsigned long long)(SD.cardSize() / (1024ULL * 1024ULL)), + (unsigned long long)(SD.usedBytes() / (1024ULL * 1024ULL))); +} + +bool StartupSdManager::ensureDirRecursive(const char* path) { + if (!path || path[0] != '/') { + logf("DIR: invalid path"); + return false; + } + + String full(path); + int start = 1; + while (start > 0 && start < (int)full.length()) { + const int slash = full.indexOf('/', start); + String partial = (slash < 0) ? full : full.substring(0, slash); + if (!SD.exists(partial.c_str()) && !SD.mkdir(partial.c_str())) { + logf("DIR: mkdir failed for %s", partial.c_str()); + return false; + } + if (slash < 0) { + break; + } + start = slash + 1; + } + return true; +} + +bool StartupSdManager::rewriteFile(const char* path, const char* payload) { + if (!path || path[0] != '/') { + return false; + } + + String dir(path); + const int slash = dir.lastIndexOf('/'); + if (slash > 0) { + dir.remove(slash); + if (!ensureDirRecursive(dir.c_str())) { + return false; + } + } + + File file = SD.open(path, FILE_WRITE); + if (!file) { + return false; + } + file.print(payload ? payload : ""); + file.flush(); + file.close(); + return true; +} + +void StartupSdManager::permissionsDemo(const char* path) { + (void)path; +} diff --git a/exercises/22_compass/lib/startup_sd/StartupSdManager.h b/exercises/22_compass/lib/startup_sd/StartupSdManager.h new file mode 100644 index 0000000..b23870e --- /dev/null +++ b/exercises/22_compass/lib/startup_sd/StartupSdManager.h @@ -0,0 +1,92 @@ +#pragma once + +#include +#include +#include +#include +#include +#include "tbeam_supreme_adapter.h" + +enum class SdWatchState : uint8_t { + UNKNOWN = 0, + ABSENT, + MOUNTED +}; + +enum class SdEvent : uint8_t { + NO_CARD, + CARD_MOUNTED, + CARD_REMOVED +}; + +using SdStatusCallback = void (*)(SdEvent event, const char* message); + +struct SdWatcherConfig { + bool enableSdRailCycle = true; + bool enablePinDumps = true; + bool recoveryRailCycleOnFullScan = true; + uint32_t recoveryRailOffMs = 250; + uint32_t recoveryRailOnSettleMs = 700; + uint32_t startupWarmupMs = 1500; + uint32_t pollIntervalAbsentMs = 1000; + uint32_t pollIntervalMountedMs = 2000; + uint32_t fullScanIntervalMs = 10000; + uint8_t votesToPresent = 2; + uint8_t votesToAbsent = 5; +}; + +class StartupSdManager { + public: + explicit StartupSdManager(Print& serial = Serial); + + bool begin(const SdWatcherConfig& cfg, SdStatusCallback callback = nullptr); + void update(); + + bool isMounted() const { return watchState_ == SdWatchState::MOUNTED; } + SdWatchState state() const { return watchState_; } + + bool consumeMountedEvent(); + bool consumeRemovedEvent(); + bool forceRemount(); + + void printCardInfo(); + bool ensureDirRecursive(const char* path); + bool rewriteFile(const char* path, const char* payload); + void permissionsDemo(const char* path); + + private: + void logf(const char* fmt, ...); + void notify(SdEvent event, const char* message); + void forceSpiDeselected(); + void dumpSdPins(const char* tag); + bool initPmuForSdPower(); + void cycleSdRail(uint32_t offMs = 250, uint32_t onSettleMs = 600); + bool tryMountWithBus(SPIClass& bus, const char* busName, uint32_t hz, bool verbose); + bool mountPreferred(bool verbose); + bool mountCardFullScan(); + bool verifyMountedCard(); + const char* cardTypeToString(uint8_t type); + void setStateMounted(); + void setStateAbsent(); + + Print& serial_; + SdWatcherConfig cfg_{}; + SdStatusCallback callback_ = nullptr; + + SPIClass sdSpiH_{HSPI}; + SPIClass sdSpiF_{FSPI}; + SPIClass* sdSpi_ = nullptr; + const char* sdBusName_ = "none"; + uint32_t sdFreq_ = 0; + XPowersLibInterface* pmu_ = nullptr; + + SdWatchState watchState_ = SdWatchState::UNKNOWN; + uint8_t presentVotes_ = 0; + uint8_t absentVotes_ = 0; + uint32_t lastPollMs_ = 0; + uint32_t lastFullScanMs_ = 0; + uint32_t logSeq_ = 0; + + bool mountedEventPending_ = false; + bool removedEventPending_ = false; +}; diff --git a/exercises/22_compass/platformio.ini b/exercises/22_compass/platformio.ini index 6dad222..f5135f9 100644 --- a/exercises/22_compass/platformio.ini +++ b/exercises/22_compass/platformio.ini @@ -19,6 +19,7 @@ lib_deps = build_flags = -I ../../shared/boards -I ../../external/microReticulum_Firmware + -I ../../../../LilyGo-LoRa-Series/lib/SensorLib/src -D BOARD_MODEL=BOARD_TBEAM_S_V1 -D OLED_SDA=17 -D OLED_SCL=18 diff --git a/exercises/22_compass/src/main.cpp b/exercises/22_compass/src/main.cpp new file mode 100644 index 0000000..e694f0b --- /dev/null +++ b/exercises/22_compass/src/main.cpp @@ -0,0 +1,759 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "SensorQMC6310.hpp" +#include "StartupSdManager.h" +#include "tbeam_supreme_adapter.h" + +namespace { + +#ifndef BOARD_ID +#define BOARD_ID "CY" +#endif + +#ifndef NODE_LABEL +#define NODE_LABEL "Cy" +#endif + +#ifndef FW_BUILD_UTC +#define FW_BUILD_UTC unknown +#endif + +#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 LOG_AP_IP_OCTET +#define LOG_AP_IP_OCTET 25 +#endif + +#ifndef MAG_DECLINATION_DEG +#define MAG_DECLINATION_DEG 0.0f +#endif + +#define STR_INNER(x) #x +#define STR(x) STR_INNER(x) + +static constexpr const char* kBoardId = BOARD_ID; +static constexpr const char* kNodeLabel = NODE_LABEL; +static constexpr const char* kBuild = STR(FW_BUILD_UTC); +static constexpr const char* kExerciseName = "Exercise 22"; +static constexpr uint32_t kSampleIntervalMs = 200; +static constexpr uint32_t kDisplayIntervalMs = 200; +static constexpr uint32_t kUiSplashMs = 1400; +static constexpr uint8_t kRtcAddress = 0x51; +static constexpr uint8_t kMagCandidateCount = 3; +static constexpr uint8_t kMagCandidates[kMagCandidateCount] = {0x1C, 0x3C, 0x0D}; +static constexpr float kDeclinationDeg = MAG_DECLINATION_DEG; +static constexpr float kDegPerRad = 57.29577951308232f; +static constexpr char kApPassword[] = "microreticulum"; + +struct ClockDateTime { + uint16_t year = 0; + uint8_t month = 0; + uint8_t day = 0; + uint8_t hour = 0; + uint8_t minute = 0; + uint8_t second = 0; +}; + +struct MagSample { + bool valid = false; + uint32_t seq = 0; + uint32_t millisSinceBoot = 0; + time_t epoch = 0; + int16_t rawX = 0; + int16_t rawY = 0; + int16_t rawZ = 0; + float x_uT = 0.0f; + float y_uT = 0.0f; + float z_uT = 0.0f; + float field_uT = 0.0f; + float headingMagDeg = 0.0f; + float headingTrueDeg = 0.0f; +}; + +XPowersLibInterface* g_pmu = nullptr; +StartupSdManager g_sd(Serial); +U8G2_SH1106_128X64_NONAME_F_HW_I2C g_oled(U8G2_R0, U8X8_PIN_NONE); +SensorQMC6310 g_qmc; +WebServer g_server(80); + +ClockDateTime g_rtcUtc{}; +MagSample g_lastSample{}; + +bool g_displayReady = false; +bool g_sdMounted = false; +bool g_logOpen = false; +bool g_magReady = false; +bool g_timeValid = false; +bool g_webReady = false; + +uint8_t g_magAddress = 0; +uint8_t g_magChipId = 0; +char g_magLabel[16] = "UNKNOWN"; +char g_logPath[96] = {0}; +char g_apSsid[32] = {0}; +File g_logFile; + +uint32_t g_lastSampleMs = 0; +uint32_t g_lastDisplayMs = 0; +uint32_t g_lastHeartbeatMs = 0; + +uint8_t toBcd(uint8_t value) { + return (uint8_t)(((value / 10U) << 4U) | (value % 10U)); +} + +uint8_t fromBcd(uint8_t value) { + return (uint8_t)(((value >> 4U) * 10U) + (value & 0x0FU)); +} + +bool isLeapYear(uint16_t year) { + return ((year % 4U) == 0U && (year % 100U) != 0U) || ((year % 400U) == 0U); +} + +uint8_t daysInMonth(uint16_t year, uint8_t month) { + static const uint8_t kDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; + if (month == 2U) { + return (uint8_t)(isLeapYear(year) ? 29U : 28U); + } + if (month >= 1U && month <= 12U) { + return kDays[month - 1U]; + } + return 0; +} + +bool isValidDateTime(const ClockDateTime& dt) { + if (dt.year < 2000U || dt.year > 2099U) return false; + if (dt.month < 1U || dt.month > 12U) return false; + if (dt.day < 1U || dt.day > daysInMonth(dt.year, dt.month)) return false; + if (dt.hour > 23U || dt.minute > 59U || dt.second > 59U) return false; + return true; +} + +int64_t daysFromCivil(int year, unsigned month, unsigned day) { + year -= (month <= 2U); + const int era = (year >= 0 ? year : year - 399) / 400; + const unsigned yoe = (unsigned)(year - era * 400); + const unsigned doy = (153U * (month + (month > 2U ? (unsigned)-3 : 9U)) + 2U) / 5U + day - 1U; + const unsigned doe = yoe * 365U + yoe / 4U - yoe / 100U + doy; + return era * 146097 + (int)doe - 719468; +} + +time_t toEpochSeconds(const ClockDateTime& dt) { + const int64_t days = daysFromCivil((int)dt.year, dt.month, dt.day); + return (time_t)(days * 86400LL + (int64_t)dt.hour * 3600LL + (int64_t)dt.minute * 60LL + (int64_t)dt.second); +} + +bool readRtc(ClockDateTime& out, bool& lowVoltageFlag) { + Wire1.beginTransmission(kRtcAddress); + Wire1.write(0x02); + if (Wire1.endTransmission(false) != 0) { + return false; + } + + const uint8_t need = 7; + const uint8_t got = Wire1.requestFrom((int)kRtcAddress, (int)need); + if (got != need) { + return false; + } + + const uint8_t sec = Wire1.read(); + const uint8_t min = Wire1.read(); + const uint8_t hour = Wire1.read(); + const uint8_t day = Wire1.read(); + (void)Wire1.read(); + const uint8_t month = Wire1.read(); + const 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); + out.year = (month & 0x80U) ? (1900U + fromBcd(year)) : (2000U + fromBcd(year)); + return true; +} + +bool writeRtc(const ClockDateTime& dt) { + if (!isValidDateTime(dt)) { + return false; + } + + Wire1.beginTransmission(kRtcAddress); + Wire1.write(0x02); + Wire1.write(toBcd(dt.second & 0x7FU)); + Wire1.write(toBcd(dt.minute)); + Wire1.write(toBcd(dt.hour)); + Wire1.write(toBcd(dt.day)); + Wire1.write(0x00); + uint8_t monthReg = toBcd(dt.month); + if (dt.year < 2000U) { + monthReg |= 0x80U; + } + Wire1.write(monthReg); + Wire1.write(toBcd((uint8_t)(dt.year % 100U))); + return Wire1.endTransmission() == 0; +} + +void setSystemTimeFromRtc(const ClockDateTime& dt) { + const time_t epoch = toEpochSeconds(dt); + const timeval tv = {.tv_sec = epoch, .tv_usec = 0}; + settimeofday(&tv, nullptr); +} + +void formatCompactUtc(const ClockDateTime& dt, char* out, size_t outSize) { + snprintf(out, + outSize, + "%04u%02u%02u_%02u%02u%02u", + (unsigned)dt.year, + (unsigned)dt.month, + (unsigned)dt.day, + (unsigned)dt.hour, + (unsigned)dt.minute, + (unsigned)dt.second); +} + +void drawLines(const char* l1, + const char* l2 = nullptr, + const char* l3 = nullptr, + const char* l4 = nullptr, + const char* l5 = nullptr) { + if (!g_displayReady) { + return; + } + g_oled.clearBuffer(); + g_oled.setFont(u8g2_font_6x12_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(); +} + +void initDisplay() { + Wire.begin(OLED_SDA, OLED_SCL); + Wire.setClock(400000); + g_oled.setI2CAddress(OLED_ADDR << 1); + g_oled.setBusClock(400000); + g_oled.begin(); + g_oled.setPowerSave(0); + g_displayReady = true; + drawLines("Exercise 22", "Magnetometer", kBoardId, "starting..."); +} + +String htmlEscape(const String& in) { + String out; + out.reserve(in.length() + 16); + for (size_t i = 0; i < in.length(); ++i) { + const char c = in[i]; + if (c == '&') out += "&"; + else if (c == '<') out += "<"; + else if (c == '>') out += ">"; + else if (c == '"') out += """; + else out += c; + } + return out; +} + +String urlEncode(const String& in) { + String out; + char hex[4]; + for (size_t i = 0; i < in.length(); ++i) { + const unsigned char c = (unsigned char)in[i]; + if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '/' || c == '~') { + out += (char)c; + } else { + snprintf(hex, sizeof(hex), "%%%02X", c); + out += hex; + } + } + return out; +} + +void handleWebIndex() { + String html; + html.reserve(4096); + html += "Exercise 22"; + html += "

Exercise 22 Magnetometer

"; + html += "

Board: "; + html += htmlEscape(String(kBoardId)); + html += " Build: "; + html += htmlEscape(String(kBuild)); + html += "

"; + html += "

Mag: "; + html += htmlEscape(String(g_magLabel)); + html += " addr=0x"; + char hex[8]; + snprintf(hex, sizeof(hex), "%02X", g_magAddress); + html += hex; + html += " chip=0x"; + snprintf(hex, sizeof(hex), "%02X", g_magChipId); + html += hex; + html += "

"; + html += "

Declination: "; + html += String(kDeclinationDeg, 2); + html += " deg

"; + html += "

Log: "; + html += htmlEscape(String(g_logOpen ? g_logPath : "(not open)")); + html += "

"; + if (g_logOpen) { + html += "

Download current log

"; + } + html += "

List SD root

"; + html += ""; + g_server.send(200, "text/html", html); +} + +void handleWebFiles() { + if (!g_sdMounted) { + g_server.send(503, "text/plain", "SD not mounted\n"); + return; + } + + File dir = SD.open("/", FILE_READ); + if (!dir || !dir.isDirectory()) { + g_server.send(500, "text/plain", "Failed to open SD root\n"); + return; + } + + String body; + body.reserve(4096); + body += "

SD Files

    "; + File entry = dir.openNextFile(); + while (entry) { + body += "
  • "; + const String name = String(entry.name()); + body += htmlEscape(name); + if (!entry.isDirectory()) { + body += " download"; + } + body += "
  • "; + entry.close(); + entry = dir.openNextFile(); + } + body += "

Back

"; + dir.close(); + g_server.send(200, "text/html", body); +} + +void handleWebDownload() { + if (!g_server.hasArg("path")) { + g_server.send(400, "text/plain", "missing path\n"); + return; + } + if (!g_sdMounted) { + g_server.send(503, "text/plain", "SD not mounted\n"); + return; + } + + String path = g_server.arg("path"); + if (!path.startsWith("/")) { + path = "/" + path; + } + + File file = SD.open(path.c_str(), FILE_READ); + if (!file || file.isDirectory()) { + g_server.send(404, "text/plain", "file not found\n"); + return; + } + + g_server.sendHeader("Content-Type", "text/plain"); + g_server.sendHeader("Content-Disposition", "attachment; filename=\"" + path.substring(path.lastIndexOf('/') + 1) + "\""); + g_server.streamFile(file, "text/plain"); + file.close(); +} + +void startWebServer() { + snprintf(g_apSsid, sizeof(g_apSsid), "Compass-%s", kBoardId); + WiFi.mode(WIFI_AP); + WiFi.setSleep(false); + const IPAddress ip(192, 168, LOG_AP_IP_OCTET, 1); + const IPAddress gw(192, 168, LOG_AP_IP_OCTET, 1); + const IPAddress nm(255, 255, 255, 0); + WiFi.softAPConfig(ip, gw, nm); + + if (!WiFi.softAP(g_apSsid, kApPassword)) { + Serial.println("wifi_ap=failed"); + return; + } + + g_server.on("/", HTTP_GET, handleWebIndex); + g_server.on("/files", HTTP_GET, handleWebFiles); + g_server.on("/download", HTTP_GET, handleWebDownload); + g_server.begin(); + g_webReady = true; + + Serial.printf("wifi_ap_ssid=%s\n", g_apSsid); + Serial.printf("wifi_ap_url=http://192.168.%u.1/\n", (unsigned)LOG_AP_IP_OCTET); +} + +bool probeI2cAddr(TwoWire& wire, uint8_t addr) { + wire.beginTransmission(addr); + return wire.endTransmission() == 0; +} + +bool detectMagnetometer() { + for (uint8_t i = 0; i < kMagCandidateCount; ++i) { + const uint8_t addr = kMagCandidates[i]; + if (!probeI2cAddr(Wire1, addr)) { + continue; + } + + if (addr == 0x3C || addr == 0x3D) { + Wire1.beginTransmission(addr); + Wire1.write((uint8_t)0x00); + if (Wire1.endTransmission(false) == 0 && Wire1.requestFrom((int)addr, 1) == 1) { + const uint8_t marker = Wire1.read(); + if (marker != 0x80) { + continue; + } + } + } + + g_magAddress = addr; + if (addr == 0x1C) { + strlcpy(g_magLabel, "QMC6310U", sizeof(g_magLabel)); + } else if (addr == 0x3C) { + strlcpy(g_magLabel, "QMC6310N", sizeof(g_magLabel)); + } else if (addr == 0x0D) { + strlcpy(g_magLabel, "QMC6309?", sizeof(g_magLabel)); + } else { + strlcpy(g_magLabel, "QMC?", sizeof(g_magLabel)); + } + return true; + } + return false; +} + +bool initMagnetometer() { + if (!detectMagnetometer()) { + return false; + } + + if (!g_qmc.begin(Wire1, g_magAddress, tbeam_supreme::i2cSda(), tbeam_supreme::i2cScl())) { + return false; + } + + g_magChipId = g_qmc.getChipID(); + const int rc = g_qmc.configMagnetometer( + SensorQMC6310::MODE_CONTINUOUS, + SensorQMC6310::RANGE_8G, + SensorQMC6310::DATARATE_200HZ, + SensorQMC6310::OSR_1, + SensorQMC6310::DSR_1); + return rc == 0; +} + +bool mountSd() { + SdWatcherConfig cfg; + cfg.enablePinDumps = false; + if (!g_sd.begin(cfg, nullptr)) { + return false; + } + g_sdMounted = g_sd.isMounted(); + return g_sdMounted; +} + +bool openLogFile() { + if (!g_sdMounted) { + return false; + } + + time_t now = time(nullptr); + if (now < 946684800) { + return false; + } + + struct tm tmUtc; + gmtime_r(&now, &tmUtc); + snprintf(g_logPath, + sizeof(g_logPath), + "/%04d%02d%02d_%02d%02d%02d_magnetometer_readings.log", + tmUtc.tm_year + 1900, + tmUtc.tm_mon + 1, + tmUtc.tm_mday, + tmUtc.tm_hour, + tmUtc.tm_min, + tmUtc.tm_sec); + + g_logFile = SD.open(g_logPath, FILE_WRITE); + if (!g_logFile) { + return false; + } + + g_logFile.println("# Exercise 22 magnetometer calibration capture"); + g_logFile.printf("# board_id\t%s\n", kBoardId); + g_logFile.printf("# build\t%s\n", kBuild); + g_logFile.printf("# mag_label\t%s\n", g_magLabel); + g_logFile.printf("# mag_address\t0x%02X\n", g_magAddress); + g_logFile.printf("# mag_chip_id\t0x%02X\n", g_magChipId); + g_logFile.printf("# declination_deg\t%.2f\n", kDeclinationDeg); + g_logFile.println("# date_utc\ttime_utc\tsample_seq\tmillis_since_boot\traw_x\traw_y\traw_z\tx_uT\ty_uT\tz_uT\tfield_uT\theading_mag_deg\theading_true_deg"); + g_logFile.flush(); + g_logOpen = true; + return true; +} + +float normalizeHeadingDeg(float heading) { + while (heading < 0.0f) heading += 360.0f; + while (heading >= 360.0f) heading -= 360.0f; + return heading; +} + +bool captureSample(MagSample& sample) { + if (!g_magReady) { + return false; + } + if (!g_qmc.isDataReady()) { + return false; + } + + g_qmc.readData(); + + sample.valid = true; + sample.seq = g_lastSample.seq + 1; + sample.millisSinceBoot = millis(); + sample.epoch = time(nullptr); + sample.rawX = g_qmc.getRawX(); + sample.rawY = g_qmc.getRawY(); + sample.rawZ = g_qmc.getRawZ(); + sample.x_uT = g_qmc.getX(); + sample.y_uT = g_qmc.getY(); + sample.z_uT = g_qmc.getZ(); + sample.field_uT = sqrtf(sample.x_uT * sample.x_uT + sample.y_uT * sample.y_uT + sample.z_uT * sample.z_uT); + sample.headingMagDeg = normalizeHeadingDeg(atan2f(sample.y_uT, sample.x_uT) * kDegPerRad); + sample.headingTrueDeg = normalizeHeadingDeg(sample.headingMagDeg + kDeclinationDeg); + return true; +} + +void formatDateTimeUtc(time_t epoch, char* dateOut, size_t dateSize, char* timeOut, size_t timeSize) { + struct tm tmUtc; + gmtime_r(&epoch, &tmUtc); + snprintf(dateOut, dateSize, "%04d%02d%02d", tmUtc.tm_year + 1900, tmUtc.tm_mon + 1, tmUtc.tm_mday); + snprintf(timeOut, timeSize, "%02d%02d%02d", tmUtc.tm_hour, tmUtc.tm_min, tmUtc.tm_sec); +} + +void printSampleToSerial(const MagSample& sample) { + char dateBuf[16]; + char timeBuf[16]; + formatDateTimeUtc(sample.epoch, dateBuf, sizeof(dateBuf), timeBuf, sizeof(timeBuf)); + Serial.printf( + "%s\t%s\t%lu\t%lu\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", + dateBuf, + timeBuf, + (unsigned long)sample.seq, + (unsigned long)sample.millisSinceBoot, + (int)sample.rawX, + (int)sample.rawY, + (int)sample.rawZ, + sample.x_uT, + sample.y_uT, + sample.z_uT, + sample.field_uT, + sample.headingMagDeg, + sample.headingTrueDeg); +} + +void appendSampleToLog(const MagSample& sample) { + if (!g_logOpen) { + return; + } + char dateBuf[16]; + char timeBuf[16]; + formatDateTimeUtc(sample.epoch, dateBuf, sizeof(dateBuf), timeBuf, sizeof(timeBuf)); + g_logFile.printf( + "%s\t%s\t%lu\t%lu\t%d\t%d\t%d\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", + dateBuf, + timeBuf, + (unsigned long)sample.seq, + (unsigned long)sample.millisSinceBoot, + (int)sample.rawX, + (int)sample.rawY, + (int)sample.rawZ, + sample.x_uT, + sample.y_uT, + sample.z_uT, + sample.field_uT, + sample.headingMagDeg, + sample.headingTrueDeg); + g_logFile.flush(); +} + +void drawLiveUi() { + if (!g_displayReady) { + return; + } + + time_t now = time(nullptr); + struct tm tmUtc; + char line1[24]; + char line2[28]; + char line3[28]; + char line4[28]; + char line5[28]; + + if (now >= 946684800 && gmtime_r(&now, &tmUtc) != nullptr) { + snprintf(line1, sizeof(line1), "%02d:%02d:%02d UTC", tmUtc.tm_hour, tmUtc.tm_min, tmUtc.tm_sec); + } else { + snprintf(line1, sizeof(line1), "time invalid"); + } + + snprintf(line2, sizeof(line2), "X:% 7.2f Y:% 7.2f", g_lastSample.x_uT, g_lastSample.y_uT); + snprintf(line3, sizeof(line3), "Z:% 7.2f F:% 7.2f", g_lastSample.z_uT, g_lastSample.field_uT); + snprintf(line4, sizeof(line4), "HdM:%6.1f T:%6.1f", g_lastSample.headingMagDeg, g_lastSample.headingTrueDeg); + snprintf(line5, sizeof(line5), "%s 0x%02X N:%lu", g_magLabel, g_magAddress, (unsigned long)g_lastSample.seq); + + g_oled.clearBuffer(); + g_oled.setFont(u8g2_font_6x12_tf); + g_oled.drawUTF8(0, 12, line1); + g_oled.drawUTF8(0, 24, line2); + g_oled.drawUTF8(0, 36, line3); + g_oled.drawUTF8(0, 48, line4); + g_oled.drawUTF8(0, 60, line5); + g_oled.sendBuffer(); +} + +void printBootSummary() { + Serial.printf("exercise=%s\n", kExerciseName); + Serial.printf("board_id=%s\n", kBoardId); + Serial.printf("node_label=%s\n", kNodeLabel); + Serial.printf("build=%s\n", kBuild); + Serial.printf("pmu_wire_pins=sda:%d scl:%d\n", tbeam_supreme::i2cSda(), tbeam_supreme::i2cScl()); + Serial.printf("oled_wire_pins=sda:%d scl:%d addr:0x%02X\n", OLED_SDA, OLED_SCL, OLED_ADDR); + Serial.printf("declination_deg=%.2f\n", kDeclinationDeg); + Serial.printf("sample_interval_ms=%lu\n", (unsigned long)kSampleIntervalMs); +} + +void appSetup() { + Serial.begin(115200); + const uint32_t serialWaitStart = millis(); + while (!Serial && (millis() - serialWaitStart) < 4000) { + delay(10); + } + delay(300); + + printBootSummary(); + initDisplay(); + drawLines("Exercise 22", "Magnetometer", "& calibration", kBoardId, "bring-up"); + + if (!tbeam_supreme::initPmuForPeripherals(g_pmu, &Serial)) { + Serial.println("pmu_init=failed"); + drawLines("Exercise 22", "PMU init failed", kBoardId, "see serial"); + return; + } + Serial.println("pmu_init=ok"); + + bool lowVoltage = false; + if (readRtc(g_rtcUtc, lowVoltage) && !lowVoltage && isValidDateTime(g_rtcUtc)) { + setSystemTimeFromRtc(g_rtcUtc); + g_timeValid = true; + char rtcStamp[32]; + formatCompactUtc(g_rtcUtc, rtcStamp, sizeof(rtcStamp)); + Serial.printf("rtc_sync=ok utc=%s\n", rtcStamp); + } else { + Serial.println("rtc_sync=invalid"); + } + + if (!mountSd()) { + Serial.println("sd_mount=failed"); + drawLines("Exercise 22", "SD mount failed", kBoardId, "see serial"); + } else { + Serial.println("sd_mount=ok"); + g_sd.printCardInfo(); + } + + g_magReady = initMagnetometer(); + if (!g_magReady) { + Serial.println("magnetometer_init=failed"); + drawLines("Exercise 22", "MAG init failed", kBoardId, "see serial"); + return; + } + + Serial.printf("magnetometer_init=ok label=%s addr=0x%02X chip=0x%02X\n", g_magLabel, g_magAddress, g_magChipId); + + if (g_timeValid && g_sdMounted) { + if (openLogFile()) { + Serial.printf("log_open=ok path=%s\n", g_logPath); + } else { + Serial.println("log_open=failed"); + } + } else { + Serial.printf("log_open=skipped time_valid=%s sd_mounted=%s\n", + g_timeValid ? "yes" : "no", + g_sdMounted ? "yes" : "no"); + } + + startWebServer(); + + drawLines("Exercise 22", "Magnetometer", g_magLabel, "rotate slowly", "logging @200ms"); + delay(kUiSplashMs); + g_lastSampleMs = millis(); + g_lastDisplayMs = millis(); + g_lastHeartbeatMs = millis(); +} + +void appLoop() { + if (g_webReady) { + g_server.handleClient(); + } + + g_sd.update(); + g_sdMounted = g_sd.isMounted(); + + const uint32_t now = millis(); + if ((uint32_t)(now - g_lastSampleMs) >= kSampleIntervalMs) { + g_lastSampleMs = now; + MagSample sample{}; + if (captureSample(sample)) { + g_lastSample = sample; + printSampleToSerial(sample); + appendSampleToLog(sample); + } + } + + if ((uint32_t)(now - g_lastDisplayMs) >= kDisplayIntervalMs) { + g_lastDisplayMs = now; + drawLiveUi(); + } + + if ((uint32_t)(now - g_lastHeartbeatMs) >= 5000) { + g_lastHeartbeatMs = now; + Serial.printf("alive seq=%lu log=%s web=%s sd=%s\n", + (unsigned long)g_lastSample.seq, + g_logOpen ? "open" : "closed", + g_webReady ? "up" : "down", + g_sdMounted ? "mounted" : "absent"); + } +} + +} // namespace + +void setup() { + appSetup(); +} + +void loop() { + appLoop(); +}