Skip to content

Commit b2d313e

Browse files
Metronome Complication (#303)
* Metronome Complication A simple metronome complication that allows user to set BPM, toggle sound, and set counts per measure. * silence warnings in metronome_face * avoid mode button in metronome settings, other tweaks --------- Co-authored-by: joeycastillo <joeycastillo@utexas.edu>
1 parent 0f5defe commit b2d313e

File tree

4 files changed

+351
-0
lines changed

4 files changed

+351
-0
lines changed

movement/make/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ SRCS += \
143143
../watch_faces/sensor/alarm_thermometer_face.c \
144144
../watch_faces/demo/beeps_face.c \
145145
../watch_faces/sensor/accel_interrupt_count_face.c \
146+
../watch_faces/complication/metronome_face.c \
146147
# New watch faces go above this line.
147148

148149
# Leave this line at the bottom of the file; it has all the targets for making your project.

movement/movement_faces.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@
118118
#include "alarm_thermometer_face.h"
119119
#include "beeps_face.h"
120120
#include "accel_interrupt_count_face.h"
121+
#include "metronome_face.h"
121122
// New includes go above this line.
122123

123124
#endif // MOVEMENT_FACES_H_
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
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+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
#ifndef METRONOME_FACE_H_
26+
#define METRONOME_FACE_H_
27+
28+
#include "movement.h"
29+
30+
/*
31+
* A Metronome watch complication
32+
* Allows the user to set the BPM, counts per measure, beep sound on/off
33+
* Screen flashes on on the beat and off on the half beat (1/8th note)
34+
* Beep will sound high for downbeat and low for subsequent beats in measure
35+
* USE:
36+
* Press Alarm to start/stop metronome_face
37+
* Hold Alarm to enter settings menu
38+
* Short Light press will move through options
39+
* Short Alarm press will increment/toggle options
40+
* Long alarm press will exit options
41+
*/
42+
43+
typedef enum {
44+
metWait,
45+
metRun,
46+
setMenu
47+
} metronome_mode_t;
48+
49+
typedef enum {
50+
hundred,
51+
ten,
52+
one,
53+
count,
54+
alarm
55+
} setting_cursor_t;
56+
57+
typedef struct {
58+
// Anything you need to keep track of, put it here!
59+
uint8_t bpm;
60+
double correction;
61+
double curCorrection;
62+
int count;
63+
int tick;
64+
int curTick;
65+
int curBeat;
66+
int halfBeat;
67+
metronome_mode_t mode : 3;
68+
setting_cursor_t setCur : 4;
69+
bool soundOn;
70+
} metronome_state_t;
71+
72+
void metronome_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
73+
void metronome_face_activate(movement_settings_t *settings, void *context);
74+
bool metronome_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
75+
void metronome_face_resign(movement_settings_t *settings, void *context);
76+
77+
#define metronome_face ((const watch_face_t){ \
78+
metronome_face_setup, \
79+
metronome_face_activate, \
80+
metronome_face_loop, \
81+
metronome_face_resign, \
82+
NULL, \
83+
})
84+
85+
#endif // METRONOME_FACE_H_
86+

0 commit comments

Comments
 (0)