Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions movement/make/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions movement/movement_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

const watch_face_t watch_faces[] = {
simple_clock_face,
squash_face,
world_clock_face,
sunrise_sunset_face,
moon_phase_face,
Expand Down
1 change: 1 addition & 0 deletions movement/movement_faces.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
173 changes: 173 additions & 0 deletions movement/watch_faces/complication/squash_face.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* MIT License
*
* 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
* 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 <stdlib.h>
#include <string.h>
#include <stdio.h>
#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];

watch_clear_display();

// The colon makes it easier to distinguis each players score
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;
}

62 changes: 62 additions & 0 deletions movement/watch_faces/complication/squash_face.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* MIT License
*
* 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
* 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;
} 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_