Jurica Resetar / Mbed 2 deprecated beep_2

Dependencies:   aconno_bsp mbed

Fork of beep by Jurica Resetar

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