From e4b36deac8b5ef7ab58830acd62e9caa81c7523b Mon Sep 17 00:00:00 2001 From: Warwick New Date: Thu, 25 Jun 2026 15:16:12 +0100 Subject: [PATCH] Eat don't eat watch now actually works --- src/c/main.c | 52 +++++++++++++++++++++++++++++++++++++++++----------- src/c/main.h | 8 +++++--- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/c/main.c b/src/c/main.c index 4d0418c..632f227 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -1,5 +1,4 @@ #include "main.h" -#include "message_keys.auto.h" #include static Window *s_main_window; @@ -11,7 +10,7 @@ static TextLayer *s_eat_layer; static ClaySettings settings; // Initialize the default settings -static void prv_default_settings() { +static void default_settings() { settings.BackgroundColor = GColorBlack; settings.ForegroundColor = GColorWhite; settings.FastBegin = "12:00"; @@ -19,19 +18,20 @@ static void prv_default_settings() { } // Read settings from persistent storage -static void prv_load_settings() { +static void load_settings() { // Load the default settings - prv_default_settings(); + default_settings(); // Read settings from persistent storage, if they exist persist_read_data(SETTINGS_KEY, &settings, sizeof(settings)); } // Save the settings to persistent storage -static void prv_save_settings() { +static void save_settings() { persist_write_data(SETTINGS_KEY, &settings, sizeof(settings)); // Update the display based on new settings update_time(); } + // Handle the response from AppMessage static void inbox_recieved_callback(DictionaryIterator *iter, void *context) { // Background Color @@ -59,13 +59,13 @@ static void inbox_recieved_callback(DictionaryIterator *iter, void *context) { } // Save the new settings to persistent storage - prv_save_settings(); + save_settings(); } static void update_time() { // Get a tm structure - time_t temp = time(NULL); - struct tm *tick_time = localtime(&temp); + time_t now = time(NULL); + struct tm *tick_time = localtime(&now); // Write the current hours and minutes into a buffer static char s_time_buffer[8]; @@ -82,9 +82,17 @@ static void update_time() { // Display the date text_layer_set_text(s_date_layer, s_date_buffer); - // TODO: compare the time and change accordingly static const char eatmsg[] = "Eat"; - text_layer_set_text(s_eat_layer, eatmsg); + static const char donteatmsg[] = "Don't Eat"; + + time_t fast_start = parse_time_str(settings.FastBegin); + time_t fast_end = parse_time_str(settings.FastEnd); + + if (now >= fast_start && now < fast_end) { + text_layer_set_text(s_eat_layer, eatmsg); + } else { + text_layer_set_text(s_eat_layer, donteatmsg); + } } static void tick_handler(struct tm *tick_time, TimeUnits units_changed) { @@ -136,7 +144,7 @@ static void main_window_unload(Window *window) { } static void init() { - prv_load_settings(); + load_settings(); app_message_register_inbox_received(inbox_recieved_callback); app_message_open(128, 128); @@ -160,3 +168,25 @@ int main(void) { app_event_loop(); deinit(); } + +static time_t parse_time_str(const char *time_string) { + if (time_string[2] != ':') + return (time_t)-1; + + int h = (time_string[0] - '0') * 10 + (time_string[1] - '0'); + int m = (time_string[3] - '0') * 10 + (time_string[4] - '0'); + + if (h < 0 || h > 23) + return (time_t)-1; + if (m < 0 || m > 59) + return (time_t)-1; + + time_t now = time(NULL); + tm *time = localtime(&now); + + time->tm_hour = h; + time->tm_min = m; + time->tm_sec = 0; + + return mktime(time); +} diff --git a/src/c/main.h b/src/c/main.h index c28adbb..8abc3b5 100644 --- a/src/c/main.h +++ b/src/c/main.h @@ -11,9 +11,11 @@ typedef struct ClaySettings { char *FastEnd; } __attribute__((__packed__)) ClaySettings; -static void prv_default_settings(); -static void prv_load_settings(); -static void prv_save_settings(); +static void default_settings(); +static void load_settings(); +static void save_settings(); + +static time_t parse_time_str(const char *time_string); int main(void); static void inbox_recieved_callback(DictionaryIterator *iter, void *context);