2021-09-28 13:06:37 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2021-10-04 08:37:15 +08:00
|
|
|
#include <limits.h>
|
2021-09-28 13:06:37 +08:00
|
|
|
#include "watch.h"
|
2021-10-17 00:58:14 +08:00
|
|
|
#include "movement.h"
|
|
|
|
#include "movement_config.h"
|
2021-09-28 13:06:37 +08:00
|
|
|
|
2021-10-17 01:23:23 +08:00
|
|
|
movement_state_t movement_state;
|
2021-10-17 01:14:52 +08:00
|
|
|
void * watch_face_contexts[MOVEMENT_NUM_FACES];
|
2021-12-21 02:35:37 +08:00
|
|
|
watch_date_time scheduled_tasks[MOVEMENT_NUM_FACES];
|
2021-10-19 00:15:57 +08:00
|
|
|
const int32_t movement_le_inactivity_deadlines[8] = {INT_MAX, 3600, 7200, 21600, 43200, 86400, 172800, 604800};
|
2021-11-29 06:17:51 +08:00
|
|
|
const int16_t movement_timeout_inactivity_deadlines[4] = {60, 120, 300, 1800};
|
2021-10-17 01:23:23 +08:00
|
|
|
movement_event_t event;
|
2021-09-28 13:06:37 +08:00
|
|
|
|
2021-11-25 23:06:49 +08:00
|
|
|
const int16_t movement_timezone_offsets[] = {
|
|
|
|
-720, // 0 : -12:00:00 (Baker Island Time)
|
|
|
|
-660, // 1 : -11:00:00 (Niue Time)
|
|
|
|
-600, // 2 : -10:00:00 (Hawaii-Aleutian Standard Time)
|
|
|
|
-570, // 3 : -9:30:00 (Marquesas Islands Time)
|
|
|
|
-540, // 4 : -9:00:00 (Alaska Standard Time)
|
|
|
|
-480, // 5 : -8:00:00 (Pacific Standard Time)
|
|
|
|
-420, // 6 : -7:00:00 (Mountain Standard Time)
|
|
|
|
-360, // 7 : -6:00:00 (Central Standard Time)
|
|
|
|
-300, // 8 : -5:00:00 (Eastern Standard Time)
|
|
|
|
-270, // 9 : -4:30:00 (Venezuelan Standard Time)
|
|
|
|
-240, // 10 : -4:00:00 (Atlantic Standard Time)
|
|
|
|
-210, // 11 : -3:30:00 (Newfoundland Standard Time)
|
|
|
|
-180, // 12 : -3:00:00 (Brasilia Time)
|
|
|
|
-150, // 13 : -2:30:00 (Newfoundland Daylight Time)
|
|
|
|
-120, // 14 : -2:00:00 (Fernando de Noronha Time)
|
|
|
|
-60, // 15 : -1:00:00 (Azores Standard Time)
|
|
|
|
0, // 16 : 0:00:00 (UTC)
|
|
|
|
60, // 17 : 1:00:00 (Central European Time)
|
|
|
|
120, // 18 : 2:00:00 (South African Standard Time)
|
|
|
|
180, // 19 : 3:00:00 (Arabia Standard Time)
|
|
|
|
210, // 20 : 3:30:00 (Iran Standard Time)
|
|
|
|
240, // 21 : 4:00:00 (Georgia Standard Time)
|
|
|
|
270, // 22 : 4:30:00 (Afghanistan Time)
|
|
|
|
300, // 23 : 5:00:00 (Pakistan Standard Time)
|
|
|
|
330, // 24 : 5:30:00 (Indian Standard Time)
|
|
|
|
345, // 25 : 5:45:00 (Nepal Time)
|
|
|
|
360, // 26 : 6:00:00 (Kyrgyzstan time)
|
|
|
|
390, // 27 : 6:30:00 (Myanmar Time)
|
|
|
|
420, // 28 : 7:00:00 (Thailand Standard Time)
|
|
|
|
480, // 29 : 8:00:00 (China Standard Time, Australian Western Standard Time)
|
|
|
|
525, // 30 : 8:45:00 (Australian Central Western Standard Time)
|
|
|
|
540, // 31 : 9:00:00 (Japan Standard Time, Korea Standard Time)
|
|
|
|
570, // 32 : 9:30:00 (Australian Central Standard Time)
|
|
|
|
600, // 33 : 10:00:00 (Australian Eastern Standard Time)
|
|
|
|
630, // 34 : 10:30:00 (Lord Howe Standard Time)
|
|
|
|
660, // 35 : 11:00:00 (Solomon Islands Time)
|
|
|
|
720, // 36 : 12:00:00 (New Zealand Standard Time)
|
|
|
|
765, // 37 : 12:45:00 (Chatham Standard Time)
|
|
|
|
780, // 38 : 13:00:00 (Tonga Time)
|
|
|
|
825, // 39 : 13:45:00 (Chatham Daylight Time)
|
|
|
|
840, // 40 : 14:00:00 (Line Islands Time)
|
|
|
|
};
|
|
|
|
|
2021-11-27 02:43:47 +08:00
|
|
|
const char movement_valid_position_0_chars[] = " AaBbCcDdEeFGgHhIiJKLMNnOoPQrSTtUuWXYZ-='+\\/0123456789";
|
|
|
|
const char movement_valid_position_1_chars[] = " ABCDEFHlJLNORTtUX-='01378";
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_mode_btn_interrupt(void);
|
|
|
|
void cb_light_btn_interrupt(void);
|
|
|
|
void cb_alarm_btn_interrupt(void);
|
|
|
|
void cb_alarm_btn_extwake(void);
|
|
|
|
void cb_alarm_fired(void);
|
|
|
|
void cb_fast_tick(void);
|
|
|
|
void cb_tick(void);
|
|
|
|
|
|
|
|
static inline void _movement_reset_inactivity_countdown(void) {
|
2021-10-20 01:33:11 +08:00
|
|
|
movement_state.le_mode_ticks = movement_le_inactivity_deadlines[movement_state.settings.bit.le_interval];
|
|
|
|
movement_state.timeout_ticks = movement_timeout_inactivity_deadlines[movement_state.settings.bit.to_interval];
|
2021-10-04 08:37:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
static inline void _movement_enable_fast_tick_if_needed(void) {
|
2021-11-29 07:15:21 +08:00
|
|
|
if (!movement_state.fast_tick_enabled) {
|
|
|
|
movement_state.fast_ticks = 0;
|
|
|
|
watch_rtc_register_periodic_callback(cb_fast_tick, 128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
static inline void _movement_disable_fast_tick_if_possible(void) {
|
2021-11-30 03:53:13 +08:00
|
|
|
if ((movement_state.light_ticks == -1) &&
|
|
|
|
(movement_state.alarm_ticks == -1) &&
|
|
|
|
((movement_state.light_down_timestamp + movement_state.mode_down_timestamp + movement_state.alarm_down_timestamp) == 0)) {
|
2021-11-29 07:15:21 +08:00
|
|
|
movement_state.fast_tick_enabled = false;
|
|
|
|
watch_rtc_disable_periodic_callback(128);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
static void _movement_handle_background_tasks(void) {
|
2021-10-24 05:55:19 +08:00
|
|
|
for(uint8_t i = 0; i < MOVEMENT_NUM_FACES; i++) {
|
|
|
|
// For each face, if the watch face wants a background task...
|
|
|
|
if (watch_faces[i].wants_background_task != NULL && watch_faces[i].wants_background_task(&movement_state.settings, watch_face_contexts[i])) {
|
|
|
|
// ...we give it one. pretty straightforward!
|
|
|
|
movement_event_t background_event = { EVENT_BACKGROUND_TASK, 0 };
|
|
|
|
watch_faces[i].loop(background_event, &movement_state.settings, watch_face_contexts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
movement_state.needs_background_tasks_handled = false;
|
|
|
|
}
|
|
|
|
|
2021-12-21 02:35:37 +08:00
|
|
|
static void _movement_handle_scheduled_tasks(void) {
|
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
|
|
|
uint8_t num_active_tasks = 0;
|
|
|
|
|
|
|
|
for(uint8_t i = 0; i < MOVEMENT_NUM_FACES; i++) {
|
|
|
|
if (scheduled_tasks[i].reg) {
|
|
|
|
if (scheduled_tasks[i].reg == date_time.reg) {
|
|
|
|
scheduled_tasks[i].reg = 0;
|
|
|
|
movement_event_t background_event = { EVENT_BACKGROUND_TASK, 0 };
|
|
|
|
watch_faces[i].loop(background_event, &movement_state.settings, watch_face_contexts[i]);
|
|
|
|
} else {
|
|
|
|
num_active_tasks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_active_tasks == 0) {
|
|
|
|
movement_state.has_scheduled_background_task = false;
|
|
|
|
} else {
|
|
|
|
_movement_reset_inactivity_countdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 00:58:14 +08:00
|
|
|
void movement_request_tick_frequency(uint8_t freq) {
|
2021-11-29 07:15:21 +08:00
|
|
|
if (freq == 128) return; // Movement uses the 128 Hz tick internally
|
|
|
|
RTC->MODE2.INTENCLR.reg = 0xFE; // disable all callbacks except the 128 Hz one
|
2021-10-17 00:58:14 +08:00
|
|
|
movement_state.subsecond = 0;
|
|
|
|
movement_state.tick_frequency = freq;
|
2021-12-04 00:30:36 +08:00
|
|
|
if (freq) watch_rtc_register_periodic_callback(cb_tick, freq);
|
2021-10-04 00:31:51 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void movement_illuminate_led(void) {
|
2021-11-29 07:15:21 +08:00
|
|
|
if (movement_state.settings.bit.led_duration) {
|
|
|
|
watch_set_led_color(movement_state.settings.bit.led_red_color ? (0xF | movement_state.settings.bit.led_red_color << 4) : 0,
|
|
|
|
movement_state.settings.bit.led_green_color ? (0xF | movement_state.settings.bit.led_green_color << 4) : 0);
|
|
|
|
movement_state.light_ticks = (movement_state.settings.bit.led_duration * 2 - 1) * 128;
|
|
|
|
_movement_enable_fast_tick_if_needed();
|
|
|
|
}
|
2021-10-04 00:31:51 +08:00
|
|
|
}
|
|
|
|
|
2021-10-17 01:14:52 +08:00
|
|
|
void movement_move_to_face(uint8_t watch_face_index) {
|
|
|
|
movement_state.watch_face_changed = true;
|
2021-10-17 01:28:52 +08:00
|
|
|
movement_state.next_watch_face = watch_face_index;
|
2021-10-04 02:26:17 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void movement_move_to_next_face(void) {
|
2021-10-17 01:14:52 +08:00
|
|
|
movement_move_to_face((movement_state.current_watch_face + 1) % MOVEMENT_NUM_FACES);
|
2021-10-04 02:26:17 +08:00
|
|
|
}
|
2021-10-04 00:31:51 +08:00
|
|
|
|
2021-12-21 02:35:37 +08:00
|
|
|
void movement_schedule_background_task(watch_date_time date_time) {
|
|
|
|
watch_date_time now = watch_rtc_get_date_time();
|
|
|
|
if (date_time.reg > now.reg) {
|
|
|
|
movement_state.has_scheduled_background_task = true;
|
|
|
|
scheduled_tasks[movement_state.current_watch_face].reg = date_time.reg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void movement_play_signal(void) {
|
2021-11-30 03:53:13 +08:00
|
|
|
watch_buzzer_play_note(BUZZER_NOTE_C8, 75);
|
|
|
|
watch_buzzer_play_note(BUZZER_NOTE_REST, 100);
|
|
|
|
watch_buzzer_play_note(BUZZER_NOTE_C8, 100);
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void movement_play_alarm(void) {
|
2021-11-30 03:53:13 +08:00
|
|
|
movement_state.alarm_ticks = 128 * 5 - 80; // 80 ticks short of 5 seconds, or 4.375 seconds (our beep is 0.375 seconds)
|
|
|
|
_movement_enable_fast_tick_if_needed();
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_init(void) {
|
2021-10-17 00:58:14 +08:00
|
|
|
memset(&movement_state, 0, sizeof(movement_state));
|
2021-10-04 08:37:15 +08:00
|
|
|
|
2021-10-17 01:28:52 +08:00
|
|
|
movement_state.settings.bit.led_green_color = 0xF;
|
|
|
|
movement_state.settings.bit.button_should_sound = true;
|
2021-10-20 01:33:11 +08:00
|
|
|
movement_state.settings.bit.le_interval = 1;
|
|
|
|
movement_state.settings.bit.led_duration = 1;
|
2021-11-25 23:06:49 +08:00
|
|
|
movement_state.settings.bit.time_zone = 16; // default to GMT
|
2021-11-29 07:15:21 +08:00
|
|
|
movement_state.light_ticks = -1;
|
2021-11-30 03:53:13 +08:00
|
|
|
movement_state.alarm_ticks = -1;
|
2021-10-17 01:40:17 +08:00
|
|
|
_movement_reset_inactivity_countdown();
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_wake_from_backup(void) {
|
2021-11-25 23:38:17 +08:00
|
|
|
movement_state.settings.reg = watch_get_backup_data(0);
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_setup(void) {
|
2021-11-25 23:38:17 +08:00
|
|
|
watch_store_backup_data(movement_state.settings.reg, 0);
|
|
|
|
|
2021-10-06 03:55:34 +08:00
|
|
|
static bool is_first_launch = true;
|
|
|
|
|
|
|
|
if (is_first_launch) {
|
2021-10-17 01:14:52 +08:00
|
|
|
for(uint8_t i = 0; i < MOVEMENT_NUM_FACES; i++) {
|
|
|
|
watch_face_contexts[i] = NULL;
|
2021-12-21 02:35:37 +08:00
|
|
|
scheduled_tasks[i].reg = 0;
|
2021-10-06 03:55:34 +08:00
|
|
|
is_first_launch = false;
|
|
|
|
}
|
2021-10-24 05:55:19 +08:00
|
|
|
|
|
|
|
// set up the 1 minute alarm (for background tasks and low power updates)
|
|
|
|
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);
|
2021-10-06 03:55:34 +08:00
|
|
|
}
|
2021-10-17 01:40:17 +08:00
|
|
|
if (movement_state.le_mode_ticks != -1) {
|
2021-10-04 08:37:15 +08:00
|
|
|
watch_disable_extwake_interrupt(BTN_ALARM);
|
2021-09-28 13:06:37 +08:00
|
|
|
|
2021-10-04 08:37:15 +08:00
|
|
|
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);
|
2021-09-28 13:06:37 +08:00
|
|
|
|
2021-10-04 08:37:15 +08:00
|
|
|
watch_enable_buzzer();
|
|
|
|
watch_enable_leds();
|
|
|
|
watch_enable_display();
|
2021-09-28 13:06:37 +08:00
|
|
|
|
2021-10-17 00:58:14 +08:00
|
|
|
movement_request_tick_frequency(1);
|
2021-10-04 08:37:15 +08:00
|
|
|
|
2021-10-17 01:14:52 +08:00
|
|
|
for(uint8_t i = 0; i < MOVEMENT_NUM_FACES; i++) {
|
2021-10-17 01:28:52 +08:00
|
|
|
watch_faces[i].setup(&movement_state.settings, &watch_face_contexts[i]);
|
2021-10-04 08:37:15 +08:00
|
|
|
}
|
2021-10-04 02:26:17 +08:00
|
|
|
|
2021-11-08 21:11:59 +08:00
|
|
|
watch_faces[movement_state.current_watch_face].activate(&movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
2021-10-06 22:25:28 +08:00
|
|
|
event.subsecond = 0;
|
|
|
|
event.event_type = EVENT_ACTIVATE;
|
2021-10-04 08:37:15 +08:00
|
|
|
}
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_prepare_for_standby(void) {
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_wake_from_standby(void) {
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
bool app_loop(void) {
|
2021-10-17 01:14:52 +08:00
|
|
|
if (movement_state.watch_face_changed) {
|
2021-10-17 01:28:52 +08:00
|
|
|
if (movement_state.settings.bit.button_should_sound) {
|
2021-10-17 01:14:52 +08:00
|
|
|
// low note for nonzero case, high note for return to watch_face 0
|
2021-10-17 01:28:52 +08:00
|
|
|
watch_buzzer_play_note(movement_state.next_watch_face ? BUZZER_NOTE_C7 : BUZZER_NOTE_C8, 50);
|
2021-10-04 07:16:14 +08:00
|
|
|
}
|
2021-10-17 01:28:52 +08:00
|
|
|
watch_faces[movement_state.current_watch_face].resign(&movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
|
|
|
movement_state.current_watch_face = movement_state.next_watch_face;
|
2021-10-04 21:51:49 +08:00
|
|
|
watch_clear_display();
|
2021-10-17 01:28:52 +08:00
|
|
|
watch_faces[movement_state.current_watch_face].activate(&movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
2021-10-06 22:25:28 +08:00
|
|
|
event.subsecond = 0;
|
|
|
|
event.event_type = EVENT_ACTIVATE;
|
2021-10-17 01:14:52 +08:00
|
|
|
movement_state.watch_face_changed = false;
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 07:15:21 +08:00
|
|
|
// if the LED should be off, turn it off
|
|
|
|
if (movement_state.light_ticks == 0) {
|
2021-09-28 13:06:37 +08:00
|
|
|
// unless the user is holding down the LIGHT button, in which case, give them more time.
|
|
|
|
if (watch_get_pin_level(BTN_LIGHT)) {
|
2021-11-29 07:15:21 +08:00
|
|
|
movement_state.light_ticks = 1;
|
2021-09-28 13:06:37 +08:00
|
|
|
} else {
|
|
|
|
watch_set_led_off();
|
2021-11-29 07:15:21 +08:00
|
|
|
movement_state.light_ticks = -1;
|
|
|
|
_movement_disable_fast_tick_if_possible();
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 05:55:19 +08:00
|
|
|
// handle background tasks, if the alarm handler told us we need to
|
|
|
|
if (movement_state.needs_background_tasks_handled) _movement_handle_background_tasks();
|
|
|
|
|
2021-12-21 02:35:37 +08:00
|
|
|
// if we have a scheduled background task, handle that here:
|
|
|
|
if (event.event_type == EVENT_TICK && movement_state.has_scheduled_background_task) _movement_handle_scheduled_tasks();
|
|
|
|
|
2021-10-17 01:40:17 +08:00
|
|
|
// if we have timed out of our low energy mode countdown, enter low energy mode.
|
|
|
|
if (movement_state.le_mode_ticks == 0) {
|
|
|
|
movement_state.le_mode_ticks = -1;
|
2021-10-04 08:37:15 +08:00
|
|
|
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;
|
2021-10-04 23:09:11 +08:00
|
|
|
|
|
|
|
// this is a little mini-runloop.
|
2021-10-17 01:40:17 +08:00
|
|
|
// as long as le_mode_ticks is -1 (i.e. we are in low energy mode), we wake up here, update the screen, and go right back to sleep.
|
|
|
|
while (movement_state.le_mode_ticks == -1) {
|
2021-10-24 05:55:19 +08:00
|
|
|
// we also have to handle background tasks here in the mini-runloop
|
|
|
|
if (movement_state.needs_background_tasks_handled) _movement_handle_background_tasks();
|
|
|
|
|
2021-10-17 04:03:27 +08:00
|
|
|
event.event_type = EVENT_LOW_ENERGY_UPDATE;
|
2021-10-17 01:28:52 +08:00
|
|
|
watch_faces[movement_state.current_watch_face].loop(event, &movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
2021-10-21 01:45:22 +08:00
|
|
|
watch_enter_sleep_mode();
|
2021-10-04 23:09:11 +08:00
|
|
|
}
|
2021-10-17 01:40:17 +08:00
|
|
|
// as soon as le_mode_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;
|
2021-10-21 01:45:22 +08:00
|
|
|
// this is a hack tho: waking from sleep mode, app_setup does get called, but it happens before we have reset our ticks.
|
2021-10-04 23:09:11 +08:00
|
|
|
// need to figure out if there's a better heuristic for determining how we woke up.
|
|
|
|
app_setup();
|
2021-10-04 08:37:15 +08:00
|
|
|
}
|
|
|
|
|
2021-10-04 23:57:57 +08:00
|
|
|
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;
|
2021-10-17 01:28:52 +08:00
|
|
|
can_sleep = watch_faces[movement_state.current_watch_face].loop(event, &movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
2021-10-06 22:25:28 +08:00
|
|
|
event.event_type = EVENT_NONE;
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-07 12:29:27 +08:00
|
|
|
// if we have timed out of our timeout countdown, give the app a hint that they can resign.
|
|
|
|
if (movement_state.timeout_ticks == 0) {
|
2021-11-30 03:04:24 +08:00
|
|
|
movement_state.timeout_ticks = -1;
|
2021-12-03 04:10:49 +08:00
|
|
|
if (movement_state.settings.bit.to_always == false) {
|
|
|
|
// if "timeout always" is false, give the current watch face a chance to exit gracefully...
|
|
|
|
event.event_type = EVENT_TIMEOUT;
|
|
|
|
}
|
2021-11-07 12:29:27 +08:00
|
|
|
event.subsecond = movement_state.subsecond;
|
|
|
|
watch_faces[movement_state.current_watch_face].loop(event, &movement_state.settings, watch_face_contexts[movement_state.current_watch_face]);
|
|
|
|
event.event_type = EVENT_NONE;
|
2021-12-03 04:10:49 +08:00
|
|
|
if (movement_state.settings.bit.to_always && movement_state.current_watch_face != 0) {
|
|
|
|
// ...but if the user has "timeout always" set, give it the boot.
|
|
|
|
movement_move_to_face(0);
|
|
|
|
}
|
2021-11-07 12:29:27 +08:00
|
|
|
}
|
|
|
|
|
2021-11-30 03:53:13 +08:00
|
|
|
// Now that we've handled all display update tasks, handle the alarm.
|
|
|
|
if (movement_state.alarm_ticks >= 0) {
|
|
|
|
uint8_t buzzer_phase = (movement_state.alarm_ticks + 80) % 128;
|
|
|
|
if(buzzer_phase == 127) {
|
|
|
|
for(uint8_t i = 0; i < 4; i++) {
|
|
|
|
// TODO: This method of playing the buzzer blocks the UI while it's beeping.
|
|
|
|
// It might be better to time it with the fast tick.
|
|
|
|
watch_buzzer_play_note(BUZZER_NOTE_C8, (i != 3) ? 50 : 75);
|
|
|
|
if (i != 3) watch_buzzer_play_note(BUZZER_NOTE_REST, 50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (movement_state.alarm_ticks == 0) {
|
|
|
|
movement_state.alarm_ticks = -1;
|
|
|
|
_movement_disable_fast_tick_if_possible();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 12:29:27 +08:00
|
|
|
event.subsecond = 0;
|
|
|
|
|
2021-11-30 23:23:20 +08:00
|
|
|
return can_sleep && (movement_state.light_ticks == -1) && !movement_state.is_buzzing;
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
static movement_event_type_t _figure_out_button_event(bool pin_level, movement_event_type_t button_down_event_type, uint8_t *down_timestamp) {
|
2021-11-30 03:53:13 +08:00
|
|
|
// force alarm off if the user pressed a button.
|
|
|
|
if (movement_state.alarm_ticks) movement_state.alarm_ticks = 0;
|
|
|
|
|
2021-11-29 07:15:21 +08:00
|
|
|
if (pin_level) {
|
|
|
|
// handle rising edge
|
|
|
|
_movement_enable_fast_tick_if_needed();
|
|
|
|
*down_timestamp = movement_state.fast_ticks + 1;
|
|
|
|
return button_down_event_type;
|
|
|
|
} else {
|
|
|
|
// this line is hack but it handles the situation where the light button was held for more than 10 seconds.
|
|
|
|
// fast tick is disabled by then, and the LED would get stuck on since there's no one left decrementing light_ticks.
|
|
|
|
if (movement_state.light_ticks == 1) movement_state.light_ticks = 0;
|
|
|
|
// now that that's out of the way, handle falling edge
|
|
|
|
uint16_t diff = movement_state.fast_ticks - *down_timestamp;
|
2021-10-04 02:26:17 +08:00
|
|
|
*down_timestamp = 0;
|
2021-11-29 07:15:21 +08:00
|
|
|
_movement_disable_fast_tick_if_possible();
|
|
|
|
// any press over a half second is considered a long press.
|
|
|
|
if (diff > 64) return button_down_event_type + 2;
|
2021-10-17 01:23:23 +08:00
|
|
|
else return button_down_event_type + 1;
|
2021-10-04 02:26:17 +08:00
|
|
|
}
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_light_btn_interrupt(void) {
|
2021-11-29 07:15:21 +08:00
|
|
|
bool pin_level = watch_get_pin_level(BTN_LIGHT);
|
2021-10-17 01:40:17 +08:00
|
|
|
_movement_reset_inactivity_countdown();
|
2021-11-29 07:15:21 +08:00
|
|
|
event.event_type = _figure_out_button_event(pin_level, EVENT_LIGHT_BUTTON_DOWN, &movement_state.light_down_timestamp);
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_mode_btn_interrupt(void) {
|
2021-11-29 07:15:21 +08:00
|
|
|
bool pin_level = watch_get_pin_level(BTN_MODE);
|
2021-10-17 01:40:17 +08:00
|
|
|
_movement_reset_inactivity_countdown();
|
2021-11-29 07:15:21 +08:00
|
|
|
event.event_type = _figure_out_button_event(pin_level, EVENT_MODE_BUTTON_DOWN, &movement_state.mode_down_timestamp);
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_alarm_btn_interrupt(void) {
|
2021-11-29 07:15:21 +08:00
|
|
|
bool pin_level = watch_get_pin_level(BTN_ALARM);
|
2021-10-17 01:40:17 +08:00
|
|
|
_movement_reset_inactivity_countdown();
|
2021-11-29 07:15:21 +08:00
|
|
|
event.event_type = _figure_out_button_event(pin_level, EVENT_ALARM_BUTTON_DOWN, &movement_state.alarm_down_timestamp);
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_alarm_btn_extwake(void) {
|
2021-10-04 23:09:11 +08:00
|
|
|
// wake up!
|
2021-10-17 01:40:17 +08:00
|
|
|
_movement_reset_inactivity_countdown();
|
2021-10-04 08:37:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_alarm_fired(void) {
|
2021-10-24 05:55:19 +08:00
|
|
|
movement_state.needs_background_tasks_handled = true;
|
2021-10-04 08:37:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_fast_tick(void) {
|
2021-11-29 07:15:21 +08:00
|
|
|
movement_state.fast_ticks++;
|
|
|
|
if (movement_state.light_ticks > 0) movement_state.light_ticks--;
|
2021-11-30 03:53:13 +08:00
|
|
|
if (movement_state.alarm_ticks > 0) movement_state.alarm_ticks--;
|
|
|
|
// this is just a fail-safe; fast tick should be disabled as soon as the button is up, the LED times out, and/or the alarm finishes.
|
2021-11-29 07:15:21 +08:00
|
|
|
// but if for whatever reason it isn't, this forces the fast tick off after 10 seconds.
|
|
|
|
if (movement_state.fast_ticks >= 1280) watch_rtc_disable_periodic_callback(128);
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_tick(void) {
|
2021-10-06 22:25:28 +08:00
|
|
|
event.event_type = EVENT_TICK;
|
2021-10-04 06:49:21 +08:00
|
|
|
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) {
|
2021-10-20 22:36:55 +08:00
|
|
|
// TODO: can we consolidate these two ticks?
|
2021-10-20 01:33:11 +08:00
|
|
|
if (movement_state.settings.bit.le_interval && movement_state.le_mode_ticks > 0) movement_state.le_mode_ticks--;
|
2021-10-19 00:15:57 +08:00
|
|
|
if (movement_state.timeout_ticks > 0) movement_state.timeout_ticks--;
|
2021-10-04 06:49:21 +08:00
|
|
|
|
2021-10-17 00:58:14 +08:00
|
|
|
movement_state.last_second = date_time.unit.second;
|
|
|
|
movement_state.subsecond = 0;
|
2021-10-04 06:49:21 +08:00
|
|
|
} else {
|
2021-10-17 00:58:14 +08:00
|
|
|
movement_state.subsecond++;
|
2021-10-04 06:49:21 +08:00
|
|
|
}
|
2021-09-28 13:06:37 +08:00
|
|
|
}
|