From fba0112823b5a0c1da8afbd044a427950907bf77 Mon Sep 17 00:00:00 2001 From: Eric Duminil <eric.duminil@gmail.com> Date: Mon, 7 Feb 2022 22:18:16 +0100 Subject: [PATCH] Splitting util into util and time_util --- ampel-firmware/ampel-firmware.h | 1 + ampel-firmware/co2_sensor.cpp | 2 +- ampel-firmware/csv_writer.cpp | 2 +- ampel-firmware/lorawan.cpp | 1 + ampel-firmware/mqtt.cpp | 2 +- ampel-firmware/mqtt.h | 2 +- ampel-firmware/time_util.cpp | 49 +++++++++++++++++++++++++++++++++ ampel-firmware/time_util.h | 13 +++++++++ ampel-firmware/util.cpp | 47 ------------------------------- ampel-firmware/util.h | 9 ------ ampel-firmware/web_server.cpp | 1 + ampel-firmware/wifi_util.cpp | 3 +- 12 files changed, 71 insertions(+), 61 deletions(-) create mode 100644 ampel-firmware/time_util.cpp create mode 100644 ampel-firmware/time_util.h diff --git a/ampel-firmware/ampel-firmware.h b/ampel-firmware/ampel-firmware.h index 41fe46b..b6a3d01 100644 --- a/ampel-firmware/ampel-firmware.h +++ b/ampel-firmware/ampel-firmware.h @@ -33,6 +33,7 @@ #endif #include "util.h" +#include "time_util.h" #include "sensor_console.h" #include "co2_sensor.h" #include "led_effects.h" diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp index 62182b1..fe02da6 100644 --- a/ampel-firmware/co2_sensor.cpp +++ b/ampel-firmware/co2_sensor.cpp @@ -1,8 +1,8 @@ #include "co2_sensor.h" #include "config.h" +#include "time_util.h" #include "led_effects.h" -#include "util.h" // Just time.h #include "sensor_console.h" #include <Wire.h> diff --git a/ampel-firmware/csv_writer.cpp b/ampel-firmware/csv_writer.cpp index 6492d3b..ec59f3c 100644 --- a/ampel-firmware/csv_writer.cpp +++ b/ampel-firmware/csv_writer.cpp @@ -1,7 +1,7 @@ #include "csv_writer.h" #include "config.h" -#include "util.h" // -> time.h +#include "time_util.h" #include "led_effects.h" #include "sensor_console.h" diff --git a/ampel-firmware/lorawan.cpp b/ampel-firmware/lorawan.cpp index 63a1239..6f849d4 100644 --- a/ampel-firmware/lorawan.cpp +++ b/ampel-firmware/lorawan.cpp @@ -5,6 +5,7 @@ #include "led_effects.h" #include "sensor_console.h" #include "util.h" +#include "time_util.h" /*** Define region and transceiver type, and ignore lmic_project_config.h from lmic library ***/ // Those values are probably okay if you're in Europe. diff --git a/ampel-firmware/mqtt.cpp b/ampel-firmware/mqtt.cpp index 0a09c87..d393e71 100644 --- a/ampel-firmware/mqtt.cpp +++ b/ampel-firmware/mqtt.cpp @@ -1,10 +1,10 @@ #include "mqtt.h" #include "config.h" -#include "util.h" // -> time.h #include "led_effects.h" #include "sensor_console.h" #include "wifi_util.h" +#include "time_util.h" #include "src/lib/PubSubClient/src/PubSubClient.h" #if defined(ESP8266) diff --git a/ampel-firmware/mqtt.h b/ampel-firmware/mqtt.h index 3ff9b16..0e39b09 100644 --- a/ampel-firmware/mqtt.h +++ b/ampel-firmware/mqtt.h @@ -3,7 +3,7 @@ #include <stdint.h> // For uint32_t & uint16_t -#include <config.h> +#include "config.h" #if !defined(MQTT_ENCRYPTED) # define MQTT_ENCRYPTED true // Old config files might not define it, and encryption was on by default. #endif diff --git a/ampel-firmware/time_util.cpp b/ampel-firmware/time_util.cpp new file mode 100644 index 0000000..b37fcaf --- /dev/null +++ b/ampel-firmware/time_util.cpp @@ -0,0 +1,49 @@ +#include "time_util.h" +#include "sensor_console.h" +#include "config.h" +#include <WiFiUdp.h> // required for NTP +#include "src/lib/NTPClient-master/NTPClient.h" // NTP + +namespace config { + const char *ntp_server = NTP_SERVER; + const long utc_offset_in_seconds = UTC_OFFSET_IN_SECONDS; // UTC+1 +} + +//NOTE: ESP32 sometimes couldn't access the NTP server, and every loop would take +1000ms +// ifdefs could be used to define functions specific to ESP32, e.g. with configTime +namespace ntp { + WiFiUDP ntpUDP; + NTPClient timeClient(ntpUDP, config::ntp_server, config::utc_offset_in_seconds, 60000UL); + bool connected_at_least_once = false; + void setLocalTime(int32_t unix_seconds); + + void initialize() { + timeClient.begin(); + sensor_console::defineIntCommand("set_time", ntp::setLocalTime, F("1618829570 (Sets time to the given UNIX time)")); + } + + void update() { + connected_at_least_once |= timeClient.update(); + } + + void getLocalTime(char *timestamp) { + timeClient.getFormattedDate(timestamp); + } + + void setLocalTime(int32_t unix_seconds) { + char time[23]; + timeClient.getFormattedDate(time); + Serial.print(F("Current time : ")); + Serial.println(time); + if (connected_at_least_once) { + Serial.println(F("NTP update already happened. Not changing anything.")); + return; + } + Serial.print(F("Setting UNIX time to : ")); + Serial.println(unix_seconds); + timeClient.setEpochTime(unix_seconds - seconds()); + timeClient.getFormattedDate(time); + Serial.print(F("Current time : ")); + Serial.println(time); + } +} diff --git a/ampel-firmware/time_util.h b/ampel-firmware/time_util.h new file mode 100644 index 0000000..fe1647c --- /dev/null +++ b/ampel-firmware/time_util.h @@ -0,0 +1,13 @@ +#ifndef AMPEL_TIME_H_INCLUDED +#define AMPEL_TIME_H_INCLUDED + +namespace ntp { + void initialize(); + void update(); + void getLocalTime(char *timestamp); +} + +//NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over. +#define seconds() (millis() / 1000UL) + +#endif diff --git a/ampel-firmware/util.cpp b/ampel-firmware/util.cpp index 0a13836..0edf43c 100644 --- a/ampel-firmware/util.cpp +++ b/ampel-firmware/util.cpp @@ -1,14 +1,5 @@ #include "util.h" #include "sensor_console.h" -#include "config.h" -#include "sensor_console.h" -#include <WiFiUdp.h> // required for NTP -#include "src/lib/NTPClient-master/NTPClient.h" // NTP - -namespace config { - const char *ntp_server = NTP_SERVER; - const long utc_offset_in_seconds = UTC_OFFSET_IN_SECONDS; // UTC+1 -} #if defined(ESP8266) # include <ESP8266WiFi.h> // required to get MAC address @@ -27,43 +18,6 @@ const char *current_board = "ESP32"; const char *current_board = "UNKNOWN"; #endif -//NOTE: ESP32 sometimes couldn't access the NTP server, and every loop would take +1000ms -// ifdefs could be used to define functions specific to ESP32, e.g. with configTime -namespace ntp { - WiFiUDP ntpUDP; - NTPClient timeClient(ntpUDP, config::ntp_server, config::utc_offset_in_seconds, 60000UL); - bool connected_at_least_once = false; - - void initialize() { - timeClient.begin(); - } - - void update() { - connected_at_least_once |= timeClient.update(); - } - - void getLocalTime(char *timestamp) { - timeClient.getFormattedDate(timestamp); - } - - void setLocalTime(int32_t unix_seconds) { - char time[23]; - timeClient.getFormattedDate(time); - Serial.print(F("Current time : ")); - Serial.println(time); - if (connected_at_least_once) { - Serial.println(F("NTP update already happened. Not changing anything.")); - return; - } - Serial.print(F("Setting UNIX time to : ")); - Serial.println(unix_seconds); - timeClient.setEpochTime(unix_seconds - seconds()); - timeClient.getFormattedDate(time); - Serial.print(F("Current time : ")); - Serial.println(time); - } -} - void Ampel::showFreeSpace() { Serial.print(F("Free heap space : ")); Serial.print(ESP.getFreeHeap()); @@ -97,7 +51,6 @@ char* getSensorId() { Ampel::Ampel() : board(current_board), sensorId(getSensorId()), macAddress(getMacString()), max_loop_duration(0) { - sensor_console::defineIntCommand("set_time", ntp::setLocalTime, F("1618829570 (Sets time to the given UNIX time)")); sensor_console::defineCommand("free", Ampel::showFreeSpace, F("(Displays available heap space)")); sensor_console::defineCommand("reset", []() { ESP.restart(); diff --git a/ampel-firmware/util.h b/ampel-firmware/util.h index 78d456c..495f7ea 100644 --- a/ampel-firmware/util.h +++ b/ampel-firmware/util.h @@ -11,12 +11,6 @@ # define esp_get_heap_fragmentation() "?" // apparently not available for ESP32 #endif -namespace ntp { - void initialize(); - void update(); - void getLocalTime(char *timestamp); -} - namespace util { template<typename Tpa, typename Tpb> inline auto min(const Tpa &a, const Tpb &b) -> decltype(a < b ? a : b) { @@ -42,7 +36,4 @@ public: extern Ampel ampel; -//NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over. -#define seconds() (millis() / 1000UL) - #endif diff --git a/ampel-firmware/web_server.cpp b/ampel-firmware/web_server.cpp index b0d520d..86ebb19 100644 --- a/ampel-firmware/web_server.cpp +++ b/ampel-firmware/web_server.cpp @@ -8,6 +8,7 @@ #include "config.h" #include "util.h" +#include "time_util.h" #include "wifi_util.h" #include "co2_sensor.h" #include "sensor_console.h" diff --git a/ampel-firmware/wifi_util.cpp b/ampel-firmware/wifi_util.cpp index 0ef6df9..7200206 100644 --- a/ampel-firmware/wifi_util.cpp +++ b/ampel-firmware/wifi_util.cpp @@ -1,7 +1,8 @@ #include "wifi_util.h" #include "config.h" -#include "util.h" // -> time.h +#include "util.h" +#include "time_util.h" #include "led_effects.h" #include "sensor_console.h" -- GitLab