util.h 1.51 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
Eric Duminil's avatar
Eric Duminil committed
12
13
#  define esp_get_max_free_block_size() ESP.getMaxFreeBlockSize()
#  define esp_get_heap_fragmentation() ESP.getHeapFragmentation()
14
#elif defined(ESP32)
Eric Duminil's avatar
Eric Duminil committed
15
#  include <WiFi.h> // required to get MAC address
Eric Duminil's avatar
Eric Duminil committed
16
17
#  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
18
19
20
21
22
#endif

namespace ntp {
  void initialize();
  void update();
23
  void getLocalTime(char *timestamp);
24
25
}

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

Eric Duminil's avatar
Eric Duminil committed
48
49
extern Ampel ampel;

Eric Duminil's avatar
Notes    
Eric Duminil committed
50
//NOTE: Only use seconds() for duration comparison, not timestamps comparison. Otherwise, problems happen when millis roll over.
51
52
53
#define seconds() (millis() / 1000UL)

#endif