From b4da0defbe6923d710fb87bdeb998e553d9bdbe5 Mon Sep 17 00:00:00 2001 From: JKomskis Date: Sun, 6 Jul 2025 08:55:50 -0700 Subject: [PATCH] Port probability face to second movement (#30) * Move out of legacy folder, add to build * Ported probability face display functions Added tap support * Fix animation for custom LCD --- .../complication/probability_face.c | 176 ---------- movement_faces.h | 1 + watch-faces.mk | 1 + watch-faces/complication/probability_face.c | 303 ++++++++++++++++++ .../complication/probability_face.h | 21 +- 5 files changed, 323 insertions(+), 179 deletions(-) delete mode 100644 legacy/watch_faces/complication/probability_face.c create mode 100644 watch-faces/complication/probability_face.c rename {legacy/watch_faces => watch-faces}/complication/probability_face.h (73%) diff --git a/legacy/watch_faces/complication/probability_face.c b/legacy/watch_faces/complication/probability_face.c deleted file mode 100644 index 56984307..00000000 --- a/legacy/watch_faces/complication/probability_face.c +++ /dev/null @@ -1,176 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2022 Spencer Bywater - * - * 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. - */ - -// Emulator only: need time() to seed the random number generator. -#if __EMSCRIPTEN__ -#include -#endif - -#include -#include -#include "probability_face.h" - -#define DEFAULT_DICE_SIDES 2 -#define PROBABILITY_ANIMATION_TICK_FREQUENCY 8 -const uint16_t NUM_DICE_TYPES = 8; // Keep this consistent with # of dice types below -const uint16_t DICE_TYPES[] = {2, 4, 6, 8, 10, 12, 20, 100}; - - -// -------------- -// Custom methods -// -------------- - -static void display_dice_roll(probability_state_t *state) { - char buf[8]; - if (state->rolled_value == 0) { - if (state->dice_sides == 100) { - sprintf(buf, " C "); - } else { - sprintf(buf, "%2d ", state->dice_sides); - } - } else if (state->dice_sides == 2) { - if (state->rolled_value == 1) { - sprintf(buf, "%2d H", state->dice_sides); - } else { - sprintf(buf, "%2d T", state->dice_sides); - } - } else if (state->dice_sides == 100) { - sprintf(buf, " C %3d", state->rolled_value); - } else { - sprintf(buf, "%2d %3d", state->dice_sides, state->rolled_value); - } - watch_display_string(buf, 4); -} - -static void generate_random_number(probability_state_t *state) { - // Emulator: use rand. Hardware: use arc4random. - #if __EMSCRIPTEN__ - state->rolled_value = rand() % state->dice_sides + 1; - #else - state->rolled_value = arc4random_uniform(state->dice_sides) + 1; - #endif -} - -static void display_dice_roll_animation(probability_state_t *state) { - if (state->is_rolling) { - if (state->animation_frame == 0) { - watch_display_string(" ", 7); - watch_set_pixel(1, 4); - watch_set_pixel(1, 6); - state->animation_frame = 1; - } else if (state->animation_frame == 1) { - watch_clear_pixel(1, 4); - watch_clear_pixel(1, 6); - watch_set_pixel(2, 4); - watch_set_pixel(0, 6); - state->animation_frame = 2; - } else if (state->animation_frame == 2) { - watch_clear_pixel(2, 4); - watch_clear_pixel(0, 6); - watch_set_pixel(2, 5); - watch_set_pixel(0, 5); - state->animation_frame = 3; - } else if (state->animation_frame == 3) { - state->animation_frame = 0; - state->is_rolling = false; - movement_request_tick_frequency(1); - display_dice_roll(state); - } - } -} - - -// --------------------------- -// Standard watch face methods -// --------------------------- -void probability_face_setup(uint8_t watch_face_index, void ** context_ptr) { - (void) watch_face_index; - if (*context_ptr == NULL) { - *context_ptr = malloc(sizeof(probability_state_t)); - memset(*context_ptr, 0, sizeof(probability_state_t)); - } - // Emulator only: Seed random number generator - #if __EMSCRIPTEN__ - srand(time(NULL)); - #endif -} - -void probability_face_activate(void *context) { - probability_state_t *state = (probability_state_t *)context; - - state->dice_sides = DEFAULT_DICE_SIDES; - state->rolled_value = 0; - watch_display_string("PR", 0); -} - -bool probability_face_loop(movement_event_t event, void *context) { - probability_state_t *state = (probability_state_t *)context; - - if (state->is_rolling && event.event_type != EVENT_TICK) { - return true; - } - - switch (event.event_type) { - case EVENT_ACTIVATE: - display_dice_roll(state); - break; - case EVENT_TICK: - display_dice_roll_animation(state); - break; - case EVENT_LIGHT_BUTTON_DOWN: - // Change how many sides the die has - for (int i = 0; i < NUM_DICE_TYPES; i++) { - if (DICE_TYPES[i] == state->dice_sides) { - if (i == NUM_DICE_TYPES - 1) { - state->dice_sides = DICE_TYPES[0]; - } else { - state->dice_sides = DICE_TYPES[i + 1]; - } - break; - } - } - state->rolled_value = 0; - display_dice_roll(state); - break; - case EVENT_ALARM_BUTTON_UP: - // Roll the die - generate_random_number(state); - state->is_rolling = true; - // Dice rolling animation begins on next tick and new roll will be displayed on completion - movement_request_tick_frequency(PROBABILITY_ANIMATION_TICK_FREQUENCY); - break; - case EVENT_LOW_ENERGY_UPDATE: - watch_display_string("SLEEP ", 4); - break; - default: - movement_default_loop_handler(event); - break; - } - - return true; -} - -void probability_face_resign(void *context) { - (void) context; -} diff --git a/movement_faces.h b/movement_faces.h index 6e12fe31..145d6eab 100644 --- a/movement_faces.h +++ b/movement_faces.h @@ -57,4 +57,5 @@ #include "periodic_table_face.h" #include "squash_face.h" #include "totp_face.h" +#include "probability_face.h" // New includes go above this line. diff --git a/watch-faces.mk b/watch-faces.mk index bcb168b8..06a81231 100644 --- a/watch-faces.mk +++ b/watch-faces.mk @@ -28,6 +28,7 @@ SRCS += \ ./watch-faces/settings/nanosec_face.c \ ./watch-faces/io/chirpy_demo_face.c \ ./watch-faces/io/irda_upload_face.c \ + ./watch-faces/complication/probability_face.c \ ./watch-faces/clock/close_enough_face.c \ ./watch-faces/complication/tarot_face.c \ ./watch-faces/complication/kitchen_conversions_face.c \ diff --git a/watch-faces/complication/probability_face.c b/watch-faces/complication/probability_face.c new file mode 100644 index 00000000..b9a991dc --- /dev/null +++ b/watch-faces/complication/probability_face.c @@ -0,0 +1,303 @@ +/* + * MIT License + * + * Copyright (c) 2022 Spencer Bywater + * + * 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. + */ + +// Emulator only: need time() to seed the random number generator. +#if __EMSCRIPTEN__ +#include +#endif + +#include +#include +#include "probability_face.h" +#include "watch_common_display.h" + +#define DEFAULT_DICE_SIDES 2 +#define PROBABILITY_ANIMATION_TICK_FREQUENCY 8 +#define TAP_DETECTION_SECONDS 5 +#define ANIMATION_FRAMES 4 +#define SEGMENTS_PER_FRAME 2 +const uint16_t NUM_DICE_TYPES = 8; // Keep this consistent with # of dice types below +const uint16_t DICE_TYPES[] = {2, 4, 6, 8, 10, 12, 20, 100}; + +// Animation frame data: each frame defines which pixels to set +// Each frame can have up to SEGMENTS_PER_FRAME pixels +// Use {255, 255} to indicate end of pixel list for a frame +typedef struct { + uint8_t com; + uint8_t seg; +} com_seg_t; + +static const com_seg_t classic_lcd_animation_frames[ANIMATION_FRAMES][SEGMENTS_PER_FRAME] = { + // Frame 0: Second #1 F and C + {{1, 4}, {1, 6}}, + // Frame 1: Second #1 A and D + {{2, 4}, {0, 6}}, + // Frame 2: Second #1 B and E + {{2, 5}, {0, 5}}, + // Frame 3: No pixels set (end animation) + {{255, 255}, {255, 255}} +}; + +static const com_seg_t custom_lcd_animation_frames[ANIMATION_FRAMES][SEGMENTS_PER_FRAME] = { + // Frame 0: Second #1 F and C + {{2, 6}, {2, 7}}, + // Frame 1: Second #1 A and D + {{3, 6}, {0, 7}}, + // Frame 2: Second #1 B and E + {{3, 7}, {0, 6}}, + // Frame 3: No pixels set (end animation) + {{255, 255}, {255, 255}} +}; + +// -------------- +// Custom methods +// -------------- + +static void abort_tap_detection(probability_state_t *state) { + state->tap_detection_ticks = 0; + movement_disable_tap_detection_if_available(); +} + +static void cycle_dice_type(probability_state_t *state) { + // Change how many sides the die has + for (int i = 0; i < NUM_DICE_TYPES; i++) + { + if (DICE_TYPES[i] == state->dice_sides) + { + if (i == NUM_DICE_TYPES - 1) + { + state->dice_sides = DICE_TYPES[0]; + } + else + { + state->dice_sides = DICE_TYPES[i + 1]; + } + break; + } + } + state->rolled_value = 0; +} + +static void display_dice_roll(probability_state_t *state) +{ + char buf[8]; + + // Display die type in top right position + if (state->dice_sides == 100) { + // Show "00" for d100 + watch_display_text_with_fallback(WATCH_POSITION_TOP_RIGHT, "00", " C"); + } else { + sprintf(buf, "%2d", state->dice_sides); + watch_display_text(WATCH_POSITION_TOP_RIGHT, buf); + } + + // Display rolled value + if (state->rolled_value == 0) { + // No roll yet - show dashes + watch_display_text(WATCH_POSITION_BOTTOM, "---- "); + } else if (state->dice_sides == 2) { + // Coin flip: show "Heads" or "Tails" across hours, minutes, and first digit of seconds + if (state->rolled_value == 1) { + // Heads + watch_display_text(WATCH_POSITION_BOTTOM, "HEAdS "); + } else { + // Tails + watch_display_text(WATCH_POSITION_BOTTOM, "TAiLS "); + } + } else { + // Normal case: show rolled value using hours and minutes + if (state->rolled_value == 100) { + // Show " 1:00" for 100 + watch_display_text(WATCH_POSITION_BOTTOM, " 100"); + } else { + // Show " :XX" for 1-99 + sprintf(buf, "%4d", state->rolled_value); + watch_display_text(WATCH_POSITION_BOTTOM, buf); + } + } +} + +static void generate_random_number(probability_state_t *state) +{ +// Emulator: use rand. Hardware: use arc4random. +#if __EMSCRIPTEN__ + state->rolled_value = rand() % state->dice_sides + 1; +#else + state->rolled_value = arc4random_uniform(state->dice_sides) + 1; +#endif +} + +static void roll_dice(probability_state_t *state) +{ + generate_random_number(state); + state->is_rolling = true; + // Dice rolling animation begins on next tick and new roll will be displayed on completion + movement_request_tick_frequency(PROBABILITY_ANIMATION_TICK_FREQUENCY); +} + +static void display_dice_roll_animation(probability_state_t *state) +{ + if (state->is_rolling) + { + const com_seg_t (*animation_frames)[SEGMENTS_PER_FRAME] = classic_lcd_animation_frames; + if (watch_get_lcd_type() == WATCH_LCD_TYPE_CUSTOM) { + animation_frames = custom_lcd_animation_frames; + } + + // Clear main display areas on first frame + if (state->animation_frame == 0) + { + watch_display_text(WATCH_POSITION_HOURS, " "); + watch_display_text(WATCH_POSITION_MINUTES, " "); + watch_display_text(WATCH_POSITION_SECONDS, " "); + } + + // Clear pixels from previous frame (except on first frame) + if (state->animation_frame > 0) + { + const com_seg_t *prev_frame = animation_frames[state->animation_frame - 1]; + for (int i = 0; i < SEGMENTS_PER_FRAME; i++) + { + if (prev_frame[i].com == 255) break; // End of pixel list + watch_clear_pixel(prev_frame[i].com, prev_frame[i].seg); + } + } + + // Set pixels for current frame + const com_seg_t *current_frame = animation_frames[state->animation_frame]; + for (int i = 0; i < SEGMENTS_PER_FRAME; i++) + { + if (current_frame[i].com == 255) break; // End of pixel list + watch_set_pixel(current_frame[i].com, current_frame[i].seg); + } + + // Advance to next frame + state->animation_frame++; + + // Check if animation is complete + if (state->animation_frame >= ANIMATION_FRAMES) + { + state->animation_frame = 0; + state->is_rolling = false; + movement_request_tick_frequency(1); + display_dice_roll(state); + } + } +} + +// --------------------------- +// Standard watch face methods +// --------------------------- +void probability_face_setup(uint8_t watch_face_index, void **context_ptr) +{ + (void)watch_face_index; + if (*context_ptr == NULL) + { + *context_ptr = malloc(sizeof(probability_state_t)); + memset(*context_ptr, 0, sizeof(probability_state_t)); + } +// Emulator only: Seed random number generator +#if __EMSCRIPTEN__ + srand(time(NULL)); +#endif +} + +void probability_face_activate(void *context) +{ + probability_state_t *state = (probability_state_t *)context; + + state->dice_sides = DEFAULT_DICE_SIDES; + state->rolled_value = 0; + + // Display face identifier + watch_display_text_with_fallback(WATCH_POSITION_TOP, "Prb", "PR"); + + // Set tick frequency to 1 for proper tap detection timing + movement_request_tick_frequency(1); + + // Enable tap detection for a few seconds when face is activated + if (movement_enable_tap_detection_if_available()) { + state->tap_detection_ticks = TAP_DETECTION_SECONDS; + } +} + +bool probability_face_loop(movement_event_t event, void *context) +{ + probability_state_t *state = (probability_state_t *)context; + + if (state->is_rolling && event.event_type != EVENT_TICK) + { + return true; + } + + switch (event.event_type) + { + case EVENT_ACTIVATE: + display_dice_roll(state); + break; + case EVENT_TICK: + display_dice_roll_animation(state); + + if (!state->is_rolling && state->tap_detection_ticks > 0) { + state->tap_detection_ticks--; + if (state->tap_detection_ticks == 0) { + movement_disable_tap_detection_if_available(); + } + } + break; + case EVENT_LIGHT_BUTTON_DOWN: + // Cycle through die types + cycle_dice_type(state); + display_dice_roll(state); + break; + case EVENT_ALARM_BUTTON_UP: + // Roll the die + roll_dice(state); + break; + case EVENT_SINGLE_TAP: + // Single tap cycles die type + cycle_dice_type(state); + display_dice_roll(state); + + // Reset tap detection timer to keep accelerometer active + state->tap_detection_ticks = TAP_DETECTION_SECONDS; + break; + case EVENT_LOW_ENERGY_UPDATE: + watch_display_text(WATCH_POSITION_BOTTOM, "SLEEP "); + break; + default: + movement_default_loop_handler(event); + break; + } + + return true; +} + +void probability_face_resign(void *context) +{ + probability_state_t *state = (probability_state_t *)context; + + // Disable tap detection to save battery + abort_tap_detection(state); +} diff --git a/legacy/watch_faces/complication/probability_face.h b/watch-faces/complication/probability_face.h similarity index 73% rename from legacy/watch_faces/complication/probability_face.h rename to watch-faces/complication/probability_face.h index 6ce1b1f3..8f69b9f4 100644 --- a/legacy/watch_faces/complication/probability_face.h +++ b/watch-faces/complication/probability_face.h @@ -31,10 +31,24 @@ * This face is a dice-rolling random number generator. * Supports dice with 2, 4, 6, 8, 10, 12, 20, or 100 sides. * - * Press LIGHT to cycle through die type. - * The current die size is indicated on the left ("C" for 100) + * Display format: + * - Top: "Prb" (custom LCD) / "PR" (classic LCD) + * - Top right: Die type (2, 4, 6, 8, 10, 12, 20, or "00" for d100) + * - Hours:Minutes: Rolled value + * - No roll: "--:--" + * - Values 1-99: " :XX" + * - Value 100: " 1:00" + * - Coin flip: "HE:AD:S " or "TA:IL:S " * - * Press ALARM to roll the selected die. + * Controls: + * - LIGHT button: Cycle through die type + * - ALARM button: Roll the selected die + * - Single tap: Cycle through die type (accelerometer) + * - Double tap: Roll the selected die (accelerometer) + * + * Note: Accelerometer is enabled for 5 seconds when face activates and + * after each tap to conserve battery. It automatically disables after + * 5 seconds of no tap input. */ #include "movement.h" @@ -44,6 +58,7 @@ typedef struct { uint8_t rolled_value; uint8_t animation_frame; bool is_rolling; + uint8_t tap_detection_ticks; } probability_state_t; void probability_face_setup(uint8_t watch_face_index, void ** context_ptr);