diff --git a/CLC-qthing/CMakeLists.txt b/CLC-qthing/CMakeLists.txt index bc7bdbc9ade75b236f3ac327d7454e384337c1a6..2daa6ed90ff637b3dc342d7defc2376143594d71 100644 --- a/CLC-qthing/CMakeLists.txt +++ b/CLC-qthing/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register( SRC_DIRS - "." "SiliconTorch" + "." "SiliconTorch" "SpiderLib" INCLUDE_DIRS "." diff --git a/CLC-qthing/SpiderLib/SNTP.cpp b/CLC-qthing/SpiderLib/SNTP.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb8de95d746a0f19e2b8d4d0154f89c2869c87b0 --- /dev/null +++ b/CLC-qthing/SpiderLib/SNTP.cpp @@ -0,0 +1,87 @@ +#include "SNTP.hpp" + +// ESP32 specific +#include "esp_log.h" +#include "esp_sntp.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +// #include "driver/ledc.h" + +// C++ system level +#include <ctime> +#include <functional> +#include <cinttypes> + +// project specific +// #include "Metrics.hpp" + + +/*extern "C" { + void setenv(char* env, char* value, int overwrite); + void tzset(); +}*/ + + +static const char* TAG = "SNTP"; + + +static void ntpTask(void* evHdl) { + + std::function<void()>* syncEventHandler = (std::function<void()>*) evHdl; + + + time_t now = 0; + struct tm timeinfo = { 0 }; + + do { + vTaskDelay(100 / portTICK_PERIOD_MS); + time(&now); + localtime_r(&now, &timeinfo); + } while (timeinfo.tm_year < 2000); + + + (*syncEventHandler)(); + + + ESP_LOGI(TAG, "Time sync complete: "); + + vTaskDelete(NULL); +} + + + +namespace SpiderLib { + + SNTP::SNTP() { + + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_setservername(0, "pool.ntp.org"); // TODO: make configurable! + sntp_init(); + + setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1); + tzset(); + + xTaskCreate(ntpTask, TAG, 8192, (void*)&syncEventHandler, 0, NULL); + } + + void SNTP::registerTimeSyncHandler(std::function<void()> f) { + if (inSync()) { + f(); + } else { + std::function<void()> _f = syncEventHandler; + syncEventHandler = [=]() { + _f(); + f(); + }; + } + } + + bool SNTP::inSync() { + return false; + } + + + + +} diff --git a/CLC-qthing/SpiderLib/SNTP.hpp b/CLC-qthing/SpiderLib/SNTP.hpp new file mode 100644 index 0000000000000000000000000000000000000000..2dc7d0d075de3a0c5b370b71190b39a69fc3e6a9 --- /dev/null +++ b/CLC-qthing/SpiderLib/SNTP.hpp @@ -0,0 +1,38 @@ +#pragma once + +// ESP32 specific +// #include "driver/ledc.h" + +// C++ system level +// #include <cinttypes> +#include <functional> + +// project specific +// #include "Metrics.hpp" + + +namespace SpiderLib { + + class SNTP { + public: + + SNTP(SNTP const&) = delete; + void operator=(SNTP const&) = delete; + + static SNTP& instance() { + static SNTP sntp; + return sntp; + } + + bool inSync(); + + void registerTimeSyncHandler(std::function<void()> f); + + private: + + SNTP(); + + std::function<void()> syncEventHandler = [](){}; + + }; +} diff --git a/CLC-qthing/device_main.cpp b/CLC-qthing/device_main.cpp index e98a28a04792745d11d485d80ea5d1e348dfe11c..8a591f9a54b044c810e276adbba35557952c6512 100644 --- a/CLC-qthing/device_main.cpp +++ b/CLC-qthing/device_main.cpp @@ -12,6 +12,7 @@ // ### LIBS FOR TESTING ### #include <cstdlib> +#include "SpiderLib/SNTP.hpp" #include "SiliconTorch/CyanBusCRC.hpp" #include "SiliconTorch/CyanBusTOUT.hpp" // ### END LIBS ### @@ -24,6 +25,16 @@ CyanLight::CyanLightControl* ctrl; void device_main() { + //SpiderLib::SNTP& ntp = SpiderLib::SNTP::instance(); + + // ntp.registerTimeSyncHandler([](){ + // ESP_LOGE("EVENT…", "…HANDLER()!"); + // }); + + cfg.apply(); + + return; + // Needed for packet parsing, animation rendering and stuff qthing::power_managment_max_power();