2024-02-25 05:27:40 +08:00
|
|
|
/* SPDX-License-Identifier: MIT */
|
|
|
|
|
2022-01-16 03:53:42 +08:00
|
|
|
/*
|
|
|
|
* MIT License
|
|
|
|
*
|
2024-02-25 05:27:40 +08:00
|
|
|
* Copyright © 2021-2023 Joey Castillo <joeycastillo@utexas.edu> <jose.castillo@gmail.com>
|
|
|
|
* Copyright © 2022 David Keck <davidskeck@users.noreply.github.com>
|
|
|
|
* Copyright © 2022 TheOnePerson <a.nebinger@web.de>
|
|
|
|
* Copyright © 2023 Jeremy O'Brien <neutral@fastmail.com>
|
|
|
|
* Copyright © 2023 Mikhail Svarichevsky <3@14.by>
|
|
|
|
* Copyright © 2023 Wesley Aptekar-Cassels <me@wesleyac.com>
|
|
|
|
* Copyright © 2024 Matheus Afonso Martins Moreira <matheus.a.m.moreira@gmail.com>
|
2022-01-16 03:53:42 +08:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-10-04 06:49:21 +08:00
|
|
|
#include <stdlib.h>
|
2024-02-25 05:19:12 +08:00
|
|
|
#include "clock_face.h"
|
2021-10-04 06:49:21 +08:00
|
|
|
#include "watch.h"
|
2021-10-24 04:13:11 +08:00
|
|
|
#include "watch_utility.h"
|
2023-01-11 05:56:26 +08:00
|
|
|
#include "watch_private_display.h"
|
2021-10-04 06:49:21 +08:00
|
|
|
|
2024-02-25 09:31:12 +08:00
|
|
|
// 2.2 volts will happen when the battery has maybe 5-10% remaining?
|
|
|
|
// we can refine this later.
|
|
|
|
#ifndef CLOCK_FACE_LOW_BATTERY_VOLTAGE_THRESHOLD
|
|
|
|
#define CLOCK_FACE_LOW_BATTERY_VOLTAGE_THRESHOLD 2200
|
|
|
|
#endif
|
|
|
|
|
2024-02-25 05:40:21 +08:00
|
|
|
typedef struct {
|
2024-02-26 01:06:19 +08:00
|
|
|
struct {
|
|
|
|
watch_date_time previous;
|
|
|
|
} date_time;
|
2024-02-25 05:40:21 +08:00
|
|
|
uint8_t last_battery_check;
|
|
|
|
uint8_t watch_face_index;
|
2024-02-25 06:44:52 +08:00
|
|
|
bool time_signal_enabled;
|
2024-02-25 05:40:21 +08:00
|
|
|
bool battery_low;
|
|
|
|
} clock_state_t;
|
|
|
|
|
2024-02-26 02:18:45 +08:00
|
|
|
static bool clock_is_in_24h_mode(movement_settings_t *settings) {
|
2024-07-10 19:22:55 +08:00
|
|
|
#ifdef CLOCK_FACE_24H_ONLY
|
|
|
|
return true;
|
|
|
|
#else
|
2024-02-26 02:18:45 +08:00
|
|
|
return settings->bit.clock_mode_24h;
|
2024-07-10 19:22:55 +08:00
|
|
|
#endif
|
2024-02-26 02:18:45 +08:00
|
|
|
}
|
|
|
|
|
2024-09-17 02:34:19 +08:00
|
|
|
static bool clock_should_set_leading_zero(movement_settings_t *settings) {
|
|
|
|
return clock_is_in_24h_mode(settings) && settings->bit.clock_24h_leading_zero;
|
|
|
|
}
|
|
|
|
|
2024-02-25 06:16:06 +08:00
|
|
|
static void clock_indicate(WatchIndicatorSegment indicator, bool on) {
|
|
|
|
if (on) {
|
|
|
|
watch_set_indicator(indicator);
|
|
|
|
} else {
|
|
|
|
watch_clear_indicator(indicator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 06:36:34 +08:00
|
|
|
static void clock_indicate_alarm(movement_settings_t *settings) {
|
2024-08-10 18:38:46 +08:00
|
|
|
clock_indicate(WATCH_INDICATOR_SIGNAL, settings->bit.alarm_enabled);
|
2022-10-26 10:57:53 +08:00
|
|
|
}
|
|
|
|
|
2024-02-25 06:44:52 +08:00
|
|
|
static void clock_indicate_time_signal(clock_state_t *clock) {
|
2024-08-10 18:38:46 +08:00
|
|
|
clock_indicate(WATCH_INDICATOR_BELL, clock->time_signal_enabled);
|
2024-02-25 06:44:52 +08:00
|
|
|
}
|
|
|
|
|
2024-02-25 06:52:51 +08:00
|
|
|
static void clock_indicate_24h(movement_settings_t *settings) {
|
2024-02-26 02:18:45 +08:00
|
|
|
clock_indicate(WATCH_INDICATOR_24H, clock_is_in_24h_mode(settings));
|
2024-02-25 06:52:51 +08:00
|
|
|
}
|
|
|
|
|
2024-02-25 07:13:07 +08:00
|
|
|
static bool clock_is_pm(watch_date_time date_time) {
|
|
|
|
return date_time.unit.hour >= 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_indicate_pm(movement_settings_t *settings, watch_date_time date_time) {
|
|
|
|
if (settings->bit.clock_mode_24h) { return; }
|
|
|
|
clock_indicate(WATCH_INDICATOR_PM, clock_is_pm(date_time));
|
|
|
|
}
|
|
|
|
|
2024-02-26 01:14:26 +08:00
|
|
|
static void clock_indicate_low_available_power(clock_state_t *clock) {
|
|
|
|
// Set the LAP indicator if battery power is low
|
|
|
|
clock_indicate(WATCH_INDICATOR_LAP, clock->battery_low);
|
|
|
|
}
|
|
|
|
|
2024-02-25 07:13:07 +08:00
|
|
|
static watch_date_time clock_24h_to_12h(watch_date_time date_time) {
|
|
|
|
date_time.unit.hour %= 12;
|
|
|
|
|
|
|
|
if (date_time.unit.hour == 0) {
|
|
|
|
date_time.unit.hour = 12;
|
|
|
|
}
|
|
|
|
|
|
|
|
return date_time;
|
|
|
|
}
|
|
|
|
|
2024-02-25 09:31:12 +08:00
|
|
|
static void clock_check_battery_periodically(clock_state_t *clock, watch_date_time date_time) {
|
|
|
|
// check the battery voltage once a day
|
|
|
|
if (date_time.unit.day == clock->last_battery_check) { return; }
|
|
|
|
|
|
|
|
clock->last_battery_check = date_time.unit.day;
|
|
|
|
|
|
|
|
watch_enable_adc();
|
|
|
|
uint16_t voltage = watch_get_vcc_voltage();
|
|
|
|
watch_disable_adc();
|
|
|
|
|
|
|
|
clock->battery_low = voltage < CLOCK_FACE_LOW_BATTERY_VOLTAGE_THRESHOLD;
|
|
|
|
|
2024-02-26 01:14:26 +08:00
|
|
|
clock_indicate_low_available_power(clock);
|
2024-02-25 09:38:28 +08:00
|
|
|
}
|
|
|
|
|
2024-02-26 01:10:04 +08:00
|
|
|
static void clock_toggle_time_signal(clock_state_t *clock) {
|
|
|
|
clock->time_signal_enabled = !clock->time_signal_enabled;
|
|
|
|
clock_indicate_time_signal(clock);
|
|
|
|
}
|
|
|
|
|
2024-09-04 05:49:13 +08:00
|
|
|
static void clock_display_all(watch_date_time date_time, bool leading_zero) {
|
2024-02-26 00:00:50 +08:00
|
|
|
char buf[10 + 1];
|
|
|
|
|
|
|
|
snprintf(
|
|
|
|
buf,
|
|
|
|
sizeof(buf),
|
2024-09-04 05:49:13 +08:00
|
|
|
leading_zero? "%s%02d%02d%02d%02d" : "%s%2d%2d%02d%02d",
|
2024-02-26 00:00:50 +08:00
|
|
|
watch_utility_get_weekday(date_time),
|
|
|
|
date_time.unit.day,
|
|
|
|
date_time.unit.hour,
|
|
|
|
date_time.unit.minute,
|
|
|
|
date_time.unit.second
|
|
|
|
);
|
|
|
|
|
|
|
|
watch_display_string(buf, 0);
|
|
|
|
}
|
|
|
|
|
2024-02-26 00:31:17 +08:00
|
|
|
static bool clock_display_some(watch_date_time current, watch_date_time previous) {
|
|
|
|
if ((current.reg >> 6) == (previous.reg >> 6)) {
|
|
|
|
// everything before seconds is the same, don't waste cycles setting those segments.
|
|
|
|
|
|
|
|
watch_display_character_lp_seconds('0' + current.unit.second / 10, 8);
|
|
|
|
watch_display_character_lp_seconds('0' + current.unit.second % 10, 9);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else if ((current.reg >> 12) == (previous.reg >> 12)) {
|
|
|
|
// everything before minutes is the same.
|
|
|
|
|
|
|
|
char buf[4 + 1];
|
|
|
|
|
|
|
|
snprintf(
|
|
|
|
buf,
|
|
|
|
sizeof(buf),
|
|
|
|
"%02d%02d",
|
|
|
|
current.unit.minute,
|
|
|
|
current.unit.second
|
|
|
|
);
|
|
|
|
|
|
|
|
watch_display_string(buf, 6);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// other stuff changed; let's do it all.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 01:06:19 +08:00
|
|
|
static void clock_display_clock(movement_settings_t *settings, clock_state_t *clock, watch_date_time current) {
|
|
|
|
if (!clock_display_some(current, clock->date_time.previous)) {
|
2024-02-26 02:18:45 +08:00
|
|
|
if (!clock_is_in_24h_mode(settings)) {
|
2024-02-26 01:06:19 +08:00
|
|
|
// if we are in 12 hour mode, do some cleanup.
|
|
|
|
clock_indicate_pm(settings, current);
|
|
|
|
current = clock_24h_to_12h(current);
|
|
|
|
}
|
2024-09-17 02:34:19 +08:00
|
|
|
clock_display_all(current, clock_should_set_leading_zero(settings));
|
2024-02-26 01:06:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 23:32:31 +08:00
|
|
|
static void clock_display_low_energy(watch_date_time date_time) {
|
2024-02-25 23:40:00 +08:00
|
|
|
char buf[10 + 1];
|
2024-02-25 23:32:31 +08:00
|
|
|
|
|
|
|
snprintf(
|
|
|
|
buf,
|
|
|
|
sizeof(buf),
|
|
|
|
"%s%2d%2d%02d ",
|
|
|
|
watch_utility_get_weekday(date_time),
|
|
|
|
date_time.unit.day,
|
|
|
|
date_time.unit.hour,
|
|
|
|
date_time.unit.minute
|
|
|
|
);
|
|
|
|
|
|
|
|
watch_display_string(buf, 0);
|
|
|
|
}
|
|
|
|
|
2024-02-25 23:40:00 +08:00
|
|
|
static void clock_start_tick_tock_animation(void) {
|
|
|
|
if (!watch_tick_animation_is_running()) {
|
|
|
|
watch_start_tick_animation(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void clock_stop_tick_tock_animation(void) {
|
|
|
|
if (watch_tick_animation_is_running()) {
|
|
|
|
watch_stop_tick_animation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 05:19:12 +08:00
|
|
|
void clock_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
|
2021-10-04 06:49:21 +08:00
|
|
|
(void) settings;
|
2021-12-21 07:02:17 +08:00
|
|
|
(void) watch_face_index;
|
2021-12-22 00:05:06 +08:00
|
|
|
|
|
|
|
if (*context_ptr == NULL) {
|
2024-02-25 05:19:12 +08:00
|
|
|
*context_ptr = malloc(sizeof(clock_state_t));
|
|
|
|
clock_state_t *state = (clock_state_t *) *context_ptr;
|
2024-02-25 06:44:52 +08:00
|
|
|
state->time_signal_enabled = false;
|
2021-12-22 00:05:06 +08:00
|
|
|
state->watch_face_index = watch_face_index;
|
|
|
|
}
|
2021-10-04 06:49:21 +08:00
|
|
|
}
|
|
|
|
|
2024-02-25 05:19:12 +08:00
|
|
|
void clock_face_activate(movement_settings_t *settings, void *context) {
|
2024-02-25 06:36:34 +08:00
|
|
|
clock_state_t *clock = (clock_state_t *) context;
|
2021-12-22 00:05:06 +08:00
|
|
|
|
2024-02-25 23:40:00 +08:00
|
|
|
clock_stop_tick_tock_animation();
|
2021-10-20 03:37:08 +08:00
|
|
|
|
2024-02-25 06:44:52 +08:00
|
|
|
clock_indicate_time_signal(clock);
|
2024-02-25 06:36:34 +08:00
|
|
|
clock_indicate_alarm(settings);
|
2024-02-25 06:52:51 +08:00
|
|
|
clock_indicate_24h(settings);
|
2021-10-20 03:37:08 +08:00
|
|
|
|
2021-10-04 06:49:21 +08:00
|
|
|
watch_set_colon();
|
2021-12-22 00:05:06 +08:00
|
|
|
|
2021-10-04 06:49:21 +08:00
|
|
|
// this ensures that none of the timestamp fields will match, so we can re-render them all.
|
2024-02-26 01:06:19 +08:00
|
|
|
clock->date_time.previous.reg = 0xFFFFFFFF;
|
2021-10-04 06:49:21 +08:00
|
|
|
}
|
|
|
|
|
2024-02-25 05:19:12 +08:00
|
|
|
bool clock_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
|
|
|
|
clock_state_t *state = (clock_state_t *) context;
|
2024-02-26 01:06:19 +08:00
|
|
|
watch_date_time current;
|
2021-10-04 06:49:21 +08:00
|
|
|
|
2021-10-06 22:25:28 +08:00
|
|
|
switch (event.event_type) {
|
2021-10-17 04:03:27 +08:00
|
|
|
case EVENT_LOW_ENERGY_UPDATE:
|
2024-02-25 23:40:00 +08:00
|
|
|
clock_start_tick_tock_animation();
|
2024-02-25 23:32:31 +08:00
|
|
|
clock_display_low_energy(watch_rtc_get_date_time());
|
|
|
|
break;
|
|
|
|
case EVENT_TICK:
|
|
|
|
case EVENT_ACTIVATE:
|
2024-02-26 00:31:17 +08:00
|
|
|
current = watch_rtc_get_date_time();
|
2024-02-26 01:06:19 +08:00
|
|
|
|
|
|
|
clock_display_clock(settings, state, current);
|
2024-02-25 23:32:31 +08:00
|
|
|
|
2024-02-26 00:31:28 +08:00
|
|
|
clock_check_battery_periodically(state, current);
|
2024-02-25 06:36:34 +08:00
|
|
|
|
2024-02-26 01:06:19 +08:00
|
|
|
state->date_time.previous = current;
|
|
|
|
|
2021-10-04 06:49:21 +08:00
|
|
|
break;
|
2021-12-22 00:05:06 +08:00
|
|
|
case EVENT_ALARM_LONG_PRESS:
|
2024-02-26 01:10:04 +08:00
|
|
|
clock_toggle_time_signal(state);
|
2021-12-22 00:05:06 +08:00
|
|
|
break;
|
|
|
|
case EVENT_BACKGROUND_TASK:
|
|
|
|
// uncomment this line to snap back to the clock face when the hour signal sounds:
|
|
|
|
// movement_move_to_face(state->watch_face_index);
|
2023-11-13 13:48:57 +08:00
|
|
|
movement_play_signal();
|
2021-10-04 06:49:21 +08:00
|
|
|
break;
|
|
|
|
default:
|
2023-01-15 03:21:04 +08:00
|
|
|
return movement_default_loop_handler(event, settings);
|
2021-10-04 06:49:21 +08:00
|
|
|
}
|
2021-10-04 21:51:49 +08:00
|
|
|
|
|
|
|
return true;
|
2021-10-04 06:49:21 +08:00
|
|
|
}
|
|
|
|
|
2024-02-25 05:19:12 +08:00
|
|
|
void clock_face_resign(movement_settings_t *settings, void *context) {
|
2021-10-04 06:49:21 +08:00
|
|
|
(void) settings;
|
|
|
|
(void) context;
|
|
|
|
}
|
2021-12-22 00:05:06 +08:00
|
|
|
|
2024-02-25 05:19:12 +08:00
|
|
|
bool clock_face_wants_background_task(movement_settings_t *settings, void *context) {
|
2021-12-22 00:05:06 +08:00
|
|
|
(void) settings;
|
2024-02-25 05:19:12 +08:00
|
|
|
clock_state_t *state = (clock_state_t *) context;
|
2024-02-25 06:44:52 +08:00
|
|
|
if (!state->time_signal_enabled) return false;
|
2021-12-22 00:05:06 +08:00
|
|
|
|
|
|
|
watch_date_time date_time = watch_rtc_get_date_time();
|
|
|
|
|
2022-01-20 02:06:12 +08:00
|
|
|
return date_time.unit.minute == 0;
|
2021-12-22 00:05:06 +08:00
|
|
|
}
|