sensor-watch/movement/movement.c

219 lines
8.2 KiB
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "watch.h"
2021-10-17 00:58:14 +08:00
#include "movement.h"
#include "movement_config.h"
2021-10-17 00:58:14 +08:00
LauncherState movement_state;
void * widget_contexts[MOVEMENT_NUM_WIDGETS];
const int32_t movement_screensaver_deadlines[8] = {INT_MAX, 3600, 7200, 21600, 43200, 86400, 172800, 604800};
2021-10-04 12:37:58 +08:00
LauncherEvent event;
void cb_mode_btn_interrupt();
void cb_light_btn_interrupt();
void cb_alarm_btn_interrupt();
void cb_alarm_btn_extwake();
void cb_alarm_fired();
void cb_tick();
2021-10-17 00:58:14 +08:00
static inline void _movement_reset_screensaver_countdown() {
// for testing, make the timeout happen 60x faster.
2021-10-17 00:58:14 +08:00
movement_state.screensaver_ticks = movement_screensaver_deadlines[movement_state.movement_settings.bit.screensaver_interval] / 60;
}
2021-10-17 00:58:14 +08:00
void movement_request_tick_frequency(uint8_t freq) {
2021-10-04 00:31:51 +08:00
watch_rtc_disable_all_periodic_callbacks();
2021-10-17 00:58:14 +08:00
movement_state.subsecond = 0;
movement_state.tick_frequency = freq;
2021-10-04 00:31:51 +08:00
watch_rtc_register_periodic_callback(cb_tick, freq);
}
2021-10-17 00:58:14 +08:00
void movement_illuminate_led() {
movement_state.light_ticks = 3;
2021-10-04 00:31:51 +08:00
}
2021-10-17 00:58:14 +08:00
void movement_move_to_widget(uint8_t widget_index) {
movement_state.widget_changed = true;
movement_state.next_widget = widget_index;
}
2021-10-17 00:58:14 +08:00
void movement_move_to_next_widget() {
movement_move_to_widget((movement_state.current_widget + 1) % MOVEMENT_NUM_WIDGETS);
}
2021-10-04 00:31:51 +08:00
void app_init() {
2021-10-17 00:58:14 +08:00
memset(&movement_state, 0, sizeof(movement_state));
2021-10-17 00:58:14 +08:00
movement_state.movement_settings.bit.led_green_color = 0xF;
movement_state.movement_settings.bit.button_should_sound = true;
movement_state.movement_settings.bit.screensaver_interval = 1;
_movement_reset_screensaver_countdown();
}
void app_wake_from_deep_sleep() {
// This app does not support deep sleep mode.
}
void app_setup() {
static bool is_first_launch = true;
if (is_first_launch) {
2021-10-17 00:58:14 +08:00
for(uint8_t i = 0; i < MOVEMENT_NUM_WIDGETS; i++) {
widget_contexts[i] = NULL;
is_first_launch = false;
}
}
2021-10-17 00:58:14 +08:00
if (movement_state.screensaver_ticks != -1) {
watch_disable_extwake_interrupt(BTN_ALARM);
2021-10-04 11:39:52 +08:00
watch_rtc_disable_alarm_callback();
watch_enable_external_interrupts();
watch_register_interrupt_callback(BTN_MODE, cb_mode_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
watch_register_interrupt_callback(BTN_LIGHT, cb_light_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
watch_register_interrupt_callback(BTN_ALARM, cb_alarm_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
watch_enable_buzzer();
watch_enable_leds();
watch_enable_display();
2021-10-17 00:58:14 +08:00
movement_request_tick_frequency(1);
2021-10-17 00:58:14 +08:00
for(uint8_t i = 0; i < MOVEMENT_NUM_WIDGETS; i++) {
widgets[i].setup(&movement_state.movement_settings, &widget_contexts[i]);
}
2021-10-17 00:58:14 +08:00
widgets[0].activate(&movement_state.movement_settings, widget_contexts[0]);
2021-10-06 22:25:28 +08:00
event.subsecond = 0;
event.event_type = EVENT_ACTIVATE;
}
}
void app_prepare_for_sleep() {
}
void app_wake_from_sleep() {
}
bool app_loop() {
2021-10-17 00:58:14 +08:00
if (movement_state.widget_changed) {
if (movement_state.movement_settings.bit.button_should_sound) {
2021-10-04 21:51:49 +08:00
// low note for nonzero case, high note for return to widget 0
2021-10-17 00:58:14 +08:00
watch_buzzer_play_note(movement_state.next_widget ? BUZZER_NOTE_C7 : BUZZER_NOTE_C8, 50);
2021-10-04 07:16:14 +08:00
}
2021-10-17 00:58:14 +08:00
widgets[movement_state.current_widget].resign(&movement_state.movement_settings, widget_contexts[movement_state.current_widget]);
movement_state.current_widget = movement_state.next_widget;
2021-10-04 21:51:49 +08:00
watch_clear_display();
2021-10-17 00:58:14 +08:00
widgets[movement_state.current_widget].activate(&movement_state.movement_settings, widget_contexts[movement_state.current_widget]);
2021-10-06 22:25:28 +08:00
event.subsecond = 0;
event.event_type = EVENT_ACTIVATE;
2021-10-17 00:58:14 +08:00
movement_state.widget_changed = false;
}
// If the LED is off and should be on, turn it on
2021-10-17 00:58:14 +08:00
if (movement_state.light_ticks > 0 && !movement_state.led_on) {
watch_set_led_color(movement_state.movement_settings.bit.led_red_color ? (0xF | movement_state.movement_settings.bit.led_red_color << 4) : 0,
movement_state.movement_settings.bit.led_green_color ? (0xF | movement_state.movement_settings.bit.led_green_color << 4) : 0);
movement_state.led_on = true;
}
// if the LED is on and should be off, turn it off
2021-10-17 00:58:14 +08:00
if (movement_state.led_on && movement_state.light_ticks == 0) {
// unless the user is holding down the LIGHT button, in which case, give them more time.
if (watch_get_pin_level(BTN_LIGHT)) {
2021-10-17 00:58:14 +08:00
movement_state.light_ticks = 3;
} else {
watch_set_led_off();
2021-10-17 00:58:14 +08:00
movement_state.led_on = false;
}
}
// if we have timed out of our screensaver countdown, enter screensaver mode.
2021-10-17 00:58:14 +08:00
if (movement_state.screensaver_ticks == 0) {
movement_state.screensaver_ticks = -1;
watch_date_time alarm_time;
alarm_time.reg = 0;
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);
2021-10-06 22:25:28 +08:00
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.
2021-10-17 00:58:14 +08:00
while (movement_state.screensaver_ticks == -1) {
2021-10-06 22:25:28 +08:00
event.event_type = EVENT_SCREENSAVER;
2021-10-17 00:58:14 +08:00
widgets[movement_state.current_widget].loop(event, &movement_state.movement_settings, widget_contexts[movement_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.
2021-10-06 22:25:28 +08:00
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();
}
static bool can_sleep = true;
2021-10-04 21:51:49 +08:00
2021-10-06 22:25:28 +08:00
if (event.event_type) {
2021-10-17 00:58:14 +08:00
event.subsecond = movement_state.subsecond;
can_sleep = widgets[movement_state.current_widget].loop(event, &movement_state.movement_settings, widget_contexts[movement_state.current_widget]);
2021-10-06 22:25:28 +08:00
event.event_type = EVENT_NONE;
event.subsecond = 0;
}
2021-10-17 00:58:14 +08:00
return can_sleep && !movement_state.led_on;
}
2021-10-04 12:37:58 +08:00
LauncherEventType _figure_out_button_event(LauncherEventType button_down_event, uint8_t *down_timestamp) {
watch_date_time date_time = watch_rtc_get_date_time();
if (*down_timestamp) {
uint8_t diff = ((61 + date_time.unit.second) - *down_timestamp) % 60;
*down_timestamp = 0;
if (diff > 1) return button_down_event + 2;
else return button_down_event + 1;
} else {
*down_timestamp = date_time.unit.second + 1;
return button_down_event;
}
}
void cb_light_btn_interrupt() {
2021-10-17 00:58:14 +08:00
_movement_reset_screensaver_countdown();
event.event_type = _figure_out_button_event(EVENT_LIGHT_BUTTON_DOWN, &movement_state.light_down_timestamp);
}
void cb_mode_btn_interrupt() {
2021-10-17 00:58:14 +08:00
_movement_reset_screensaver_countdown();
event.event_type = _figure_out_button_event(EVENT_MODE_BUTTON_DOWN, &movement_state.mode_down_timestamp);
}
void cb_alarm_btn_interrupt() {
2021-10-17 00:58:14 +08:00
_movement_reset_screensaver_countdown();
event.event_type = _figure_out_button_event(EVENT_ALARM_BUTTON_DOWN, &movement_state.alarm_down_timestamp);
}
void cb_alarm_btn_extwake() {
// wake up!
2021-10-17 00:58:14 +08:00
_movement_reset_screensaver_countdown();
}
void cb_alarm_fired() {
2021-10-06 22:25:28 +08:00
event.event_type = EVENT_SCREENSAVER;
}
void cb_tick() {
2021-10-06 22:25:28 +08:00
event.event_type = EVENT_TICK;
watch_date_time date_time = watch_rtc_get_date_time();
2021-10-17 00:58:14 +08:00
if (date_time.unit.second != movement_state.last_second) {
if (movement_state.light_ticks) movement_state.light_ticks--;
if (movement_state.movement_settings.bit.screensaver_interval && movement_state.screensaver_ticks > 0) movement_state.screensaver_ticks--;
2021-10-17 00:58:14 +08:00
movement_state.last_second = date_time.unit.second;
movement_state.subsecond = 0;
} else {
2021-10-17 00:58:14 +08:00
movement_state.subsecond++;
}
}