Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern TFT_eSPI tft;
- extern WiFiUDP udp;
- extern int scheduled_program;
- extern int cur_program;
- class watch {
- private:
- // bullshit
- unsigned long last_update = 0;
- unsigned long last_millis = millis();
- unsigned long epoch = 0;
- unsigned long last_set_epoch = 0;
- float seconds_since_update = 0;
- String last_time;
- long update_frequency = 60000*60; // once every hour
- int hour_int = 0;
- int minute_int = 0;
- int display_status = 3;
- int failed_packets = 0;
- String get_time_string() {
- String text = "";
- if (hour_int < 10) {
- text += "0";
- }
- text += hour_int;
- text += ":";
- if (minute_int < 10) {
- text += "0";
- }
- text += minute_int;
- return text;
- }
- // wifi bullshit
- void get_time() {
- unsigned long time_epoch = epoch;
- unsigned long timeout = millis()+10000;
- bool finished = false;
- while (!finished && timeout > millis()) {
- // send an NTP packet to a time server
- WiFi.hostByName(time_server_name, time_server_ip);
- send_ntp_packet(time_server_ip);
- delay(1000);
- int cb = udp.parsePacket();
- if (cb) {
- udp.read(packetBuffer, ntp_packet_size); // read the packet into the buffer
- //the timestamp starts at byte 40 of the received packet and is four bytes,
- // or two words, long. First, esxtract the two words:
- unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
- unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
- unsigned long secsSince1900 = highWord << 16 | lowWord;
- // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
- int addHrs = 1;
- const unsigned long seventyYears = 2208988800UL - (addHrs*3600); // 2208981600UL - 1 hour: CUL;
- // settings:
- // let's say time is 00:29
- // we have 2208985200UL
- // remove 3600 seconds ( 1 hour ) to advance time
- // or do +1 to revert
- time_epoch = secsSince1900 - seventyYears;
- if (time_epoch) {
- finished = true;
- }
- }
- }
- if (timeout < millis()) {
- failed_packets += 1;
- }
- last_set_epoch = time_epoch;
- }
- public:
- void setup() {
- tft.fillScreen(TFT_BLACK);
- tft.setCursor(0, 0);
- tft.println("...");
- // no wifi status
- long timeout = millis()+(10*1000);
- while (WiFi.status() != WL_CONNECTED && timeout > millis()) {
- delay(1000);
- }
- if (WiFi.status() != WL_CONNECTED) {
- tft.println("no wifi");
- tft.println("reset...");
- WiFi.begin(ssid, pass);
- delay(1000);
- cur_program = 0;
- scheduled_program = 1;
- return;
- }
- // force first update, must be connected to wifi
- tft.setRotation(1);
- get_time();
- }
- void loop() {
- if (WiFi.status() == WL_CONNECTED) {
- display_status = 1;
- } else {
- display_status = 0;
- }
- float millis_delta = millis()-last_millis;
- last_millis = millis();
- seconds_since_update += millis_delta/1000;
- epoch = last_set_epoch + int(seconds_since_update);
- hour_int = ((epoch % 86400) / 3600); // print the hour (86400 equals secs per day)
- minute_int = ((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
- // updating the time
- if ((millis() >= last_update+update_frequency) && (display_status == 1)) {
- last_update = millis();
- get_time();
- seconds_since_update = 0;
- }
- if (get_time_string() != last_time) {
- // reset display
- tft.fillScreen(TFT_BLACK);
- // draw main text
- tft.setTextColor(TFT_WHITE);
- tft.setTextFont(7);
- tft.setTextSize(3);
- tft.setCursor(33, 80);
- tft.print(get_time_string());
- last_time = get_time_string();
- }
- // misc stuff
- tft.fillRect(30, 50, 100, 20, TFT_BLACK);
- tft.setCursor(33, 50);
- tft.setTextSize(1);
- tft.setTextFont(1);
- tft.setTextColor(TFT_GREEN);
- // ticker
- if ((sin(seconds_since_update*2) > 0) == 0) {
- tft.print("[ tick ] ");
- } else {
- tft.print("[ ] ");
- }
- // wifi warning ticker
- if (display_status == 0) {
- tft.setTextColor(TFT_RED);
- if ((sin(seconds_since_update*5) > 0) == 0) {
- tft.print("[ wifi ] ");
- } else {
- tft.print("[ ] ");
- }
- }
- // failed packet warning ticker
- tft.setTextColor(TFT_YELLOW);
- tft.print("[ !");
- tft.print(failed_packets);
- tft.print(" ]");
- delay(200);
- }
- };
Add Comment
Please, Sign In to add comment