LED-Clock/realtime.ino

129 lines
3.6 KiB
Arduino
Raw Normal View History

2022-01-30 11:45:00 +00:00
// https://github.com/esp8266/Arduino/commit/a05a71fa9d2e6b143cb34f01b47e22c4b66b80a1
//#ifdef ESP8266
//bool getLocalTimeMe(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
2022-01-30 11:45:00 +00:00
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 timeRefresh( bool from_sntp ) {
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();
}
2022-01-30 11:45:00 +00:00
void setNTP() {
//init and get the time
2022-01-30 11:45:00 +00:00
#ifdef ESP8266
//sntp_servermode_dhcp(0);
configTime(MYTZ, ntpServerParam.value());
yield();
settimeofday_cb(timeRefresh);
2022-01-30 11:45:00 +00:00
#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);
2022-01-30 11:45:00 +00:00
#endif
Serial.println(printLocalTime());
timeRefresh(false);
2022-01-30 11:45:00 +00:00
}
// Scheduler
void clockTickCallback() {
#ifdef LDR_PIN
ldrValue = (ldrValue + analogRead(LDR_PIN));
#endif
if (clockwiseRing) {
if (currentSec >= 60) {
currentSec = 0;
currentMin++;
if (!NTPreachable) {
setNTP();
2022-01-30 11:45:00 +00:00
}
if (currentMin >= 60) {
currentMin = 0;
currentHour++;
if (currentHour >= 12) {
currentHour = 0;
}
}
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) {
setNTP();
2022-01-30 11:45:00 +00:00
}
if (currentMin < 0) {
currentMin = 59;
currentHour--;
if (currentHour < 0) {
currentHour = 11;
}
}
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--;
}
}