util.h 1.37 KB
Newer Older
1
2
3
4
#ifndef AMPEL_UTIL_H_INCLUDED
#define AMPEL_UTIL_H_INCLUDED

#if defined(ESP8266)
Eric Duminil's avatar
Eric Duminil committed
5
#  include <ESP8266WiFi.h> // required to get MAC address
Eric Duminil's avatar
Eric Duminil committed
6
7
#  define esp_get_max_free_block_size() ESP.getMaxFreeBlockSize()
#  define esp_get_heap_fragmentation() ESP.getHeapFragmentation()
8
#elif defined(ESP32)
Eric Duminil's avatar
Eric Duminil committed
9
#  include <WiFi.h> // required to get MAC address
Eric Duminil's avatar
Eric Duminil committed
10
11
#  define esp_get_max_free_block_size() ESP.getMaxAllocHeap() //largest block of heap that can be allocated.
#  define esp_get_heap_fragmentation() "?" // apparently not available for ESP32
12
13
14
15
16
#endif

namespace ntp {
  void initialize();
  void update();
17
  void getLocalTime(char *timestamp);
18
19
}

20
21
22
23
24
25
26
27
28
29
30
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;
  }
}
31
class Ampel {
Eric Duminil's avatar
Eric Duminil committed
32
33
private:
  static void showFreeSpace();
34
public:
Eric Duminil's avatar
Eric Duminil committed
35
  const char *version = "v0.2.3-DEV"; // Update manually after significant changes.
Eric Duminil's avatar
Eric Duminil committed
36
  const char *board;
Eric Duminil's avatar
Eric Duminil committed
37
  const char *sensorId;
38
  const char *macAddress;
Eric Duminil's avatar
Eric Duminil committed
39
  uint32_t max_loop_duration;
40
41
  Ampel();
};
42

Eric Duminil's avatar
Eric Duminil committed
43
44
extern Ampel ampel;

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
47
48
#define seconds() (millis() / 1000UL)

#endif