Commit fba01128 authored by Eric Duminil's avatar Eric Duminil
Browse files

Splitting util into util and time_util

parent b1475814
Pipeline #5746 canceled with stage
......@@ -33,6 +33,7 @@
#endif
#include "util.h"
#include "time_util.h"
#include "sensor_console.h"
#include "co2_sensor.h"
#include "led_effects.h"
......
#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>
......
#include "csv_writer.h"
#include "config.h"
#include "util.h" // -> time.h
#include "time_util.h"
#include "led_effects.h"
#include "sensor_console.h"
......
......@@ -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.
......
#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)
......
......@@ -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
......
#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);
}
}
#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
#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();
......
......@@ -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
......@@ -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"
......
#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"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment