launcher: clean up structs

This commit is contained in:
Joey Castillo 2021-10-06 10:25:28 -04:00
parent 12ee922135
commit 3516f4295e
6 changed files with 31 additions and 34 deletions

View file

@ -84,8 +84,8 @@ void app_setup() {
}
widgets[0].activate(&launcher_state.launcher_settings, widget_contexts[0]);
event.value = 0;
event.bit.event_type = EVENT_ACTIVATE;
event.subsecond = 0;
event.event_type = EVENT_ACTIVATE;
}
}
@ -105,8 +105,8 @@ bool app_loop() {
launcher_state.current_widget = launcher_state.next_widget;
watch_clear_display();
widgets[launcher_state.current_widget].activate(&launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
event.value = 0;
event.bit.event_type = EVENT_ACTIVATE;
event.subsecond = 0;
event.event_type = EVENT_ACTIVATE;
launcher_state.widget_changed = false;
}
@ -137,17 +137,18 @@ bool app_loop() {
alarm_time.unit.second = 59; // after a match, the alarm fires at the next rising edge of CLK_RTC_CNT, so 59 seconds lets us update at :00
watch_rtc_register_alarm_callback(cb_alarm_fired, alarm_time, ALARM_MATCH_SS);
watch_register_extwake_callback(BTN_ALARM, cb_alarm_btn_extwake, true);
event.value = 0;
event.event_type = EVENT_NONE;
event.subsecond = 0;
// this is a little mini-runloop.
// as long as screensaver_ticks is -1 (i.e. screensaver is active), we wake up here, update the screen, and go right back to sleep.
while (launcher_state.screensaver_ticks == -1) {
event.bit.event_type = EVENT_SCREENSAVER;
event.event_type = EVENT_SCREENSAVER;
widgets[launcher_state.current_widget].loop(event, &launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
watch_enter_shallow_sleep(true);
}
// as soon as screensaver_ticks is reset by the extwake handler, we bail out of the loop and reactivate ourselves.
event.bit.event_type = EVENT_ACTIVATE;
event.event_type = EVENT_ACTIVATE;
// this is a hack tho: waking from shallow sleep, app_setup does get called, but it happens before we have reset our ticks.
// need to figure out if there's a better heuristic for determining how we woke up.
app_setup();
@ -155,10 +156,11 @@ bool app_loop() {
static bool can_sleep = true;
if (event.bit.event_type) {
event.bit.subsecond = launcher_state.subsecond;
if (event.event_type) {
event.subsecond = launcher_state.subsecond;
can_sleep = widgets[launcher_state.current_widget].loop(event, &launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
event.value = 0;
event.event_type = EVENT_NONE;
event.subsecond = 0;
}
return can_sleep && !launcher_state.led_on;
@ -179,17 +181,17 @@ LauncherEventType _figure_out_button_event(LauncherEventType button_down_event,
void cb_light_btn_interrupt() {
_launcher_reset_screensaver_countdown();
event.bit.event_type = _figure_out_button_event(EVENT_LIGHT_BUTTON_DOWN, &launcher_state.light_down_timestamp);
event.event_type = _figure_out_button_event(EVENT_LIGHT_BUTTON_DOWN, &launcher_state.light_down_timestamp);
}
void cb_mode_btn_interrupt() {
_launcher_reset_screensaver_countdown();
event.bit.event_type = _figure_out_button_event(EVENT_MODE_BUTTON_DOWN, &launcher_state.mode_down_timestamp);
event.event_type = _figure_out_button_event(EVENT_MODE_BUTTON_DOWN, &launcher_state.mode_down_timestamp);
}
void cb_alarm_btn_interrupt() {
_launcher_reset_screensaver_countdown();
event.bit.event_type = _figure_out_button_event(EVENT_ALARM_BUTTON_DOWN, &launcher_state.alarm_down_timestamp);
event.event_type = _figure_out_button_event(EVENT_ALARM_BUTTON_DOWN, &launcher_state.alarm_down_timestamp);
}
void cb_alarm_btn_extwake() {
@ -198,11 +200,11 @@ void cb_alarm_btn_extwake() {
}
void cb_alarm_fired() {
event.bit.event_type = EVENT_SCREENSAVER;
event.event_type = EVENT_SCREENSAVER;
}
void cb_tick() {
event.bit.event_type = EVENT_TICK;
event.event_type = EVENT_TICK;
watch_date_time date_time = watch_rtc_get_date_time();
if (date_time.unit.second != launcher_state.last_second) {
if (launcher_state.light_ticks) launcher_state.light_ticks--;

View file

@ -37,29 +37,24 @@ typedef enum {
EVENT_ALARM_LONG_PRESS, // The alarm button was held for >2 seconds, and released.
} LauncherEventType;
typedef union {
struct {
uint32_t event_type : 8;
uint32_t subsecond : 8;
uint32_t reserved : 16;
} bit;
uint32_t value;
typedef struct {
uint8_t event_type;
uint8_t subsecond;
} LauncherEvent;
typedef void (*launcher_widget_setup)(LauncherSettings *settings, void ** context_ptr);
typedef void (*launcher_widget_activate)(LauncherSettings *settings, void *context);
typedef bool (*launcher_widget_loop)(LauncherEvent event, LauncherSettings *settings, void *context);
typedef void (*launcher_widget_resign)(LauncherSettings *settings, void *context);
typedef struct WatchWidget {
typedef struct {
launcher_widget_setup setup;
launcher_widget_activate activate;
launcher_widget_loop loop;
launcher_widget_resign resign;
} WatchWidget;
typedef struct LauncherState {
typedef struct {
// properties stored in BACKUP register
LauncherSettings launcher_settings;

View file

@ -25,7 +25,7 @@ bool simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, v
watch_date_time date_time;
uint32_t previous_date_time;
switch (event.bit.event_type) {
switch (event.event_type) {
case EVENT_ACTIVATE:
case EVENT_TICK:
case EVENT_SCREENSAVER:
@ -33,11 +33,11 @@ bool simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, v
previous_date_time = *((uint32_t *)context);
*((uint32_t *)context) = date_time.reg;
if (date_time.reg >> 6 == previous_date_time >> 6 && event.bit.event_type != EVENT_SCREENSAVER) {
if (date_time.reg >> 6 == previous_date_time >> 6 && event.event_type != EVENT_SCREENSAVER) {
// everything before seconds is the same, don't waste cycles setting those segments.
pos = 8;
sprintf(buf, "%02d", date_time.unit.second);
} else if (date_time.reg >> 12 == previous_date_time >> 12 && event.bit.event_type != EVENT_SCREENSAVER) {
} else if (date_time.reg >> 12 == previous_date_time >> 12 && event.event_type != EVENT_SCREENSAVER) {
// everything before minutes is the same.
pos = 6;
sprintf(buf, "%02d%02d", date_time.unit.minute, date_time.unit.second);
@ -54,7 +54,7 @@ bool simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, v
if (date_time.unit.hour == 0) date_time.unit.hour = 12;
}
pos = 0;
if (event.bit.event_type == EVENT_SCREENSAVER) {
if (event.event_type == EVENT_SCREENSAVER) {
sprintf(buf, "%s%2d%2d%02d ", weekdays[simple_clock_widget_get_weekday(date_time.unit.year, date_time.unit.month, date_time.unit.day)], date_time.unit.day, date_time.unit.hour, date_time.unit.minute);
} else {
sprintf(buf, "%s%2d%2d%02d%02d", weekdays[simple_clock_widget_get_weekday(date_time.unit.year, date_time.unit.month, date_time.unit.day)], date_time.unit.day, date_time.unit.hour, date_time.unit.minute, date_time.unit.second);

View file

@ -20,7 +20,7 @@ bool pulseometer_widget_loop(LauncherEvent event, LauncherSettings *settings, vo
char buf[14];
// starts at index 15
const uint8_t pulse_lookup[] = {240, 225, 212, 200, 189, 180, 171, 164, 157, 150, 144, 138, 133, 129, 124, 120, 116, 113, 109, 106, 103, 100, 97, 95, 92, 90, 88, 86, 84, 82, 80, 78, 77, 75, 73, 72, 71, 69, 68, 67, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 55, 54, 53, 52, 51, 51, 50, 49, 49, 48, 47, 47, 46, 46, 45, 44, 44, 43, 43, 42, 42, 41, 41, 40, 40, 40};
switch (event.bit.event_type) {
switch (event.event_type) {
case EVENT_TICK:
if (pulsometer_state->pulse == 0 && !pulsometer_state->measuring) {
switch (pulsometer_state->ticks % 5) {

View file

@ -19,7 +19,7 @@ void preferences_widget_activate(LauncherSettings *settings, void *context) {
bool preferences_widget_loop(LauncherEvent event, LauncherSettings *settings, void *context) {
printf("preferences_widget_loop\n");
uint8_t current_page = *((uint8_t *)context);
switch (event.bit.event_type) {
switch (event.event_type) {
case EVENT_MODE_BUTTON_UP:
watch_set_led_off();
launcher_move_to_next_widget();
@ -53,7 +53,7 @@ bool preferences_widget_loop(LauncherEvent event, LauncherSettings *settings, vo
watch_display_string((char *)preferences_widget_titles[current_page], 0);
if (event.bit.subsecond % 2) return current_page <= 2;
if (event.subsecond % 2) return current_page <= 2;
char buf[3];
switch (current_page) {
case 0:

View file

@ -21,7 +21,7 @@ bool set_time_widget_loop(LauncherEvent event, LauncherSettings *settings, void
const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
watch_date_time date_time = watch_rtc_get_date_time();
switch (event.bit.event_type) {
switch (event.event_type) {
case EVENT_MODE_BUTTON_UP:
launcher_move_to_next_widget();
return false;
@ -79,7 +79,7 @@ bool set_time_widget_loop(LauncherEvent event, LauncherSettings *settings, void
watch_clear_indicator(WATCH_INDICATOR_PM);
sprintf(buf, "%s %2d%02d%02d", set_time_widget_titles[current_page], date_time.unit.year + 20, date_time.unit.month, date_time.unit.day);
}
if (event.bit.subsecond % 2) {
if (event.subsecond % 2) {
switch (current_page) {
case 0:
case 3: