play songs
Dependencies: mbed
main.cpp@3:c04186a77ede, 2016-05-12 (annotated)
- Committer:
- maclobdell
- Date:
- Thu May 12 17:37:43 2016 +0000
- Revision:
- 3:c04186a77ede
- Parent:
- 2:610196c7b41c
- Child:
- 4:02dc2ab8eabb
removed section to be added by attendees
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maclobdell | 0:0fb17f0d39e7 | 1 | #include "mbed.h" // this tells us to load mbed related functions |
maclobdell | 1:8922cc02aec7 | 2 | #include "tones.h" // list of all the tones and their frequencies |
maclobdell | 1:8922cc02aec7 | 3 | #include "rtos.h" // using the rtos for timing features. Just want to block the play_song function until it is time to play the next note. |
maclobdell | 0:0fb17f0d39e7 | 4 | |
maclobdell | 0:0fb17f0d39e7 | 5 | PwmOut buzzer(D3); // our buzzer is a PWM output (pulse-width modulation) |
maclobdell | 0:0fb17f0d39e7 | 6 | |
maclobdell | 0:0fb17f0d39e7 | 7 | static int BPM = 120; |
maclobdell | 0:0fb17f0d39e7 | 8 | |
maclobdell | 0:0fb17f0d39e7 | 9 | static void silence() { |
maclobdell | 0:0fb17f0d39e7 | 10 | buzzer.write(0.0f); // silence! |
maclobdell | 0:0fb17f0d39e7 | 11 | } |
maclobdell | 0:0fb17f0d39e7 | 12 | |
maclobdell | 0:0fb17f0d39e7 | 13 | // this is our function that plays a tone. |
maclobdell | 0:0fb17f0d39e7 | 14 | // Takes in a tone frequency, and after duration (in ms.) we stop playing again |
maclobdell | 0:0fb17f0d39e7 | 15 | static void play_tone(int tone) { |
maclobdell | 2:610196c7b41c | 16 | buzzer.period_us(1000000/(tone)); |
maclobdell | 0:0fb17f0d39e7 | 17 | buzzer.write(0.10f); // 10% duty cycle, otherwise it's too loud |
maclobdell | 0:0fb17f0d39e7 | 18 | } |
maclobdell | 0:0fb17f0d39e7 | 19 | |
maclobdell | 0:0fb17f0d39e7 | 20 | static void play_song(int notes_left, int* melody, int* duration) { |
maclobdell | 1:8922cc02aec7 | 21 | |
maclobdell | 0:0fb17f0d39e7 | 22 | // YOUR CODE HERE |
maclobdell | 2:610196c7b41c | 23 | |
maclobdell | 0:0fb17f0d39e7 | 24 | } |
maclobdell | 0:0fb17f0d39e7 | 25 | |
maclobdell | 0:0fb17f0d39e7 | 26 | // this code runs when the microcontroller starts up |
maclobdell | 1:8922cc02aec7 | 27 | int main() { |
maclobdell | 2:610196c7b41c | 28 | |
maclobdell | 0:0fb17f0d39e7 | 29 | // declare a melody |
maclobdell | 0:0fb17f0d39e7 | 30 | int melody[] = { |
maclobdell | 2:610196c7b41c | 31 | NOTE_G4, NOTE_G4, NOTE_G4, NOTE_DS3, NOTE_AS4, |
maclobdell | 2:610196c7b41c | 32 | NOTE_G4, NOTE_DS3, NOTE_AS4, NOTE_G4, |
maclobdell | 2:610196c7b41c | 33 | NOTE_D4, NOTE_D4, NOTE_D4, NOTE_DS4, NOTE_AS4, |
maclobdell | 2:610196c7b41c | 34 | NOTE_FS4, NOTE_DS4, NOTE_AS4, NOTE_G4 |
maclobdell | 0:0fb17f0d39e7 | 35 | }; |
maclobdell | 2:610196c7b41c | 36 | |
maclobdell | 0:0fb17f0d39e7 | 37 | // note durations: 4 = quarter note, 8 = eighth note, etc.: |
maclobdell | 0:0fb17f0d39e7 | 38 | int duration[] = { |
maclobdell | 2:610196c7b41c | 39 | 2, 2, 2, 4, 4, |
maclobdell | 2:610196c7b41c | 40 | 2, 4, 4, 1, |
maclobdell | 2:610196c7b41c | 41 | 2, 2, 2, 4, 4, |
maclobdell | 2:610196c7b41c | 42 | 2, 4, 4, 1 |
maclobdell | 2:610196c7b41c | 43 | }; |
maclobdell | 0:0fb17f0d39e7 | 44 | |
maclobdell | 0:0fb17f0d39e7 | 45 | // melody & duration are on the heap, need to get them on the stack |
maclobdell | 0:0fb17f0d39e7 | 46 | int *m = new int[sizeof(melody) / sizeof(int)]; |
maclobdell | 0:0fb17f0d39e7 | 47 | memcpy(m, melody, sizeof(melody)); |
maclobdell | 0:0fb17f0d39e7 | 48 | int *d = new int[sizeof(duration) / sizeof(int)]; |
maclobdell | 0:0fb17f0d39e7 | 49 | memcpy(d, duration, sizeof(duration)); |
maclobdell | 0:0fb17f0d39e7 | 50 | |
maclobdell | 0:0fb17f0d39e7 | 51 | if (sizeof(melody) != sizeof(duration)) { |
maclobdell | 0:0fb17f0d39e7 | 52 | printf("Melody and duration do not have same number of elements! Aborting!\r\n"); |
maclobdell | 2:610196c7b41c | 53 | return 1; |
maclobdell | 0:0fb17f0d39e7 | 54 | } |
maclobdell | 0:0fb17f0d39e7 | 55 | |
maclobdell | 0:0fb17f0d39e7 | 56 | play_song(sizeof(melody) / sizeof(int), m, d); |
maclobdell | 2:610196c7b41c | 57 | |
maclobdell | 2:610196c7b41c | 58 | return 0; |
maclobdell | 0:0fb17f0d39e7 | 59 | } |