From a0524ba4e6bbb65eef9a4a1f1d4f5039241a2cf9 Mon Sep 17 00:00:00 2001 From: Jeremy O'Brien Date: Tue, 10 Jan 2023 22:59:17 -0500 Subject: Implement tarot card face --- movement/make/Makefile | 1 + movement/movement_faces.h | 1 + movement/watch_faces/complication/tarot_face.c | 204 +++++++++++++++++++++++++ movement/watch_faces/complication/tarot_face.h | 50 ++++++ 4 files changed, 256 insertions(+) create mode 100644 movement/watch_faces/complication/tarot_face.c create mode 100644 movement/watch_faces/complication/tarot_face.h diff --git a/movement/make/Makefile b/movement/make/Makefile index a11f49f3..48d985f4 100644 --- a/movement/make/Makefile +++ b/movement/make/Makefile @@ -84,6 +84,7 @@ SRCS += \ ../watch_faces/complication/databank_face.c \ ../watch_faces/complication/tempchart_face.c \ ../watch_faces/complication/tally_face.c \ + ../watch_faces/complication/tarot_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 2506671f..8ca3930f 100644 --- a/movement/movement_faces.h +++ b/movement/movement_faces.h @@ -69,6 +69,7 @@ #include "databank_face.h" #include "tempchart_face.h" #include "tally_face.h" +#include "tarot_face.h" // New includes go above this line. #endif // MOVEMENT_FACES_H_ diff --git a/movement/watch_faces/complication/tarot_face.c b/movement/watch_faces/complication/tarot_face.c new file mode 100644 index 00000000..f845fa12 --- /dev/null +++ b/movement/watch_faces/complication/tarot_face.c @@ -0,0 +1,204 @@ +/* + * MIT License + * + * Copyright (c) 2022 Jeremy O'Brien + * Base code copied from Spencer Bywater's probability face + * + * 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 "tarot_face.h" + +#define NUM_TAROT_CARDS 22 +#define TAROT_ANIMATION_TICK_FREQUENCY 8 + + +// -------------- +// Custom methods +// -------------- + +static char *major_arcana[NUM_TAROT_CARDS] = { + " FOOL ", + "Mgcian", + "HiPrst", + "Empres", + "Empror", + "Hiroph", + "Lovers", + "Chriot", + "Strgth", + "Hermit", + " Frtun", + "Justce", + "HngMan", + " Death", + " tmprn", + " Devil", + " Tower", + " Star", + " MOON ", + " Sun ", + "Jdgmnt", + " World", +}; + +static void tarot_display(tarot_state_t *state) { + char buf[8]; + + if (state->drawn_cards[0] != 0xff) { + sprintf(buf, "%d", state->current_card + 1); + watch_display_string(buf, 3); + sprintf(buf, "%s", major_arcana[state->drawn_cards[state->current_card]]); + watch_display_string(buf, 4); + } +} + +static uint8_t draw_one_card(void) { + // Emulator: use rand. Hardware: use arc4random. +#if __EMSCRIPTEN__ + return rand() % NUM_TAROT_CARDS; +#else + return arc4random_uniform(NUM_TAROT_CARDS); +#endif +} + +static void pick_cards(tarot_state_t *state) { + // this is all a bit verbose but I prefer simplicity over clever tricks + state->drawn_cards[0] = draw_one_card(); + + state->drawn_cards[1] = draw_one_card(); + while (state->drawn_cards[1] == state->drawn_cards[0]) + state->drawn_cards[1] = draw_one_card(); + + state->drawn_cards[2] = draw_one_card(); + while (state->drawn_cards[2] == state->drawn_cards[1] || + state->drawn_cards[2] == state->drawn_cards[0]) + state->drawn_cards[2] = draw_one_card(); + + state->current_card = 0; +} + +static void display_animation(tarot_state_t *state) { + if (state->is_picking) { + 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_picking = false; + movement_request_tick_frequency(1); + tarot_display(state); + } + } +} + + +// --------------------------- +// Standard watch face methods +// --------------------------- +void tarot_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(tarot_state_t)); + memset(*context_ptr, 0, sizeof(tarot_state_t)); + } + // Emulator only: Seed random number generator + #if __EMSCRIPTEN__ + srand(time(NULL)); + #endif +} + +void tarot_face_activate(movement_settings_t *settings, void *context) { + (void) settings; + tarot_state_t *state = (tarot_state_t *)context; + + watch_display_string("TA", 0); + state->current_card = 0; + state->drawn_cards[0] = 0xff; +} + +bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + (void) settings; + tarot_state_t *state = (tarot_state_t *)context; + + if (state->is_picking && event.event_type != EVENT_TICK) { + return true; + } + + switch (event.event_type) { + case EVENT_ACTIVATE: + watch_display_string(" DRAW ", 4); + break; + case EVENT_TICK: + display_animation(state); + break; + case EVENT_MODE_BUTTON_UP: + movement_move_to_next_face(); + break; + case EVENT_LIGHT_BUTTON_UP: + // Change how many sides the die has + if (state->drawn_cards[0] != 0xff) { + state->current_card = (state->current_card + 1) % 3; + tarot_display(state); + } + break; + case EVENT_ALARM_BUTTON_UP: + // Draw cards + watch_display_string(" ", 4); + pick_cards(state); + state->is_picking = true; + // card picking animation begins on next tick and new cards will be displayed on completion + movement_request_tick_frequency(TAROT_ANIMATION_TICK_FREQUENCY); + break; + case EVENT_LOW_ENERGY_UPDATE: + watch_display_string("SLEEP ", 4); + break; + default: + break; + } + + return true; +} + +void tarot_face_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; +} diff --git a/movement/watch_faces/complication/tarot_face.h b/movement/watch_faces/complication/tarot_face.h new file mode 100644 index 00000000..1ef201fa --- /dev/null +++ b/movement/watch_faces/complication/tarot_face.h @@ -0,0 +1,50 @@ +/* + * MIT License + * + * Copyright (c) 2022 Jeremy O'Brien + * + * 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 TAROT_FACE_H_ +#define TAROT_FACE_H_ + +#include "movement.h" + +typedef struct { + uint8_t drawn_cards[3]; + uint8_t current_card; + uint8_t animation_frame; + bool is_picking; +} tarot_state_t; + +void tarot_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void tarot_face_activate(movement_settings_t *settings, void *context); +bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void tarot_face_resign(movement_settings_t *settings, void *context); + +#define tarot_face ((const watch_face_t){ \ + tarot_face_setup, \ + tarot_face_activate, \ + tarot_face_loop, \ + tarot_face_resign, \ + NULL, \ +}) + +#endif // TAROT_FACE_H_ -- cgit v1.2.3 From dc5290410eb56c7d4974950d469d84c3a6602eb4 Mon Sep 17 00:00:00 2001 From: joeycastillo Date: Wed, 11 Jan 2023 12:15:42 -0500 Subject: tweak some arcana for readability --- movement/watch_faces/complication/tarot_face.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/movement/watch_faces/complication/tarot_face.c b/movement/watch_faces/complication/tarot_face.c index f845fa12..fd8df139 100644 --- a/movement/watch_faces/complication/tarot_face.c +++ b/movement/watch_faces/complication/tarot_face.c @@ -43,23 +43,23 @@ static char *major_arcana[NUM_TAROT_CARDS] = { " FOOL ", "Mgcian", - "HiPrst", - "Empres", - "Empror", + "HPrsts", + "En&prs", // Empress + "En&por", // Emperor "Hiroph", "Lovers", "Chriot", "Strgth", - "Hermit", + "Hrn&it", // Hermit " Frtun", "Justce", - "HngMan", + "Hangn&", // Hangman " Death", " tmprn", " Devil", " Tower", " Star", - " MOON ", + "n&OON ", // Moon " Sun ", "Jdgmnt", " World", @@ -165,7 +165,9 @@ bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void switch (event.event_type) { case EVENT_ACTIVATE: - watch_display_string(" DRAW ", 4); + watch_display_string(" draWW", 4); + watch_clear_pixel(2, 3); + watch_clear_pixel(1, 4); break; case EVENT_TICK: display_animation(state); -- cgit v1.2.3 From a65d728877fc38ad5d1d749356befc2d7c6b0aba Mon Sep 17 00:00:00 2001 From: joeycastillo Date: Wed, 11 Jan 2023 12:49:44 -0500 Subject: tarot: tweak to display of 'world' card --- movement/watch_faces/complication/tarot_face.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/movement/watch_faces/complication/tarot_face.c b/movement/watch_faces/complication/tarot_face.c index fd8df139..16a989d8 100644 --- a/movement/watch_faces/complication/tarot_face.c +++ b/movement/watch_faces/complication/tarot_face.c @@ -62,7 +62,7 @@ static char *major_arcana[NUM_TAROT_CARDS] = { "n&OON ", // Moon " Sun ", "Jdgmnt", - " World", + "W orld", }; static void tarot_display(tarot_state_t *state) { @@ -73,6 +73,12 @@ static void tarot_display(tarot_state_t *state) { watch_display_string(buf, 3); sprintf(buf, "%s", major_arcana[state->drawn_cards[state->current_card]]); watch_display_string(buf, 4); + if (state->drawn_cards[state->current_card] == NUM_TAROT_CARDS - 1) { + // special tweak for "World" + watch_set_pixel(1, 20); + watch_set_pixel(2, 21); + watch_set_pixel(1, 17); + } } } -- cgit v1.2.3 From 0293daa926d5b426ad482145f33a5b818fa332d5 Mon Sep 17 00:00:00 2001 From: Jeremy O'Brien Date: Wed, 11 Jan 2023 11:39:54 -0500 Subject: Implement card flipping and arbitrary # of card draws up to 10 --- movement/watch_faces/complication/tarot_face.c | 125 +++++++++++++++++-------- movement/watch_faces/complication/tarot_face.h | 5 +- 2 files changed, 91 insertions(+), 39 deletions(-) diff --git a/movement/watch_faces/complication/tarot_face.c b/movement/watch_faces/complication/tarot_face.c index 16a989d8..7e40de9d 100644 --- a/movement/watch_faces/complication/tarot_face.c +++ b/movement/watch_faces/complication/tarot_face.c @@ -32,15 +32,16 @@ #include #include "tarot_face.h" -#define NUM_TAROT_CARDS 22 #define TAROT_ANIMATION_TICK_FREQUENCY 8 +#define FLIPPED_BIT_POS 7 +#define FLIPPED_MASK ((uint8_t)(1 << FLIPPED_BIT_POS)) // -------------- // Custom methods // -------------- -static char *major_arcana[NUM_TAROT_CARDS] = { +static char *major_arcana[] = { " FOOL ", "Mgcian", "HPrsts", @@ -64,47 +65,85 @@ static char *major_arcana[NUM_TAROT_CARDS] = { "Jdgmnt", "W orld", }; +#define NUM_TAROT_CARDS (sizeof(major_arcana) / sizeof(*major_arcana)) + +#define WORLD_CARD_INDEX (NUM_TAROT_CARDS - 1) + +static void init_deck(tarot_state_t *state) { + memset(state->drawn_cards, 0xff, sizeof(state->drawn_cards)); + state->current_card = 0; +} static void tarot_display(tarot_state_t *state) { - char buf[8]; - - if (state->drawn_cards[0] != 0xff) { - sprintf(buf, "%d", state->current_card + 1); - watch_display_string(buf, 3); - sprintf(buf, "%s", major_arcana[state->drawn_cards[state->current_card]]); - watch_display_string(buf, 4); - if (state->drawn_cards[state->current_card] == NUM_TAROT_CARDS - 1) { - // special tweak for "World" - watch_set_pixel(1, 20); - watch_set_pixel(2, 21); - watch_set_pixel(1, 17); - } + char buf[9]; + uint8_t card; + bool flipped; + + if (state->drawn_cards[0] == 0xff) { + /* deck is initialized; show current draw mode */ + watch_clear_indicator(WATCH_INDICATOR_SIGNAL); + sprintf(buf, "%2d draWW", state->num_cards_to_draw); + watch_display_string(buf, 2); + watch_clear_pixel(2, 3); + watch_clear_pixel(1, 4); + return; + } + + sprintf(buf, "%2d", state->current_card + 1); + watch_display_string(buf, 2); + + card = state->drawn_cards[state->current_card]; + flipped = (card & FLIPPED_MASK) ? true : false; // check flipped bit + card &= ~FLIPPED_MASK; // remove the flipped bit + sprintf(buf, "%s", major_arcana[card]); + watch_display_string(buf, 4); + if (state->drawn_cards[state->current_card] == WORLD_CARD_INDEX) { + // special tweak for "World" + watch_set_pixel(1, 20); + watch_set_pixel(2, 21); + watch_set_pixel(1, 17); + } + if (flipped) { + watch_set_indicator(WATCH_INDICATOR_SIGNAL); + } else { + watch_clear_indicator(WATCH_INDICATOR_SIGNAL); } } -static uint8_t draw_one_card(void) { +static uint8_t get_rand_num(uint8_t num_values) { // Emulator: use rand. Hardware: use arc4random. #if __EMSCRIPTEN__ - return rand() % NUM_TAROT_CARDS; + return rand() % num_values; #else - return arc4random_uniform(NUM_TAROT_CARDS); + return arc4random_uniform(num_values); #endif } -static void pick_cards(tarot_state_t *state) { - // this is all a bit verbose but I prefer simplicity over clever tricks - state->drawn_cards[0] = draw_one_card(); +static uint8_t draw_one_card(void) { + return get_rand_num(NUM_TAROT_CARDS); +} - state->drawn_cards[1] = draw_one_card(); - while (state->drawn_cards[1] == state->drawn_cards[0]) - state->drawn_cards[1] = draw_one_card(); +static bool already_drawn(tarot_state_t *state, uint8_t drawn_card) { + for (int i = 0; state->drawn_cards[i] != 0xff && i < state->num_cards_to_draw; i++) { + if ((state->drawn_cards[i] & ~FLIPPED_MASK) == drawn_card) { + return true; + } + } - state->drawn_cards[2] = draw_one_card(); - while (state->drawn_cards[2] == state->drawn_cards[1] || - state->drawn_cards[2] == state->drawn_cards[0]) - state->drawn_cards[2] = draw_one_card(); + return false; +} - state->current_card = 0; +static void pick_cards(tarot_state_t *state) { + uint8_t card; + + for (int i = 0; i < state->num_cards_to_draw; i++) { + card = draw_one_card(); + while (already_drawn(state, card)) { + card = draw_one_card(); + } + card |= get_rand_num(2) << FLIPPED_BIT_POS; // randomly flip the card + state->drawn_cards[i] = card; + } } static void display_animation(tarot_state_t *state) { @@ -157,8 +196,8 @@ void tarot_face_activate(movement_settings_t *settings, void *context) { tarot_state_t *state = (tarot_state_t *)context; watch_display_string("TA", 0); - state->current_card = 0; - state->drawn_cards[0] = 0xff; + init_deck(state); + state->num_cards_to_draw = 3; } bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { @@ -171,9 +210,7 @@ bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void switch (event.event_type) { case EVENT_ACTIVATE: - watch_display_string(" draWW", 4); - watch_clear_pixel(2, 3); - watch_clear_pixel(1, 4); + tarot_display(state); break; case EVENT_TICK: display_animation(state); @@ -182,15 +219,27 @@ bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void movement_move_to_next_face(); break; case EVENT_LIGHT_BUTTON_UP: - // Change how many sides the die has - if (state->drawn_cards[0] != 0xff) { - state->current_card = (state->current_card + 1) % 3; - tarot_display(state); + // cycle through the drawn cards + if (state->drawn_cards[0] == 0xff) { + // deck is inited; cycle through # cards to draw + state->num_cards_to_draw++; + if (state->num_cards_to_draw > 10) { + state->num_cards_to_draw = 3; + } + } else { + state->current_card = (state->current_card + 1) % state->num_cards_to_draw; } + tarot_display(state); + break; + case EVENT_LIGHT_LONG_PRESS: + init_deck(state); + tarot_display(state); break; case EVENT_ALARM_BUTTON_UP: // Draw cards watch_display_string(" ", 4); + watch_clear_indicator(WATCH_INDICATOR_SIGNAL); + init_deck(state); pick_cards(state); state->is_picking = true; // card picking animation begins on next tick and new cards will be displayed on completion diff --git a/movement/watch_faces/complication/tarot_face.h b/movement/watch_faces/complication/tarot_face.h index 1ef201fa..e9ffc896 100644 --- a/movement/watch_faces/complication/tarot_face.h +++ b/movement/watch_faces/complication/tarot_face.h @@ -27,10 +27,13 @@ #include "movement.h" +#define MAX_CARDS_TO_DRAW 10 + typedef struct { - uint8_t drawn_cards[3]; + uint8_t drawn_cards[MAX_CARDS_TO_DRAW]; uint8_t current_card; uint8_t animation_frame; + uint8_t num_cards_to_draw; bool is_picking; } tarot_state_t; -- cgit v1.2.3 From e43a43944fc3766effb01058a2bc64701a27009c Mon Sep 17 00:00:00 2001 From: Jeremy O'Brien Date: Wed, 11 Jan 2023 13:33:51 -0500 Subject: tarot: move comment to where it belongs --- movement/watch_faces/complication/tarot_face.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/movement/watch_faces/complication/tarot_face.c b/movement/watch_faces/complication/tarot_face.c index 7e40de9d..00d7a12d 100644 --- a/movement/watch_faces/complication/tarot_face.c +++ b/movement/watch_faces/complication/tarot_face.c @@ -219,7 +219,6 @@ bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void movement_move_to_next_face(); break; case EVENT_LIGHT_BUTTON_UP: - // cycle through the drawn cards if (state->drawn_cards[0] == 0xff) { // deck is inited; cycle through # cards to draw state->num_cards_to_draw++; @@ -227,6 +226,7 @@ bool tarot_face_loop(movement_event_t event, movement_settings_t *settings, void state->num_cards_to_draw = 3; } } else { + // cycle through the drawn cards state->current_card = (state->current_card + 1) % state->num_cards_to_draw; } tarot_display(state); -- cgit v1.2.3 From 784defcd552eaba04e45c7f6c77ced1515b0d329 Mon Sep 17 00:00:00 2001 From: joeycastillo Date: Wed, 11 Jan 2023 13:45:11 -0500 Subject: revert tweak to 'world' card --- movement/watch_faces/complication/tarot_face.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/movement/watch_faces/complication/tarot_face.c b/movement/watch_faces/complication/tarot_face.c index 00d7a12d..04c55360 100644 --- a/movement/watch_faces/complication/tarot_face.c +++ b/movement/watch_faces/complication/tarot_face.c @@ -63,7 +63,7 @@ static char *major_arcana[] = { "n&OON ", // Moon " Sun ", "Jdgmnt", - "W orld", + " World", }; #define NUM_TAROT_CARDS (sizeof(major_arcana) / sizeof(*major_arcana)) @@ -97,12 +97,6 @@ static void tarot_display(tarot_state_t *state) { card &= ~FLIPPED_MASK; // remove the flipped bit sprintf(buf, "%s", major_arcana[card]); watch_display_string(buf, 4); - if (state->drawn_cards[state->current_card] == WORLD_CARD_INDEX) { - // special tweak for "World" - watch_set_pixel(1, 20); - watch_set_pixel(2, 21); - watch_set_pixel(1, 17); - } if (flipped) { watch_set_indicator(WATCH_INDICATOR_SIGNAL); } else { -- cgit v1.2.3