|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2023 Austin Teets |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <stdlib.h> |
| 26 | +#include <string.h> |
| 27 | +#include "metronome_face.h" |
| 28 | +#include "watch.h" |
| 29 | + |
| 30 | +static const int8_t _sound_seq_start[] = {BUZZER_NOTE_C8, 2, 0}; |
| 31 | +static const int8_t _sound_seq_beat[] = {BUZZER_NOTE_C6, 2, 0}; |
| 32 | + |
| 33 | +void metronome_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) { |
| 34 | + (void) settings; |
| 35 | + (void) watch_face_index; |
| 36 | + if (*context_ptr == NULL) { |
| 37 | + *context_ptr = malloc(sizeof(metronome_state_t)); |
| 38 | + memset(*context_ptr, 0, sizeof(metronome_state_t)); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +void metronome_face_activate(movement_settings_t *settings, void *context) { |
| 43 | + (void) settings; |
| 44 | + metronome_state_t *state = (metronome_state_t *)context; |
| 45 | + movement_request_tick_frequency(2); |
| 46 | + if (state->bpm == 0) { |
| 47 | + state->count = 4; |
| 48 | + state->bpm = 120; |
| 49 | + state->soundOn = true; |
| 50 | + } |
| 51 | + state->mode = metWait; |
| 52 | + state->correction = 0; |
| 53 | + state->setCur = hundred; |
| 54 | +} |
| 55 | + |
| 56 | +static void _metronome_face_update_lcd(metronome_state_t *state) { |
| 57 | + char buf[11]; |
| 58 | + if (state->soundOn) { |
| 59 | + watch_set_indicator(WATCH_INDICATOR_BELL); |
| 60 | + } else { |
| 61 | + watch_clear_indicator(WATCH_INDICATOR_BELL); |
| 62 | + } |
| 63 | + sprintf(buf, "MN %d %03d%s", state->count, state->bpm, "bp"); |
| 64 | + watch_display_string(buf, 0); |
| 65 | +} |
| 66 | + |
| 67 | +static void _metronome_start_stop(metronome_state_t *state) { |
| 68 | + if (state->mode != metRun) { |
| 69 | + movement_request_tick_frequency(64); |
| 70 | + state->mode = metRun; |
| 71 | + watch_clear_display(); |
| 72 | + double ticks = 3840.0 / (double)state->bpm; |
| 73 | + state->tick = (int) ticks; |
| 74 | + state->curTick = (int) ticks; |
| 75 | + state->halfBeat = (int)(state->tick/2); |
| 76 | + state->curCorrection = ticks - state->tick; |
| 77 | + state->correction = ticks - state->tick; |
| 78 | + state->curBeat = 1; |
| 79 | + } else { |
| 80 | + state->mode = metWait; |
| 81 | + movement_request_tick_frequency(2); |
| 82 | + _metronome_face_update_lcd(state); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +static void _metronome_tick_beat(metronome_state_t *state) { |
| 87 | + char buf[11]; |
| 88 | + if (state->soundOn) { |
| 89 | + if (state->curBeat == 1) { |
| 90 | + watch_buzzer_play_sequence((int8_t *)_sound_seq_start, NULL); |
| 91 | + } else { |
| 92 | + watch_buzzer_play_sequence((int8_t *)_sound_seq_beat, NULL); |
| 93 | + } |
| 94 | + } |
| 95 | + sprintf(buf, "MN %d %03d%s", state->count, state->bpm, "bp"); |
| 96 | + watch_display_string(buf, 0); |
| 97 | +} |
| 98 | + |
| 99 | +static void _metronome_event_tick(uint8_t subsecond, metronome_state_t *state) { |
| 100 | + (void) subsecond; |
| 101 | + |
| 102 | + if (state->curCorrection >= 1) { |
| 103 | + state->curCorrection -= 1; |
| 104 | + state->curTick -= 1; |
| 105 | + } |
| 106 | + int diff = state->curTick - state->tick; |
| 107 | + if(diff == 0) { |
| 108 | + _metronome_tick_beat(state); |
| 109 | + state->curTick = 0; |
| 110 | + state->curCorrection += state->correction; |
| 111 | + if (state->curBeat < state->count ) { |
| 112 | + state->curBeat += 1; |
| 113 | + } else { |
| 114 | + state->curBeat = 1; |
| 115 | + } |
| 116 | + } else { |
| 117 | + if (state->curTick == state->halfBeat) { |
| 118 | + watch_clear_display(); |
| 119 | + } |
| 120 | + state->curTick += 1; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +static void _metronome_setting_tick(uint8_t subsecond, metronome_state_t *state) { |
| 125 | + char buf[13]; |
| 126 | + sprintf(buf, "MN %d %03d%s", state->count, state->bpm, "bp"); |
| 127 | + if (subsecond%2 == 0) { |
| 128 | + switch (state->setCur) { |
| 129 | + case hundred: |
| 130 | + buf[5] = ' '; |
| 131 | + break; |
| 132 | + case ten: |
| 133 | + buf[6] = ' '; |
| 134 | + break; |
| 135 | + case one: |
| 136 | + buf[7] = ' '; |
| 137 | + break; |
| 138 | + case count: |
| 139 | + buf[3] = ' '; |
| 140 | + break; |
| 141 | + case alarm: |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + if (state->setCur == alarm) { |
| 146 | + sprintf(buf, "MN 8eep%s", state->soundOn ? "On" : " -"); |
| 147 | + } |
| 148 | + if (state->soundOn) { |
| 149 | + watch_set_indicator(WATCH_INDICATOR_BELL); |
| 150 | + } else { |
| 151 | + watch_clear_indicator(WATCH_INDICATOR_BELL); |
| 152 | + } |
| 153 | + watch_display_string(buf, 0); |
| 154 | +} |
| 155 | + |
| 156 | +static void _metronome_update_setting(metronome_state_t *state) { |
| 157 | + char buf[13]; |
| 158 | + switch (state->setCur) { |
| 159 | + case hundred: |
| 160 | + if (state->bpm < 100) { |
| 161 | + state->bpm += 100; |
| 162 | + } else { |
| 163 | + state->bpm -= 100; |
| 164 | + } |
| 165 | + break; |
| 166 | + case ten: |
| 167 | + if ((state->bpm / 10) % 10 < 9) { |
| 168 | + state->bpm += 10; |
| 169 | + } else { |
| 170 | + state->bpm -= 90; |
| 171 | + } |
| 172 | + break; |
| 173 | + case one: |
| 174 | + if (state->bpm%10 < 9) { |
| 175 | + state->bpm += 1; |
| 176 | + } else { |
| 177 | + state->bpm -= 9; |
| 178 | + } |
| 179 | + break; |
| 180 | + case count: |
| 181 | + if (state->count < 9) { |
| 182 | + state->count += 1; |
| 183 | + } else { |
| 184 | + state->count = 2; |
| 185 | + } |
| 186 | + break; |
| 187 | + case alarm: |
| 188 | + state->soundOn = !state->soundOn; |
| 189 | + break; |
| 190 | + } |
| 191 | + sprintf(buf, "MN %d %03d%s", state->count % 10, state->bpm, "bp"); |
| 192 | + if (state->setCur == alarm) { |
| 193 | + sprintf(buf, "MN 8eep%s", state->soundOn ? "On" : " -"); |
| 194 | + } |
| 195 | + if (state->soundOn) { |
| 196 | + watch_set_indicator(WATCH_INDICATOR_BELL); |
| 197 | + } else { |
| 198 | + watch_clear_indicator(WATCH_INDICATOR_BELL); |
| 199 | + } |
| 200 | + watch_display_string(buf, 0); |
| 201 | +} |
| 202 | + |
| 203 | +bool metronome_face_loop(movement_event_t event, movement_settings_t *settings, void *context) { |
| 204 | + metronome_state_t *state = (metronome_state_t *)context; |
| 205 | + |
| 206 | + switch (event.event_type) { |
| 207 | + case EVENT_ACTIVATE: |
| 208 | + _metronome_face_update_lcd(state); |
| 209 | + break; |
| 210 | + case EVENT_TICK: |
| 211 | + if (state->mode == metRun){ |
| 212 | + _metronome_event_tick(event.subsecond, state); |
| 213 | + } else if (state->mode == setMenu) { |
| 214 | + _metronome_setting_tick(event.subsecond, state); |
| 215 | + } |
| 216 | + break; |
| 217 | + case EVENT_ALARM_BUTTON_UP: |
| 218 | + if (state->mode == setMenu) { |
| 219 | + _metronome_update_setting(state); |
| 220 | + } else { |
| 221 | + _metronome_start_stop(state); |
| 222 | + } |
| 223 | + break; |
| 224 | + case EVENT_LIGHT_BUTTON_DOWN: |
| 225 | + if (state->mode == setMenu) { |
| 226 | + if (state->setCur < alarm) { |
| 227 | + state->setCur += 1; |
| 228 | + } else { |
| 229 | + state->setCur = hundred; |
| 230 | + } |
| 231 | + } |
| 232 | + break; |
| 233 | + case EVENT_ALARM_LONG_PRESS: |
| 234 | + if (state->mode != metRun && state->mode != setMenu) { |
| 235 | + movement_request_tick_frequency(2); |
| 236 | + state->mode = setMenu; |
| 237 | + _metronome_face_update_lcd(state); |
| 238 | + } else if (state->mode == setMenu) { |
| 239 | + state->mode = metWait; |
| 240 | + _metronome_face_update_lcd(state); |
| 241 | + } |
| 242 | + break; |
| 243 | + case EVENT_MODE_BUTTON_UP: |
| 244 | + movement_move_to_next_face(); |
| 245 | + break; |
| 246 | + case EVENT_TIMEOUT: |
| 247 | + if (state->mode != metRun) { |
| 248 | + movement_move_to_face(0); |
| 249 | + } |
| 250 | + break; |
| 251 | + case EVENT_LOW_ENERGY_UPDATE: |
| 252 | + break; |
| 253 | + default: |
| 254 | + return movement_default_loop_handler(event, settings); |
| 255 | + } |
| 256 | + return true; |
| 257 | +} |
| 258 | + |
| 259 | +void metronome_face_resign(movement_settings_t *settings, void *context) { |
| 260 | + (void) settings; |
| 261 | + (void) context; |
| 262 | +} |
| 263 | + |
0 commit comments