Eat don't eat watch now actually works

This commit is contained in:
Warwick New 2026-06-25 15:16:12 +01:00
parent e6f714a657
commit e4b36deac8
2 changed files with 46 additions and 14 deletions

View file

@ -1,5 +1,4 @@
#include "main.h" #include "main.h"
#include "message_keys.auto.h"
#include <pebble.h> #include <pebble.h>
static Window *s_main_window; static Window *s_main_window;
@ -11,7 +10,7 @@ static TextLayer *s_eat_layer;
static ClaySettings settings; static ClaySettings settings;
// Initialize the default settings // Initialize the default settings
static void prv_default_settings() { static void default_settings() {
settings.BackgroundColor = GColorBlack; settings.BackgroundColor = GColorBlack;
settings.ForegroundColor = GColorWhite; settings.ForegroundColor = GColorWhite;
settings.FastBegin = "12:00"; settings.FastBegin = "12:00";
@ -19,19 +18,20 @@ static void prv_default_settings() {
} }
// Read settings from persistent storage // Read settings from persistent storage
static void prv_load_settings() { static void load_settings() {
// Load the default settings // Load the default settings
prv_default_settings(); default_settings();
// Read settings from persistent storage, if they exist // Read settings from persistent storage, if they exist
persist_read_data(SETTINGS_KEY, &settings, sizeof(settings)); persist_read_data(SETTINGS_KEY, &settings, sizeof(settings));
} }
// Save the settings to persistent storage // Save the settings to persistent storage
static void prv_save_settings() { static void save_settings() {
persist_write_data(SETTINGS_KEY, &settings, sizeof(settings)); persist_write_data(SETTINGS_KEY, &settings, sizeof(settings));
// Update the display based on new settings // Update the display based on new settings
update_time(); update_time();
} }
// Handle the response from AppMessage // Handle the response from AppMessage
static void inbox_recieved_callback(DictionaryIterator *iter, void *context) { static void inbox_recieved_callback(DictionaryIterator *iter, void *context) {
// Background Color // Background Color
@ -59,13 +59,13 @@ static void inbox_recieved_callback(DictionaryIterator *iter, void *context) {
} }
// Save the new settings to persistent storage // Save the new settings to persistent storage
prv_save_settings(); save_settings();
} }
static void update_time() { static void update_time() {
// Get a tm structure // Get a tm structure
time_t temp = time(NULL); time_t now = time(NULL);
struct tm *tick_time = localtime(&temp); struct tm *tick_time = localtime(&now);
// Write the current hours and minutes into a buffer // Write the current hours and minutes into a buffer
static char s_time_buffer[8]; static char s_time_buffer[8];
@ -82,9 +82,17 @@ static void update_time() {
// Display the date // Display the date
text_layer_set_text(s_date_layer, s_date_buffer); text_layer_set_text(s_date_layer, s_date_buffer);
// TODO: compare the time and change accordingly
static const char eatmsg[] = "Eat"; static const char eatmsg[] = "Eat";
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); 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) { 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() { static void init() {
prv_load_settings(); load_settings();
app_message_register_inbox_received(inbox_recieved_callback); app_message_register_inbox_received(inbox_recieved_callback);
app_message_open(128, 128); app_message_open(128, 128);
@ -160,3 +168,25 @@ int main(void) {
app_event_loop(); app_event_loop();
deinit(); 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);
}

View file

@ -11,9 +11,11 @@ typedef struct ClaySettings {
char *FastEnd; char *FastEnd;
} __attribute__((__packed__)) ClaySettings; } __attribute__((__packed__)) ClaySettings;
static void prv_default_settings(); static void default_settings();
static void prv_load_settings(); static void load_settings();
static void prv_save_settings(); static void save_settings();
static time_t parse_time_str(const char *time_string);
int main(void); int main(void);
static void inbox_recieved_callback(DictionaryIterator *iter, void *context); static void inbox_recieved_callback(DictionaryIterator *iter, void *context);