Skip to content

Commit

Permalink
Added Json timezone support with MYCILA_NTP_JSON_SUPPORT
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Jan 29, 2024
1 parent 762b6c1 commit 0c5a220
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ jobs:
- name: Install core
run: arduino-cli core install --additional-urls "${{ matrix.index_url }}" ${{ matrix.core }}

- name: Install ArduinoJson
run: arduino-cli lib install ArduinoJson

- name: Build NTP
run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/NTP/NTP.ino"

- name: Build NTPJson
run: arduino-cli compile --library . --warnings all -b ${{ matrix.board }} "examples/NTPJson/NTPJson.ino" ---build-property build.extra_flags=-DMYCILA_NTP_JSON_SUPPORT

platformio:
name: PlatformIO
needs: cpplint
Expand Down Expand Up @@ -108,3 +114,6 @@ jobs:

- name: Build NTP
run: platformio ci "examples/NTP/NTP.ino" -l '.' -b ${{ matrix.board }}

- name: Build NTPJson
run: PLATFORMIO_BUILD_FLAGS="-Wall -Wextra -DMYCILA_NTP_JSON_SUPPORT" platformio ci "examples/NTPJson/NTPJson.ino" -l '.' --project-option="lib_deps=ArduinoJson" -b ${{ matrix.board }}
27 changes: 27 additions & 0 deletions examples/NTPJson/NTPJson.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <MycilaNTP.h>

#include <HardwareSerial.h>
#include <WiFi.h>

#include <ArduinoJson.h>

void setup() {
Serial.begin(115200);
while (!Serial)
continue;

uint32_t start = ESP.getFreeHeap();

JsonDocument doc;
Mycila::NTP.timezonesToJson(doc.to<JsonObject>());
serializeJsonPretty(doc, Serial);
Serial.println();

Serial.print("Heap used: ");
Serial.print(start - ESP.getFreeHeap());
Serial.println(" bytes");
}

void loop() {
delay(1000);
}
3 changes: 3 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[env]
build_flags = -Wall -Wextra
; build_flags = -Wall -Wextra -D MYCILA_NTP_JSON_SUPPORT
lib_deps = ArduinoJson

upload_protocol = esptool
upload_port = /dev/cu.usbserial-0001
Expand All @@ -11,6 +13,7 @@ monitor_filters = esp32_exception_decoder, log2file
[platformio]
lib_dir = .
src_dir = examples/NTP
; src_dir = examples/NTPJson

[env:esp32]
platform = espressif32@6.5.0
Expand Down
16 changes: 16 additions & 0 deletions src/MycilaNTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ bool Mycila::NTPClass::update(const timeval* tv) {
return false;
}

#ifdef MYCILA_NTP_JSON_SUPPORT
void Mycila::NTPClass::timezonesToJson(const JsonObject& doc) const {
char* start = const_cast<char*>(MYCILA_NTP_SPEC);
char* token = strstr(start, "=");
while (token != nullptr) {
const String timezone = String(start, static_cast<size_t>(token - start));
start = token + 1;
token = strstr(start, "\n");
const String spec = String(start, static_cast<size_t>(token - start));
start = token + 1;
token = strstr(start, "=");
doc[timezone] = spec;
}
}
#endif

namespace Mycila {
NTPClass NTP;
} // namespace Mycila
8 changes: 8 additions & 0 deletions src/MycilaNTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include <WString.h>
#include <functional>

#ifdef MYCILA_NTP_JSON_SUPPORT
#include <ArduinoJson.h>
#endif

#define MYCILA_NTP_VERSION "1.0.0"
#define MYCILA_NTP_VERSION_MAJOR 1
#define MYCILA_NTP_VERSION_MINOR 0
Expand All @@ -34,6 +38,10 @@ namespace Mycila {
// true when time is synced, never changes after
bool isSynced() const { return _synced; }

#ifdef MYCILA_NTP_JSON_SUPPORT
void timezonesToJson(const JsonObject& doc) const;
#endif

private:
volatile bool _synced = false;
String _spec;
Expand Down

0 comments on commit 0c5a220

Please sign in to comment.