greenhouse
jukebox.cpp
Go to the documentation of this file.
1 #include "jukebox.h"
2 #include "ThisThread.h"
3 #include "mbed.h"
4 
5 using namespace Jukebox;
6 
7 PwmOut buzzer(D3);
8 bool isPlaying = false;
9 
10 #define NOTE_C4 262 // Defining note frequency
11 #define NOTE_D4 294
12 #define NOTE_E4 330
13 #define NOTE_F4 349
14 #define NOTE_G4 392
15 #define NOTE_A4 440
16 #define NOTE_B4 494
17 #define NOTE_C5 523
18 #define NOTE_D5 587
19 #define NOTE_E5 659
20 #define NOTE_F5 698
21 #define NOTE_G5 784
22 #define NOTE_A5 880
23 #define NOTE_B5 988
24 
25 float notes[] = { // Note of the song, 0 is a rest/pulse
28 
31 
34  NOTE_A4, 0,
35 
38 
40  // Repeat of first part
42  0, NOTE_A4, NOTE_G4, NOTE_A4, 0,
43 
46 
49  NOTE_A4, 0,
50 
53  // End of Repeat
54 
55  NOTE_E5, 0, 0, NOTE_F5, 0, 0, NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5,
56  NOTE_D5, 0, 0, NOTE_D5, 0, 0, NOTE_C5, 0, 0, NOTE_B4, NOTE_C5, 0, NOTE_B4,
57  0, NOTE_A4,
58 
59  NOTE_E5, 0, 0, NOTE_F5, 0, 0, NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5,
60  NOTE_D5, 0, 0, NOTE_D5, 0, 0, NOTE_C5, 0, 0, NOTE_B4, NOTE_C5, 0, NOTE_B4,
61  0, NOTE_A4};
62 int duration[] =
63  { // duration of each note (in ms) Quarter Note is set to 250 ms
64  125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125,
65  125, 125, 125, 375, 125,
66 
67  125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125,
68  125, 125, 125, 375, 125,
69 
70  125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125,
71  125, 125, 125, 125, 250, 125,
72 
73  125, 125, 250, 125, 125, 250, 125, 250, 125, 125, 125, 250, 125, 125,
74  125, 125, 375, 375,
75 
76  250, 125,
77  // Rpeat of First Part
78  125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 375, 125,
79 
80  125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125,
81  125, 125, 125, 375, 125,
82 
83  125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125,
84  125, 125, 125, 125, 250, 125,
85 
86  125, 125, 250, 125, 125, 250, 125, 250, 125, 125, 125, 250, 125, 125,
87  125, 125, 375, 375,
88  // End of Repeat
89 
90  250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 125, 125, 125,
91  375, 250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 500,
92 
93  250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 125, 125, 125,
94  375, 250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 500};
95 
96 
97 void Jukebox::play() {
98  if (isPlaying) {
99  return;
100  }
101 
102  isPlaying = true;
103  buzzer.resume();
104 
105  const int songspeed = 1.5;
106  float result;
107  float bzz = 0.5;
108 
109  for (int i = 0; i < 203; i++) {
110  if (!isPlaying) {
111  stop();
112  }
113 
114  int w = duration[i] * songspeed;
115  if (notes[i] == 0) {
116  result = 1;
117  bzz = 0;
118  } else {
119  result = 1 / notes[i];
120  bzz = 0.4;
121  }
122 
123  buzzer.period(result);
124  buzzer.write(bzz);
125 
126  wait_us(w * 1000);
127  }
128 
129  buzzer.suspend();
130 }
131 
133  buzzer.suspend();
134  isPlaying = false;
135 }
136 
138  return isPlaying;
139 }
#define NOTE_E4
Definition: jukebox.cpp:12
#define NOTE_D5
Definition: jukebox.cpp:18
int duration[]
Definition: jukebox.cpp:62
#define NOTE_E5
Definition: jukebox.cpp:19
#define NOTE_A4
Definition: jukebox.cpp:15
#define NOTE_G4
Definition: jukebox.cpp:14
#define NOTE_F5
Definition: jukebox.cpp:20
#define NOTE_C5
Definition: jukebox.cpp:17
#define NOTE_B4
Definition: jukebox.cpp:16
PwmOut buzzer(D3)
float notes[]
Definition: jukebox.cpp:25
bool isPlaying
Definition: jukebox.cpp:8
#define NOTE_G5
Definition: jukebox.cpp:21
Definition: jukebox.h:3
bool is_playing()
Definition: jukebox.cpp:137
void stop()
Definition: jukebox.cpp:132
void play()
Definition: jukebox.cpp:97