util.h 1.22 KB
Newer Older
1
2
3
4
#ifndef AMPEL_UTIL_H_INCLUDED
#define AMPEL_UTIL_H_INCLUDED
#include <Arduino.h>
#include "config.h"
5
#include "sensor_console.h"
6

Eric Duminil's avatar
Eric Duminil committed
7
#include <WiFiUdp.h> // required for NTP
8
9
10
#include "src/lib/NTPClient-master/NTPClient.h" // NTP

#if defined(ESP8266)
Eric Duminil's avatar
Eric Duminil committed
11
#  include <ESP8266WiFi.h> // required to get MAC address
12
13
#  define get_free_heap_size() system_get_free_heap_size()
#elif defined(ESP32)
Eric Duminil's avatar
Eric Duminil committed
14
#  include <WiFi.h> // required to get MAC address
15
16
17
18
19
20
#  define get_free_heap_size() esp_get_free_heap_size()
#endif

namespace ntp {
  void initialize();
  void update();
21
  void getLocalTime(char *timestamp);
22
23
}

24
25
26
27
28
29
30
31
32
33
34
namespace util {
  template<typename Tpa, typename Tpb>
  inline auto min(const Tpa &a, const Tpb &b) -> decltype(a < b ? a : b) {
    return b < a ? b : a;
  }

  template<typename Tpa, typename Tpb>
  inline auto max(const Tpa &a, const Tpb &b) -> decltype(b > a ? b : a) {
    return b > a ? b : a;
  }
}
35
class Ampel {
Eric Duminil's avatar
Eric Duminil committed
36
37
private:
  static void showFreeSpace();
38
public:
Eric Duminil's avatar
Eric Duminil committed
39
  const char *board;
Eric Duminil's avatar
Eric Duminil committed
40
  const char *sensorId;
Eric Duminil's avatar
Eric Duminil committed
41
  uint32_t max_loop_duration;
42
43
  Ampel();
};
44

Eric Duminil's avatar
Notes    
Eric Duminil committed
45
//NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over.
46
#define seconds() (millis() / 1000UL)
Eric Duminil's avatar
Eric Duminil committed
47
extern Ampel ampel;
48
49

#endif