From ea4d5d0e295d6f2761b3f5aecb8a01dcf3386322 Mon Sep 17 00:00:00 2001 From: James Haggerty Date: Tue, 2 Jan 2024 09:20:54 +1100 Subject: [PATCH 1/3] Expose character mapping internals and add invert Allowing the faces to access the internal segment mapping code makes it easier to have custom characters and have fancy effects. --- movement/watch_faces/clock/clock_face.c | 4 +- .../clock/minute_repeater_decimal_face.c | 4 +- .../clock/repetition_minute_face.c | 4 +- .../clock/simple_clock_bin_led_face.c | 4 +- .../watch_faces/clock/simple_clock_face.c | 4 +- .../shared/watch/watch_private_display.c | 105 ++++++++++-------- .../shared/watch/watch_private_display.h | 18 ++- watch-library/shared/watch/watch_slcd.h | 76 ++++++++++++- 8 files changed, 156 insertions(+), 63 deletions(-) diff --git a/movement/watch_faces/clock/clock_face.c b/movement/watch_faces/clock/clock_face.c index eab5cd8d3..ea2dae0ed 100644 --- a/movement/watch_faces/clock/clock_face.c +++ b/movement/watch_faces/clock/clock_face.c @@ -146,8 +146,8 @@ 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); + watch_display_character_lp('0' + current.unit.second / 10, 8); + watch_display_character_lp('0' + current.unit.second % 10, 9); return true; diff --git a/movement/watch_faces/clock/minute_repeater_decimal_face.c b/movement/watch_faces/clock/minute_repeater_decimal_face.c index 2cedc3075..02e4dcde5 100644 --- a/movement/watch_faces/clock/minute_repeater_decimal_face.c +++ b/movement/watch_faces/clock/minute_repeater_decimal_face.c @@ -129,8 +129,8 @@ bool minute_repeater_decimal_face_loop(movement_event_t event, movement_settings if ((date_time.reg >> 6) == (previous_date_time >> 6) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before seconds is the same, don't waste cycles setting those segments. - watch_display_character_lp_seconds('0' + date_time.unit.second / 10, 8); - watch_display_character_lp_seconds('0' + date_time.unit.second % 10, 9); + watch_display_character_lp('0' + date_time.unit.second / 10, 8); + watch_display_character_lp('0' + date_time.unit.second % 10, 9); break; } else if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before minutes is the same. diff --git a/movement/watch_faces/clock/repetition_minute_face.c b/movement/watch_faces/clock/repetition_minute_face.c index e9e5e3197..3453f6889 100644 --- a/movement/watch_faces/clock/repetition_minute_face.c +++ b/movement/watch_faces/clock/repetition_minute_face.c @@ -114,8 +114,8 @@ bool repetition_minute_face_loop(movement_event_t event, movement_settings_t *se if ((date_time.reg >> 6) == (previous_date_time >> 6) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before seconds is the same, don't waste cycles setting those segments. - watch_display_character_lp_seconds('0' + date_time.unit.second / 10, 8); - watch_display_character_lp_seconds('0' + date_time.unit.second % 10, 9); + watch_display_character_lp('0' + date_time.unit.second / 10, 8); + watch_display_character_lp('0' + date_time.unit.second % 10, 9); break; } else if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before minutes is the same. diff --git a/movement/watch_faces/clock/simple_clock_bin_led_face.c b/movement/watch_faces/clock/simple_clock_bin_led_face.c index cf39c1886..fa1c90488 100644 --- a/movement/watch_faces/clock/simple_clock_bin_led_face.c +++ b/movement/watch_faces/clock/simple_clock_bin_led_face.c @@ -140,8 +140,8 @@ bool simple_clock_bin_led_face_loop(movement_event_t event, movement_settings_t if ((date_time.reg >> 6) == (previous_date_time >> 6) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before seconds is the same, don't waste cycles setting those segments. - watch_display_character_lp_seconds('0' + date_time.unit.second / 10, 8); - watch_display_character_lp_seconds('0' + date_time.unit.second % 10, 9); + watch_display_character_lp('0' + date_time.unit.second / 10, 8); + watch_display_character_lp('0' + date_time.unit.second % 10, 9); break; } else if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before minutes is the same. diff --git a/movement/watch_faces/clock/simple_clock_face.c b/movement/watch_faces/clock/simple_clock_face.c index fbc2c4b3e..35984472a 100644 --- a/movement/watch_faces/clock/simple_clock_face.c +++ b/movement/watch_faces/clock/simple_clock_face.c @@ -97,8 +97,8 @@ bool simple_clock_face_loop(movement_event_t event, movement_settings_t *setting if ((date_time.reg >> 6) == (previous_date_time >> 6) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before seconds is the same, don't waste cycles setting those segments. - watch_display_character_lp_seconds('0' + date_time.unit.second / 10, 8); - watch_display_character_lp_seconds('0' + date_time.unit.second % 10, 9); + watch_display_character_lp('0' + date_time.unit.second / 10, 8); + watch_display_character_lp('0' + date_time.unit.second % 10, 9); break; } else if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { // everything before minutes is the same. diff --git a/watch-library/shared/watch/watch_private_display.c b/watch-library/shared/watch/watch_private_display.c index c12957d9c..7a5ab1d97 100644 --- a/watch-library/shared/watch/watch_private_display.c +++ b/watch-library/shared/watch/watch_private_display.c @@ -33,7 +33,9 @@ static const uint32_t IndicatorSegments[] = { SLCD_SEGID(1, 10), // WATCH_INDICATOR_LAP }; -void watch_display_character(uint8_t character, uint8_t position) { +static bool invert = false; + +uint8_t watch_convert_char_to_segdata(uint8_t character, uint8_t position) { // special cases for positions 4 and 6 if (position == 4 || position == 6) { if (character == '7') character = '&'; // "lowercase" 7 @@ -64,65 +66,72 @@ void watch_display_character(uint8_t character, uint8_t position) { } else { if (character == 'R') character = 'r'; // R needs to be lowercase almost everywhere } - if (position == 0) { - watch_clear_pixel(0, 15); // clear funky ninth segment - } else { + if (position != 0) { if (character == 'I') character = 'l'; // uppercase I only works in position 0 } - uint64_t segmap = Segment_Map[position]; - uint64_t segdata = Character_Set[character - 0x20]; + return Character_Set[character - 0x20]; +} - for (int i = 0; i < 8; i++) { - uint8_t com = (segmap & 0xFF) >> 6; - if (com > 2) { - // COM3 means no segment exists; skip it. - segmap = segmap >> 8; - segdata = segdata >> 1; - continue; - } - uint8_t seg = segmap & 0x3F; - - if (segdata & 1) - watch_set_pixel(com, seg); - else - watch_clear_pixel(com, seg); - - segmap = segmap >> 8; - segdata = segdata >> 1; - } +uint8_t watch_convert_char_to_segdata_lp(uint8_t character) { + // i.e. remove any special casing cf watch_convert_char_to_segdata + return Character_Set[character - 0x20]; +} + +void watch_display_character(uint8_t character, uint8_t position) { + watch_display_segdata(watch_convert_char_to_segdata(character, position), position); + + // Handle the segments that are not in our usual map at all. + if (position == 0) { + if (character == 'B' || character == 'D' || character == '@') watch_set_pixel(0, 15); // bottom left serif + else watch_clear_pixel(0, 15); + + } else if (position == 1) { + if (character == 'T') watch_set_pixel(1, 12); // add descender (left hand side vertical line) - if (character == 'T' && position == 1) watch_set_pixel(1, 12); // add descender - else if (position == 0 && (character == 'B' || character == 'D' || character == '@')) watch_set_pixel(0, 15); // add funky ninth segment - else if (position == 1 && (character == 'B' || character == 'D' || character == '@')) watch_set_pixel(0, 12); // add funky ninth segment + if (character == 'B' || character == 'D' || character == '@') watch_set_pixel(0, 12); // top left serif + else watch_clear_pixel(0, 12); + } } -void watch_display_character_lp_seconds(uint8_t character, uint8_t position) { - // Will only work for digits and for positions 8 and 9 - but less code & checks to reduce power consumption +// Allow us to rotate characters 180 degrees (see watch_private_display.h for bit position diagram). +static const uint8_t vert_invert_map[8] = { + 3, 4, 5, 0, 1, 2, 6, 7, +}; + +void watch_display_segment(uint8_t bit_pos, uint8_t position, bool on) { + uint64_t segmap = Segment_Map[position] >> (8 * bit_pos); + uint8_t com = (segmap & 0xFF) >> 6; - uint64_t segmap = Segment_Map[position]; - uint64_t segdata = Character_Set[character - 0x20]; + if (com > 2) { + // COM3 means no segment exists; skip it. + return; + } + uint8_t seg = segmap & 0x3F; + + if (on) { + watch_set_pixel(com, seg); + } else { + watch_clear_pixel(com, seg); + } +} + +void watch_display_segdata(uint8_t segdata, uint8_t position) { for (int i = 0; i < 8; i++) { - uint8_t com = (segmap & 0xFF) >> 6; - if (com > 2) { - // COM3 means no segment exists; skip it. - segmap = segmap >> 8; - segdata = segdata >> 1; - continue; - } - uint8_t seg = segmap & 0x3F; - - if (segdata & 1) - watch_set_pixel(com, seg); - else - watch_clear_pixel(com, seg); - - segmap = segmap >> 8; - segdata = segdata >> 1; + uint8_t bit_pos = invert ? vert_invert_map[i] : i; + watch_display_segment(bit_pos, position, (1 << i) & segdata); } } +void watch_display_invert(bool inv) { + invert = inv; +} + +void watch_display_character_lp(uint8_t character, uint8_t position) { + watch_display_segdata(watch_convert_char_to_segdata_lp(character), position); +} + void watch_display_string(char *string, uint8_t position) { size_t i = 0; while(string[i] != 0) { @@ -133,7 +142,7 @@ void watch_display_string(char *string, uint8_t position) { // uncomment this line to see screen output on terminal, i.e. // FR 29 // 11 50 23 - // note that for partial displays (positon > 0) it will only show the characters that were updated. + // note that for partial displays (position > 0) it will only show the characters that were updated. // printf("________\n %c%c %c%c\n%c%c %c%c %c%c\n--------\n", (position > 0) ? ' ' : string[0], (position > 1) ? ' ' : string[1 - position], (position > 2) ? ' ' : string[2 - position], (position > 3) ? ' ' : string[3 - position], (position > 4) ? ' ' : string[4 - position], (position > 5) ? ' ' : string[5 - position], (position > 6) ? ' ' : string[6 - position], (position > 7) ? ' ' : string[7 - position], (position > 8) ? ' ' : string[8 - position], (position > 9) ? ' ' : string[9 - position]); } diff --git a/watch-library/shared/watch/watch_private_display.h b/watch-library/shared/watch/watch_private_display.h index 606b8dc3e..12c355ef6 100644 --- a/watch-library/shared/watch/watch_private_display.h +++ b/watch-library/shared/watch/watch_private_display.h @@ -27,6 +27,20 @@ #include "hpl_slcd_config.h" #include "driver_init.h" +// Bit position -> segment mapping for Character_Set. +// --0-- +// | | +// 5 1 +// | | +// --6-- +// | | +// 4 2 +// | | +// --3-- +// +// 7 is the middle divider (i.e. only position 0). + + static const uint8_t Character_Set[] = { 0b00000000, // @@ -141,8 +155,4 @@ static const uint64_t Segment_Map[] = { static const uint8_t Num_Chars = 10; -void watch_display_character(uint8_t character, uint8_t position); -void watch_display_character_lp_seconds(uint8_t character, uint8_t position); - - #endif diff --git a/watch-library/shared/watch/watch_slcd.h b/watch-library/shared/watch/watch_slcd.h index 3f550bb07..5dba43c48 100644 --- a/watch-library/shared/watch/watch_slcd.h +++ b/watch-library/shared/watch/watch_slcd.h @@ -74,7 +74,7 @@ void watch_clear_pixel(uint8_t com, uint8_t seg); void watch_clear_display(void); /** @brief Displays a string at the given position, starting from the top left. There are ten digits. - A space in any position will clear that digit. + * A space in any position will clear that digit. * @param string A null-terminated string. * @param position The position where you wish to start displaying the string. The day of week digits * are positions 0 and 1; the day of month digits are positions 2 and 3, and the main @@ -84,6 +84,79 @@ void watch_clear_display(void); */ void watch_display_string(char *string, uint8_t position); +/** @brief Displays a character at the given position, starting from the top left. There are ten digits. + * A space in any position will clear that digit. + * @param character A single character to display. + * @param position The position where you wish to display the character. The day of week digits + * are positions 0 and 1; the day of month digits are positions 2 and 3, and the main + * clock line occupies positions 4-9. + */ +void watch_display_character(uint8_t character, uint8_t position); + +/** @brief Displays a character at the given position, starting from the top left. There are ten digits. + * lp refers to 'low-power', as this does _not_ do any special casing to try to handle the pecularities of each position, + * This means it's best used to display numbers in the expected form. + * @param character A single character to display. + * @param position The position where you wish to display the character. The day of week digits + * are positions 0 and 1; the day of month digits are positions 2 and 3, and the main + * clock line occupies positions 4-9. + */ +void watch_display_character_lp(uint8_t character, uint8_t position); + +/** @brief Attempt to invert the display (i.e. rotate all characters 180 degrees). + * As with watch_display_character_lp, due to the limitations of the display this works best + * with numbers. This affects all subsequent calls to watch_display_string/character. + * @param inv whether to invert (true) or not invert (false). + */ +void watch_display_invert(bool inv); + +/** @brief Display arbitrary segment data at a particular position. + * cf watch_display_character. + * @param segdata Raw bits which are mapped to the position. See below. + * @param position The position where you wish to display the character. The day of week digits + * are positions 0 and 1; the day of month digits are positions 2 and 3, and the main + * clock line occupies positions 4-9. + * @note The bits are mapped as follows: + * + * --0-- + * | | + * 5 1 + * | | + * --6-- + * | | + * 4 2 + * | | + * --3-- + * + * 7 is the middle divider. i.e. only position 0. + * + * If you want to display/clear an individual segment, use watch_display_segment. + */ +void watch_display_segdata(uint8_t segdata, uint8_t position); + +/** @brief Generate segdata for a position that could be used as input to watch_display_segdata. + * @param character A single character to convert. + * @param position The position to generate the character for. + * @note You might wonder why the position is necessary for this function even though + * we don't actually display the character here. This is because we generate different segment + * data depending on the characte to handle the limitiations of the position. + */ +uint8_t watch_convert_char_to_segdata(uint8_t character, uint8_t position); + +/** @brief Generate segdata for a position that could be used as input to watch_display_segdata (lp). + * This is analagous to watch_display_character_lp. + * @param character A single character to convert. + */ +uint8_t watch_convert_char_to_segdata_lp(uint8_t character); + +/** @brief Map an individual segment to a particular position. See watch_display_segdata for segment info. + * This is similar to watch_set_pixel, but maps the segments for an arbitrary spot on the display. + * @param bit_pos Bit position (0 to 7). + * @param position Character position. + * @param on Whether to set or clear at that position. + */ +void watch_display_segment(uint8_t bit_pos, uint8_t position, bool on); + /** @brief Turns the colon segment on. */ void watch_set_colon(void); @@ -147,5 +220,6 @@ bool watch_tick_animation_is_running(void); * @details This will stop the animation and clear all segments in position 8. */ void watch_stop_tick_animation(void); + /// @} #endif From 53186a3f258a29773b522d1cb38b905a80175b16 Mon Sep 17 00:00:00 2001 From: James Haggerty Date: Mon, 1 Jan 2024 20:53:24 +1100 Subject: [PATCH 2/3] Balanced nonary clock face What's the point of having a customisable wrist watch unless you have bizarre timing schemes? This maintains the normal concept of hours, but divides each hour into 9*9*9*9 seconds, so that we can display the time in nonary. But not just nonary, this is _balanced_ nonary, such that our digit values are -4, -3, -2, -1, 0, 1, 2, 3, 4 Midday is therefore 00:00:00. An example of counting before midday and after: 00:00:0(-4) 00:00:0(-3) 00:00:0(-2) 00:00:0(-1) 00:00:00 00:00:01 00:00:02 00:00:03 00:00:04 00:00:1(-4) 00:00:1(-3) 00:00:1(-2) 00:00:1(-1) 00:00:10 00:00:11 etc. This has the advantage that you can always see what hour/minute you're closer to. But wait - how can we display the negative digits? Well, to do this we use the top 'bar' in a digit as the negative sign, and for those numbers compress them into the bottom 4 segments, and then simply use the number of other segments filled as the digit. So, counting from -4 to 4: __ __ __ __ __ __ __ | | __ __ __| |__|, __|,__|,__,|__|, |, |,| |,| | Sadly, because the top and the bottom segments are tied, we can't easily use the same digit for positive and negative values without running into 'normal' numbers in confusing ways. --- movement/make/Makefile | 1 + movement/movement_faces.h | 1 + .../watch_faces/clock/nonary_clock_face.c | 235 ++++++++++++++++++ .../watch_faces/clock/nonary_clock_face.h | 101 ++++++++ 4 files changed, 338 insertions(+) create mode 100644 movement/watch_faces/clock/nonary_clock_face.c create mode 100644 movement/watch_faces/clock/nonary_clock_face.h diff --git a/movement/make/Makefile b/movement/make/Makefile index da5486b0c..ce80bac86 100644 --- a/movement/make/Makefile +++ b/movement/make/Makefile @@ -129,6 +129,7 @@ SRCS += \ ../watch_faces/clock/minute_repeater_decimal_face.c \ ../watch_faces/complication/tuning_tones_face.c \ ../watch_faces/complication/kitchen_conversions_face.c \ + ../watch_faces/clock/nonary_clock_face.c \ # New watch faces go above this line. # Leave this line at the bottom of the file; it has all the targets for making your project. diff --git a/movement/movement_faces.h b/movement/movement_faces.h index 35571109a..cfb57e63d 100644 --- a/movement/movement_faces.h +++ b/movement/movement_faces.h @@ -104,6 +104,7 @@ #include "minute_repeater_decimal_face.h" #include "tuning_tones_face.h" #include "kitchen_conversions_face.h" +#include "nonary_clock_face.h" // New includes go above this line. #endif // MOVEMENT_FACES_H_ diff --git a/movement/watch_faces/clock/nonary_clock_face.c b/movement/watch_faces/clock/nonary_clock_face.c new file mode 100644 index 000000000..9d95395c4 --- /dev/null +++ b/movement/watch_faces/clock/nonary_clock_face.c @@ -0,0 +1,235 @@ +/* + * MIT License + * + * Copyright (c) 2024 James Haggerty + * + * 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. + */ + +#include +#include +#include "nonary_clock_face.h" +#include "watch.h" +#include "watch_utility.h" +#include "watch_private_display.h" + +static const int TICK_FREQUENCY = 8; + +static void _update_alarm_indicator(bool settings_alarm_enabled, nonary_clock_state_t *state) { + state->alarm_enabled = settings_alarm_enabled; + if (state->alarm_enabled) watch_set_indicator(WATCH_INDICATOR_SIGNAL); + else watch_clear_indicator(WATCH_INDICATOR_SIGNAL); +} + +void nonary_clock_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { + (void) settings; + (void) watch_face_index; + + if (*context_ptr == NULL) { + *context_ptr = malloc(sizeof(nonary_clock_state_t)); + nonary_clock_state_t *state = (nonary_clock_state_t *)*context_ptr; + state->signal_enabled = false; + state->watch_face_index = watch_face_index; + } +} + +static const int32_t NONARY_SECS_IN_HOUR = 9 * 9 * 9 * 9; + +/* + * Bit position -> segment mapping. + * --0-- + * | | + * 5 1 + * | | + * --6-- + * | | + * 4 2 + * | | + * --3-- + * + * Digits (-4,-3,-2-1,0,1,2,3,4) + * __ __ __ __ __ + * __ __ | | __ __ __| + * |__|, __|,__|,__,|__|, |, |,| |,| | + */ +uint8_t nonary_digit_map[] = { + 0b01011101, // -4 + 0b01001101, // -3 + 0b00001101, // -2 + 0b00001001, // -1 + 0b00111111, // 0 + 0b00000100, // 1 + 0b01000100, // 2 + 0b01010100, // 3 + 0b01010110, // 4 +}; + +static uint8_t previous_display[4]; + +static void display_nonary(int32_t val, int pos, int max_len) { + int sign = val >= 0 ? 1 : -1; + val = abs(val); + for (int i = 0; i < max_len && pos >= 0; ++i) { + int digit = val % 9; + val /= 9; + if (digit >= 5) { + digit -= 9; + val += 1; + } + uint8_t c = nonary_digit_map[digit * sign + 4]; + if (previous_display[pos] != c) { + watch_display_segdata(c, pos--); + } + } +} + +static int32_t find_nonary_secs_from_hour(watch_date_time date_time, int ticks) { + int32_t nonary_secs_past_hour = ((date_time.unit.minute * 60 + date_time.unit.second) * TICK_FREQUENCY + ticks) * NONARY_SECS_IN_HOUR / TICK_FREQUENCY / 3600; + + return nonary_secs_past_hour <= NONARY_SECS_IN_HOUR / 2 ? nonary_secs_past_hour : (nonary_secs_past_hour - NONARY_SECS_IN_HOUR); +} + +void nonary_clock_face_activate(movement_settings_t *settings, void *context) { + nonary_clock_state_t *state = (nonary_clock_state_t *)context; + + if (watch_tick_animation_is_running()) watch_stop_tick_animation(); + + if (settings->bit.clock_mode_24h) watch_set_indicator(WATCH_INDICATOR_24H); + + // handle chime indicator + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); + else watch_clear_indicator(WATCH_INDICATOR_BELL); + + // show alarm indicator if there is an active alarm + _update_alarm_indicator(settings->bit.alarm_enabled, state); + + watch_set_colon(); + + // this ensures that none of the timestamp fields will match, so we can re-render them all. + state->previous_date_time = 0xFFFFFFFF; + + // Since our seconds aren't really seconds, we need a higher tick frequency + // to avoid skips and jerky time changes. + movement_request_tick_frequency(TICK_FREQUENCY); + + memset(previous_display, ' ', sizeof(previous_display)); +} + +bool nonary_clock_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + static uint8_t ticks = 0; + static int32_t previous_nonary_secs_from_hour = 0; + nonary_clock_state_t *state = (nonary_clock_state_t *)context; + + watch_date_time date_time; + uint32_t previous_date_time; + switch (event.event_type) { + case EVENT_TICK: + case EVENT_ACTIVATE: + case EVENT_LOW_ENERGY_UPDATE: + date_time = watch_rtc_get_date_time(); + previous_date_time = state->previous_date_time; + state->previous_date_time = date_time.reg; + + if (event.event_type == EVENT_TICK) { + if (previous_date_time == date_time.reg) { + ++ticks; + } else { + ticks = 0; + } + } + + // check the battery voltage once a day... + if (date_time.unit.day != state->last_battery_check) { + state->last_battery_check = date_time.unit.day; + watch_enable_adc(); + uint16_t voltage = watch_get_vcc_voltage(); + watch_disable_adc(); + // 2.2 volts will happen when the battery has maybe 5-10% remaining? + // we can refine this later. + state->battery_low = (voltage < 2200); + } + + // ...and set the LAP indicator if low. + if (state->battery_low) watch_set_indicator(WATCH_INDICATOR_LAP); + + // handle alarm indicator + if (state->alarm_enabled != settings->bit.alarm_enabled) _update_alarm_indicator(settings->bit.alarm_enabled, state); + + int32_t nonary_secs_from_hour = find_nonary_secs_from_hour(date_time, ticks); + if (previous_nonary_secs_from_hour != nonary_secs_from_hour) { + display_nonary(nonary_secs_from_hour, 9, 4); + previous_nonary_secs_from_hour = nonary_secs_from_hour; + } + + if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { + break; + } + + int32_t nonary_hour = (int32_t)date_time.unit.hour - 12 + (nonary_secs_from_hour < 0); + display_nonary(nonary_hour, 5, 2); + char buf[5]; + sprintf(buf, "%s%2d", watch_utility_get_weekday(date_time), date_time.unit.day); + watch_display_string(buf, 0); + + if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { + watch_display_character_lp(' ', 8); + watch_display_character_lp(' ', 9); + if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); + } + break; + case EVENT_ALARM_LONG_PRESS: + state->signal_enabled = !state->signal_enabled; + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); + else watch_clear_indicator(WATCH_INDICATOR_BELL); + 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); + #ifdef SIGNAL_TUNE_DEFAULT + movement_play_signal(); + #else + //movement_play_tune(); + #endif + break; + default: + return movement_default_loop_handler(event, settings); + } + + return true; +} + +void nonary_clock_face_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; + + movement_request_tick_frequency(1); +} + +bool nonary_clock_face_wants_background_task(movement_settings_t *settings, void *context) { + (void) settings; + nonary_clock_state_t *state = (nonary_clock_state_t *)context; + if (!state->signal_enabled) return false; + + watch_date_time date_time = watch_rtc_get_date_time(); + + // TODO to make this work better, we need to have the CLOCK->COUNT32 change + // (so we can set COMP to an appropriate interval). + // At the moment, this will not fire at appropriate times. + return date_time.unit.minute == 0; +} diff --git a/movement/watch_faces/clock/nonary_clock_face.h b/movement/watch_faces/clock/nonary_clock_face.h new file mode 100644 index 000000000..24210a081 --- /dev/null +++ b/movement/watch_faces/clock/nonary_clock_face.h @@ -0,0 +1,101 @@ +/* + * MIT License + * + * Copyright (c) 2024 James Haggerty + * + * 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. + */ + +#ifndef NONARY_CLOCK_FACE_H_ +#define NONARY_CLOCK_FACE_H_ + +/* + * Balanced Nonary Clock Face + * + * Like simple clock face, but... confusing? + * + * What's the point of having a customisable wrist watch unless you have bizarre timing schemes? + * This maintains the normal concept of hours, but divides each hour into + * 9*9*9*9 seconds, so that we can display the time in nonary. + * + * But not just any nonary, this is _balanced_ nonary, such that our digit + * values are -4, -3, -2, -1, 0, 1, 2, 3, 4 + * + * Midday is therefore 00:00:00. An example of counting before midday + * and after: + * + * 00:00:0(-4) + * 00:00:0(-3) + * 00:00:0(-2) + * 00:00:0(-1) + * 00:00:00 + * 00:00:01 + * 00:00:02 + * 00:00:03 + * 00:00:04 + * 00:00:1(-4) + * 00:00:1(-3) + * 00:00:1(-2) + * 00:00:1(-1) + * 00:00:10 + * 00:00:11 + * etc. + * + * This has the advantage that you can always see what hour/minute you're closer to. + * + * But wait - how can we display the negative digits? Well, to do this + * we use the top 'bar' in a digit as the negative sign, and for those + * numbers compress them into the bottom 4 segments, and then simply use + * the number of other segments filled as the digit. So, counting + * from -4 to 4: + * __ __ __ __ __ + * __ __ | | __ __ __| + * |__|, __|,__|,__,|__|, |, |,| |,| | + * + * Sadly, because the top and the bottom segments are tied, we can't + * easily use the same digit for positive and negative values without + * running into 'normal' numbers in confusing ways. + */ + +#include "movement.h" + +typedef struct { + uint32_t previous_date_time; + uint8_t last_battery_check; + uint8_t watch_face_index; + bool signal_enabled; + bool battery_low; + bool alarm_enabled; +} nonary_clock_state_t; + +void nonary_clock_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void nonary_clock_face_activate(movement_settings_t *settings, void *context); +bool nonary_clock_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void nonary_clock_face_resign(movement_settings_t *settings, void *context); +bool nonary_clock_face_wants_background_task(movement_settings_t *settings, void *context); + +#define nonary_clock_face ((const watch_face_t){ \ + nonary_clock_face_setup, \ + nonary_clock_face_activate, \ + nonary_clock_face_loop, \ + nonary_clock_face_resign, \ + nonary_clock_face_wants_background_task, \ +}) + +#endif From 4e7f713d5233bc9fae250391c90b57a3e2221176 Mon Sep 17 00:00:00 2001 From: James Haggerty Date: Sat, 20 Jan 2024 08:41:08 +1100 Subject: [PATCH 3/3] Fix issue with hour updating and memory bug --- .../watch_faces/clock/nonary_clock_face.c | 31 ++++++++++--------- .../watch_faces/clock/nonary_clock_face.h | 2 +- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/movement/watch_faces/clock/nonary_clock_face.c b/movement/watch_faces/clock/nonary_clock_face.c index 9d95395c4..50123a318 100644 --- a/movement/watch_faces/clock/nonary_clock_face.c +++ b/movement/watch_faces/clock/nonary_clock_face.c @@ -80,7 +80,7 @@ uint8_t nonary_digit_map[] = { 0b01010110, // 4 }; -static uint8_t previous_display[4]; +static uint8_t previous_display[10]; static void display_nonary(int32_t val, int pos, int max_len) { int sign = val >= 0 ? 1 : -1; @@ -122,7 +122,7 @@ void nonary_clock_face_activate(movement_settings_t *settings, void *context) { watch_set_colon(); // this ensures that none of the timestamp fields will match, so we can re-render them all. - state->previous_date_time = 0xFFFFFFFF; + state->previous_date_time.reg = 0xFFFFFFFF; // Since our seconds aren't really seconds, we need a higher tick frequency // to avoid skips and jerky time changes. @@ -137,17 +137,17 @@ bool nonary_clock_face_loop(movement_event_t event, movement_settings_t *setting nonary_clock_state_t *state = (nonary_clock_state_t *)context; watch_date_time date_time; - uint32_t previous_date_time; + watch_date_time previous_date_time; switch (event.event_type) { - case EVENT_TICK: case EVENT_ACTIVATE: + case EVENT_TICK: case EVENT_LOW_ENERGY_UPDATE: date_time = watch_rtc_get_date_time(); previous_date_time = state->previous_date_time; - state->previous_date_time = date_time.reg; + state->previous_date_time = date_time; if (event.event_type == EVENT_TICK) { - if (previous_date_time == date_time.reg) { + if (previous_date_time.reg == date_time.reg) { ++ticks; } else { ticks = 0; @@ -172,26 +172,27 @@ bool nonary_clock_face_loop(movement_event_t event, movement_settings_t *setting if (state->alarm_enabled != settings->bit.alarm_enabled) _update_alarm_indicator(settings->bit.alarm_enabled, state); int32_t nonary_secs_from_hour = find_nonary_secs_from_hour(date_time, ticks); - if (previous_nonary_secs_from_hour != nonary_secs_from_hour) { - display_nonary(nonary_secs_from_hour, 9, 4); - previous_nonary_secs_from_hour = nonary_secs_from_hour; - } - - if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { + if (previous_nonary_secs_from_hour == nonary_secs_from_hour) { break; } + previous_nonary_secs_from_hour = nonary_secs_from_hour; + display_nonary(nonary_secs_from_hour, 9, 4); int32_t nonary_hour = (int32_t)date_time.unit.hour - 12 + (nonary_secs_from_hour < 0); display_nonary(nonary_hour, 5, 2); - char buf[5]; - sprintf(buf, "%s%2d", watch_utility_get_weekday(date_time), date_time.unit.day); - watch_display_string(buf, 0); if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { watch_display_character_lp(' ', 8); watch_display_character_lp(' ', 9); if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); } + + if (date_time.unit.day != previous_date_time.unit.day) { + char buf[5]; + sprintf(buf, "%s%2d", watch_utility_get_weekday(date_time), date_time.unit.day); + watch_display_string(buf, 0); + } + break; case EVENT_ALARM_LONG_PRESS: state->signal_enabled = !state->signal_enabled; diff --git a/movement/watch_faces/clock/nonary_clock_face.h b/movement/watch_faces/clock/nonary_clock_face.h index 24210a081..972bb012e 100644 --- a/movement/watch_faces/clock/nonary_clock_face.h +++ b/movement/watch_faces/clock/nonary_clock_face.h @@ -76,7 +76,7 @@ #include "movement.h" typedef struct { - uint32_t previous_date_time; + watch_date_time previous_date_time; uint8_t last_battery_check; uint8_t watch_face_index; bool signal_enabled;