Jurica Resetar / Mbed 2 deprecated beep

Dependencies:   aconno_bsp mbed

Dependents:   acd52832_beep_buzzer

Fork of beep by Peter Drescher

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers beep.cpp Source File

beep.cpp

00001 #include "beep.h"
00002 #include "mbed.h"
00003 #include "notes.h"
00004 
00005 /** class to make sound with a buzzer, based on a PwmOut
00006  *   The class use a timeout to switch off the sound  - it is not blocking while making noise
00007  *
00008  * Example:
00009  * @code
00010  * // Beep with 1Khz for 0.5 seconds
00011  * #include "mbed.h"
00012  * #include "beep.h"
00013  * 
00014  * Beep buzzer(p21);
00015  * 
00016  * int main() {
00017  *       ...
00018  *   buzzer.beep(1000,0.5);    
00019  *       ...
00020  * }
00021  * @endcode
00022  */
00023 
00024 int notes[] = { 0,
00025 NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4,
00026 NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5,
00027 NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6,
00028 NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7
00029 };
00030 
00031 
00032 using namespace mbed;
00033  // constructor
00034  /** Create a Beep object connected to the specified PwmOut pin
00035   *
00036   * @param pin PwmOut pin to connect to 
00037   */
00038     
00039 Beep::Beep(PinName pin) : _pwm(pin) {
00040     _pwm.write(0.0);     // after creating it have to be off
00041 }
00042 
00043  /** stop the beep instantaneous 
00044   * usually not used 
00045   */
00046 void Beep::nobeep() {
00047     _pwm.write(0.0);
00048 }
00049 
00050 /** Beep with given frequency and duration.
00051  *
00052  * @param frequency - the frequency of the tone in Hz
00053  * @param time - the duration of the tone in seconds
00054  */
00055      
00056 void Beep::beep(float freq, float time) {
00057 
00058     _pwm.period(1.0/freq);
00059     _pwm.write(0.5);            // 50% duty cycle - beep on
00060     toff.attach(this,&Beep::nobeep, time);   // time to off
00061 }
00062 
00063 
00064 void Beep::playRttl(char *p){
00065   char default_dur = 4;
00066   char default_oct = 6;
00067   int bpm = 63;
00068   int num;
00069   long wholenote;
00070   long duration;
00071   char note;
00072   char scale;
00073 
00074   // format: d=N,o=N,b=NNN:
00075   // find the start (skip name, etc)
00076 
00077   while(*p != ':') p++;    // ignore name
00078   p++;                     // skip ':'
00079 
00080   // get default duration
00081   if(*p == 'd')
00082   {
00083     p++; p++;              // skip "d="
00084     num = 0;
00085     while(isdigit(*p))
00086     {
00087       num = (num * 10) + (*p++ - '0');
00088     }
00089     if(num > 0) default_dur = num;
00090     p++;                   // skip comma
00091   }
00092 
00093 
00094   // get default octave
00095   if(*p == 'o')
00096   {
00097     p++; p++;              // skip "o="
00098     num = *p++ - '0';
00099     if(num >= 3 && num <=7) default_oct = num;
00100     p++;                   // skip comma
00101   }
00102 
00103 
00104   // get BPM
00105   if(*p == 'b')
00106   {
00107     p++; p++;              // skip "b="
00108     num = 0;
00109     while(isdigit(*p))
00110     {
00111       num = (num * 10) + (*p++ - '0');
00112     }
00113     bpm = num;
00114     p++;                   // skip colon
00115   }
00116 
00117 
00118   // BPM usually expresses the number of quarter notes per minute
00119   wholenote = (60 * 1000L / bpm) * 4;  // this is the time for whole note (in seconds)
00120     
00121 
00122 
00123   // now begin note loop
00124   while(*p)
00125   {
00126     // first, get note duration, if available
00127     num = 0;
00128     while(isdigit(*p))
00129     {
00130       num = (num * 10) + (*p++ - '0');
00131     }
00132     
00133     if(num) duration = wholenote / num;
00134     else duration = wholenote / default_dur;  // we will need to check if we are a dotted note after
00135 
00136     // now get the note
00137     note = 0;
00138 
00139     switch(*p)
00140     {
00141       case 'c':
00142         note = 1;
00143         break;
00144       case 'd':
00145         note = 3;
00146         break;
00147       case 'e':
00148         note = 5;
00149         break;
00150       case 'f':
00151         note = 6;
00152         break;
00153       case 'g':
00154         note = 8;
00155         break;
00156       case 'a':
00157         note = 10;
00158         break;
00159       case 'b':
00160         note = 12;
00161         break;
00162       case 'p':
00163       default:
00164         note = 0;
00165     }
00166     p++;
00167 
00168     // now, get optional '#' sharp
00169     if(*p == '#')
00170     {
00171       note++;
00172       p++;
00173     }
00174 
00175     // now, get optional '.' dotted note
00176     if(*p == '.')
00177     {
00178       duration += duration/2;
00179       p++;
00180     }
00181   
00182     // now, get scale
00183     if(isdigit(*p))
00184     {
00185       scale = *p - '0';
00186       p++;
00187     }
00188     else
00189     {
00190       scale = default_oct;
00191     }
00192 
00193     scale += OCTAVE_OFFSET;
00194 
00195     if(*p == ',')
00196       p++;       // skip comma for next note (or we may be at the end)
00197 
00198     // now play the note
00199 
00200     if(note)
00201     {
00202       beep(notes[(scale - 4) * 12 + note], (float)duration/1000);
00203       wait((float)duration/1000);
00204     }
00205     else
00206     {
00207       //wait((float)duration/1000);
00208     }
00209   }
00210 }
00211