Make noise with a piezo buzzer. Use a pwm pin. IO expander ints done.
Fork of beep by
beep.cpp@6:936ba3699e47, 2016-09-23 (annotated)
- Committer:
- jurica238814
- Date:
- Fri Sep 23 12:12:16 2016 +0000
- Revision:
- 6:936ba3699e47
- Parent:
- 5:49c961e79a12
IO expander ints done.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:18e4a9c978ec | 1 | #include "beep.h" |
dreschpe | 0:18e4a9c978ec | 2 | #include "mbed.h" |
jurica238814 | 5:49c961e79a12 | 3 | #include "notes.h" |
jurica238814 | 6:936ba3699e47 | 4 | #include "MCP23017.h" |
dreschpe | 0:18e4a9c978ec | 5 | |
dreschpe | 2:a34405c20cf5 | 6 | /** class to make sound with a buzzer, based on a PwmOut |
dreschpe | 3:5a8242af60ba | 7 | * The class use a timeout to switch off the sound - it is not blocking while making noise |
dreschpe | 3:5a8242af60ba | 8 | * |
dreschpe | 3:5a8242af60ba | 9 | * Example: |
dreschpe | 3:5a8242af60ba | 10 | * @code |
dreschpe | 3:5a8242af60ba | 11 | * // Beep with 1Khz for 0.5 seconds |
dreschpe | 3:5a8242af60ba | 12 | * #include "mbed.h" |
dreschpe | 3:5a8242af60ba | 13 | * #include "beep.h" |
dreschpe | 3:5a8242af60ba | 14 | * |
dreschpe | 3:5a8242af60ba | 15 | * Beep buzzer(p21); |
dreschpe | 3:5a8242af60ba | 16 | * |
dreschpe | 3:5a8242af60ba | 17 | * int main() { |
dreschpe | 3:5a8242af60ba | 18 | * ... |
dreschpe | 3:5a8242af60ba | 19 | * buzzer.beep(1000,0.5); |
dreschpe | 3:5a8242af60ba | 20 | * ... |
dreschpe | 3:5a8242af60ba | 21 | * } |
dreschpe | 3:5a8242af60ba | 22 | * @endcode |
dreschpe | 3:5a8242af60ba | 23 | */ |
dreschpe | 2:a34405c20cf5 | 24 | |
jurica238814 | 5:49c961e79a12 | 25 | int notes[] = { 0, |
jurica238814 | 5:49c961e79a12 | 26 | NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, |
jurica238814 | 5:49c961e79a12 | 27 | NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, |
jurica238814 | 5:49c961e79a12 | 28 | NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6, |
jurica238814 | 5:49c961e79a12 | 29 | NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7 |
jurica238814 | 5:49c961e79a12 | 30 | }; |
jurica238814 | 5:49c961e79a12 | 31 | |
jurica238814 | 5:49c961e79a12 | 32 | |
dreschpe | 0:18e4a9c978ec | 33 | using namespace mbed; |
dreschpe | 3:5a8242af60ba | 34 | // constructor |
dreschpe | 2:a34405c20cf5 | 35 | /** Create a Beep object connected to the specified PwmOut pin |
dreschpe | 3:5a8242af60ba | 36 | * |
dreschpe | 3:5a8242af60ba | 37 | * @param pin PwmOut pin to connect to |
dreschpe | 3:5a8242af60ba | 38 | */ |
dreschpe | 2:a34405c20cf5 | 39 | |
dreschpe | 0:18e4a9c978ec | 40 | Beep::Beep(PinName pin) : _pwm(pin) { |
dreschpe | 0:18e4a9c978ec | 41 | _pwm.write(0.0); // after creating it have to be off |
dreschpe | 0:18e4a9c978ec | 42 | } |
dreschpe | 0:18e4a9c978ec | 43 | |
dreschpe | 2:a34405c20cf5 | 44 | /** stop the beep instantaneous |
dreschpe | 3:5a8242af60ba | 45 | * usually not used |
dreschpe | 3:5a8242af60ba | 46 | */ |
dreschpe | 0:18e4a9c978ec | 47 | void Beep::nobeep() { |
dreschpe | 0:18e4a9c978ec | 48 | _pwm.write(0.0); |
dreschpe | 0:18e4a9c978ec | 49 | } |
dreschpe | 0:18e4a9c978ec | 50 | |
dreschpe | 2:a34405c20cf5 | 51 | /** Beep with given frequency and duration. |
dreschpe | 3:5a8242af60ba | 52 | * |
dreschpe | 3:5a8242af60ba | 53 | * @param frequency - the frequency of the tone in Hz |
dreschpe | 3:5a8242af60ba | 54 | * @param time - the duration of the tone in seconds |
dreschpe | 3:5a8242af60ba | 55 | */ |
dreschpe | 2:a34405c20cf5 | 56 | |
dreschpe | 0:18e4a9c978ec | 57 | void Beep::beep(float freq, float time) { |
dreschpe | 0:18e4a9c978ec | 58 | |
dreschpe | 0:18e4a9c978ec | 59 | _pwm.period(1.0/freq); |
dreschpe | 0:18e4a9c978ec | 60 | _pwm.write(0.5); // 50% duty cycle - beep on |
dreschpe | 0:18e4a9c978ec | 61 | toff.attach(this,&Beep::nobeep, time); // time to off |
dreschpe | 0:18e4a9c978ec | 62 | } |
dreschpe | 0:18e4a9c978ec | 63 | |
dreschpe | 0:18e4a9c978ec | 64 | |
jurica238814 | 5:49c961e79a12 | 65 | void Beep::playRttl(char *p){ |
jurica238814 | 5:49c961e79a12 | 66 | char default_dur = 4; |
jurica238814 | 5:49c961e79a12 | 67 | char default_oct = 6; |
jurica238814 | 5:49c961e79a12 | 68 | int bpm = 63; |
jurica238814 | 5:49c961e79a12 | 69 | int num; |
jurica238814 | 5:49c961e79a12 | 70 | long wholenote; |
jurica238814 | 5:49c961e79a12 | 71 | long duration; |
jurica238814 | 5:49c961e79a12 | 72 | char note; |
jurica238814 | 5:49c961e79a12 | 73 | char scale; |
jurica238814 | 5:49c961e79a12 | 74 | |
jurica238814 | 5:49c961e79a12 | 75 | // format: d=N,o=N,b=NNN: |
jurica238814 | 5:49c961e79a12 | 76 | // find the start (skip name, etc) |
jurica238814 | 5:49c961e79a12 | 77 | |
jurica238814 | 5:49c961e79a12 | 78 | while(*p != ':') p++; // ignore name |
jurica238814 | 5:49c961e79a12 | 79 | p++; // skip ':' |
jurica238814 | 5:49c961e79a12 | 80 | |
jurica238814 | 5:49c961e79a12 | 81 | // get default duration |
jurica238814 | 5:49c961e79a12 | 82 | if(*p == 'd') |
jurica238814 | 5:49c961e79a12 | 83 | { |
jurica238814 | 5:49c961e79a12 | 84 | p++; p++; // skip "d=" |
jurica238814 | 5:49c961e79a12 | 85 | num = 0; |
jurica238814 | 5:49c961e79a12 | 86 | while(isdigit(*p)) |
jurica238814 | 5:49c961e79a12 | 87 | { |
jurica238814 | 5:49c961e79a12 | 88 | num = (num * 10) + (*p++ - '0'); |
jurica238814 | 5:49c961e79a12 | 89 | } |
jurica238814 | 5:49c961e79a12 | 90 | if(num > 0) default_dur = num; |
jurica238814 | 5:49c961e79a12 | 91 | p++; // skip comma |
jurica238814 | 5:49c961e79a12 | 92 | } |
jurica238814 | 5:49c961e79a12 | 93 | |
jurica238814 | 5:49c961e79a12 | 94 | |
jurica238814 | 5:49c961e79a12 | 95 | // get default octave |
jurica238814 | 5:49c961e79a12 | 96 | if(*p == 'o') |
jurica238814 | 5:49c961e79a12 | 97 | { |
jurica238814 | 5:49c961e79a12 | 98 | p++; p++; // skip "o=" |
jurica238814 | 5:49c961e79a12 | 99 | num = *p++ - '0'; |
jurica238814 | 5:49c961e79a12 | 100 | if(num >= 3 && num <=7) default_oct = num; |
jurica238814 | 5:49c961e79a12 | 101 | p++; // skip comma |
jurica238814 | 5:49c961e79a12 | 102 | } |
jurica238814 | 5:49c961e79a12 | 103 | |
jurica238814 | 5:49c961e79a12 | 104 | |
jurica238814 | 5:49c961e79a12 | 105 | // get BPM |
jurica238814 | 5:49c961e79a12 | 106 | if(*p == 'b') |
jurica238814 | 5:49c961e79a12 | 107 | { |
jurica238814 | 5:49c961e79a12 | 108 | p++; p++; // skip "b=" |
jurica238814 | 5:49c961e79a12 | 109 | num = 0; |
jurica238814 | 5:49c961e79a12 | 110 | while(isdigit(*p)) |
jurica238814 | 5:49c961e79a12 | 111 | { |
jurica238814 | 5:49c961e79a12 | 112 | num = (num * 10) + (*p++ - '0'); |
jurica238814 | 5:49c961e79a12 | 113 | } |
jurica238814 | 5:49c961e79a12 | 114 | bpm = num; |
jurica238814 | 5:49c961e79a12 | 115 | p++; // skip colon |
jurica238814 | 5:49c961e79a12 | 116 | } |
jurica238814 | 5:49c961e79a12 | 117 | |
jurica238814 | 5:49c961e79a12 | 118 | |
jurica238814 | 5:49c961e79a12 | 119 | // BPM usually expresses the number of quarter notes per minute |
jurica238814 | 5:49c961e79a12 | 120 | wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in seconds) |
jurica238814 | 5:49c961e79a12 | 121 | |
dreschpe | 0:18e4a9c978ec | 122 | |
dreschpe | 0:18e4a9c978ec | 123 | |
jurica238814 | 5:49c961e79a12 | 124 | // now begin note loop |
jurica238814 | 5:49c961e79a12 | 125 | while(*p) |
jurica238814 | 5:49c961e79a12 | 126 | { |
jurica238814 | 5:49c961e79a12 | 127 | // first, get note duration, if available |
jurica238814 | 5:49c961e79a12 | 128 | num = 0; |
jurica238814 | 5:49c961e79a12 | 129 | while(isdigit(*p)) |
jurica238814 | 5:49c961e79a12 | 130 | { |
jurica238814 | 5:49c961e79a12 | 131 | num = (num * 10) + (*p++ - '0'); |
jurica238814 | 5:49c961e79a12 | 132 | } |
jurica238814 | 5:49c961e79a12 | 133 | |
jurica238814 | 5:49c961e79a12 | 134 | if(num) duration = wholenote / num; |
jurica238814 | 5:49c961e79a12 | 135 | else duration = wholenote / default_dur; // we will need to check if we are a dotted note after |
jurica238814 | 5:49c961e79a12 | 136 | |
jurica238814 | 5:49c961e79a12 | 137 | // now get the note |
jurica238814 | 5:49c961e79a12 | 138 | note = 0; |
jurica238814 | 5:49c961e79a12 | 139 | |
jurica238814 | 5:49c961e79a12 | 140 | switch(*p) |
jurica238814 | 5:49c961e79a12 | 141 | { |
jurica238814 | 5:49c961e79a12 | 142 | case 'c': |
jurica238814 | 5:49c961e79a12 | 143 | note = 1; |
jurica238814 | 5:49c961e79a12 | 144 | break; |
jurica238814 | 5:49c961e79a12 | 145 | case 'd': |
jurica238814 | 5:49c961e79a12 | 146 | note = 3; |
jurica238814 | 5:49c961e79a12 | 147 | break; |
jurica238814 | 5:49c961e79a12 | 148 | case 'e': |
jurica238814 | 5:49c961e79a12 | 149 | note = 5; |
jurica238814 | 5:49c961e79a12 | 150 | break; |
jurica238814 | 5:49c961e79a12 | 151 | case 'f': |
jurica238814 | 5:49c961e79a12 | 152 | note = 6; |
jurica238814 | 5:49c961e79a12 | 153 | break; |
jurica238814 | 5:49c961e79a12 | 154 | case 'g': |
jurica238814 | 5:49c961e79a12 | 155 | note = 8; |
jurica238814 | 5:49c961e79a12 | 156 | break; |
jurica238814 | 5:49c961e79a12 | 157 | case 'a': |
jurica238814 | 5:49c961e79a12 | 158 | note = 10; |
jurica238814 | 5:49c961e79a12 | 159 | break; |
jurica238814 | 5:49c961e79a12 | 160 | case 'b': |
jurica238814 | 5:49c961e79a12 | 161 | note = 12; |
jurica238814 | 5:49c961e79a12 | 162 | break; |
jurica238814 | 5:49c961e79a12 | 163 | case 'p': |
jurica238814 | 5:49c961e79a12 | 164 | default: |
jurica238814 | 5:49c961e79a12 | 165 | note = 0; |
jurica238814 | 5:49c961e79a12 | 166 | } |
jurica238814 | 5:49c961e79a12 | 167 | p++; |
jurica238814 | 5:49c961e79a12 | 168 | |
jurica238814 | 5:49c961e79a12 | 169 | // now, get optional '#' sharp |
jurica238814 | 5:49c961e79a12 | 170 | if(*p == '#') |
jurica238814 | 5:49c961e79a12 | 171 | { |
jurica238814 | 5:49c961e79a12 | 172 | note++; |
jurica238814 | 5:49c961e79a12 | 173 | p++; |
jurica238814 | 5:49c961e79a12 | 174 | } |
jurica238814 | 5:49c961e79a12 | 175 | |
jurica238814 | 5:49c961e79a12 | 176 | // now, get optional '.' dotted note |
jurica238814 | 5:49c961e79a12 | 177 | if(*p == '.') |
jurica238814 | 5:49c961e79a12 | 178 | { |
jurica238814 | 5:49c961e79a12 | 179 | duration += duration/2; |
jurica238814 | 5:49c961e79a12 | 180 | p++; |
jurica238814 | 5:49c961e79a12 | 181 | } |
jurica238814 | 5:49c961e79a12 | 182 | |
jurica238814 | 5:49c961e79a12 | 183 | // now, get scale |
jurica238814 | 5:49c961e79a12 | 184 | if(isdigit(*p)) |
jurica238814 | 5:49c961e79a12 | 185 | { |
jurica238814 | 5:49c961e79a12 | 186 | scale = *p - '0'; |
jurica238814 | 5:49c961e79a12 | 187 | p++; |
jurica238814 | 5:49c961e79a12 | 188 | } |
jurica238814 | 5:49c961e79a12 | 189 | else |
jurica238814 | 5:49c961e79a12 | 190 | { |
jurica238814 | 5:49c961e79a12 | 191 | scale = default_oct; |
jurica238814 | 5:49c961e79a12 | 192 | } |
jurica238814 | 5:49c961e79a12 | 193 | |
jurica238814 | 5:49c961e79a12 | 194 | scale += OCTAVE_OFFSET; |
jurica238814 | 5:49c961e79a12 | 195 | |
jurica238814 | 5:49c961e79a12 | 196 | if(*p == ',') |
jurica238814 | 5:49c961e79a12 | 197 | p++; // skip comma for next note (or we may be at the end) |
jurica238814 | 5:49c961e79a12 | 198 | |
jurica238814 | 5:49c961e79a12 | 199 | // now play the note |
jurica238814 | 5:49c961e79a12 | 200 | |
jurica238814 | 5:49c961e79a12 | 201 | if(note) |
jurica238814 | 5:49c961e79a12 | 202 | { |
jurica238814 | 5:49c961e79a12 | 203 | beep(notes[(scale - 4) * 12 + note], (float)duration/1000); |
jurica238814 | 5:49c961e79a12 | 204 | wait((float)duration/1000); |
jurica238814 | 5:49c961e79a12 | 205 | } |
jurica238814 | 5:49c961e79a12 | 206 | else |
jurica238814 | 5:49c961e79a12 | 207 | { |
jurica238814 | 5:49c961e79a12 | 208 | //wait((float)duration/1000); |
jurica238814 | 5:49c961e79a12 | 209 | } |
jurica238814 | 6:936ba3699e47 | 210 | |
jurica238814 | 6:936ba3699e47 | 211 | /* |
jurica238814 | 6:936ba3699e47 | 212 | * SEE IF THERE'S INTERRUPT OCCURED |
jurica238814 | 6:936ba3699e47 | 213 | */ |
jurica238814 | 6:936ba3699e47 | 214 | if (getInt()){ |
jurica238814 | 6:936ba3699e47 | 215 | nobeep(); |
jurica238814 | 6:936ba3699e47 | 216 | return; |
jurica238814 | 6:936ba3699e47 | 217 | } |
jurica238814 | 5:49c961e79a12 | 218 | } |
jurica238814 | 5:49c961e79a12 | 219 | } |
jurica238814 | 5:49c961e79a12 | 220 |