/* * Timeutil.ino * * This script handle date time update on the homepage. Will only activate * when the current page is 0 (aka home page) */ //Entry point for scheuler void datetimeUpdateCallback(){ if (currentPage == 0){ updateTimeInfo(); } updateDisplayDimming(); } //Return if the current time is night bool isNight() { int currentHour = ntpClient.getHours(); // Night is defined from 7:00 PM to 7:00 AM (19:00 to 7:00 in 24-hour format) // Change this to fit your life style return (currentHour >= 19 || currentHour < 7); } //Update the dimming state of the display void updateDisplayDimming(){ int currentHour = ntpClient.getHours(); int newDimmingValue = 30; switch(currentHour){ case 0: case 1: case 2: case 3: case 4: newDimmingValue = 3; break; case 5: newDimmingValue = 10; break; case 6: newDimmingValue = 15; break; case 7: newDimmingValue = 20; break; case 8: newDimmingValue = 25; break; case 9: newDimmingValue = 30; break; case 10: case 11: case 12: case 13: case 14: case 15: newDimmingValue = 40; break; case 16: newDimmingValue = 30; break; case 17: case 18: newDimmingValue = 25; break; case 19: case 20: case 21: newDimmingValue = 20; break; case 22: newDimmingValue = 15; break; case 23: newDimmingValue = 5; break; default: newDimmingValue = 50; // Default to maximum brightness if the hour is out of expected range } sendCommand("dim=" + String(newDimmingValue)); } //Pad any time string with 0 String timeZeroPad(int input){ if (input < 10){ return "0" + String(input); } return String(input); } //Update the on screen display datetime void updateTimeInfo(){ ntpClient.update(); time_t epochTime = ntpClient.getEpochTime(); //Update time display String currentTime = timeZeroPad(ntpClient.getHours()) + ":" + timeZeroPad(ntpClient.getMinutes()); sendCommand("home.time.txt=\"" + currentTime + "\""); //Update am/pm indicator String currentAfterNoon = "am"; if (ntpClient.getHours() >=12){ currentAfterNoon = "pm"; } sendCommand("home.ampm.txt=\"" + currentAfterNoon + "\""); //Updat the date display struct tm *ptm = gmtime ((time_t *)&epochTime); String currentDate = String(ptm->tm_year+1900) + "/" + String(ptm->tm_mon+1) + "/" + String(ptm->tm_mday); sendCommand("home.date.txt=\"" + currentDate + "\""); //Update day of week String weekDay = weekDays[ntpClient.getDay()]; sendCommand("home.dow.txt=\"" + weekDay + "\""); //Update the backgrounds on all pages if (!isNight()){ //Morning sendCommand("home.homebg.pic=1"); sendCommand("forcast.forbg.pic=3"); sendCommand("iot.iotbg.pic=5"); }else{ //Night sendCommand("home.homebg.pic=0"); sendCommand("forcast.forbg.pic=2"); sendCommand("iot.iotbg.pic=4"); } }