Make noise with a piezo buzzer. Use a pwm pin.
Dependents: acd52832_beep_buzzer
Fork of beep by
Revision 5:49c961e79a12, committed 2016-09-22
- Comitter:
- jurica238814
- Date:
- Thu Sep 22 18:11:47 2016 +0000
- Parent:
- 4:d8e14429a95f
- Commit message:
- Beep RTTL done =)
Changed in this revision
diff -r d8e14429a95f -r 49c961e79a12 aconno_bsp.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/aconno_bsp.lib Thu Sep 22 18:11:47 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/jurica238814/code/aconno_bsp/#6d9c6c231034
diff -r d8e14429a95f -r 49c961e79a12 beep.cpp --- a/beep.cpp Tue Sep 11 08:21:45 2012 +0000 +++ b/beep.cpp Thu Sep 22 18:11:47 2016 +0000 @@ -1,5 +1,6 @@ #include "beep.h" #include "mbed.h" +#include "notes.h" /** class to make sound with a buzzer, based on a PwmOut * The class use a timeout to switch off the sound - it is not blocking while making noise @@ -20,6 +21,14 @@ * @endcode */ +int notes[] = { 0, +NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, +NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, +NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6, +NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7 +}; + + using namespace mbed; // constructor /** Create a Beep object connected to the specified PwmOut pin @@ -52,5 +61,151 @@ } +void Beep::playRttl(char *p){ + char default_dur = 4; + char default_oct = 6; + int bpm = 63; + int num; + long wholenote; + long duration; + char note; + char scale; + + // format: d=N,o=N,b=NNN: + // find the start (skip name, etc) + + while(*p != ':') p++; // ignore name + p++; // skip ':' + + // get default duration + if(*p == 'd') + { + p++; p++; // skip "d=" + num = 0; + while(isdigit(*p)) + { + num = (num * 10) + (*p++ - '0'); + } + if(num > 0) default_dur = num; + p++; // skip comma + } + + + // get default octave + if(*p == 'o') + { + p++; p++; // skip "o=" + num = *p++ - '0'; + if(num >= 3 && num <=7) default_oct = num; + p++; // skip comma + } + + + // get BPM + if(*p == 'b') + { + p++; p++; // skip "b=" + num = 0; + while(isdigit(*p)) + { + num = (num * 10) + (*p++ - '0'); + } + bpm = num; + p++; // skip colon + } + + + // BPM usually expresses the number of quarter notes per minute + wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in seconds) + + // now begin note loop + while(*p) + { + // first, get note duration, if available + num = 0; + while(isdigit(*p)) + { + num = (num * 10) + (*p++ - '0'); + } + + if(num) duration = wholenote / num; + else duration = wholenote / default_dur; // we will need to check if we are a dotted note after + + // now get the note + note = 0; + + switch(*p) + { + case 'c': + note = 1; + break; + case 'd': + note = 3; + break; + case 'e': + note = 5; + break; + case 'f': + note = 6; + break; + case 'g': + note = 8; + break; + case 'a': + note = 10; + break; + case 'b': + note = 12; + break; + case 'p': + default: + note = 0; + } + p++; + + // now, get optional '#' sharp + if(*p == '#') + { + note++; + p++; + } + + // now, get optional '.' dotted note + if(*p == '.') + { + duration += duration/2; + p++; + } + + // now, get scale + if(isdigit(*p)) + { + scale = *p - '0'; + p++; + } + else + { + scale = default_oct; + } + + scale += OCTAVE_OFFSET; + + if(*p == ',') + p++; // skip comma for next note (or we may be at the end) + + // now play the note + + if(note) + { + beep(notes[(scale - 4) * 12 + note], (float)duration/1000); + wait((float)duration/1000); + } + else + { + //wait((float)duration/1000); + } + } +} +
diff -r d8e14429a95f -r 49c961e79a12 beep.h --- a/beep.h Tue Sep 11 08:21:45 2012 +0000 +++ b/beep.h Thu Sep 22 18:11:47 2016 +0000 @@ -3,6 +3,9 @@ #include "mbed.h" +#define isdigit(n) (n >= '0' && n <= '9') +#define OCTAVE_OFFSET 0 + /** class to make sound with a buzzer, based on a PwmOut * The class use a timeout to switch off the sound - it is not blocking while making noise * @@ -44,6 +47,7 @@ * @param time - the duration of the tone in seconds */ void beep (float frequency, float time); + void playRttl(char *p); /** stop the beep instantaneous * usually not used
diff -r d8e14429a95f -r 49c961e79a12 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Sep 22 18:11:47 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/abea610beb85 \ No newline at end of file