diff options
Diffstat (limited to 'movement')
29 files changed, 1595 insertions, 59 deletions
diff --git a/movement/alt_fw/standard.h b/movement/alt_fw/backer.h index 3abcf457..3abcf457 100644 --- a/movement/alt_fw/standard.h +++ b/movement/alt_fw/backer.h diff --git a/movement/alt_fw/deep_space_now.h b/movement/alt_fw/deep_space_now.h index f07fe2cb..6cb34237 100644 --- a/movement/alt_fw/deep_space_now.h +++ b/movement/alt_fw/deep_space_now.h @@ -32,15 +32,15 @@ #define MOVEMENT_CUSTOM_BOOT_COMMANDS() { \ /* Standard Time */\ - /*\ watch_store_backup_data(0x1e0c0c, 4);\ watch_store_backup_data(0x010115, 5);\ watch_store_backup_data(0x130105, 6);\ - */\ /* Daylight Saving Time */\ + /*\ watch_store_backup_data(0x1f0c0c, 4);\ watch_store_backup_data(0x020115, 5);\ watch_store_backup_data(0x110105, 6);\ + */\ watch_store_backup_data(0x0597b9, 2);\ } diff --git a/movement/alt_fw/the_stargazer.h b/movement/alt_fw/the_stargazer.h index 50a89aad..a13dc3ac 100644 --- a/movement/alt_fw/the_stargazer.h +++ b/movement/alt_fw/the_stargazer.h @@ -27,11 +27,8 @@ #include "movement_faces.h" -#define MOVEMENT_CUSTOM_BOOT_COMMANDS() { \ - movement_state.settings.bit.led_green_color = 0x0;\ - movement_state.settings.bit.led_red_color = 0xF;\ - watch_store_backup_data(movement_state.settings.reg, 0);\ -} +#define MOVEMENT_DEFAULT_RED_COLOR 0xF +#define MOVEMENT_DEFAULT_GREEN_COLOR 0x0 const watch_face_t watch_faces[] = { simple_clock_face, diff --git a/movement/filesystem.c b/movement/filesystem.c index d122a7fa..2b345eda 100644 --- a/movement/filesystem.c +++ b/movement/filesystem.c @@ -166,6 +166,29 @@ bool filesystem_read_file(char *filename, char *buf, int32_t length) { return false; } +bool filesystem_read_line(char *filename, char *buf, int32_t *offset, int32_t length) { + memset(buf, 0, length + 1); + int32_t file_size = filesystem_get_file_size(filename); + if (file_size > 0) { + int err = lfs_file_open(&lfs, &file, filename, LFS_O_RDONLY); + if (err < 0) return false; + err = lfs_file_seek(&lfs, &file, *offset, LFS_SEEK_SET); + if (err < 0) return false; + err = lfs_file_read(&lfs, &file, buf, min(length - 1, file_size - *offset)); + if (err < 0) return false; + for(int i = 0; i < length; i++) { + (*offset)++; + if (buf[i] == '\n') { + buf[i] = 0; + break; + } + } + return lfs_file_close(&lfs, &file) == LFS_ERR_OK; + } + + return false; +} + static void filesystem_cat(char *filename) { info.type = 0; lfs_stat(&lfs, filename, &info); @@ -191,6 +214,14 @@ bool filesystem_write_file(char *filename, char *text, int32_t length) { return lfs_file_close(&lfs, &file) == LFS_ERR_OK; } +bool filesystem_append_file(char *filename, char *text, int32_t length) { + int err = lfs_file_open(&lfs, &file, filename, LFS_O_WRONLY | LFS_O_CREAT | LFS_O_APPEND); + if (err < 0) return false; + err = lfs_file_write(&lfs, &file, text, length); + if (err < 0) return false; + return lfs_file_close(&lfs, &file) == LFS_ERR_OK; +} + void filesystem_process_command(char *line) { printf("$ %s", line); char *command = strtok(line, " \n"); @@ -223,7 +254,7 @@ void filesystem_process_command(char *line) { memset(text, 0, 248); size_t pos = 0; char *word = strtok(NULL, " \n"); - while (strcmp(word, ">")) { + while (strcmp(word, ">") && strcmp(word, ">>")) { sprintf(text + pos, "%s ", word); pos += strlen(word) + 1; word = strtok(NULL, " \n"); @@ -235,8 +266,12 @@ void filesystem_process_command(char *line) { printf("usage: echo text > file\n"); } else if (strchr(filename, '/') || strchr(filename, '\\')) { printf("subdirectories are not supported\n"); - } else { + } else if (!strcmp(word, ">")) { filesystem_write_file(filename, text, strlen(text)); + filesystem_append_file(filename, "\n", 1); + } else if (!strcmp(word, ">>")) { + filesystem_append_file(filename, text, strlen(text)); + filesystem_append_file(filename, "\n", 1); } free(text); } else { diff --git a/movement/filesystem.h b/movement/filesystem.h index b0fb7f58..3cd3d092 100644 --- a/movement/filesystem.h +++ b/movement/filesystem.h @@ -69,6 +69,17 @@ int32_t filesystem_get_file_size(char *filename); */ bool filesystem_read_file(char *filename, char *buf, int32_t length); +/** @brief Reads a line from a file into a buffer + * @param filename the file you wish to read + * @param buf A buffer of at least length + 1 bytes; the file will be read into this buffer, + * and the last byte (buf[length]) will be set to 0 as a null terminator. + * @param offset Pointer to an int representing the offset into the file. This will be updated + * to reflect the offset of the next line. + * @param length The maximum number of bytes to read + * @return true if the read was successful; false otherwise + */ +bool filesystem_read_line(char *filename, char *buf, int32_t *offset, int32_t length); + /** @brief Writes file to the filesystem * @param filename the file you wish to write * @param text The contents of the file @@ -77,6 +88,14 @@ bool filesystem_read_file(char *filename, char *buf, int32_t length); */ bool filesystem_write_file(char *filename, char *text, int32_t length); +/** @brief Appends text to file on the filesystem + * @param filename the file you wish to write + * @param text The contents to write + * @param length The number of bytes to write + * @return true if the write was successful; false otherwise + */ +bool filesystem_append_file(char *filename, char *text, int32_t length); + /** @brief Handles the interactive file browser when Movement is plugged in to USB. * @param line The command that the user typed into the serial console. */ diff --git a/movement/lib/base32/base32.c b/movement/lib/base32/base32.c new file mode 100644 index 00000000..548f9a4a --- /dev/null +++ b/movement/lib/base32/base32.c @@ -0,0 +1,221 @@ +/** + * base32 (de)coder implementation as specified by RFC4648. + * + * Copyright (c) 2010 Adrien Kunysz + * + * 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 <assert.h> // assert() +#include <limits.h> // CHAR_BIT + +#include "base32.h" + +/** + * Let this be a sequence of plain data before encoding: + * + * 01234567 01234567 01234567 01234567 01234567 + * +--------+--------+--------+--------+--------+ + * |< 0 >< 1| >< 2 ><|.3 >< 4.|>< 5 ><.|6 >< 7 >| + * +--------+--------+--------+--------+--------+ + * + * There are 5 octets of 8 bits each in each sequence. + * There are 8 blocks of 5 bits each in each sequence. + * + * You probably want to refer to that graph when reading the algorithms in this + * file. We use "octet" instead of "byte" intentionnaly as we really work with + * 8 bits quantities. This implementation will probably not work properly on + * systems that don't have exactly 8 bits per (unsigned) char. + **/ + +static size_t min(size_t x, size_t y) +{ + return x < y ? x : y; +} + +static const unsigned char PADDING_CHAR = '='; + +/** + * Pad the given buffer with len padding characters. + */ +static void pad(unsigned char *buf, int len) +{ + for (int i = 0; i < len; i++) + buf[i] = PADDING_CHAR; +} + +/** + * This convert a 5 bits value into a base32 character. + * Only the 5 least significant bits are used. + */ +static unsigned char encode_char(unsigned char c) +{ + static unsigned char base32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + return base32[c & 0x1F]; // 0001 1111 +} + +/** + * Decode given character into a 5 bits value. + * Returns -1 iff the argument given was an invalid base32 character + * or a padding character. + */ +static int decode_char(unsigned char c) +{ + int retval = -1; + + if (c >= 'A' && c <= 'Z') + retval = c - 'A'; + if (c >= '2' && c <= '7') + retval = c - '2' + 26; + + assert(retval == -1 || ((retval & 0x1F) == retval)); + + return retval; +} + +/** + * Given a block id between 0 and 7 inclusive, this will return the index of + * the octet in which this block starts. For example, given 3 it will return 1 + * because block 3 starts in octet 1: + * + * +--------+--------+ + * | ......<|.3 >....| + * +--------+--------+ + * octet 1 | octet 2 + */ +static int get_octet(int block) +{ + assert(block >= 0 && block < 8); + return (block*5) / 8; +} + +/** + * Given a block id between 0 and 7 inclusive, this will return how many bits + * we can drop at the end of the octet in which this block starts. + * For example, given block 0 it will return 3 because there are 3 bits + * we don't care about at the end: + * + * +--------+- + * |< 0 >...| + * +--------+- + * + * Given block 1, it will return -2 because there + * are actually two bits missing to have a complete block: + * + * +--------+- + * |.....< 1|.. + * +--------+- + **/ +static int get_offset(int block) +{ + assert(block >= 0 && block < 8); + return (8 - 5 - (5*block) % 8); +} + +/** + * Like "b >> offset" but it will do the right thing with negative offset. + * We need this as bitwise shifting by a negative offset is undefined + * behavior. + */ +static unsigned char shift_right(unsigned char byte, int offset) +{ + if (offset > 0) + return byte >> offset; + else + return byte << -offset; +} + +static unsigned char shift_left(unsigned char byte, int offset) +{ + return shift_right(byte, - offset); +} + +/** + * Encode a sequence. A sequence is no longer than 5 octets by definition. + * Thus passing a length greater than 5 to this function is an error. Encoding + * sequences shorter than 5 octets is supported and padding will be added to the + * output as per the specification. + */ +static void encode_sequence(const unsigned char *plain, int len, unsigned char *coded) +{ + assert(CHAR_BIT == 8); // not sure this would work otherwise + assert(len >= 0 && len <= 5); + + for (int block = 0; block < 8; block++) { + int octet = get_octet(block); // figure out which octet this block starts in + int junk = get_offset(block); // how many bits do we drop from this octet? + + if (octet >= len) { // we hit the end of the buffer + pad(&coded[block], 8 - block); + return; + } + + unsigned char c = shift_right(plain[octet], junk); // first part + + if (junk < 0 // is there a second part? + && octet < len - 1) // is there still something to read? + { + c |= shift_right(plain[octet+1], 8 + junk); + } + coded[block] = encode_char(c); + } +} + +void base32_encode(const unsigned char *plain, size_t len, unsigned char *coded) +{ + // All the hard work is done in encode_sequence(), + // here we just need to feed it the data sequence by sequence. + for (size_t i = 0, j = 0; i < len; i += 5, j += 8) { + encode_sequence(&plain[i], min(len - i, 5), &coded[j]); + } +} + +static int decode_sequence(const unsigned char *coded, unsigned char *plain) +{ + assert(CHAR_BIT == 8); + assert(coded && plain); + + plain[0] = 0; + for (int block = 0; block < 8; block++) { + int offset = get_offset(block); + int octet = get_octet(block); + + int c = decode_char(coded[block]); + if (c < 0) // invalid char, stop here + return octet; + + plain[octet] |= shift_left(c, offset); + if (offset < 0) { // does this block overflows to next octet? + assert(octet < 4); + plain[octet+1] = shift_left(c, 8 + offset); + } + } + return 5; +} + +size_t base32_decode(const unsigned char *coded, unsigned char *plain) +{ + size_t written = 0; + for (size_t i = 0, j = 0; ; i += 8, j += 5) { + int n = decode_sequence(&coded[i], &plain[j]); + written += n; + if (n < 5) + return written; + } +} diff --git a/movement/lib/base32/base32.h b/movement/lib/base32/base32.h new file mode 100644 index 00000000..abf2d034 --- /dev/null +++ b/movement/lib/base32/base32.h @@ -0,0 +1,66 @@ +/** + * base32 (de)coder implementation as specified by RFC4648. + * + * Copyright (c) 2010 Adrien Kunysz + * + * 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 __BASE32_H_ +#define __BASE32_H_ + +#include <stddef.h> // size_t + +/** + * Returns the length of the output buffer required to encode len bytes of + * data into base32. This is a macro to allow users to define buffer size at + * compilation time. + */ +#define BASE32_LEN(len) (((len)/5)*8 + ((len) % 5 ? 8 : 0)) + +/** + * Returns the length of the output buffer required to decode a base32 string + * of len characters. Please note that len must be a multiple of 8 as per + * definition of a base32 string. This is a macro to allow users to define + * buffer size at compilation time. + */ +#define UNBASE32_LEN(len) (((len)/8)*5) + +/** + * Encode the data pointed to by plain into base32 and store the + * result at the address pointed to by coded. The "coded" argument + * must point to a location that has enough available space + * to store the whole coded string. The resulting string will only + * contain characters from the [A-Z2-7=] set. The "len" arguments + * define how many bytes will be read from the "plain" buffer. + **/ +void base32_encode(const unsigned char *plain, size_t len, unsigned char *coded); + +/** + * Decode the null terminated string pointed to by coded and write + * the decoded data into the location pointed to by plain. The + * "plain" argument must point to a location that has enough available + * space to store the whole decoded string. + * Returns the length of the decoded string. This may be less than + * expected due to padding. If an invalid base32 character is found + * in the coded string, decoding will stop at that point. + **/ +size_t base32_decode(const unsigned char *coded, unsigned char *plain); + +#endif diff --git a/movement/make/Makefile b/movement/make/Makefile index 61eb6c1e..60084594 100755..100644 --- a/movement/make/Makefile +++ b/movement/make/Makefile @@ -18,6 +18,7 @@ INCLUDES += \ -I../watch_faces/demo/ \ -I../../littlefs/ \ -I../lib/TOTP-MCU/ \ + -I../lib/base32/ \ -I../lib/sunriset/ \ -I../lib/vsop87/ \ -I../lib/astrolib/ \ @@ -31,6 +32,7 @@ INCLUDES += \ SRCS += \ ../lib/TOTP-MCU/sha1.c \ ../lib/TOTP-MCU/TOTP.c \ + ../lib/base32/base32.c \ ../lib/sunriset/sunriset.c \ ../lib/vsop87/vsop87a_milli.c \ ../lib/astrolib/astrolib.c \ @@ -41,6 +43,7 @@ SRCS += \ ../watch_faces/clock/simple_clock_face.c \ ../watch_faces/clock/world_clock_face.c \ ../watch_faces/clock/beats_face.c \ + ../watch_faces/clock/weeknumber_clock_face.c \ ../watch_faces/settings/preferences_face.c \ ../watch_faces/settings/set_time_face.c \ ../watch_faces/sensor/thermistor_readout_face.c \ @@ -55,6 +58,7 @@ SRCS += \ ../watch_faces/complication/day_one_face.c \ ../watch_faces/complication/stopwatch_face.c \ ../watch_faces/complication/totp_face.c \ + ../watch_faces/complication/totp_face_lfs.c \ ../watch_faces/complication/sunrise_sunset_face.c \ ../watch_faces/complication/countdown_face.c \ ../watch_faces/complication/sailing_face.c \ @@ -69,6 +73,7 @@ SRCS += \ ../watch_faces/complication/probability_face.c \ ../watch_faces/complication/wake_face.c \ ../watch_faces/demo/frequency_correction_face.c \ + ../watch_faces/complication/alarm_face.c \ ../watch_faces/complication/ratemeter_face.c \ # New watch faces go above this line. diff --git a/movement/make/make_alternate_fw.sh b/movement/make/make_alternate_fw.sh index 575c9e52..d1ce7673 100755 --- a/movement/make/make_alternate_fw.sh +++ b/movement/make/make_alternate_fw.sh @@ -3,7 +3,7 @@ fw_dir="firmware/download" sim_dir="firmware/simulate" colors=("green" "blue") -variants=("standard" "alt_time" "deep_space_now" "focus" "the_athlete" "the_backpacker" "the_stargazer") +variants=("standard" "backer" "alt_time" "deep_space_now" "focus" "the_athlete" "the_backpacker" "the_stargazer") if [ -d "$fw_dir" ] ; then rm -r "$fw_dir" @@ -25,12 +25,12 @@ do make LED=$COLOR FIRMWARE=$VARIANT mv "build/watch.uf2" "$fw_dir/$variant-$color.uf2" done - make clean + rm -rf ./build-sim emmake make FIRMWARE=$VARIANT mkdir "$sim_dir/$variant/" - mv "build/watch.wasm" "$sim_dir/$variant/" - mv "build/watch.js" "$sim_dir/$variant/" - mv "build/watch.html" "$sim_dir/$variant/index.html" + mv "build-sim/watch.wasm" "$sim_dir/$variant/" + mv "build-sim/watch.js" "$sim_dir/$variant/" + mv "build-sim/watch.html" "$sim_dir/$variant/index.html" done echo "Done." diff --git a/movement/movement.c b/movement/movement.c index 09ebf0c2..3997b4a4 100644 --- a/movement/movement.c +++ b/movement/movement.c @@ -22,6 +22,8 @@ * SOFTWARE. */ +#define MOVEMENT_LONG_PRESS_TICKS 64 + #include <stdio.h> #include <string.h> #include <limits.h> @@ -35,7 +37,9 @@ #ifndef MOVEMENT_FIRMWARE #include "movement_config.h" #elif MOVEMENT_FIRMWARE == MOVEMENT_FIRMWARE_STANDARD -#include "alt_fw/standard.h" +#include "movement_config.h" +#elif MOVEMENT_FIRMWARE == MOVEMENT_FIRMWARE_BACKER +#include "alt_fw/backer.h" #elif MOVEMENT_FIRMWARE == MOVEMENT_FIRMWARE_ALT_TIME #include "alt_fw/alt_time.h" #elif MOVEMENT_FIRMWARE == MOVEMENT_FIRMWARE_FOCUS @@ -50,6 +54,19 @@ #include "alt_fw/deep_space_now.h" #endif +// Default to no secondary face behaviour. +#ifndef MOVEMENT_SECONDARY_FACE_INDEX +#define MOVEMENT_SECONDARY_FACE_INDEX 0 +#endif + +// Set default LED colors if not set +#ifndef MOVEMENT_DEFAULT_RED_COLOR +#define MOVEMENT_DEFAULT_RED_COLOR 0x0 +#endif +#ifndef MOVEMENT_DEFAULT_GREEN_COLOR +#define MOVEMENT_DEFAULT_GREEN_COLOR 0xF +#endif + #if __EMSCRIPTEN__ #include <emscripten.h> #endif @@ -207,7 +224,13 @@ void movement_move_to_face(uint8_t watch_face_index) { } void movement_move_to_next_face(void) { - movement_move_to_face((movement_state.current_watch_face + 1) % MOVEMENT_NUM_FACES); + uint16_t face_max; + if (MOVEMENT_SECONDARY_FACE_INDEX) { + face_max = (movement_state.current_watch_face < (int16_t)MOVEMENT_SECONDARY_FACE_INDEX) ? MOVEMENT_SECONDARY_FACE_INDEX : MOVEMENT_NUM_FACES; + } else { + face_max = MOVEMENT_NUM_FACES; + } + movement_move_to_face((movement_state.current_watch_face + 1) % face_max); } void movement_schedule_background_task(watch_date_time date_time) { @@ -250,11 +273,16 @@ void movement_play_signal(void) { } void movement_play_alarm(void) { + movement_play_alarm_beeps(5, BUZZER_NOTE_C8); +} + +void movement_play_alarm_beeps(uint8_t rounds, BuzzerNote alarm_note) { + if (rounds == 0) rounds = 1; + if (rounds > 20) rounds = 20; movement_request_wake(); - // alarm length: 75 ticks short of 5 seconds, or 4.414 seconds: - // our tone is 0.375 seconds of beep and 0.625 of silence, repeated five times. - // so 4.375 + a few ticks to wake up from sleep mode. - movement_state.alarm_ticks = 128 * 5 - 75; + movement_state.alarm_note = alarm_note; + // our tone is 0.375 seconds of beep and 0.625 of silence, repeated as given. + movement_state.alarm_ticks = 128 * rounds - 75; _movement_enable_fast_tick_if_needed(); } @@ -266,7 +294,8 @@ uint8_t movement_claim_backup_register(void) { void app_init(void) { memset(&movement_state, 0, sizeof(movement_state)); - movement_state.settings.bit.led_green_color = 0xF; + movement_state.settings.bit.led_red_color = MOVEMENT_DEFAULT_RED_COLOR; + movement_state.settings.bit.led_green_color = MOVEMENT_DEFAULT_GREEN_COLOR; movement_state.settings.bit.button_should_sound = true; movement_state.settings.bit.le_interval = 1; movement_state.settings.bit.led_duration = 1; @@ -421,14 +450,18 @@ bool app_loop(void) { can_sleep = watch_faces[movement_state.current_watch_face].loop(event, &movement_state.settings, watch_face_contexts[movement_state.current_watch_face]); // Long-pressing MODE brings one back to the first face, provided that the watch face hasn't decided to send them elsewhere - // (and we're not currently on the first face). + // (and we're not currently on the first face). If we're currently on the first face, a long press + // of MODE sends us to the secondary faces (if defined). // Note that it's the face's responsibility to provide some way to get to the next face, so if EVENT_MODE_BUTTON_* is // used for face functionality EVENT_MODE_LONG_PRESS should probably be handled and next_face() triggered in the face // (which would effectively disable the normal 'long press to face 0' behaviour). if (event.event_type == EVENT_MODE_LONG_PRESS - && movement_state.current_watch_face > 0 && !movement_state.watch_face_changed) { - movement_move_to_face(0); + if (movement_state.current_watch_face != 0) { + movement_move_to_face(0); + } else if (MOVEMENT_SECONDARY_FACE_INDEX) { + movement_move_to_face(MOVEMENT_SECONDARY_FACE_INDEX); + } } event.event_type = EVENT_NONE; } @@ -453,10 +486,13 @@ bool app_loop(void) { if (movement_state.alarm_ticks >= 0) { uint8_t buzzer_phase = (movement_state.alarm_ticks + 80) % 128; if(buzzer_phase == 127) { + // failsafe: buzzer could have been disabled in the meantime + if (!watch_is_buzzer_or_led_enabled()) watch_enable_buzzer(); + // play 4 beeps plus pause for(uint8_t i = 0; i < 4; i++) { // TODO: This method of playing the buzzer blocks the UI while it's beeping. // It might be better to time it with the fast tick. - watch_buzzer_play_note(BUZZER_NOTE_C8, (i != 3) ? 50 : 75); + watch_buzzer_play_note(movement_state.alarm_note, (i != 3) ? 50 : 75); if (i != 3) watch_buzzer_play_note(BUZZER_NOTE_REST, 50); } } @@ -503,7 +539,7 @@ bool app_loop(void) { return can_sleep; } -static movement_event_type_t _figure_out_button_event(bool pin_level, movement_event_type_t button_down_event_type, uint8_t *down_timestamp) { +static movement_event_type_t _figure_out_button_event(bool pin_level, movement_event_type_t button_down_event_type, uint16_t *down_timestamp) { // force alarm off if the user pressed a button. if (movement_state.alarm_ticks) movement_state.alarm_ticks = 0; @@ -513,15 +549,15 @@ static movement_event_type_t _figure_out_button_event(bool pin_level, movement_e *down_timestamp = movement_state.fast_ticks + 1; return button_down_event_type; } else { - // this line is hack but it handles the situation where the light button was held for more than 10 seconds. + // this line is hack but it handles the situation where the light button was held for more than 20 seconds. // fast tick is disabled by then, and the LED would get stuck on since there's no one left decrementing light_ticks. if (movement_state.light_ticks == 1) movement_state.light_ticks = 0; // now that that's out of the way, handle falling edge uint16_t diff = movement_state.fast_ticks - *down_timestamp; *down_timestamp = 0; _movement_disable_fast_tick_if_possible(); - // any press over a half second is considered a long press. - if (diff > 64) return button_down_event_type + 2; + // any press over a half second is considered a long press. Fire the long-up event + if (diff > MOVEMENT_LONG_PRESS_TICKS) return button_down_event_type + 3; else return button_down_event_type + 1; } } @@ -557,9 +593,21 @@ void cb_fast_tick(void) { movement_state.fast_ticks++; if (movement_state.light_ticks > 0) movement_state.light_ticks--; if (movement_state.alarm_ticks > 0) movement_state.alarm_ticks--; + // check timestamps and auto-fire the long-press events + // Notice: is it possible that two or more buttons have an identical timestamp? In this case + // only one of these buttons would receive the long press event. Don't bother for now... + if (movement_state.light_down_timestamp > 0) + if (movement_state.fast_ticks - movement_state.light_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1) + event.event_type = EVENT_LIGHT_LONG_PRESS; + if (movement_state.mode_down_timestamp > 0) + if (movement_state.fast_ticks - movement_state.mode_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1) + event.event_type = EVENT_MODE_LONG_PRESS; + if (movement_state.alarm_down_timestamp > 0) + if (movement_state.fast_ticks - movement_state.alarm_down_timestamp == MOVEMENT_LONG_PRESS_TICKS + 1) + event.event_type = EVENT_ALARM_LONG_PRESS; // this is just a fail-safe; fast tick should be disabled as soon as the button is up, the LED times out, and/or the alarm finishes. - // but if for whatever reason it isn't, this forces the fast tick off after 10 seconds. - if (movement_state.fast_ticks >= 1280) watch_rtc_disable_periodic_callback(128); + // but if for whatever reason it isn't, this forces the fast tick off after 20 seconds. + if (movement_state.fast_ticks >= 128 * 20) watch_rtc_disable_periodic_callback(128); } void cb_tick(void) { diff --git a/movement/movement.h b/movement/movement.h index 79222e8c..9dc5fe82 100644 --- a/movement/movement.h +++ b/movement/movement.h @@ -61,7 +61,8 @@ typedef union { // altimeter to display feet or meters as easily as it tells a thermometer to display degrees in F or C. bool clock_mode_24h : 1; // indicates whether clock should use 12 or 24 hour mode. bool use_imperial_units : 1; // indicates whether to use metric units (the default) or imperial. - uint8_t reserved : 7; // room for more preferences if needed. + bool alarm_enabled : 1; // indicates wheter there is at least one alarm enabled. + uint8_t reserved : 6; // room for more preferences if needed. } bit; uint32_t reg; } movement_settings_t; @@ -108,14 +109,17 @@ typedef enum { EVENT_BACKGROUND_TASK, // Your watch face is being invoked to perform a background task. Don't update the display here; you may not be in the foreground. EVENT_TIMEOUT, // Your watch face has been inactive for a while. You may want to resign, depending on your watch face's intended use case. EVENT_LIGHT_BUTTON_DOWN, // The light button has been pressed, but not yet released. - EVENT_LIGHT_BUTTON_UP, // The light button was pressed and released. - EVENT_LIGHT_LONG_PRESS, // The light button was held for >2 seconds, and released. + EVENT_LIGHT_BUTTON_UP, // The light button was pressed for less than half a second, and released. + EVENT_LIGHT_LONG_PRESS, // The light button was held for over half a second, but not yet released. + EVENT_LIGHT_LONG_UP, // The light button was held for over half a second, and released. EVENT_MODE_BUTTON_DOWN, // The mode button has been pressed, but not yet released. - EVENT_MODE_BUTTON_UP, // The mode button was pressed and released. - EVENT_MODE_LONG_PRESS, // The mode button was held for >2 seconds, and released. NOTE: your watch face will resign immediately after receiving this event. + EVENT_MODE_BUTTON_UP, // The mode button was pressed for less than half a second, and released. + EVENT_MODE_LONG_PRESS, // The mode button was held for over half a second, but not yet released. + EVENT_MODE_LONG_UP, // The mode button was held for over half a second, and released. NOTE: your watch face will resign immediately after receiving this event. EVENT_ALARM_BUTTON_DOWN, // The alarm button has been pressed, but not yet released. - EVENT_ALARM_BUTTON_UP, // The alarm button was pressed and released. - EVENT_ALARM_LONG_PRESS, // The alarm button was held for >2 seconds, and released. + EVENT_ALARM_BUTTON_UP, // The alarm button was pressed for less than half a second, and released. + EVENT_ALARM_LONG_PRESS, // The alarm button was held for over half a second, but not yet released. + EVENT_ALARM_LONG_UP, // The alarm button was held for over half a second, and released. } movement_event_type_t; typedef struct { @@ -252,11 +256,12 @@ typedef struct { // alarm stuff int16_t alarm_ticks; bool is_buzzing; + BuzzerNote alarm_note; // button tracking for long press - uint8_t light_down_timestamp; - uint8_t mode_down_timestamp; - uint8_t alarm_down_timestamp; + uint16_t light_down_timestamp; + uint16_t mode_down_timestamp; + uint16_t alarm_down_timestamp; // background task handling bool needs_background_tasks_handled; @@ -300,6 +305,7 @@ void movement_request_wake(void); void movement_play_signal(void); void movement_play_alarm(void); +void movement_play_alarm_beeps(uint8_t rounds, BuzzerNote alarm_note); uint8_t movement_claim_backup_register(void); diff --git a/movement/movement_config.h b/movement/movement_config.h index 94456776..9e446d4d 100644 --- a/movement/movement_config.h +++ b/movement/movement_config.h @@ -32,11 +32,18 @@ const watch_face_t watch_faces[] = { world_clock_face, sunrise_sunset_face, moon_phase_face, - thermistor_readout_face, + stopwatch_face, preferences_face, set_time_face, }; #define MOVEMENT_NUM_FACES (sizeof(watch_faces) / sizeof(watch_face_t)) +/* Determines what face to go to from the first face if you've already set + * a mode long press to go to the first face in preferences, and + * excludes these faces from the normal rotation. + * Usually it makes sense to set this to the preferences face. + */ +#define MOVEMENT_SECONDARY_FACE_INDEX 0 // or (MOVEMENT_NUM_FACES - 2) + #endif // MOVEMENT_CONFIG_H_ diff --git a/movement/movement_faces.h b/movement/movement_faces.h index 518a4358..9333401b 100644 --- a/movement/movement_faces.h +++ b/movement/movement_faces.h @@ -39,6 +39,7 @@ #include "voltage_face.h" #include "stopwatch_face.h" #include "totp_face.h" +#include "totp_face_lfs.h" #include "lis2dw_logging_face.h" #include "demo_face.h" #include "hello_there_face.h" @@ -56,7 +57,9 @@ #include "probability_face.h" #include "wake_face.h" #include "frequency_correction_face.h" +#include "alarm_face.h" #include "ratemeter_face.h" +#include "weeknumber_clock_face.h" // New includes go above this line. #endif // MOVEMENT_FACES_H_ diff --git a/movement/watch_faces/clock/simple_clock_face.c b/movement/watch_faces/clock/simple_clock_face.c index 23f36672..7721b12a 100644 --- a/movement/watch_faces/clock/simple_clock_face.c +++ b/movement/watch_faces/clock/simple_clock_face.c @@ -27,6 +27,12 @@ #include "watch.h" #include "watch_utility.h" +static void _update_alarm_indicator(bool settings_alarm_enabled, simple_clock_state_t *state) { + state->alarm_enabled = settings_alarm_enabled; + if (state->alarm_enabled) watch_set_indicator(WATCH_INDICATOR_SIGNAL); + else watch_clear_indicator(WATCH_INDICATOR_SIGNAL); +} + void simple_clock_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { (void) settings; (void) watch_face_index; @@ -45,7 +51,13 @@ void simple_clock_face_activate(movement_settings_t *settings, void *context) { if (watch_tick_animation_is_running()) watch_stop_tick_animation(); if (settings->bit.clock_mode_24h) watch_set_indicator(WATCH_INDICATOR_24H); - if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_SIGNAL); + + // handle chime indicator + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); + else watch_clear_indicator(WATCH_INDICATOR_BELL); + + // show alarm indicator if there is an active alarm + _update_alarm_indicator(settings->bit.alarm_enabled, state); watch_set_colon(); @@ -111,6 +123,8 @@ bool simple_clock_face_loop(movement_event_t event, movement_settings_t *setting } } watch_display_string(buf, pos); + // handle alarm indicator + if (state->alarm_enabled != settings->bit.alarm_enabled) _update_alarm_indicator(settings->bit.alarm_enabled, state); break; case EVENT_MODE_BUTTON_UP: movement_move_to_next_face(); @@ -120,8 +134,8 @@ bool simple_clock_face_loop(movement_event_t event, movement_settings_t *setting break; case EVENT_ALARM_LONG_PRESS: state->signal_enabled = !state->signal_enabled; - if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_SIGNAL); - else watch_clear_indicator(WATCH_INDICATOR_SIGNAL); + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); + else watch_clear_indicator(WATCH_INDICATOR_BELL); break; case EVENT_BACKGROUND_TASK: // uncomment this line to snap back to the clock face when the hour signal sounds: diff --git a/movement/watch_faces/clock/simple_clock_face.h b/movement/watch_faces/clock/simple_clock_face.h index aa625700..1e9babad 100644 --- a/movement/watch_faces/clock/simple_clock_face.h +++ b/movement/watch_faces/clock/simple_clock_face.h @@ -33,6 +33,7 @@ typedef struct { uint8_t watch_face_index; bool signal_enabled; bool battery_low; + bool alarm_enabled; } simple_clock_state_t; void simple_clock_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); diff --git a/movement/watch_faces/clock/weeknumber_clock_face.c b/movement/watch_faces/clock/weeknumber_clock_face.c new file mode 100644 index 00000000..e49e5abf --- /dev/null +++ b/movement/watch_faces/clock/weeknumber_clock_face.c @@ -0,0 +1,171 @@ +/* + * MIT License + * + * Copyright (c) 2022 Joey Castillo + * + * 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 "weeknumber_clock_face.h" +#include "watch.h" +#include "watch_utility.h" + +static void _update_alarm_indicator(bool settings_alarm_enabled, weeknumber_clock_state_t *state) { + state->alarm_enabled = settings_alarm_enabled; + if (state->alarm_enabled) watch_set_indicator(WATCH_INDICATOR_SIGNAL); + else watch_clear_indicator(WATCH_INDICATOR_SIGNAL); +} + +void weeknumber_clock_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(weeknumber_clock_state_t)); + weeknumber_clock_state_t *state = (weeknumber_clock_state_t *)*context_ptr; + state->signal_enabled = false; + state->watch_face_index = watch_face_index; + } +} + +void weeknumber_clock_face_activate(movement_settings_t *settings, void *context) { + weeknumber_clock_state_t *state = (weeknumber_clock_state_t *)context; + + if (watch_tick_animation_is_running()) watch_stop_tick_animation(); + + if (settings->bit.clock_mode_24h) watch_set_indicator(WATCH_INDICATOR_24H); + + // handle chime indicator + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); + else watch_clear_indicator(WATCH_INDICATOR_BELL); + + // show alarm indicator if there is an active alarm + _update_alarm_indicator(settings->bit.alarm_enabled, state); + + watch_set_colon(); + + // this ensures that none of the timestamp fields will match, so we can re-render them all. + state->previous_date_time = 0xFFFFFFFF; +} + +bool weeknumber_clock_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + weeknumber_clock_state_t *state = (weeknumber_clock_state_t *)context; + char buf[11]; + uint8_t pos; + + watch_date_time date_time; + uint32_t previous_date_time; + switch (event.event_type) { + case EVENT_ACTIVATE: + case EVENT_TICK: + case EVENT_LOW_ENERGY_UPDATE: + date_time = watch_rtc_get_date_time(); + previous_date_time = state->previous_date_time; + state->previous_date_time = date_time.reg; + + // check the battery voltage once a day... + if (date_time.unit.day != state->last_battery_check) { + state->last_battery_check = date_time.unit.day; + watch_enable_adc(); + uint16_t voltage = watch_get_vcc_voltage(); + watch_disable_adc(); + // 2.2 volts will happen when the battery has maybe 5-10% remaining? + // we can refine this later. + state->battery_low = (voltage < 2200); + } + + // ...and set the LAP indicator if low. + if (state->battery_low) watch_set_indicator(WATCH_INDICATOR_LAP); + + if ((date_time.reg >> 12) == (previous_date_time >> 12) && event.event_type != EVENT_LOW_ENERGY_UPDATE) { + // everything before minutes is the same. + pos = 6; + sprintf(buf, "%02d%02d", date_time.unit.minute, watch_utility_get_weeknumber(date_time.unit.year, date_time.unit.month, date_time.unit.day)); + } else { + // other stuff changed; let's do it all. + if (!settings->bit.clock_mode_24h) { + // if we are in 12 hour mode, do some cleanup. + if (date_time.unit.hour < 12) { + watch_clear_indicator(WATCH_INDICATOR_PM); + } else { + watch_set_indicator(WATCH_INDICATOR_PM); + } + date_time.unit.hour %= 12; + if (date_time.unit.hour == 0) date_time.unit.hour = 12; + } + pos = 0; + if (event.event_type == EVENT_LOW_ENERGY_UPDATE) { + if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); + sprintf(buf, "%s%2d%2d%02d ", watch_utility_get_weekday(date_time), date_time.unit.day, date_time.unit.hour, date_time.unit.minute); + } else { + sprintf(buf, "%s%2d%2d%02d%02d", watch_utility_get_weekday(date_time), date_time.unit.day, date_time.unit.hour, date_time.unit.minute, watch_utility_get_weeknumber(date_time.unit.year, date_time.unit.month, date_time.unit.day)); + } + } + watch_display_string(buf, pos); + // handle alarm indicator + if (state->alarm_enabled != settings->bit.alarm_enabled) _update_alarm_indicator(settings->bit.alarm_enabled, state); + break; + case EVENT_MODE_BUTTON_UP: + movement_move_to_next_face(); + return false; + case EVENT_LIGHT_BUTTON_DOWN: + movement_illuminate_led(); + break; + case EVENT_ALARM_LONG_PRESS: + state->signal_enabled = !state->signal_enabled; + if (state->signal_enabled) watch_set_indicator(WATCH_INDICATOR_BELL); + else watch_clear_indicator(WATCH_INDICATOR_BELL); + break; + case EVENT_BACKGROUND_TASK: + // uncomment this line to snap back to the clock face when the hour signal sounds: + // movement_move_to_face(state->watch_face_index); + if (watch_is_buzzer_or_led_enabled()) { + // if we are in the foreground, we can just beep. + movement_play_signal(); + } else { + // if we were in the background, we need to enable the buzzer peripheral first, + watch_enable_buzzer(); + // beep quickly (this call blocks for 275 ms), + movement_play_signal(); + // and then turn the buzzer peripheral off again. + watch_disable_buzzer(); + } + break; + default: + break; + } + + return true; +} + +void weeknumber_clock_face_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; +} + +bool weeknumber_clock_face_wants_background_task(movement_settings_t *settings, void *context) { + (void) settings; + weeknumber_clock_state_t *state = (weeknumber_clock_state_t *)context; + if (!state->signal_enabled) return false; + + watch_date_time date_time = watch_rtc_get_date_time(); + + return date_time.unit.minute == 0; +} diff --git a/movement/watch_faces/clock/weeknumber_clock_face.h b/movement/watch_faces/clock/weeknumber_clock_face.h new file mode 100644 index 00000000..f0298ea8 --- /dev/null +++ b/movement/watch_faces/clock/weeknumber_clock_face.h @@ -0,0 +1,53 @@ +/* + * MIT License + * + * Copyright (c) 2022 Joey Castillo + * + * 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 WEEKNUMBER_CLOCK_FACE_H_ +#define WEEKNUMBER_CLOCK_FACE_H_ + +#include "movement.h" + +typedef struct { + uint32_t previous_date_time; + uint8_t last_battery_check; + uint8_t watch_face_index; + bool signal_enabled; + bool battery_low; + bool alarm_enabled; +} weeknumber_clock_state_t; + +void weeknumber_clock_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void weeknumber_clock_face_activate(movement_settings_t *settings, void *context); +bool weeknumber_clock_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void weeknumber_clock_face_resign(movement_settings_t *settings, void *context); +bool weeknumber_clock_face_wants_background_task(movement_settings_t *settings, void *context); + +#define weeknumber_clock_face ((const watch_face_t){ \ + weeknumber_clock_face_setup, \ + weeknumber_clock_face_activate, \ + weeknumber_clock_face_loop, \ + weeknumber_clock_face_resign, \ + weeknumber_clock_face_wants_background_task, \ +}) + +#endif // SIMPLE_CLOCK_FACE_H_ diff --git a/movement/watch_faces/complication/alarm_face.c b/movement/watch_faces/complication/alarm_face.c new file mode 100644 index 00000000..35e2890f --- /dev/null +++ b/movement/watch_faces/complication/alarm_face.c @@ -0,0 +1,455 @@ +/* + * MIT License + * + * Copyright (c) 2022 Andreas Nebinger + * + * 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 "alarm_face.h" +#include "watch.h" +#include "watch_utility.h" +#include "watch_private_display.h" + +/* + Implements 16 alarm slots on the sensor watch + + Usage: + - In normal mode, the alarm button cycles through all 16 alarms. + - Pressing the alarm button long in normal mode toggles the corresponding alarm on or off. + (Whereas pressing the alarm button extra long brings you back to alarm no. 1.) + - Pressing the light button enters setting mode and cycles through the settings of each alarm. + (Long pressing the light button enters setting mode without illuminating the led.) + - In setting mode an alarm slot is selected by pressing the alarm button when the slot number + in the upper right corner is blinking. + - For each alarm slot, you can select the day. These are the day modes: + - ED = the alarm rings every day + - 1t = the alarm fires only one time and is erased afterwards + - MF = the alarm fires Mondays to Fridays + - WN = the alarm fires on weekends (Sa/Su) + - MO to SU = the alarm fires only on the given day of week + - You can fast cycle through hour or minute setting via long press of the alarm button. + - You can select the tone in which the alarm is played. (Three pitch levels available.) + - You can select how many "beep rounds" are played for each alarm. 1 to 9 rounds, plus extra + long ('L') and extra short ('o') alarms. + - The simple watch face indicates if any alarm is set within the next 24h by showing the signal + indicator. +*/ + +typedef enum { + alarm_setting_idx_alarm, + alarm_setting_idx_day, + alarm_setting_idx_hour, + alarm_setting_idx_minute, + alarm_setting_idx_pitch, + alarm_setting_idx_beeps +} alarm_setting_idx_t; + +static const char _dow_strings[ALARM_DAY_STATES + 1][2] ={"AL", "MO", "TU", "WE", "TH", "FR", "SA", "SO", "ED", "1t", "MF", "WN"}; +static const uint8_t _blink_idx[ALARM_SETTING_STATES] = {2, 0, 4, 6, 8, 9}; +static const uint8_t _blink_idx2[ALARM_SETTING_STATES] = {3, 1, 5, 7, 8, 9}; +static const BuzzerNote _buzzer_notes[3] = {BUZZER_NOTE_B6, BUZZER_NOTE_C8, BUZZER_NOTE_A8}; +static const uint8_t _buzzer_segdata[3][2] = {{0, 3}, {1, 3}, {2, 2}}; + +static int8_t _wait_ticks; + +static uint8_t _get_weekday_idx(watch_date_time date_time) { + date_time.unit.year += 20; + if (date_time.unit.month <= 2) { + date_time.unit.month += 12; + date_time.unit.year--; + } + return (date_time.unit.day + 13 * (date_time.unit.month + 1) / 5 + date_time.unit.year + date_time.unit.year / 4 + 525 - 2) % 7; +} + +static void _alarm_set_signal(alarm_state_t *state) { + if (state->alarm[state->alarm_idx].enabled) + watch_set_indicator(WATCH_INDICATOR_SIGNAL); + else + watch_clear_indicator(WATCH_INDICATOR_SIGNAL); +} + +static void _alarm_face_draw(movement_settings_t *settings, alarm_state_t *state, uint8_t subsecond) { + char buf[12]; + + uint8_t i = 0; + if (state->is_setting) { + // display the actual day indicating string for the current alarm + i = state->alarm[state->alarm_idx].day + 1; + } + //handle am/pm for hour display + uint8_t h = state->alarm[state->alarm_idx].hour; + if (!settings->bit.clock_mode_24h) { + if (h >= 12) { + watch_set_indicator(WATCH_INDICATOR_PM); + h %= 12; + } else { + watch_clear_indicator(WATCH_INDICATOR_PM); + } + if (h == 0) h = 12; + } + sprintf(buf, "%c%c%2d%2d%02d ", + _dow_strings[i][0], _dow_strings[i][1], + (state->alarm_idx + 1), + h, + state->alarm[state->alarm_idx].minute); + // blink items if in settings mode + if (state->is_setting && subsecond % 2 && state->setting_state < alarm_setting_idx_pitch && !state->alarm_quick_ticks) { + buf[_blink_idx[state->setting_state]] = buf[_blink_idx2[state->setting_state]] = ' '; + } + watch_display_string(buf, 0); + + if (state->is_setting) { + // draw pitch level indicator + if ((subsecond % 2) == 0 || (state->setting_state != alarm_setting_idx_pitch)) { + for (i = 0; i <= state->alarm[state->alarm_idx].pitch && i < 3; i++) + watch_set_pixel(_buzzer_segdata[i][0], _buzzer_segdata[i][1]); + } + // draw beep rounds indicator + if ((subsecond % 2) == 0 || (state->setting_state != alarm_setting_idx_beeps)) { + if (state->alarm[state->alarm_idx].beeps == ALARM_MAX_BEEP_ROUNDS - 1) + watch_display_character('L', _blink_idx[alarm_setting_idx_beeps]); + else { + if (state->alarm[state->alarm_idx].beeps == 0) + watch_display_character('o', _blink_idx[alarm_setting_idx_beeps]); + else + watch_display_character(state->alarm[state->alarm_idx].beeps + 48, _blink_idx[alarm_setting_idx_beeps]); + } + } + } + + // set alarm indicator + _alarm_set_signal(state); +} + +static void _alarm_initiate_setting(movement_settings_t *settings, alarm_state_t *state, uint8_t subsecond) { + state->is_setting = true; + state->setting_state = 0; + movement_request_tick_frequency(4); + _alarm_face_draw(settings, state, subsecond); +} + +static void _alarm_resume_setting(movement_settings_t *settings, alarm_state_t *state, uint8_t subsecond) { + state->is_setting = false; + movement_request_tick_frequency(1); + _alarm_face_draw(settings, state, subsecond); +} + +static void _alarm_update_alarm_enabled(movement_settings_t *settings, alarm_state_t *state) { + // save indication for active alarms to movement settings + bool active_alarms = false; + watch_date_time now; + bool now_init = false; + uint8_t weekday_idx; + uint16_t now_minutes_of_day; + uint16_t alarm_minutes_of_day; + for (uint8_t i = 0; i < ALARM_ALARMS; i++) { + if (state->alarm[i].enabled) { + // figure out if alarm is to go off in the next 24 h + if (state->alarm[i].day == ALARM_DAY_EACH_DAY || state->alarm[i].day == ALARM_DAY_ONE_TIME) { + active_alarms = true; + break; + } else { + if (!now_init) { + now = watch_rtc_get_date_time(); + now_init = true; + weekday_idx = _get_weekday_idx(now); + now_minutes_of_day = now.unit.hour * 60 + now.unit.minute; + } + alarm_minutes_of_day = state->alarm[i].hour * 60 + state->alarm[i].minute; + // no more shortcuts: check days and times for all possible cases... + if ((state->alarm[i].day == weekday_idx && alarm_minutes_of_day >= now_minutes_of_day) + || ((weekday_idx + 1) % 7 == state->alarm[i].day && alarm_minutes_of_day <= now_minutes_of_day) + || (state->alarm[i].day == ALARM_DAY_WORKDAY && (weekday_idx < 4 + || (weekday_idx = 5 && alarm_minutes_of_day >= now_minutes_of_day) + || (weekday_idx = 6 && alarm_minutes_of_day <= now_minutes_of_day))) + || (state->alarm[i].day == ALARM_DAY_WEEKEND && (weekday_idx == 5 + || (weekday_idx = 6 && alarm_minutes_of_day >= now_minutes_of_day) + || (weekday_idx = 4 && alarm_minutes_of_day <= now_minutes_of_day)))) { + active_alarms = true; + break; + } + } + } + } + settings->bit.alarm_enabled = active_alarms; +} + +static void _alarm_play_short_beep(uint8_t pitch_idx) { + // play a short double beep + watch_buzzer_play_note(_buzzer_notes[pitch_idx], 50); + watch_buzzer_play_note(BUZZER_NOTE_REST, 50); + watch_buzzer_play_note(_buzzer_notes[pitch_idx], 70); +} + +static void _alarm_indicate_beep(alarm_state_t *state) { + // play an example for the current beep setting + if (state->alarm[state->alarm_idx].beeps == 0) { + // short double beep + _alarm_play_short_beep(state->alarm[state->alarm_idx].pitch); + } else { + // regular alarm beep + movement_play_alarm_beeps(1, _buzzer_notes[state->alarm[state->alarm_idx].pitch]); + } +} + +static void _abort_quick_ticks(alarm_state_t *state) { + // abort counting quick ticks + if (state->alarm_quick_ticks) { + state->alarm[state->alarm_idx].enabled = true; + state->alarm_quick_ticks = false; + movement_request_tick_frequency(4); + } +} + +void alarm_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(alarm_state_t)); + alarm_state_t *state = (alarm_state_t *)*context_ptr; + memset(*context_ptr, 0, sizeof(alarm_state_t)); + // initialize the default alarm values + for (uint8_t i = 0; i < ALARM_ALARMS; i++) { + state->alarm[i].day = ALARM_DAY_EACH_DAY; + state->alarm[i].beeps = 5; + state->alarm[i].pitch = 1; + } + state->alarm_handled_minute = -1; + _wait_ticks = -1; + } +} + +void alarm_face_activate(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; + watch_set_colon(); +} + +void alarm_face_resign(movement_settings_t *settings, void *context) { + alarm_state_t *state = (alarm_state_t *)context; + state->is_setting = false; + _alarm_update_alarm_enabled(settings, state); + watch_set_led_off(); + state->alarm_quick_ticks = false; + _wait_ticks = -1; + movement_request_tick_frequency(1); +} + +bool alarm_face_wants_background_task(movement_settings_t *settings, void *context) { + (void) settings; + alarm_state_t *state = (alarm_state_t *)context; + watch_date_time now = watch_rtc_get_date_time(); + // just a failsafe: never fire more than one alarm within a minute + if (state->alarm_handled_minute == now.unit.minute) return false; + state->alarm_handled_minute = now.unit.minute; + // check the rest + for (uint8_t i = 0; i < ALARM_ALARMS; i++) { + if (state->alarm[i].enabled) { + if (state->alarm[i].minute == now.unit.minute) { + if (state->alarm[i].hour == now.unit.hour) { + state->alarm_playing_idx = i; + if (state->alarm[i].day == ALARM_DAY_EACH_DAY || state->alarm[i].day == ALARM_DAY_ONE_TIME) return true; + uint8_t weekday_idx = _get_weekday_idx(now); + if (state->alarm[i].day == weekday_idx) return true; + if (state->alarm[i].day == ALARM_DAY_WORKDAY && weekday_idx < 5) return true; + if (state->alarm[i].day == ALARM_DAY_WEEKEND && weekday_idx >= 5) return true; + } + } + } + } + state->alarm_handled_minute = -1; + // update the movement's alarm indicator five times an hour + if (now.unit.minute % 12 == 0) _alarm_update_alarm_enabled(settings, state); + return false; +} + +bool alarm_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { + (void) settings; + alarm_state_t *state = (alarm_state_t *)context; + + switch (event.event_type) { + case EVENT_TICK: + if (state->alarm_quick_ticks) { + // we are in fast cycling mode + if (state->setting_state == alarm_setting_idx_hour) { + state->alarm[state->alarm_idx].hour = (state->alarm[state->alarm_idx].hour + 1) % 24; + } else if (state->setting_state == alarm_setting_idx_minute) { + state->alarm[state->alarm_idx].minute = (state->alarm[state->alarm_idx].minute + 1) % 60; + } else _abort_quick_ticks(state); + } else if (!state->is_setting) { + if (_wait_ticks >= 0) _wait_ticks++; + if (_wait_ticks == 2) { + // extra long press of alarm button + _wait_ticks = -1; + if (state->alarm_idx) { + // revert change of enabled flag and show it briefly + state->alarm[state->alarm_idx].enabled ^= 1; + _alarm_set_signal(state); + delay_ms(275); + state->alarm_idx = 0; + } + } else break; // no need to do anything when we are not in settings mode and no quick ticks are running + } + // fall through + case EVENT_ACTIVATE: + _alarm_face_draw(settings, state, event.subsecond); + break; + case EVENT_LIGHT_BUTTON_UP: + if (!state->is_setting) { + movement_illuminate_led(); + _alarm_initiate_setting(settings, state, event.subsecond); + break; + } + state->setting_state += 1; + if (state->setting_state >= ALARM_SETTING_STATES) { + // we have done a full settings cycle, so resume to normal + _alarm_resume_setting(settings, state, event.subsecond); + } + break; + case EVENT_LIGHT_LONG_PRESS: + if (state->is_setting) { + _alarm_resume_setting(settings, state, event.subsecond); + } else { + _alarm_initiate_setting(settings, state, event.subsecond); + } + break; + case EVENT_ALARM_BUTTON_UP: + if (!state->is_setting) { + // stop wait ticks counter + _wait_ticks = -1; + // cycle through the alarms + state->alarm_idx = (state->alarm_idx + 1) % (ALARM_ALARMS); + } else { + // handle the settings behaviour + switch (state->setting_state) { + case alarm_setting_idx_alarm: + // alarm selection + state->alarm_idx = (state->alarm_idx + 1) % (ALARM_ALARMS); + break; + case alarm_setting_idx_day: + // day selection + state->alarm[state->alarm_idx].day = (state->alarm[state->alarm_idx].day + 1) % (ALARM_DAY_STATES); + break; + case alarm_setting_idx_hour: + // hour selection + _abort_quick_ticks(state); + state->alarm[state->alarm_idx].hour = (state->alarm[state->alarm_idx].hour + 1) % 24; + break; + case alarm_setting_idx_minute: + // minute selection + _abort_quick_ticks(state); + state->alarm[state->alarm_idx].minute = (state->alarm[state->alarm_idx].minute + 1) % 60; + break; + case alarm_setting_idx_pitch: + // pitch level + state->alarm[state->alarm_idx].pitch = (state->alarm[state->alarm_idx].pitch + 1) % 3; + // play sound to show user what this is for + _alarm_indicate_beep(state); + break; + case alarm_setting_idx_beeps: + // number of beeping rounds selection + state->alarm[state->alarm_idx].beeps = (state->alarm[state->alarm_idx].beeps + 1) % ALARM_MAX_BEEP_ROUNDS; + // play sounds when user reaches 'short' length and also one time on regular beep length + if (state->alarm[state->alarm_idx].beeps <= 1) _alarm_indicate_beep(state); + break; + default: + break; + } + // auto enable an alarm if user sets anything + if (state->setting_state > alarm_setting_idx_alarm) state->alarm[state->alarm_idx].enabled = true; + } + _alarm_face_draw(settings, state, event.subsecond); + break; + case EVENT_ALARM_LONG_PRESS: + if (!state->is_setting) { + // toggle the enabled flag for current alarm + state->alarm[state->alarm_idx].enabled ^= 1; + // start wait ticks counter + _wait_ticks = 0; + } else { + // handle the long press settings behaviour + switch (state->setting_state) { + case alarm_setting_idx_alarm: + // alarm selection + state->alarm_idx = 0; + break; + case alarm_setting_idx_minute: + case alarm_setting_idx_hour: + // initiate fast cycling for hour or minute settings + movement_request_tick_frequency(8); + state->alarm_quick_ticks = true; + break; + default: + break; + } + } + _alarm_face_draw(settings, state, event.subsecond); + break; + case EVENT_ALARM_LONG_UP: + if (state->is_setting) { + if (state->setting_state == alarm_setting_idx_hour || state->setting_state == alarm_setting_idx_minute) + _abort_quick_ticks(state); + } else _wait_ticks = -1; + break; + case EVENT_BACKGROUND_TASK: + // play alarm + if (state->alarm[state->alarm_playing_idx].beeps == 0) { + // short beep + if (watch_is_buzzer_or_led_enabled()) { + _alarm_play_short_beep(state->alarm[state->alarm_playing_idx].pitch); + } else { + // enable, play beep and disable buzzer again + watch_enable_buzzer(); + _alarm_play_short_beep(state->alarm[state->alarm_playing_idx].pitch); + watch_disable_buzzer(); + } + } else { + // regular alarm beeps + movement_play_alarm_beeps((state->alarm[state->alarm_playing_idx].beeps == (ALARM_MAX_BEEP_ROUNDS - 1) ? 20 : state->alarm[state->alarm_playing_idx].beeps), + _buzzer_notes[state->alarm[state->alarm_playing_idx].pitch]); + } + // one time alarm? -> erase it + if (state->alarm[state->alarm_playing_idx].day == ALARM_DAY_ONE_TIME) { + state->alarm[state->alarm_playing_idx].day = ALARM_DAY_EACH_DAY; + state->alarm[state->alarm_playing_idx].minute = state->alarm[state->alarm_playing_idx].hour = 0; + state->alarm[state->alarm_playing_idx].beeps = 5; + state->alarm[state->alarm_playing_idx].pitch = 1; + state->alarm[state->alarm_playing_idx].enabled = false; + _alarm_update_alarm_enabled(settings, state); + } + break; + case EVENT_MODE_BUTTON_UP: + movement_move_to_next_face(); + break; + case EVENT_TIMEOUT: + movement_move_to_face(0); + break; + default: + break; + } + + return true; +}
\ No newline at end of file diff --git a/movement/watch_faces/complication/alarm_face.h b/movement/watch_faces/complication/alarm_face.h new file mode 100644 index 00000000..dafbee5e --- /dev/null +++ b/movement/watch_faces/complication/alarm_face.h @@ -0,0 +1,79 @@ +/* + * MIT License + * + * Copyright (c) 2022 Andreas Nebinger + * + * 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 ALARM_FACE_H_ +#define ALARM_FACE_H_ + +#include "movement.h" + +/* +A face for setting various alarms +*/ + +#define ALARM_ALARMS 16 // no of available alarm slots (be aware: only 4 bits reserved for this value in struct below) +#define ALARM_DAY_STATES 11 // no of different day settings +#define ALARM_DAY_EACH_DAY 7 +#define ALARM_DAY_ONE_TIME 8 +#define ALARM_DAY_WORKDAY 9 +#define ALARM_DAY_WEEKEND 10 +#define ALARM_MAX_BEEP_ROUNDS 11 // maximum number of beeping rounds for an alarm slot (including short and long alarms) +#define ALARM_SETTING_STATES 6 + +typedef struct { + uint8_t day : 4; // day of week: 0=MO, 1=TU, 2=WE, 3=TH, 4=FR, 5=SA, 6=SU, 7=each day, 8=one time alarm, 9=Weekdays, 10=Weekend + uint8_t hour : 5; + uint8_t minute : 6; + uint8_t beeps : 4; + uint8_t pitch :2; + bool enabled : 1; +} alarm_setting_t; + +typedef struct { + uint8_t alarm_idx : 4; + uint8_t alarm_playing_idx : 4; + uint8_t setting_state : 3; + int8_t alarm_handled_minute; + bool alarm_quick_ticks : 1; + bool is_setting : 1; + alarm_setting_t alarm[ALARM_ALARMS]; +} alarm_state_t; + + +void alarm_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void alarm_face_activate(movement_settings_t *settings, void *context); +bool alarm_face_loop(movement_event_t event, movement_settings_t *settings, void *context); +void alarm_face_resign(movement_settings_t *settings, void *context); +bool alarm_face_wants_background_task(movement_settings_t *settings, void *context); + +#define alarm_face ((const watch_face_t){ \ + alarm_face_setup, \ + alarm_face_activate, \ + alarm_face_loop, \ + alarm_face_resign, \ + alarm_face_wants_background_task, \ +}) + +#endif // ALARM_FACE_H_ diff --git a/movement/watch_faces/complication/countdown_face.c b/movement/watch_faces/complication/countdown_face.c index 81836f37..92400e66 100644 --- a/movement/watch_faces/complication/countdown_face.c +++ b/movement/watch_faces/complication/countdown_face.c @@ -95,7 +95,7 @@ static void reset(countdown_state_t *state) { } static void ring(countdown_state_t *state) { - movement_play_signal(); + movement_play_alarm(); reset(state); } @@ -132,6 +132,7 @@ void countdown_face_activate(movement_settings_t *settings, void *context) { if(state->mode == cd_running) { watch_date_time now = watch_rtc_get_date_time(); state->now_ts = watch_utility_date_time_to_unix_time(now, get_tz_offset(settings)); + watch_set_indicator(WATCH_INDICATOR_BELL); } } @@ -179,7 +180,10 @@ bool countdown_face_loop(movement_event_t event, movement_settings_t *settings, reset(state); break; case cd_waiting: - start(state, settings); + if (!(state->minutes == 0 && state->seconds == 0)) { + // Only start the timer if we have a valid time. + start(state, settings); + } break; case cd_setting: settings_increment(state); @@ -192,7 +196,7 @@ bool countdown_face_loop(movement_event_t event, movement_settings_t *settings, break; case EVENT_ALARM_LONG_PRESS: if (state->mode == cd_setting) { - state->minutes = DEFAULT_MINUTES; + state->minutes = 0; state->seconds = 0; draw(state, event.subsecond); break; diff --git a/movement/watch_faces/complication/counter_face.c b/movement/watch_faces/complication/counter_face.c index ac0388ab..fb03ce67 100644 --- a/movement/watch_faces/complication/counter_face.c +++ b/movement/watch_faces/complication/counter_face.c @@ -59,6 +59,7 @@ bool counter_face_loop(movement_event_t event, movement_settings_t *settings, vo state->counter_idx=0;//reset counter index } print_counter(state); + beep_counter(state); break; case EVENT_ALARM_LONG_PRESS: state->counter_idx=0; // reset counter index @@ -77,6 +78,27 @@ bool counter_face_loop(movement_event_t event, movement_settings_t *settings, vo return true; } +// beep counter index times +void beep_counter(counter_state_t *state) { + + int low_count = state->counter_idx/5; + int high_count = state->counter_idx - low_count * 5; + + for (int i=0; i<low_count; i++) { + watch_buzzer_play_note(BUZZER_NOTE_A6, 50); + watch_buzzer_play_note(BUZZER_NOTE_REST, 100); + } + + //sleep between high and low + watch_buzzer_play_note(BUZZER_NOTE_REST, 200); + + for (int i=0; i<high_count; i++) { + watch_buzzer_play_note(BUZZER_NOTE_B6, 50); + watch_buzzer_play_note(BUZZER_NOTE_REST, 100); + } +} + + // print counter index at the center of display. void print_counter(counter_state_t *state) { char buf[14]; diff --git a/movement/watch_faces/complication/counter_face.h b/movement/watch_faces/complication/counter_face.h index 2d389a15..430f5a8e 100644 --- a/movement/watch_faces/complication/counter_face.h +++ b/movement/watch_faces/complication/counter_face.h @@ -39,6 +39,7 @@ bool counter_face_loop(movement_event_t event, movement_settings_t *settings, vo void counter_face_resign(movement_settings_t *settings, void *context); void print_counter(counter_state_t *state); +void beep_counter(counter_state_t *state); #define counter_face ((const watch_face_t){ \ counter_face_setup, \ diff --git a/movement/watch_faces/complication/pulsometer_face.c b/movement/watch_faces/complication/pulsometer_face.c index 1d6f2086..ea7aad59 100644 --- a/movement/watch_faces/complication/pulsometer_face.c +++ b/movement/watch_faces/complication/pulsometer_face.c @@ -59,7 +59,7 @@ bool pulsometer_face_loop(movement_event_t event, movement_settings_t *settings, movement_request_tick_frequency(PULSOMETER_FACE_FREQUENCY); break; case EVENT_ALARM_BUTTON_UP: - case EVENT_ALARM_LONG_PRESS: + case EVENT_ALARM_LONG_UP: pulsometer_state->measuring = false; movement_request_tick_frequency(1); break; diff --git a/movement/watch_faces/complication/stopwatch_face.c b/movement/watch_faces/complication/stopwatch_face.c index e85bbd65..2a69e9d5 100644 --- a/movement/watch_faces/complication/stopwatch_face.c +++ b/movement/watch_faces/complication/stopwatch_face.c @@ -29,6 +29,12 @@ #include "watch.h" #include "watch_utility.h" +// distant future for background task: January 1, 2083 +// see stopwatch_face_activate for details +static const watch_date_time distant_future = { + .unit = {0, 0, 0, 1, 1, 63} +}; + void stopwatch_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { (void) settings; (void) watch_face_index; @@ -49,6 +55,7 @@ static void _stopwatch_face_update_display(stopwatch_state_t *stopwatch_state, b if (stopwatch_state->seconds_counted >= 3456000) { // display maxes out just shy of 40 days, thanks to the limit on the day digits (0-39) stopwatch_state->running = false; + movement_cancel_background_task(); watch_display_string("st39235959", 0); return; } @@ -72,12 +79,21 @@ static void _stopwatch_face_update_display(stopwatch_state_t *stopwatch_state, b void stopwatch_face_activate(movement_settings_t *settings, void *context) { (void) settings; - (void) context; if (watch_tick_animation_is_running()) watch_stop_tick_animation(); + + stopwatch_state_t *stopwatch_state = (stopwatch_state_t *)context; + if (stopwatch_state->running) { + // because the low power update happens on the minute mark, and the wearer could start + // the stopwatch anytime, the low power update could fire up to 59 seconds later than + // we need it to, causing the stopwatch to display stale data. + // So let's schedule a background task that will never fire. This will keep the watch + // from entering low energy mode while the stopwatch is on screen. This background task + // will remain scheduled until the stopwatch stops OR this watch face resigns. + movement_schedule_background_task(distant_future); + } } bool stopwatch_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { - (void) settings; stopwatch_state_t *stopwatch_state = (stopwatch_state_t *)context; switch (event.event_type) { @@ -103,6 +119,9 @@ bool stopwatch_face_loop(movement_event_t event, movement_settings_t *settings, } break; case EVENT_ALARM_BUTTON_DOWN: + if (settings->bit.button_should_sound) { + watch_buzzer_play_note(BUZZER_NOTE_C8, 50); + } stopwatch_state->running = !stopwatch_state->running; if (stopwatch_state->running) { // we're running now, so we need to set the start_time. @@ -118,15 +137,28 @@ bool stopwatch_face_loop(movement_event_t event, movement_settings_t *settings, // and resume from the "virtual" start time that's that many seconds ago. stopwatch_state->start_time = watch_utility_date_time_from_unix_time(timestamp, 0); } + // schedule our keepalive task when running... + movement_schedule_background_task(distant_future); + } else { + // and cancel it when stopped. + movement_cancel_background_task(); } break; case EVENT_TIMEOUT: // explicitly ignore the timeout event so we stay on screen break; case EVENT_LOW_ENERGY_UPDATE: - if (!watch_tick_animation_is_running()) watch_start_tick_animation(500); - _stopwatch_face_update_display(stopwatch_state, false); - watch_set_indicator(WATCH_INDICATOR_BELL); + if (!watch_tick_animation_is_running()) watch_start_tick_animation(1000); + if (!stopwatch_state->running) { + // since the tick animation is running, displaying the stopped time could be misleading, + // as it could imply that the stopwatch is running. instead, show a blank display to + // indicate that we are in sleep mode. + watch_display_string("st ---- ", 0); + } else { + // this OTOH shouldn't happen anymore; if we're running, we shouldn't enter low energy mode + _stopwatch_face_update_display(stopwatch_state, false); + watch_set_indicator(WATCH_INDICATOR_BELL); + } break; default: break; @@ -138,4 +170,8 @@ bool stopwatch_face_loop(movement_event_t event, movement_settings_t *settings, void stopwatch_face_resign(movement_settings_t *settings, void *context) { (void) settings; (void) context; -}
\ No newline at end of file + + // regardless of whether we're running or stopped, cancel the task + // that was keeping us awake while on screen. + movement_cancel_background_task(); +} diff --git a/movement/watch_faces/complication/sunrise_sunset_face.c b/movement/watch_faces/complication/sunrise_sunset_face.c index 8dea812e..7807de83 100644 --- a/movement/watch_faces/complication/sunrise_sunset_face.c +++ b/movement/watch_faces/complication/sunrise_sunset_face.c @@ -96,6 +96,11 @@ static void _sunrise_sunset_face_update(movement_settings_t *settings, sunrise_s if (seconds < 30) scratch_time.unit.minute = floor(minutes); else scratch_time.unit.minute = ceil(minutes); + if (scratch_time.unit.minute == 60) { + scratch_time.unit.minute = 0; + scratch_time.unit.hour = (scratch_time.unit.hour + 1) % 24; + } + if (date_time.reg < scratch_time.reg) _sunrise_sunset_set_expiration(state, scratch_time); if (date_time.reg < scratch_time.reg || show_next_match) { @@ -118,6 +123,11 @@ static void _sunrise_sunset_face_update(movement_settings_t *settings, sunrise_s if (seconds < 30) scratch_time.unit.minute = floor(minutes); else scratch_time.unit.minute = ceil(minutes); + if (scratch_time.unit.minute == 60) { + scratch_time.unit.minute = 0; + scratch_time.unit.hour = (scratch_time.unit.hour + 1) % 24; + } + if (date_time.reg < scratch_time.reg) _sunrise_sunset_set_expiration(state, scratch_time); if (date_time.reg < scratch_time.reg || show_next_match) { @@ -371,8 +381,11 @@ bool sunrise_sunset_face_loop(movement_event_t event, movement_settings_t *setti } break; case EVENT_TIMEOUT: - if (state->page || state->rise_index) { - // on timeout, exit settings mode and return to the next sunrise or sunset + if (watch_get_backup_data(1) == 0) { + // if no location set, return home + movement_move_to_face(0); + } else if (state->page || state->rise_index) { + // otherwise on timeout, exit settings mode and return to the next sunrise or sunset state->page = 0; state->rise_index = 0; movement_request_tick_frequency(1); diff --git a/movement/watch_faces/complication/tomato_face.c b/movement/watch_faces/complication/tomato_face.c index 37798daf..ed5554f2 100644 --- a/movement/watch_faces/complication/tomato_face.c +++ b/movement/watch_faces/complication/tomato_face.c @@ -124,6 +124,7 @@ void tomato_face_activate(movement_settings_t *settings, void *context) { if (state->mode == tomato_run) { watch_date_time now = watch_rtc_get_date_time(); state->now_ts = watch_utility_date_time_to_unix_time(now, get_tz_offset(settings)); + watch_set_indicator(WATCH_INDICATOR_BELL); } watch_set_colon(); } diff --git a/movement/watch_faces/complication/totp_face_lfs.c b/movement/watch_faces/complication/totp_face_lfs.c new file mode 100644 index 00000000..0b542653 --- /dev/null +++ b/movement/watch_faces/complication/totp_face_lfs.c @@ -0,0 +1,253 @@ +#include <stdlib.h> +#include <string.h> +#include <math.h> + +#include "TOTP.h" +#include "base32.h" + +#include "watch.h" +#include "watch_utility.h" +#include "filesystem.h" + +#include "totp_face_lfs.h" + +/* Reads from a file totp_uris.txt where each line is what's in a QR code: + * e.g. + * otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example + * otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 + * This is also the same as what Aegis exports in plain-text format. + * + * Minimal sanitisation of input, however. + * + * At the moment, to get the records onto the filesystem, start a serial connection and do: + * echo otpauth://totp/Example:alice@google.com?secret=JBSWY3DPEHPK3PXP&issuer=Example > totp_uris.txt + * echo otpauth://totp/ACME%20Co:john.doe@email.com?secret=HXDMVJECJJWSRB3HWIZR4IFUGFTMXBOZ&issuer=ACME%20Co&algorithm=SHA1&digits=6&period=30 >> totp_uris.txt + * (note the double >> in the second one) + * + * You may want to customise the characters that appear to identify the 2FA code. These are just the first two characters of the issuer, + * and it's fine to modify the URI. + */ + + +#define MAX_TOTP_RECORDS 20 +#define MAX_TOTP_SECRET_SIZE 48 +#define TOTP_FILE "totp_uris.txt" + +const char* TOTP_URI_START = "otpauth://totp/"; + +struct totp_record { + uint8_t *secret; + size_t secret_size; + char label[2]; + uint32_t period; +}; + +static struct totp_record totp_records[MAX_TOTP_RECORDS]; +static int num_totp_records = 0; + +static void init_totp_record(struct totp_record *totp_record) { + totp_record->secret_size = 0; + totp_record->label[0] = 'A'; + totp_record->label[1] = 'A'; + totp_record->period = 30; +} + +static bool totp_face_lfs_read_param(struct totp_record *totp_record, char *param, char *value) { + if (!strcmp(param, "issuer")) { + if (value[0] == '\0' || value[1] == '\0') { + printf("TOTP issuer must be >= 2 chars, got '%s'\n", value); + return false; + } + totp_record->label[0] = value[0]; + totp_record->label[1] = value[1]; + } else if (!strcmp(param, "secret")) { + if (UNBASE32_LEN(strlen(value)) > MAX_TOTP_SECRET_SIZE) { + printf("TOTP secret too long: %s\n", value); + return false; + } + totp_record->secret = malloc(UNBASE32_LEN(strlen(value))); + totp_record->secret_size = base32_decode((unsigned char *)value, totp_record->secret); + if (totp_record->secret_size == 0) { + free(totp_record->secret); + printf("TOTP can't decode secret: %s\n", value); + return false; + } + } else if (!strcmp(param, "digits")) { + if (!strcmp(param, "6")) { + printf("TOTP got %s, not 6 digits\n", value); + return false; + } + } else if (!strcmp(param, "period")) { + totp_record->period = atoi(value); + if (totp_record->period == 0) { + printf("TOTP invalid period %s\n", value); + return false; + } + } else if (!strcmp(param, "algorithm")) { + if (!strcmp(param, "SHA1")) { + printf("TOTP ignored due to algorithm %s\n", value); + return false; + } + } + + return true; +} + +static void totp_face_lfs_read_file(char *filename) { + // For 'format' of file, see comment at top. + const size_t uri_start_len = strlen(TOTP_URI_START); + + if (!filesystem_file_exists(filename)) { + printf("TOTP file error: %s\n", filename); + return; + } + + char line[256]; + int32_t offset = 0; + while (filesystem_read_line(filename, line, &offset, 255) && strlen(line)) { + if (num_totp_records == MAX_TOTP_RECORDS) { + printf("TOTP max records: %d\n", MAX_TOTP_RECORDS); + break; + } + + // Check that it looks like a URI + if (strncmp(TOTP_URI_START, line, uri_start_len)) { + printf("TOTP invalid uri start: %s\n", line); + continue; + } + + // Check that we can find a '?' (to start our parameters) + char *param; + char *param_saveptr = NULL; + char *params = strchr(line + uri_start_len, '?'); + if (params == NULL) { + printf("TOTP no params: %s\n", line); + continue; + } + + // Process the parameters and put them in the record + init_totp_record(&totp_records[num_totp_records]); + bool error = false; + param = strtok_r(params + 1, "&", ¶m_saveptr); + do { + char *param_middle = strchr(param, '='); + *param_middle = '\0'; + error = error || !totp_face_lfs_read_param(&totp_records[num_totp_records], param, param_middle + 1); + } while ((param = strtok_r(NULL, "&", ¶m_saveptr))); + + if (error) { + totp_records[num_totp_records].secret_size = 0; + continue; + } + + // If we found a probably valid TOTP record, keep it. + if (totp_records[num_totp_records].secret_size) { + num_totp_records += 1; + } else { + printf("TOTP missing secret: %s\n", line); + } + } +} + +void totp_face_lfs_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(totp_lfs_state_t)); + } + +#if !(__EMSCRIPTEN__) + if (num_totp_records == 0) { + totp_face_lfs_read_file(TOTP_FILE); + } +#endif +} + +static void totp_face_set_record(totp_lfs_state_t *totp_state, int i) { + if (num_totp_records == 0 && i >= num_totp_records) { + return; + } + + totp_state->current_index = i; + TOTP(totp_records[i].secret, totp_records[i].secret_size, totp_records[i].period); + totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp); + totp_state->steps = totp_state->timestamp / totp_records[i].period; +} + +void totp_face_lfs_activate(movement_settings_t *settings, void *context) { + (void) settings; + memset(context, 0, sizeof(totp_lfs_state_t)); + totp_lfs_state_t *totp_state = (totp_lfs_state_t *)context; + +#if __EMSCRIPTEN__ + if (num_totp_records == 0) { + // Doing this here rather than in setup makes things a bit more pleasant in the simulator, since there's no easy way to trigger + // setup again after uploading the data. + totp_face_lfs_read_file(TOTP_FILE); + } +#endif + + totp_state->timestamp = watch_utility_date_time_to_unix_time(watch_rtc_get_date_time(), movement_timezone_offsets[settings->bit.time_zone] * 60); + totp_face_set_record(totp_state, 0); +} + +static void totp_face_display(totp_lfs_state_t *totp_state) { + uint8_t index = totp_state->current_index; + char buf[14]; + + if (num_totp_records == 0) { + watch_display_string("No2F Codes", 0); + return; + } + + div_t result = div(totp_state->timestamp, totp_records[index].period); + if (result.quot != totp_state->steps) { + totp_state->current_code = getCodeFromTimestamp(totp_state->timestamp); + totp_state->steps = result.quot; + } + uint8_t valid_for = totp_records[index].period - result.rem; + + sprintf(buf, "%c%c%2d%06lu", totp_records[index].label[0], totp_records[index].label[1], valid_for, totp_state->current_code); + + watch_display_string(buf, 0); +} + +bool totp_face_lfs_loop(movement_event_t event, movement_settings_t *settings, void *context) { + (void) settings; + + totp_lfs_state_t *totp_state = (totp_lfs_state_t *)context; + + switch (event.event_type) { + case EVENT_TICK: + totp_state->timestamp++; + totp_face_display(totp_state); + break; + case EVENT_ACTIVATE: + totp_face_display(totp_state); + break; + case EVENT_MODE_BUTTON_UP: + movement_move_to_next_face(); + break; + case EVENT_LIGHT_BUTTON_DOWN: + movement_illuminate_led(); + break; + case EVENT_TIMEOUT: + movement_move_to_face(0); + break; + case EVENT_ALARM_BUTTON_UP: + totp_face_set_record(totp_state, (totp_state->current_index + 1) % num_totp_records); + totp_face_display(totp_state); + break; + case EVENT_ALARM_BUTTON_DOWN: + case EVENT_ALARM_LONG_PRESS: + default: + break; + } + + return true; +} + +void totp_face_lfs_resign(movement_settings_t *settings, void *context) { + (void) settings; + (void) context; +} diff --git a/movement/watch_faces/complication/totp_face_lfs.h b/movement/watch_faces/complication/totp_face_lfs.h new file mode 100644 index 00000000..0f388bc3 --- /dev/null +++ b/movement/watch_faces/complication/totp_face_lfs.h @@ -0,0 +1,26 @@ +#ifndef TOTP_FACE_LFS_H_ +#define TOTP_FACE_LFS_H_ + +#include "movement.h" + +typedef struct { + uint32_t timestamp; + uint8_t steps; + uint32_t current_code; + uint8_t current_index; +} totp_lfs_state_t; + +void totp_face_lfs_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr); +void totp_face_lfs_activate(movement_settings_t *settings, void *context); +bool totp_face_lfs_loop(movement_event_t event, movement_settings_t *settings, void *context); +void totp_face_lfs_resign(movement_settings_t *settings, void *context); + +#define totp_face_lfs ((const watch_face_t){ \ + totp_face_lfs_setup, \ + totp_face_lfs_activate, \ + totp_face_lfs_loop, \ + totp_face_lfs_resign, \ + NULL, \ +}) + +#endif // TOTP_FACE_LFS_H_ diff --git a/movement/watch_faces/settings/set_time_face.c b/movement/watch_faces/settings/set_time_face.c index af5421f1..1605f119 100644 --- a/movement/watch_faces/settings/set_time_face.c +++ b/movement/watch_faces/settings/set_time_face.c @@ -66,8 +66,8 @@ bool set_time_face_loop(movement_event_t event, movement_settings_t *settings, v date_time.unit.second = 0; break; case 3: // year - // only allow 2021-2030. fix this sometime next decade - date_time.unit.year = ((date_time.unit.year % 10) + 1); + // only allow 2021-2050. fix this if we make it that far. + date_time.unit.year = ((date_time.unit.year % 30) + 1); break; case 4: // month date_time.unit.month = (date_time.unit.month % 12) + 1; |