-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ino
44 lines (36 loc) · 856 Bytes
/
main.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "include/config.h"
#include "include/melody.h"
static void play();
static void nop();
static void button_interrupt_handler();
typedef void job_fn_t();
static volatile job_fn_t* work = play;
static size_t note_iter = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
attachInterrupt(
digitalPinToInterrupt(BUTTON_PIN),
button_interrupt_handler,
FALLING
);
}
void loop() {
work();
}
static void play() {
struct Note note;
memcpy_P(¬e, MELODY + note_iter, sizeof(note));
digitalWrite(LED_PIN, HIGH);
tone(PIEZO_PIN, note.freq, note.dur);
digitalWrite(LED_PIN, LOW);
delay(note.pause);
note_iter = (note_iter + 1) % MELODY_LEN;
}
static void nop() {}
static void button_interrupt_handler() {
if (work == play) {
work = nop;
} else {
work = play;
}
}