/* SPDX-License-Identifier: MIT Copyright (c) 2024 Casey Reeves and the LuminaSensum contributors Copyright (c) 2024 John Watts and the LuminaSensum contributors */ #include "ButtonThread.h" #include "Clock.h" #include "Filesystem.h" #include "FlashErase.h" #include "MainBD.h" #include "MainFilesystem.h" #include "MyUSBMSD.h" #include "NetworkHandler.h" #include "mbed-trace/mbed_trace.h" #include "mbed.h" #define TRACE_GROUP "APP" #if MBED_CONF_MBED_TRACE_ENABLE // mutex setup for tracing in thread safe mode static Mutex traceMutex; static void trace_mutex_wait() { traceMutex.lock(); } static void trace_mutex_release() { traceMutex.unlock(); } void setup_tracing(void) { char trace_filters[] = "APP,FS,FLER,NET,SNTP,USBM"; mbed_trace_mutex_wait_function_set(trace_mutex_wait); mbed_trace_mutex_release_function_set(trace_mutex_release); mbed_trace_init(); mbed_trace_include_filters_set(trace_filters); mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL); tr_debug("Debug tracing enabled"); tr_info("Info tracing enabled"); tr_warn("Warning tracing enabled"); tr_err("Error tracing enabled"); } #else void setup_tracing(void) { // do nothing } #endif extern "C" int lang_main(void); int main() { setup_tracing(); tr_info("Project Tardis"); clock_setup(); tr_info("The time is %s", clock_iso8601string_now()); mountFilesystem(mainBD, mainFS); Thread buttonThread; buttonThread.start(buttonTask); network_init(); network_connect(); lang_main(); while (true) { int presses = waitForPresses(600s); if (presses == 1) { unmountFilesystem(mainBD, mainFS); doUSBMSD(mainBD); mountFilesystem(mainBD, mainFS); } if (presses == 2) { unmountFilesystem(mainBD, mainFS); doErase(mainBD); } if (presses == 3) { if (!network_is_connected()) { tr_err("Can't sync clock without network " "connection!"); continue; } NetworkInterface *net = network_interface(); tr_info("Syncing clock..."); tr_info("The current time is %s", clock_iso8601string_now()); SNTPError err = clock_sync(*net); if (err != SNTPSuccess) { tr_err("Failed to sync clock, error %i", err); } else { tr_info("The new time is %s", clock_iso8601string_now()); } // Wait until the nearest second std::chrono::microseconds future = clock_now(); future += std::chrono::seconds(1); future = std::chrono::floor<std::chrono::seconds>( future); kernel_timepoint target = real_to_kernel_time(future); ThisThread::sleep_until(target); tr_info("Waited to %s", clock_iso8601string_now()); } } unmountFilesystem(mainBD, mainFS); buttonThread.terminate(); return 0; }