summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.devcontainer/Dockerfile50
-rw-r--r--.devcontainer/devcontainer.json17
-rwxr-xr-xmovement/make/Makefile1
-rw-r--r--movement/movement_faces.h1
-rw-r--r--movement/watch_faces/complication/probability_face.c182
-rw-r--r--movement/watch_faces/complication/probability_face.h51
6 files changed, 302 insertions, 0 deletions
diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile
new file mode 100644
index 00000000..55565a07
--- /dev/null
+++ b/.devcontainer/Dockerfile
@@ -0,0 +1,50 @@
+FROM ubuntu:22.10
+
+# TODO: install emscripten (https://emscripten.org/docs/getting_started/downloads.html)
+
+# TODO: Clean this up once buildkit is supported gracefully in devcontainers
+# https://github.com/microsoft/vscode-remote-release/issues/1409
+
+ARG X86_64_TOOLCHAIN_FILENAME="gcc-arm-none-eabi-10.3-2021.07-x86_64-linux.tar.bz2"
+ARG X86_64_TOOLCHAIN="https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.07/gcc-arm-none-eabi-10.3-2021.07-x86_64-linux.tar.bz2"
+ARG X86_64_TOOLCHAIN_CHECKSUM="b56ae639d9183c340f065ae114a30202"
+
+ARG AARCH64_TOOLCHAIN_FILENAME="gcc-arm-none-eabi-10.3-2021.07-aarch64-linux.tar.bz2"
+ARG AARCH64_TOOLCHAIN="https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.07/gcc-arm-none-eabi-10.3-2021.07-aarch64-linux.tar.bz2"
+ARG AARCH64_TOOLCHAIN_CHECKSUM="c20b0535d01f8d4418341d893c62a782"
+
+WORKDIR /setup
+
+# Install required packages
+RUN apt-get update \
+ && apt-get install -y --no-install-recommends \
+ # make is used as the build system
+ make \
+ # git is used for fetching submodules & interactive development
+ git \
+ # bzip2 is required to extract the Arm toolchain
+ bzip2 \
+ # ca certs need to be available for fetching git submodules
+ ca-certificates \
+ # python is used to convert binaries to uf2 files
+ python3 python-is-python3
+
+# Download and verify both x86-64 and aarch64 toolchains. This is unfortunate and
+# slows down the build, but it's a clean-ish option until buildkit can be used.
+ADD $X86_64_TOOLCHAIN $X86_64_TOOLCHAIN_FILENAME
+ADD $AARCH64_TOOLCHAIN $AARCH64_TOOLCHAIN_FILENAME
+
+RUN echo "${X86_64_TOOLCHAIN_CHECKSUM} ${X86_64_TOOLCHAIN_FILENAME}" | md5sum --check
+RUN echo "${AARCH64_TOOLCHAIN_CHECKSUM} ${AARCH64_TOOLCHAIN_FILENAME}" | md5sum --check
+
+# Extract toolchain directly into /usr
+RUN /bin/sh -c 'set -ex && \
+ ARCH=`uname -m` && \
+ if [ "$ARCH" = "x86_64" ]; then \
+ tar --strip-components=1 -C /usr -xjf $X86_64_TOOLCHAIN_FILENAME ; \
+ else \
+ tar --strip-components=1 -C /usr -xjf $AARCH64_TOOLCHAIN_FILENAME ; \
+ fi'
+
+RUN rm $X86_64_TOOLCHAIN_FILENAME
+RUN rm $AARCH64_TOOLCHAIN_FILENAME \ No newline at end of file
diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json
new file mode 100644
index 00000000..a71a0f22
--- /dev/null
+++ b/.devcontainer/devcontainer.json
@@ -0,0 +1,17 @@
+// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
+// https://github.com/microsoft/vscode-dev-containers/tree/v0.231.6/containers/docker-existing-dockerfile
+{
+ "name": "GNU Arm Embedded Environment",
+ // Sets the run context to one level up instead of the .devcontainer folder.
+ "context": "..",
+ // Set the location of the dockerfile to use
+ "dockerFile": "Dockerfile",
+ // Set *default* container specific settings.json values on container create.
+ "settings": {},
+ // Add the IDs of extensions you want installed when the container is created.
+ "extensions": [
+ "ms-vscode.cpptools"
+ ]
+ // Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
+ // "remoteUser": "vscode"
+} \ No newline at end of file
diff --git a/movement/make/Makefile b/movement/make/Makefile
index 0936a26c..7c7cf2d6 100755
--- a/movement/make/Makefile
+++ b/movement/make/Makefile
@@ -61,6 +61,7 @@ SRCS += \
../watch_faces/complication/orrery_face.c \
../watch_faces/complication/astronomy_face.c \
../watch_faces/complication/tomato_face.c \
+ ../watch_faces/complication/probability_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 e7e78ff4..7c51b6ff 100644
--- a/movement/movement_faces.h
+++ b/movement/movement_faces.h
@@ -52,6 +52,7 @@
#include "orrery_face.h"
#include "astronomy_face.h"
#include "tomato_face.h"
+#include "probability_face.h"
// New includes go above this line.
#endif // MOVEMENT_FACES_H_
diff --git a/movement/watch_faces/complication/probability_face.c b/movement/watch_faces/complication/probability_face.c
new file mode 100644
index 00000000..7b056b33
--- /dev/null
+++ b/movement/watch_faces/complication/probability_face.c
@@ -0,0 +1,182 @@
+/*
+ * 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 <time.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#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(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(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(movement_settings_t *settings, void *context) {
+ (void) settings;
+ 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, movement_settings_t *settings, void *context) {
+ (void) settings;
+ 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_MODE_BUTTON_UP:
+ movement_move_to_next_face();
+ break;
+ case EVENT_LIGHT_BUTTON_UP:
+ // 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:
+ break;
+ }
+
+ return true;
+}
+
+void probability_face_resign(movement_settings_t *settings, void *context) {
+ (void) settings;
+ (void) context;
+}
diff --git a/movement/watch_faces/complication/probability_face.h b/movement/watch_faces/complication/probability_face.h
new file mode 100644
index 00000000..c6d3638f
--- /dev/null
+++ b/movement/watch_faces/complication/probability_face.h
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+
+#ifndef PROBABILITY_FACE_H_
+#define PROBABILITY_FACE_H_
+
+#include "movement.h"
+
+typedef struct {
+ uint8_t dice_sides;
+ uint8_t rolled_value;
+ uint8_t animation_frame;
+ bool is_rolling;
+} probability_state_t;
+
+void probability_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void probability_face_activate(movement_settings_t *settings, void *context);
+bool probability_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void probability_face_resign(movement_settings_t *settings, void *context);
+
+#define probability_face ((const watch_face_t){ \
+ probability_face_setup, \
+ probability_face_activate, \
+ probability_face_loop, \
+ probability_face_resign, \
+ NULL, \
+})
+
+#endif // PROBABILITY_FACE_H_
+