-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuzzer.cpp
More file actions
148 lines (121 loc) · 3.23 KB
/
Copy pathbuzzer.cpp
File metadata and controls
148 lines (121 loc) · 3.23 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <Arduino.h>
#include "pt.h"
#include "pt-sem.h"
#include "deadSimpleTimer.h"
#include "buzzer.h"
#include "hardwareConsts.h"
#include "log4arduino.h"
//private static
pt_sem Buzzer::playFoodIntroSemaphore;
pt Buzzer::pt;
void Buzzer::setup()
{
LOG("Entering buzzer setup");
ledcSetup(BUZZER_CHANNEL, BUZZER_FREQUENCY, BUZZER_RESOLUTION_BITS);
ledcAttachPin(UC_PIN_BUZZER, BUZZER_CHANNEL);
PT_SEM_INIT(&playFoodIntroSemaphore, 0);
PT_INIT(&pt);
LOG("Finished buzzer setup");
}
void Buzzer::playFoodIntro()
{
PT_SEM_SIGNAL(pt, &playFoodIntroSemaphore);
}
void Buzzer::longBeep()
{
buzzStart(NOTE_E5, 1);
delay(500);
buzzStop();
delay(100);
}
void Buzzer::shortBeep()
{
buzzStart(NOTE_A7, 1);
delay(50);
buzzStop();
}
int Buzzer::protothread()
{
//the variables used in protothreads have to be static
static int thisNote;
static DeadSimpleTimer::deadTimer buzzingTimer;
static unsigned int noteDuration;
static unsigned int pauseBetweenNotes;
PT_BEGIN(&pt);
while(1) {
PT_SEM_WAIT(&pt, &playFoodIntroSemaphore);
for (thisNote = 0; thisNote < themeSize; thisNote++)
{
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = (1000*1000)/2 / 4, eighth note = 1 000 000 /8, etc.
noteDuration = (1000*1000) / tempo[thisNote];
buzzStart(melody[thisNote], 1);
DeadSimpleTimer::setUs(&buzzingTimer, noteDuration);
PT_WAIT_UNTIL(&pt, DeadSimpleTimer::expired(&buzzingTimer));
buzzStop();
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
pauseBetweenNotes = noteDuration * 1;
DeadSimpleTimer::setUs(&buzzingTimer, pauseBetweenNotes);
PT_WAIT_UNTIL(&pt, DeadSimpleTimer::expired(&buzzingTimer));
}
thisNote = 0;
}
PT_END(&pt);
}
void Buzzer::buzzStart(long frequency, int duty)
{
ledcWrite(BUZZER_CHANNEL, duty);
ledcWriteTone(BUZZER_CHANNEL, frequency);
}
void Buzzer::buzzStop()
{
ledcWrite(BUZZER_CHANNEL, 0);
}
const int Buzzer::melody[16] = {
NOTE_E7, NOTE_E7, 0, NOTE_E7,
0, NOTE_C7, NOTE_E7, 0,
NOTE_G7, 0, 0, 0,
NOTE_G6, 0, 0, 0,
/*
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0,NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0,
NOTE_C7, 0, 0, NOTE_G6,
0, 0, NOTE_E6, 0,
0, NOTE_A6, 0, NOTE_B6,
0, NOTE_AS6, NOTE_A6, 0,
NOTE_G6, NOTE_E7, NOTE_G7,
NOTE_A7, 0, NOTE_F7, NOTE_G7,
0, NOTE_E7, 0,NOTE_C7,
NOTE_D7, NOTE_B6, 0, 0*/
};
const int Buzzer::tempo[16] = {
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
/*
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,
9, 9, 9,
12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12,*/
};