]> git.earman.xyz Git - sensor-watch.git/commitdiff
have close enough to update less and clean up code
authorRuben Nic <RubenSandwich@users.noreply.github.com>
Wed, 15 May 2024 16:30:00 +0000 (12:30 -0400)
committerRuben Nic <RubenSandwich@users.noreply.github.com>
Wed, 15 May 2024 16:30:00 +0000 (12:30 -0400)
movement/watch_faces/clock/close_enough_clock_face.c
movement/watch_faces/clock/close_enough_clock_face.h

index 58db37a10961bb9838ecfe9e68d875cbaef1494f..19bde697ccfd063581266507172170a19b77ae30 100644 (file)
 
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 #include "close_enough_clock_face.h"
 #include "watch.h"
 #include "watch_utility.h"
-#include "watch_private_display.h"
-
-static const char first_words[12][3] = {
-    "  ", // "HH  OC",
-    " 5", // " 5 past HH",
-    "10", // "10 past HH",
-    "15", // "15 past HH",
-    "20", // "20 past HH",
-    "25", // "25 past HH",
-    "30", // "30 past HH",
-    "35", // "35 past HH",
-    "40", // "20 two HH+1",
-    "15", // "15 two HH+1",
-    "10", // "10 two HH+1",
-    " 5"  // " 5 two HH+1"
-};
 
-static const char second_words[12][3] = {
-    "OC", // "HH  OC",
-    " P", // " 5 past HH",
-    " P", // "10 past HH",
-    " P", // "15 past HH",
-    " P", // "20 past HH",
-    " P", // "25 past HH",
-    " P", // "30 past HH",
-    " P", // "35 past HH",
-    " 2", // "20 two HH+1",
-    " 2", // "15 two HH+1",
-    " 2", // "10 two HH+1",
-    " 2"  // " 5 two HH+1"
+const char *words[12][2] = {
+    {"  ", "OC"}, // "HH  OC",
+    {" 5", " P"}, // " 5 past HH",
+    {"10", " P"}, // "10 past HH",
+    {"15", " P"}, // "15 past HH",
+    {"20", " P"}, // "20 past HH",
+    {"25", " P"}, // "25 past HH",
+    {"30", " P"}, // "30 past HH",
+    {"35", " P"}, // "35 past HH",
+    {"40", " P"}, // "40 past HH",
+    {"15", " 2"}, // "15 two HH+1",
+    {"10", " 2"}, // "10 two HH+1",
+    {" 5", " 2"}, // " 5 two HH+1",
 };
 
-static const int hour_switch_index = 8;
+static const int hour_switch_index = 9;
 
 static void _update_alarm_indicator(bool settings_alarm_enabled, close_enough_clock_state_t *state) {
     state->alarm_enabled = settings_alarm_enabled;
@@ -95,20 +80,26 @@ void close_enough_clock_face_activate(movement_settings_t *settings, void *conte
 
     // this ensures that none of the five_minute_periods will match, so we always rerender when the face activates
     state->prev_five_minute_period = -1;
+    state->prev_min_checked = -1;
 }
 
 bool close_enough_clock_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
     close_enough_clock_state_t *state = (close_enough_clock_state_t *)context;
-    char buf[11];
 
+    char buf[11];
     watch_date_time date_time;
+    bool show_next_hour = false;
     int prev_five_minute_period;
+    int prev_min_checked;
+    int close_enough_hour;
+
     switch (event.event_type) {
         case EVENT_ACTIVATE:
         case EVENT_TICK:
         case EVENT_LOW_ENERGY_UPDATE:
             date_time = watch_rtc_get_date_time();
             prev_five_minute_period = state->prev_five_minute_period;
+            prev_min_checked = state->prev_min_checked;
 
             // check the battery voltage once a day...
             if (date_time.unit.day != state->last_battery_check) {
@@ -126,27 +117,51 @@ bool close_enough_clock_face_loop(movement_event_t event, movement_settings_t *s
                 watch_set_indicator(WATCH_INDICATOR_LAP);
             }
 
+            // same minute, skip update
+            if (date_time.unit.minute == prev_min_checked) {
+                break;
+            } else {
+                state->prev_min_checked = date_time.unit.minute;
+            }
+
             int five_minute_period = (date_time.unit.minute / 5) % 12;
 
+            // If we are 60% to the next 5 interval, move up to the next period
+            if (fmodf(date_time.unit.minute / 5.0f, 1.0f) > 0.5f) {
+                // If we are on the last 5 interval and moving to the next period we need to display the next hour because we are wrapping around
+                if (five_minute_period == 11) {
+                    show_next_hour = true;
+                }
+
+                five_minute_period = (five_minute_period + 1) % 12;
+            }
+
             // same five_minute_period, skip update
             if (five_minute_period == prev_five_minute_period) {
                 break;
             }
 
-            // move from "x mins past y" to "x mins to y+1"
-            if (five_minute_period >= hour_switch_index) {
-                date_time.unit.hour += 1;
-                date_time.unit.hour %= 24;
+            // we don't want to modify date_time.unit.hour just in case other watch faces use it
+            close_enough_hour = date_time.unit.hour;
+
+            // move from "MM(mins) P HH" to "MM(mins) 2 HH+1"
+            if (five_minute_period >= hour_switch_index || show_next_hour) {
+                close_enough_hour = (close_enough_hour + 1) % 24;
             }
 
             if (!settings->bit.clock_mode_24h) {
                 // if we are in 12 hour mode, do some cleanup.
-                if (date_time.unit.hour < 12) {
+                if (close_enough_hour < 12) {
                     watch_clear_indicator(WATCH_INDICATOR_PM);
                 } else {
                     watch_set_indicator(WATCH_INDICATOR_PM);
                 }
 
+                close_enough_hour %= 12;
+                if (close_enough_hour == 0) {
+                    close_enough_hour = 12;
+                }
+
                 date_time.unit.hour %= 12;
                 if (date_time.unit.hour == 0) {
                     date_time.unit.hour = 12;
@@ -156,14 +171,14 @@ bool close_enough_clock_face_loop(movement_event_t event, movement_settings_t *s
             char first_word[3];
             char second_word[3];
             char third_word[3];
-            if (five_minute_period == 0) {
-                sprintf(first_word, "%2d", date_time.unit.hour);
-                strncpy(second_word, first_words[five_minute_period], 3);
-                strncpy(third_word, second_words[five_minute_period], 3);
+            if (five_minute_period == 0) { // "HH  OC",
+                sprintf(first_word, "%2d", close_enough_hour);
+                strncpy(second_word, words[five_minute_period][0], 3);
+                strncpy(third_word, words[five_minute_period][1], 3);
             } else {
-                strncpy(first_word, first_words[five_minute_period], 3);
-                strncpy(second_word, second_words[five_minute_period], 3);
-                sprintf(third_word, "%2d", date_time.unit.hour);
+                strncpy(first_word, words[five_minute_period][0], 3);
+                strncpy(second_word, words[five_minute_period][1], 3);
+                sprintf(third_word, "%2d", close_enough_hour);
             }
 
             sprintf(
index 341357be97a4b2a5bb578320e1e34840a99674b5..736a6c66a0f9d6cf3dfeedffca349585f5973947 100644 (file)
@@ -40,6 +40,7 @@
 
 typedef struct {
     int prev_five_minute_period;
+    int prev_min_checked;
     uint8_t last_battery_check;
     bool battery_low;
     bool alarm_enabled;