update existing apps to use new RTC API

This commit is contained in:
Joey Castillo 2021-09-28 19:53:29 -04:00
parent 6334af0136
commit 1163041624
2 changed files with 49 additions and 73 deletions

View File

@ -21,13 +21,6 @@ void app_wake_from_deep_sleep() {
} }
void app_setup() { void app_setup() {
struct calendar_date_time date_time;
watch_get_date_time(&date_time);
if (date_time.date.year < 2020) {
date_time.date.year = 2020;
watch_set_date_time(date_time);
}
watch_enable_external_interrupts(); watch_enable_external_interrupts();
watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING); 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_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_RISING);
@ -64,7 +57,7 @@ void app_setup() {
watch_enable_display(); watch_enable_display();
watch_register_tick_callback(cb_tick); watch_rtc_register_tick_callback(cb_tick);
} }
/** /**
@ -191,9 +184,8 @@ float read_humidity(int32_t t_fine) {
} }
void log_data() { void log_data() {
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
watch_get_date_time(&date_time); uint8_t hour = date_time.unit.hour;
uint8_t hour = date_time.time.hour;
int8_t temperature = read_temperature(NULL); int8_t temperature = read_temperature(NULL);
for(int i = 0; i < MAX_DATA_POINTS - 1; i++) { for(int i = 0; i < MAX_DATA_POINTS - 1; i++) {
@ -205,12 +197,11 @@ void log_data() {
} }
void do_clock_mode() { void do_clock_mode() {
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
const char months[12][3] = {"JA", "FE", "MR", "AR", "MA", "JN", "JL", "AU", "SE", "OC", "NO", "dE"}; const char months[12][3] = {"JA", "FE", "MR", "AR", "MA", "JN", "JL", "AU", "SE", "OC", "NO", "dE"};
watch_get_date_time(&date_time); watch_display_string((char *)months[date_time.unit.month - 1], 0);
watch_display_string((char *)months[date_time.date.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);
sprintf(buf, "%2d%2d%02d%02d", date_time.date.day, date_time.time.hour, date_time.time.min, date_time.time.sec);
watch_display_string(buf, 2); watch_display_string(buf, 2);
watch_set_colon(); watch_set_colon();
} }
@ -271,28 +262,27 @@ void prefs_mode_handle_secondary_button() {
} }
void do_set_time_mode() { void do_set_time_mode() {
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
watch_get_date_time(&date_time);
watch_display_string(" ", 0); watch_display_string(" ", 0);
switch (application_state.page) { switch (application_state.page) {
case 0: // hour case 0: // hour
sprintf(buf, "ST t%2d", date_time.time.hour); sprintf(buf, "ST t%2d", date_time.unit.hour);
break; break;
case 1: // minute case 1: // minute
sprintf(buf, "ST t %02d", date_time.time.min); sprintf(buf, "ST t %02d", date_time.unit.minute);
break; break;
case 2: // second case 2: // second
sprintf(buf, "ST t %02d", date_time.time.sec); sprintf(buf, "ST t %02d", date_time.unit.second);
break; break;
case 3: // year case 3: // year
sprintf(buf, "ST d%2d", date_time.date.year - 2000); sprintf(buf, "ST d%2d", date_time.unit.year + 20);
break; break;
case 4: // month case 4: // month
sprintf(buf, "ST d %02d", date_time.date.month); sprintf(buf, "ST d %02d", date_time.unit.month);
break; break;
case 5: // day case 5: // day
sprintf(buf, "ST d %02d", date_time.date.day); sprintf(buf, "ST d %02d", date_time.unit.day);
break; break;
} }
watch_display_string(buf, 0); watch_display_string(buf, 0);
@ -305,37 +295,36 @@ void set_time_mode_handle_primary_button() {
} }
void set_time_mode_handle_secondary_button() { void set_time_mode_handle_secondary_button() {
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
watch_get_date_time(&date_time);
const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31}; const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
switch (application_state.page) { switch (application_state.page) {
case 0: // hour case 0: // hour
date_time.time.hour = (date_time.time.hour + 1) % 24; date_time.unit.hour = (date_time.unit.hour + 1) % 24;
break; break;
case 1: // minute case 1: // minute
date_time.time.min = (date_time.time.min + 1) % 60; date_time.unit.minute = (date_time.unit.minute + 1) % 60;
break; break;
case 2: // second case 2: // second
date_time.time.sec = 0; date_time.unit.second = 0;
break; break;
case 3: // year case 3: // year
// only allow 2021-2030. fix this sometime next decade // only allow 2021-2030. fix this sometime next decade
date_time.date.year = ((date_time.date.year % 10) + 1) + 2020; date_time.unit.year = ((date_time.unit.year % 10) + 1);
break; break;
case 4: // month case 4: // month
date_time.date.month = ((date_time.date.month + 1) % 12); date_time.unit.month = ((date_time.unit.month + 1) % 12);
break; break;
case 5: // day case 5: // day
date_time.date.day = date_time.date.day + 1; date_time.unit.day = date_time.unit.day + 1;
// can't set to the 29th on a leap year. if it's february 29, set to 11:59 on the 28th. // 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. // and it should roll over.
if (date_time.date.day > days_in_month[date_time.date.month - 1]) { if (date_time.unit.day > days_in_month[date_time.unit.month - 1]) {
date_time.date.day = 1; date_time.unit.day = 1;
} }
break; break;
} }
watch_set_date_time(date_time); watch_rtc_set_date_time(date_time);
} }
void cb_mode_pressed() { void cb_mode_pressed() {
@ -377,9 +366,8 @@ void cb_alarm_pressed() {
void cb_tick() { void cb_tick() {
// TODO: use alarm interrupt to trigger data acquisition. // TODO: use alarm interrupt to trigger data acquisition.
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
watch_get_date_time(&date_time); if (date_time.unit.minute == 0 && date_time.unit.second == 0) {
if (date_time.time.min == 0 && date_time.time.sec == 0) {
log_data(); log_data();
} }

View File

@ -51,13 +51,6 @@ void app_wake_from_deep_sleep() {
} }
void app_setup() { void app_setup() {
struct calendar_date_time date_time;
watch_get_date_time(&date_time);
if (date_time.date.year < 2020) {
date_time.date.year = 2020;
watch_set_date_time(date_time);
}
watch_enable_external_interrupts(); watch_enable_external_interrupts();
watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_RISING); 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_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_RISING);
@ -67,7 +60,7 @@ void app_setup() {
watch_enable_leds(); watch_enable_leds();
watch_enable_display(); watch_enable_display();
watch_register_tick_callback(cb_tick); watch_rtc_register_tick_callback(cb_tick);
} }
void app_prepare_for_sleep() { void app_prepare_for_sleep() {
@ -128,24 +121,21 @@ bool app_loop() {
} }
void do_clock_mode() { void do_clock_mode() {
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
const char months[12][3] = {"JA", "FE", "MR", "AR", "MA", "JN", "JL", "AU", "SE", "OC", "NO", "dE"}; const char months[12][3] = {"JA", "FE", "MR", "AR", "MA", "JN", "JL", "AU", "SE", "OC", "NO", "dE"};
watch_get_date_time(&date_time); watch_display_string((char *)months[date_time.unit.month - 1], 0);
watch_display_string((char *)months[date_time.date.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);
sprintf(buf, "%2d%2d%02d%02d", date_time.date.day, date_time.time.hour, date_time.time.min, date_time.time.sec);
watch_display_string(buf, 2); watch_display_string(buf, 2);
watch_set_colon(); watch_set_colon();
} }
void do_beats_mode() { void do_beats_mode() {
watch_clear_colon(); watch_clear_colon();
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
uint16_t beats = clock2beats(date_time.unit.hour, date_time.unit.minute, date_time.unit.second, UTC_OFFSET);
watch_get_date_time(&date_time);
uint16_t beats = clock2beats(date_time.time.hour, date_time.time.min, date_time.time.sec, UTC_OFFSET);
sprintf(buf, "bt %04d ", beats); sprintf(buf, "bt %04d ", beats);
watch_display_string(buf, 0); watch_display_string(buf, 0);
@ -164,28 +154,27 @@ uint16_t clock2beats(uint16_t hours, uint16_t minutes, uint16_t seconds, int16_t
} }
void do_set_time_mode() { void do_set_time_mode() {
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
watch_get_date_time(&date_time);
watch_display_string(" ", 0); watch_display_string(" ", 0);
switch (application_state.page) { switch (application_state.page) {
case 0: // hour case 0: // hour
sprintf(buf, "ST t%2d", date_time.time.hour); sprintf(buf, "ST t%2d", date_time.unit.hour);
break; break;
case 1: // minute case 1: // minute
sprintf(buf, "ST t %02d", date_time.time.min); sprintf(buf, "ST t %02d", date_time.unit.minute);
break; break;
case 2: // second case 2: // second
sprintf(buf, "ST t %02d", date_time.time.sec); sprintf(buf, "ST t %02d", date_time.unit.second);
break; break;
case 3: // year case 3: // year
sprintf(buf, "ST d%2d", date_time.date.year - 2000); sprintf(buf, "ST d%2d", date_time.unit.year + 20);
break; break;
case 4: // month case 4: // month
sprintf(buf, "ST d %02d", date_time.date.month); sprintf(buf, "ST d %02d", date_time.unit.month);
break; break;
case 5: // day case 5: // day
sprintf(buf, "ST d %02d", date_time.date.day); sprintf(buf, "ST d %02d", date_time.unit.day);
break; break;
} }
watch_display_string(buf, 0); watch_display_string(buf, 0);
@ -198,37 +187,36 @@ void set_time_mode_handle_primary_button() {
} }
void set_time_mode_handle_secondary_button() { void set_time_mode_handle_secondary_button() {
struct calendar_date_time date_time; watch_date_time date_time = watch_rtc_get_date_time();
watch_get_date_time(&date_time);
const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31}; const uint8_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 30, 31, 30, 31, 30, 31};
switch (application_state.page) { switch (application_state.page) {
case 0: // hour case 0: // hour
date_time.time.hour = (date_time.time.hour + 1) % 24; date_time.unit.hour = (date_time.unit.hour + 1) % 24;
break; break;
case 1: // minute case 1: // minute
date_time.time.min = (date_time.time.min + 1) % 60; date_time.unit.minute = (date_time.unit.minute + 1) % 60;
break; break;
case 2: // second case 2: // second
date_time.time.sec = 0; date_time.unit.second = 0;
break; break;
case 3: // year case 3: // year
// only allow 2021-2030. fix this sometime next decade // only allow 2021-2030. fix this sometime next decade
date_time.date.year = ((date_time.date.year % 10) + 1) + 2020; date_time.unit.year = ((date_time.unit.year % 10) + 1);
break; break;
case 4: // month case 4: // month
date_time.date.month = ((date_time.date.month + 1) % 12); date_time.unit.month = ((date_time.unit.month + 1) % 12);
break; break;
case 5: // day case 5: // day
date_time.date.day = date_time.date.day + 1; date_time.unit.day = date_time.unit.day + 1;
// can't set to the 29th on a leap year. if it's february 29, set to 11:59 on the 28th. // 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. // and it should roll over.
if (date_time.date.day > days_in_month[date_time.date.month - 1]) { if (date_time.unit.day > days_in_month[date_time.unit.month - 1]) {
date_time.date.day = 1; date_time.unit.day = 1;
} }
break; break;
} }
watch_set_date_time(date_time); watch_rtc_set_date_time(date_time);
} }
void cb_mode_pressed() { void cb_mode_pressed() {