timeutil.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Timeutil.ino
  3. *
  4. * This script handle date time update on the homepage. Will only activate
  5. * when the current page is 0 (aka home page)
  6. */
  7. //Entry point for scheuler
  8. void datetimeUpdateCallback(){
  9. if (currentPage == 0){
  10. updateTimeInfo();
  11. }
  12. updateDisplayDimming();
  13. }
  14. //Return if the current time is night
  15. bool isNight() {
  16. int currentHour = ntpClient.getHours();
  17. // Night is defined from 7:00 PM to 7:00 AM (19:00 to 7:00 in 24-hour format)
  18. // Change this to fit your life style
  19. return (currentHour >= 19 || currentHour < 7);
  20. }
  21. //Update the dimming state of the display
  22. void updateDisplayDimming(){
  23. int currentHour = ntpClient.getHours();
  24. int newDimmingValue = 30;
  25. switch(currentHour){
  26. case 0:
  27. case 1:
  28. case 2:
  29. case 3:
  30. case 4:
  31. newDimmingValue = 3;
  32. break;
  33. case 5:
  34. newDimmingValue = 10;
  35. break;
  36. case 6:
  37. newDimmingValue = 15;
  38. break;
  39. case 7:
  40. newDimmingValue = 20;
  41. break;
  42. case 8:
  43. newDimmingValue = 25;
  44. break;
  45. case 9:
  46. newDimmingValue = 30;
  47. break;
  48. case 10:
  49. case 11:
  50. case 12:
  51. case 13:
  52. case 14:
  53. case 15:
  54. newDimmingValue = 40;
  55. break;
  56. case 16:
  57. newDimmingValue = 30;
  58. break;
  59. case 17:
  60. case 18:
  61. newDimmingValue = 25;
  62. break;
  63. case 19:
  64. case 20:
  65. case 21:
  66. newDimmingValue = 20;
  67. break;
  68. case 22:
  69. newDimmingValue = 15;
  70. break;
  71. case 23:
  72. newDimmingValue = 5;
  73. break;
  74. default:
  75. newDimmingValue = 50; // Default to maximum brightness if the hour is out of expected range
  76. }
  77. sendCommand("dim=" + String(newDimmingValue));
  78. }
  79. //Pad any time string with 0
  80. String timeZeroPad(int input){
  81. if (input < 10){
  82. return "0" + String(input);
  83. }
  84. return String(input);
  85. }
  86. //Update the on screen display datetime
  87. void updateTimeInfo(){
  88. ntpClient.update();
  89. time_t epochTime = ntpClient.getEpochTime();
  90. //Update time display
  91. String currentTime = timeZeroPad(ntpClient.getHours()) + ":" + timeZeroPad(ntpClient.getMinutes());
  92. sendCommand("home.time.txt=\"" + currentTime + "\"");
  93. //Update am/pm indicator
  94. String currentAfterNoon = "am";
  95. if (ntpClient.getHours() >=12){
  96. currentAfterNoon = "pm";
  97. }
  98. sendCommand("home.ampm.txt=\"" + currentAfterNoon + "\"");
  99. //Updat the date display
  100. struct tm *ptm = gmtime ((time_t *)&epochTime);
  101. String currentDate = String(ptm->tm_year+1900) + "/" + String(ptm->tm_mon+1) + "/" + String(ptm->tm_mday);
  102. sendCommand("home.date.txt=\"" + currentDate + "\"");
  103. //Update day of week
  104. String weekDay = weekDays[ntpClient.getDay()];
  105. sendCommand("home.dow.txt=\"" + weekDay + "\"");
  106. //Update the backgrounds on all pages
  107. if (!isNight()){
  108. //Morning
  109. sendCommand("home.homebg.pic=1");
  110. sendCommand("forcast.forbg.pic=3");
  111. sendCommand("iot.iotbg.pic=5");
  112. }else{
  113. //Night
  114. sendCommand("home.homebg.pic=0");
  115. sendCommand("forcast.forbg.pic=2");
  116. sendCommand("iot.iotbg.pic=4");
  117. }
  118. }