From 391f8af7730a743d900eaef36ac6eb958bfa2a16 Mon Sep 17 00:00:00 2001 From: Johan Oskarsson Date: Mon, 14 Apr 2025 17:09:34 -0700 Subject: [PATCH 1/3] Add face for keeping score in squash games --- movement/make/Makefile | 1 + movement/movement_config.h | 1 + movement/movement_faces.h | 1 + .../watch_faces/complication/squash_face.c | 169 ++++++++++++++++++ .../watch_faces/complication/squash_face.h | 63 +++++++ 5 files changed, 235 insertions(+) create mode 100644 movement/watch_faces/complication/squash_face.c create mode 100644 movement/watch_faces/complication/squash_face.h diff --git a/movement/make/Makefile b/movement/make/Makefile index 2116ebaba..f48a358a0 100644 --- a/movement/make/Makefile +++ b/movement/make/Makefile @@ -149,6 +149,7 @@ SRCS += \ ../watch_faces/sensor/accel_interrupt_count_face.c \ ../watch_faces/complication/metronome_face.c \ ../watch_faces/complication/smallchess_face.c \ + ../watch_faces/complication/squash_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_config.h b/movement/movement_config.h index 10a30af77..5fff94069 100644 --- a/movement/movement_config.h +++ b/movement/movement_config.h @@ -29,6 +29,7 @@ const watch_face_t watch_faces[] = { simple_clock_face, + squash_face, world_clock_face, sunrise_sunset_face, moon_phase_face, diff --git a/movement/movement_faces.h b/movement/movement_faces.h index 630f264aa..01ff9a2ca 100644 --- a/movement/movement_faces.h +++ b/movement/movement_faces.h @@ -123,6 +123,7 @@ #include "accel_interrupt_count_face.h" #include "metronome_face.h" #include "smallchess_face.h" +#include "squash_face.h" // New includes go above this line. #endif // MOVEMENT_FACES_H_ diff --git a/movement/watch_faces/complication/squash_face.c b/movement/watch_faces/complication/squash_face.c new file mode 100644 index 000000000..742263f01 --- /dev/null +++ b/movement/watch_faces/complication/squash_face.c @@ -0,0 +1,169 @@ +/* + * MIT License + * + * Copyright (c) 2025 <#author_name#> + * + * 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 +#include "squash_face.h" + +#define POINTS_TO_WIN_GAME 11 +#define MIN_POINT_DIFFERENCE 2 +#define GAMES_TO_WIN_MATCH 3 + +static void update_display(squash_state_t *state) { + char buf[16]; + + // Clear the display + watch_clear_display(); + + watch_set_colon(); + + // Show games won in small digits + sprintf(buf, "%d %d", state->player1_games, state->player2_games); + watch_display_string(buf, 0); + + // Show current score: P1-P2 + sprintf(buf, "%02d%02d", state->player1_score, state->player2_score); + watch_display_string(buf, 4); + + // If game over, show indicator + if (state->is_game_over) { + watch_set_indicator(WATCH_INDICATOR_LAP); + } else { + watch_clear_indicator(WATCH_INDICATOR_LAP); + } +} + +static void check_game_status(squash_state_t *state) { + // Check if a player has won the current game + if ((state->player1_score >= POINTS_TO_WIN_GAME || state->player2_score >= POINTS_TO_WIN_GAME) && + abs(state->player1_score - state->player2_score) >= MIN_POINT_DIFFERENCE) { + + // Award a game to the winner + if (state->player1_score > state->player2_score) { + state->player1_games++; + movement_play_signal(); + } else { + state->player2_games++; + movement_play_signal(); + } + + // Check if the match is over + if (state->player1_games >= GAMES_TO_WIN_MATCH || state->player2_games >= GAMES_TO_WIN_MATCH) { + state->is_game_over = true; + movement_play_signal(); + } else { + // Reset for next game + state->player1_score = 0; + state->player2_score = 0; + } + } +} + +static void reset_match(squash_state_t *state) { + state->player1_score = 0; + state->player2_score = 0; + state->player1_games = 0; + state->player2_games = 0; + state->is_game_over = false; + + movement_play_signal(); +} + +void squash_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(squash_state_t)); + memset(*context_ptr, 0, sizeof(squash_state_t)); + } +} + +void squash_face_activate(movement_settings_t *settings, void *context) { + (void) settings; + squash_state_t *state = (squash_state_t *)context; + + update_display(state); +} + +bool squash_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + squash_state_t *state = (squash_state_t *)context; + + switch (event.event_type) { + case EVENT_ACTIVATE: + update_display(state); + break; + + case EVENT_LIGHT_BUTTON_DOWN: + // Suppress default LED behavior + break; + + case EVENT_LIGHT_BUTTON_UP: + if (!state->is_game_over) { + // Increment player 1's score + state->player1_score++; + check_game_status(state); + update_display(state); + } + break; + + case EVENT_ALARM_BUTTON_UP: + if (!state->is_game_over) { + // Increment player 2's score + state->player2_score++; + check_game_status(state); + update_display(state); + } + break; + + case EVENT_MODE_LONG_PRESS: + // Reset the match + reset_match(state); + update_display(state); + return true; + + case EVENT_ALARM_LONG_PRESS: + update_display(state); + break; + + case EVENT_TIMEOUT: + break; + + case EVENT_LOW_ENERGY_UPDATE: + update_display(state); + watch_start_tick_animation(500); + break; + + default: + return movement_default_loop_handler(event, settings); + } + + return true; +} + +void squash_face_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; +} + diff --git a/movement/watch_faces/complication/squash_face.h b/movement/watch_faces/complication/squash_face.h new file mode 100644 index 000000000..80ba0a189 --- /dev/null +++ b/movement/watch_faces/complication/squash_face.h @@ -0,0 +1,63 @@ +/* + * MIT License + * + * Copyright (c) 2025 <#author_name#> + * + * 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 SQUASH_FACE_H_ +#define SQUASH_FACE_H_ + +#include "movement.h" + +/* + * Squash Scoring Face + * + * Keep track of scores in a squash match: + * - Light button: Increment player 1's score + * - Alarm button: Increment player 2's score + * - Mode button long press: Reset scores + * - Mode button: Switch to next watch face + */ + +typedef struct { + uint8_t player1_score; + uint8_t player2_score; + uint8_t player1_games; + uint8_t player2_games; + bool is_game_over; + uint8_t display_mode; // 0 = current score, 1 = games won +} squash_state_t; + +void squash_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void squash_face_activate(movement_settings_t *settings, void *context); +bool squash_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void squash_face_resign(movement_settings_t *settings, void *context); + +#define squash_face ((const watch_face_t){ \ + squash_face_setup, \ + squash_face_activate, \ + squash_face_loop, \ + squash_face_resign, \ + NULL, \ +}) + +#endif // SQUASH_FACE_H_ + From a58ea3f9d101c2a98786f169963e8fff43a332be Mon Sep 17 00:00:00 2001 From: Johan Oskarsson Date: Tue, 15 Apr 2025 07:54:33 -0700 Subject: [PATCH 2/3] Minor cleanup --- movement/watch_faces/complication/squash_face.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/movement/watch_faces/complication/squash_face.c b/movement/watch_faces/complication/squash_face.c index 742263f01..9eec8addf 100644 --- a/movement/watch_faces/complication/squash_face.c +++ b/movement/watch_faces/complication/squash_face.c @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2025 <#author_name#> + * Copyright (c) 2025 Johan Oskarsson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -27,16 +27,20 @@ #include #include "squash_face.h" +// https://en.wikipedia.org/wiki/Squash_(sport)#Scoring_system +// Using "point-a-rally scoring (PARS)" to 11 below. #define POINTS_TO_WIN_GAME 11 +// For example if both players have 10 points one of them has to get to 12, not 11, to win. #define MIN_POINT_DIFFERENCE 2 +// First to 3 games won (max 5 games played) #define GAMES_TO_WIN_MATCH 3 static void update_display(squash_state_t *state) { char buf[16]; - // Clear the display watch_clear_display(); + // The colon makes it easier to distinguis each players score watch_set_colon(); // Show games won in small digits From 8c829535beaa2f11b36370e0d7b29d7eb8a53754 Mon Sep 17 00:00:00 2001 From: Johan Oskarsson Date: Tue, 15 Apr 2025 08:00:09 -0700 Subject: [PATCH 3/3] Remove unused variable --- movement/watch_faces/complication/squash_face.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/movement/watch_faces/complication/squash_face.h b/movement/watch_faces/complication/squash_face.h index 80ba0a189..47d71d588 100644 --- a/movement/watch_faces/complication/squash_face.h +++ b/movement/watch_faces/complication/squash_face.h @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2025 <#author_name#> + * Copyright (c) 2025 Johan Oskarsson * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -43,7 +43,6 @@ typedef struct { uint8_t player1_games; uint8_t player2_games; bool is_game_over; - uint8_t display_mode; // 0 = current score, 1 = games won } squash_state_t; void squash_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);