2021-09-15 06:52:15 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "watch.h"
|
|
|
|
|
2022-01-26 04:03:22 +08:00
|
|
|
const int8_t UTC_OFFSET = 4; // set to your current UTC offset to see correct beats time
|
2021-10-01 04:32:30 +08:00
|
|
|
const uint8_t BEAT_REFRESH_FREQUENCY = 8;
|
2021-09-15 06:52:15 +08:00
|
|
|
|
|
|
|
typedef enum ApplicationMode {
|
|
|
|
MODE_CLOCK = 0, // Displays month, day and current time.
|
|
|
|
MODE_BEATS,
|
|
|
|
MODE_SET, // (ST) Set time and date
|
|
|
|
NUM_MODES // Last item in the enum, it's the number of cases.
|
|
|
|
} ApplicationMode;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct ApplicationState {
|
|
|
|
// Internal application state
|
|
|
|
ApplicationMode mode; // Current mode
|
|
|
|
bool mode_changed; // Lets us perform one-time setup for a given mode
|
|
|
|
uint16_t mode_ticks; // Timeout for the mode (returns to clock after timeout expires)
|
|
|
|
uint8_t light_ticks; // Timeout for the light
|
|
|
|
bool led_on; // Indicates that the LED is on
|
|
|
|
uint8_t page; // Tracks the current page in log, prefs or settings.
|
2021-10-01 04:32:30 +08:00
|
|
|
uint8_t last_second; // lets us see when the second changed, for subsecond timing
|
|
|
|
uint8_t subsecond; // a value from 0 to (BEAT_REFRESH_FREQUENCY - 1) indicating the fractional second
|
2021-09-15 06:52:15 +08:00
|
|
|
} ApplicationState;
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void do_clock_mode(void);
|
|
|
|
void do_beats_mode(void);
|
|
|
|
void do_set_time_mode(void);
|
|
|
|
void set_time_mode_handle_primary_button(void);
|
|
|
|
void set_time_mode_handle_secondary_button(void);
|
2021-09-15 06:52:15 +08:00
|
|
|
|
2021-10-01 04:32:30 +08:00
|
|
|
float clock2beats(uint16_t, uint16_t, uint16_t, int16_t);
|
2021-09-15 06:52:15 +08:00
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_light_pressed(void);
|
|
|
|
void cb_mode_pressed(void);
|
|
|
|
void cb_alarm_pressed(void);
|
|
|
|
void cb_tick(void);
|
|
|
|
void cb_fast_tick(void);
|
2021-09-15 06:52:15 +08:00
|
|
|
|
|
|
|
ApplicationState application_state;
|
|
|
|
char buf[16] = {0};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Zeroes out the application state struct.
|
|
|
|
*/
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_init(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
memset(&application_state, 0, sizeof(application_state));
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_wake_from_backup(void) {
|
2021-10-21 01:45:22 +08:00
|
|
|
// This app does not support BACKUP mode.
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_setup(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
watch_enable_external_interrupts();
|
|
|
|
watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING);
|
|
|
|
watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_RISING);
|
|
|
|
watch_register_extwake_callback(BTN_ALARM, cb_alarm_pressed, true);
|
|
|
|
|
|
|
|
watch_enable_buzzer();
|
|
|
|
watch_enable_leds();
|
|
|
|
watch_enable_display();
|
|
|
|
|
2021-09-29 07:53:29 +08:00
|
|
|
watch_rtc_register_tick_callback(cb_tick);
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_prepare_for_standby(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void app_wake_from_standby(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
static void update_tick_frequency(void) {
|
2021-10-01 04:32:30 +08:00
|
|
|
watch_rtc_disable_all_periodic_callbacks();
|
|
|
|
if (application_state.mode == MODE_BEATS) {
|
|
|
|
watch_rtc_register_periodic_callback(cb_fast_tick, BEAT_REFRESH_FREQUENCY);
|
|
|
|
} else {
|
|
|
|
watch_rtc_register_tick_callback(cb_tick);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
bool app_loop(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
// play a beep if the mode has changed in response to a user's press of the MODE button
|
|
|
|
if (application_state.mode_changed) {
|
|
|
|
// low note for nonzero case, high note for return to clock
|
|
|
|
watch_buzzer_play_note(application_state.mode ? BUZZER_NOTE_C7 : BUZZER_NOTE_C8, 100);
|
2021-10-01 04:32:30 +08:00
|
|
|
update_tick_frequency();
|
2021-09-15 06:52:15 +08:00
|
|
|
application_state.mode_changed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the user is not in clock mode and the mode timeout has expired, return them to clock mode
|
|
|
|
if (application_state.mode != MODE_CLOCK && application_state.mode_ticks == 0) {
|
|
|
|
application_state.mode = MODE_CLOCK;
|
|
|
|
application_state.mode_changed = true;
|
2021-10-01 04:32:30 +08:00
|
|
|
update_tick_frequency();
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the LED is off and should be on, turn it on
|
|
|
|
if (application_state.light_ticks > 0 && !application_state.led_on) {
|
|
|
|
watch_set_led_green();
|
|
|
|
application_state.led_on = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the LED is on and should be off, turn it off
|
|
|
|
if (application_state.led_on && application_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)) {
|
|
|
|
application_state.light_ticks = 3;
|
|
|
|
} else {
|
|
|
|
watch_set_led_off();
|
|
|
|
application_state.led_on = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (application_state.mode) {
|
|
|
|
case MODE_CLOCK:
|
|
|
|
do_clock_mode();
|
|
|
|
break;
|
|
|
|
case MODE_BEATS:
|
|
|
|
do_beats_mode();
|
|
|
|
break;
|
|
|
|
case MODE_SET:
|
|
|
|
do_set_time_mode();
|
|
|
|
break;
|
|
|
|
case NUM_MODES:
|
|
|
|
// dummy case, just silences a warning
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
application_state.mode_changed = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void do_clock_mode(void) {
|
2021-09-29 07:53:29 +08:00
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
2021-09-15 06:52:15 +08:00
|
|
|
const char months[12][3] = {"JA", "FE", "MR", "AR", "MA", "JN", "JL", "AU", "SE", "OC", "NO", "dE"};
|
|
|
|
|
2021-09-29 07:53:29 +08:00
|
|
|
watch_display_string((char *)months[date_time.unit.month - 1], 0);
|
|
|
|
sprintf(buf, "%2d%2d%02d%02d", date_time.unit.day, date_time.unit.hour, date_time.unit.minute, date_time.unit.second);
|
2021-09-15 06:52:15 +08:00
|
|
|
watch_display_string(buf, 2);
|
|
|
|
watch_set_colon();
|
2021-09-29 07:53:29 +08:00
|
|
|
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void do_beats_mode(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
watch_clear_colon();
|
|
|
|
|
2021-09-29 07:53:29 +08:00
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
2021-10-01 04:32:30 +08:00
|
|
|
float beats = clock2beats(date_time.unit.hour, date_time.unit.minute, date_time.unit.second, UTC_OFFSET);
|
|
|
|
sprintf(buf, "bt %6.0f", beats * 100);
|
2021-09-15 06:52:15 +08:00
|
|
|
|
|
|
|
watch_display_string(buf, 0);
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:32:30 +08:00
|
|
|
float clock2beats(uint16_t hours, uint16_t minutes, uint16_t seconds, int16_t utc_offset) {
|
|
|
|
float beats = seconds + ((float)application_state.subsecond / (float)BEAT_REFRESH_FREQUENCY);
|
2021-09-15 06:52:15 +08:00
|
|
|
beats += 60 * minutes;
|
2021-10-01 04:32:30 +08:00
|
|
|
beats += (float)hours * 60 * 60;
|
2022-01-26 04:03:22 +08:00
|
|
|
beats += (1 - utc_offset) * 60 * 60; // offset from utc + 1 since beats in in UTC+1
|
2021-09-15 06:52:15 +08:00
|
|
|
|
|
|
|
beats /= 86.4; // convert to beats
|
2021-10-01 04:32:30 +08:00
|
|
|
while(beats > 1000) beats -= 1000; // beats %= 1000 but for a float
|
2021-09-15 06:52:15 +08:00
|
|
|
|
2021-10-01 04:32:30 +08:00
|
|
|
return beats;
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void do_set_time_mode(void) {
|
2021-09-29 07:53:29 +08:00
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
2021-09-15 06:52:15 +08:00
|
|
|
|
|
|
|
watch_display_string(" ", 0);
|
|
|
|
switch (application_state.page) {
|
|
|
|
case 0: // hour
|
2021-09-29 07:53:29 +08:00
|
|
|
sprintf(buf, "ST t%2d", date_time.unit.hour);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 1: // minute
|
2021-09-29 07:53:29 +08:00
|
|
|
sprintf(buf, "ST t %02d", date_time.unit.minute);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 2: // second
|
2021-09-29 07:53:29 +08:00
|
|
|
sprintf(buf, "ST t %02d", date_time.unit.second);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 3: // year
|
2021-09-29 07:53:29 +08:00
|
|
|
sprintf(buf, "ST d%2d", date_time.unit.year + 20);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 4: // month
|
2021-09-29 07:53:29 +08:00
|
|
|
sprintf(buf, "ST d %02d", date_time.unit.month);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 5: // day
|
2021-09-29 07:53:29 +08:00
|
|
|
sprintf(buf, "ST d %02d", date_time.unit.day);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
watch_display_string(buf, 0);
|
|
|
|
watch_set_pixel(1, 12); // required for T in position 1
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void set_time_mode_handle_primary_button(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
application_state.page++;
|
|
|
|
if (application_state.page == 6) application_state.page = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void set_time_mode_handle_secondary_button(void) {
|
2021-09-29 07:53:29 +08:00
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
2021-09-15 06:52:15 +08:00
|
|
|
const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
|
|
|
|
|
|
|
|
switch (application_state.page) {
|
|
|
|
case 0: // hour
|
2021-09-29 07:53:29 +08:00
|
|
|
date_time.unit.hour = (date_time.unit.hour + 1) % 24;
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 1: // minute
|
2021-09-29 07:53:29 +08:00
|
|
|
date_time.unit.minute = (date_time.unit.minute + 1) % 60;
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 2: // second
|
2021-09-29 07:53:29 +08:00
|
|
|
date_time.unit.second = 0;
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 3: // year
|
|
|
|
// only allow 2021-2030. fix this sometime next decade
|
2021-09-29 07:53:29 +08:00
|
|
|
date_time.unit.year = ((date_time.unit.year % 10) + 1);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 4: // month
|
2021-09-29 07:53:29 +08:00
|
|
|
date_time.unit.month = ((date_time.unit.month + 1) % 12);
|
2021-09-15 06:52:15 +08:00
|
|
|
break;
|
|
|
|
case 5: // day
|
2021-09-29 07:53:29 +08:00
|
|
|
date_time.unit.day = date_time.unit.day + 1;
|
2021-09-15 06:52:15 +08:00
|
|
|
// can't set to the 29th on a leap year. if it's february 29, set to 11:59 on the 28th.
|
|
|
|
// and it should roll over.
|
2021-09-29 07:53:29 +08:00
|
|
|
if (date_time.unit.day > days_in_month[date_time.unit.month - 1]) {
|
|
|
|
date_time.unit.day = 1;
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-09-29 07:53:29 +08:00
|
|
|
watch_rtc_set_date_time(date_time);
|
2021-09-15 06:52:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_mode_pressed(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
application_state.mode = (application_state.mode + 1) % NUM_MODES;
|
|
|
|
application_state.mode_changed = true;
|
|
|
|
application_state.mode_ticks = 300;
|
|
|
|
application_state.page = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_light_pressed(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
switch (application_state.mode) {
|
|
|
|
case MODE_SET:
|
|
|
|
set_time_mode_handle_secondary_button();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
application_state.light_ticks = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_alarm_pressed(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
switch (application_state.mode) {
|
|
|
|
case MODE_SET:
|
|
|
|
set_time_mode_handle_primary_button();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_tick(void) {
|
2021-09-15 06:52:15 +08:00
|
|
|
if (application_state.light_ticks > 0) {
|
|
|
|
application_state.light_ticks--;
|
|
|
|
}
|
|
|
|
if (application_state.mode_ticks > 0) {
|
|
|
|
application_state.mode_ticks--;
|
|
|
|
}
|
|
|
|
}
|
2021-10-01 04:32:30 +08:00
|
|
|
|
2021-12-06 13:49:26 +08:00
|
|
|
void cb_fast_tick(void) {
|
2021-10-01 04:32:30 +08:00
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
|
|
|
if (date_time.unit.second != application_state.last_second) {
|
|
|
|
application_state.last_second = date_time.unit.second;
|
|
|
|
application_state.subsecond = 0;
|
|
|
|
} else {
|
|
|
|
application_state.subsecond++;
|
|
|
|
}
|
|
|
|
}
|