130 lines
3.6 KiB
C++
130 lines
3.6 KiB
C++
|
|
#ifdef ESP8266
|
|
bool getLocalTime(struct tm * info, uint32_t ms)
|
|
{
|
|
uint32_t start = millis();
|
|
time_t now;
|
|
while ((millis() - start) <= ms) {
|
|
time(&now);
|
|
localtime_r(&now, info);
|
|
if (info->tm_year > (2016 - 1900)) {
|
|
return true;
|
|
}
|
|
delay(10);
|
|
}
|
|
return false;
|
|
}
|
|
#endif
|
|
|
|
String printLocalTime() {
|
|
if (!getLocalTime(&timeinfo, 200)) {
|
|
Serial.println("Failed to obtain time");
|
|
NTPreachable = false;
|
|
return "N/A";
|
|
}
|
|
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
|
|
|
|
char timeStringBuff[50]; //50 chars should be enough
|
|
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
|
|
//Serial.println(timeStringBuff);
|
|
NTPreachable = true;
|
|
return timeStringBuff;
|
|
|
|
}
|
|
|
|
|
|
void syncNTP() {
|
|
if (iotWebConf.getState() == iotwebconf::OnLine) {
|
|
//init and get the time
|
|
#ifdef ESP8266
|
|
//sntp_servermode_dhcp(0);
|
|
configTime(MYTZ, ntpServerParam.value());
|
|
#endif
|
|
#ifdef ESP32
|
|
configTime(0, 0, ntpServer);
|
|
// TZ string information: https://www.gnu.org/software/libc/manual/html_node/TZ-Variable.html
|
|
setenv("TZ", "AEST-10", 1);
|
|
tzset(); // save the TZ variable
|
|
//setTimeZone(long offset, int daylight);
|
|
//configTime(gmtOffset_sec, daylightOffset_sec, ntpServerParamValue);
|
|
#endif
|
|
Serial.println(printLocalTime());
|
|
if (NTPreachable) {
|
|
if (clockwiseRing) {
|
|
currentSec = timeinfo.tm_sec;
|
|
currentMin = timeinfo.tm_min;
|
|
currentHour = MOD(timeinfo.tm_hour, 12);
|
|
}
|
|
else {
|
|
//inverted logic - remaining time
|
|
currentSec = 60 - timeinfo.tm_sec;
|
|
currentMin = 60 - timeinfo.tm_min;
|
|
currentHour = 12 - MOD(timeinfo.tm_hour, 12);
|
|
}
|
|
Serial.println("NTP mapped: " + String(int(currentHour * NUM_LEDS / 12)) + ":" + String(int(currentMin * NUM_LEDS / 60)) + ":" + String(int(currentSec * NUM_LEDS / 60)));
|
|
bootAnim.disable();
|
|
iotWebConfConfigSaved();
|
|
ledRefresh.enable();
|
|
#ifdef LDR_PIN
|
|
brightnessFading.enable();
|
|
#endif
|
|
}
|
|
clockTick.enable(); //enable to check for ntp every 60 seconds
|
|
brightnessAdjustment.enable();
|
|
}
|
|
}
|
|
|
|
// Scheduler
|
|
void clockTickCallback() {
|
|
#ifdef LDR_PIN
|
|
ldrValue = (ldrValue + analogRead(LDR_PIN));
|
|
#endif
|
|
if (clockwiseRing) {
|
|
if (currentSec >= 60) {
|
|
currentSec = 0;
|
|
currentMin++;
|
|
if (!NTPreachable) {
|
|
syncNTP();
|
|
}
|
|
if (currentMin >= 60) {
|
|
currentMin = 0;
|
|
currentHour++;
|
|
if (currentHour >= 12) {
|
|
currentHour = 0;
|
|
syncNTP();
|
|
}
|
|
}
|
|
if (followingHour) {
|
|
hourOffset = int(currentMin * NUM_LEDS / 60 / 12);
|
|
//Serial.println("clockwise hourOffset: " + String(hourOffset));
|
|
}
|
|
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour / (12 / NUM_LEDS)) + ":" + String(currentMin / (60 / NUM_LEDS)) + ":" + String(currentSec / (60 / NUM_LEDS)));
|
|
}
|
|
currentSec++;
|
|
|
|
}
|
|
else {
|
|
if (currentSec < 0) {
|
|
currentSec = 59;
|
|
currentMin--;
|
|
if (!NTPreachable) {
|
|
syncNTP();
|
|
}
|
|
if (currentMin < 0) {
|
|
currentMin = 59;
|
|
currentHour--;
|
|
if (currentHour < 0) {
|
|
currentHour = 11;
|
|
syncNTP();
|
|
}
|
|
}
|
|
if (followingHour) {
|
|
hourOffset = 0 - int((60 - currentMin * NUM_LEDS / 60) / 12); //negative value
|
|
//Serial.println("Anticlockwise hourOffset: " + String(hourOffset));
|
|
}
|
|
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour / (12 / NUM_LEDS)) + ":" + String(currentMin / (60 / NUM_LEDS)) + ":" + String(currentSec / (60 / NUM_LEDS)));
|
|
}
|
|
currentSec--;
|
|
}
|
|
}
|