-
Notifications
You must be signed in to change notification settings - Fork 0
/
buzzer.cpp
74 lines (51 loc) · 1.08 KB
/
buzzer.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "buzzer.h"
uint16_t buzzerPattern;
uint16_t buzzerPeriod;
PinBuzzer pinBuzzer;
void buzzerSet(uint16_t pattern, uint16_t period) {
buzzerPattern = pattern;
buzzerPeriod = period;
}
void buzzerBegin() {
pinBuzzer.mode(IOMode::OutputLow);
}
void buzzerTick() {
static uint16_t counter;
static uint8_t patIndex;
static uint16_t pat;
if (++counter < buzzerPeriod) return;
counter = 0;
pinBuzzer.write(pat & 1);
pat >>= 1;
if (++patIndex < 16) return;
patIndex = 0;
pat = buzzerPattern;
}
void buzzerBeep() {
// Short beep
pinBuzzer.write(1);
delay(250);
pinBuzzer.write(0);
}
uint16_t ledPattern;
uint16_t ledPeriod;
PinLED pinLED;
void ledSet(uint16_t pattern, uint16_t period) {
ledPattern = pattern;
ledPeriod = period;
}
void ledBegin() {
pinLED.mode(IOMode::OutputLow);
}
void ledTick() {
static uint8_t patIndex;
static uint16_t pat;
static uint16_t counter;
if (++counter < ledPeriod) return;
counter = 0;
pinLED.write(! (pat & 1) );
pat >>= 1;
if (++patIndex < 16) return;
patIndex = 0;
pat = ledPattern;
}