AceTime  0.5
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
flash.h
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_COMMON_FLASH_H
7 #define ACE_TIME_COMMON_FLASH_H
8 
9 #include <stdint.h>
10 
15 #define ACE_TIME_USE_PROGMEM 1
16 #if ACE_TIME_USE_PROGMEM
17  #define ACE_TIME_PROGMEM PROGMEM
18 #else
19  #define ACE_TIME_PROGMEM
20 #endif
21 
22 // Include the correct pgmspace.h depending on architecture. Define a
23 // consistent acetime_strcmp_P() which can be passed as a function pointer
24 // into the ZoneManager template class.
25 #if defined(__AVR__)
26  #include <avr/pgmspace.h>
27  #define FPSTR(p) (reinterpret_cast<const __FlashStringHelper *>(p))
28  #define acetime_strcmp_P strcmp_P
29 #elif defined(TEENSYDUINO)
30  #include <avr/pgmspace.h>
31  #define FPSTR(p) (reinterpret_cast<const __FlashStringHelper *>(p))
32  // Teensyduino defines strcmp_P(a, b) as strcmp(a,b), which cannot be
33  // passed as a function pointer, so we have to use strcmp() directly.
34  #define acetime_strcmp_P strcmp
35 #elif defined(ESP8266)
36  #include <pgmspace.h>
37  // ESP8266 2.5.2 defines strcmp_P() as a macro, so we have to provide a real
38  // function.
39  inline int acetime_strcmp_P(const char* str1, const char* str2P) {
40  return strcmp_P((str1), (str2P));
41  }
42 
43  extern "C" {
44  const char* strchr_P(const char* s, int c);
45  const char* strrchr_P(const char* s, int c);
46  }
47 
48 #elif defined(ESP32)
49  #include <pgmspace.h>
50  // Fix incorrect definition of FPSTR in ESP32, see
51  // https://github.com/espressif/arduino-esp32/issues/1371
52  #undef FPSTR
53  #define FPSTR(p) (reinterpret_cast<const __FlashStringHelper *>(p))
54  #define acetime_strcmp_P strcmp_P
55 
56  extern "C" {
57  const char* strchr_P(const char* s, int c);
58  const char* strrchr_P(const char* s, int c);
59  }
60 
61 #elif defined(__linux__) or defined(__APPLE__)
62  #include <pgmspace.h>
63  #define FPSTR(p) (reinterpret_cast<const __FlashStringHelper *>(p))
64  #define acetime_strcmp_P strcmp_P
65 #else
66  #error Unsupported platform
67 #endif
68 
73 inline int acetime_strcmp_PP(const char* a, const char* b) {
74  if (a == b) { return 0; }
75  if (a == nullptr) { return -1; }
76  if (b == nullptr) { return 1; }
77 
78  while (true) {
79  uint8_t ca = pgm_read_byte(a);
80  uint8_t cb = pgm_read_byte(b);
81  if (ca != cb) return (int) ca - (int) cb;
82  if (ca == '\0') return 0;
83  a++;
84  b++;
85  }
86 }
87 
88 #endif