theblackshibe

Untitled

Mar 22nd, 2021
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. extern TFT_eSPI tft;
  2. extern WiFiUDP udp;
  3.  
  4. extern int scheduled_program;
  5. extern int cur_program;
  6.  
  7. class watch {
  8.  
  9. private:
  10. // bullshit
  11. unsigned long last_update = 0;
  12. unsigned long last_millis = millis();
  13. unsigned long epoch = 0;
  14. unsigned long last_set_epoch = 0;
  15. float seconds_since_update = 0;
  16.  
  17. String last_time;
  18. long update_frequency = 60000*60; // once every hour
  19. int hour_int = 0;
  20. int minute_int = 0;
  21. int display_status = 3;
  22. int failed_packets = 0;
  23.  
  24. String get_time_string() {
  25.  
  26. String text = "";
  27. if (hour_int < 10) {
  28. text += "0";
  29. }
  30. text += hour_int;
  31. text += ":";
  32.  
  33. if (minute_int < 10) {
  34. text += "0";
  35. }
  36. text += minute_int;
  37.  
  38. return text;
  39. }
  40.  
  41. // wifi bullshit
  42. void get_time() {
  43.  
  44. unsigned long time_epoch = epoch;
  45. unsigned long timeout = millis()+10000;
  46. bool finished = false;
  47.  
  48. while (!finished && timeout > millis()) {
  49.  
  50. // send an NTP packet to a time server
  51. WiFi.hostByName(time_server_name, time_server_ip);
  52. send_ntp_packet(time_server_ip);
  53.  
  54. delay(1000);
  55.  
  56. int cb = udp.parsePacket();
  57. if (cb) {
  58. udp.read(packetBuffer, ntp_packet_size); // read the packet into the buffer
  59.  
  60. //the timestamp starts at byte 40 of the received packet and is four bytes,
  61. // or two words, long. First, esxtract the two words:
  62.  
  63. unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  64. unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  65. unsigned long secsSince1900 = highWord << 16 | lowWord;
  66.  
  67. // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  68. int addHrs = 1;
  69. const unsigned long seventyYears = 2208988800UL - (addHrs*3600); // 2208981600UL - 1 hour: CUL;
  70.  
  71. // settings:
  72. // let's say time is 00:29
  73. // we have 2208985200UL
  74. // remove 3600 seconds ( 1 hour ) to advance time
  75. // or do +1 to revert
  76.  
  77. time_epoch = secsSince1900 - seventyYears;
  78.  
  79. if (time_epoch) {
  80. finished = true;
  81. }
  82. }
  83. }
  84.  
  85. if (timeout < millis()) {
  86. failed_packets += 1;
  87. }
  88.  
  89. last_set_epoch = time_epoch;
  90. }
  91.  
  92. public:
  93. void setup() {
  94.  
  95. tft.fillScreen(TFT_BLACK);
  96. tft.setCursor(0, 0);
  97. tft.println("...");
  98.  
  99. // no wifi status
  100. long timeout = millis()+(10*1000);
  101. while (WiFi.status() != WL_CONNECTED && timeout > millis()) {
  102. delay(1000);
  103. }
  104.  
  105. if (WiFi.status() != WL_CONNECTED) {
  106. tft.println("no wifi");
  107. tft.println("reset...");
  108.  
  109. WiFi.begin(ssid, pass);
  110. delay(1000);
  111.  
  112. cur_program = 0;
  113. scheduled_program = 1;
  114.  
  115. return;
  116. }
  117.  
  118. // force first update, must be connected to wifi
  119. tft.setRotation(1);
  120. get_time();
  121. }
  122.  
  123. void loop() {
  124.  
  125. if (WiFi.status() == WL_CONNECTED) {
  126. display_status = 1;
  127. } else {
  128. display_status = 0;
  129. }
  130.  
  131. float millis_delta = millis()-last_millis;
  132. last_millis = millis();
  133.  
  134. seconds_since_update += millis_delta/1000;
  135. epoch = last_set_epoch + int(seconds_since_update);
  136.  
  137. hour_int = ((epoch % 86400) / 3600); // print the hour (86400 equals secs per day)
  138. minute_int = ((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
  139.  
  140. // updating the time
  141. if ((millis() >= last_update+update_frequency) && (display_status == 1)) {
  142.  
  143. last_update = millis();
  144. get_time();
  145. seconds_since_update = 0;
  146. }
  147.  
  148. if (get_time_string() != last_time) {
  149. // reset display
  150. tft.fillScreen(TFT_BLACK);
  151.  
  152. // draw main text
  153. tft.setTextColor(TFT_WHITE);
  154. tft.setTextFont(7);
  155. tft.setTextSize(3);
  156. tft.setCursor(33, 80);
  157. tft.print(get_time_string());
  158.  
  159. last_time = get_time_string();
  160. }
  161.  
  162. // misc stuff
  163. tft.fillRect(30, 50, 100, 20, TFT_BLACK);
  164.  
  165. tft.setCursor(33, 50);
  166. tft.setTextSize(1);
  167. tft.setTextFont(1);
  168. tft.setTextColor(TFT_GREEN);
  169.  
  170. // ticker
  171. if ((sin(seconds_since_update*2) > 0) == 0) {
  172. tft.print("[ tick ] ");
  173. } else {
  174. tft.print("[ ] ");
  175. }
  176.  
  177. // wifi warning ticker
  178. if (display_status == 0) {
  179. tft.setTextColor(TFT_RED);
  180. if ((sin(seconds_since_update*5) > 0) == 0) {
  181. tft.print("[ wifi ] ");
  182. } else {
  183. tft.print("[ ] ");
  184. }
  185. }
  186.  
  187. // failed packet warning ticker
  188. tft.setTextColor(TFT_YELLOW);
  189. tft.print("[ !");
  190. tft.print(failed_packets);
  191. tft.print(" ]");
  192.  
  193. delay(200);
  194. }
  195. };
  196.  
  197.  
Add Comment
Please, Sign In to add comment