VFD modular clock firmware

Dependencies:   DipCortex-EEprom RTC flw mbed

Committer:
Backstr?m
Date:
Tue Feb 24 23:01:40 2015 +0900
Revision:
12:dfb422107412
Parent:
0:f6e68b4ce169
Added tag v1.0.2 for changeset 34b344fdec98

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Backstrom 0:f6e68b4ce169 1 #include "beep.h"
Backstrom 0:f6e68b4ce169 2 #include "mbed.h"
Backstrom 0:f6e68b4ce169 3
Backstrom 0:f6e68b4ce169 4 /** class to make sound with a buzzer, based on a PwmOut
Backstrom 0:f6e68b4ce169 5 * The class use a timeout to switch off the sound - it is not blocking while making noise
Backstrom 0:f6e68b4ce169 6 *
Backstrom 0:f6e68b4ce169 7 * Example:
Backstrom 0:f6e68b4ce169 8 * @code
Backstrom 0:f6e68b4ce169 9 * // Beep with 1Khz for 0.5 seconds
Backstrom 0:f6e68b4ce169 10 * #include 'mbed.h'
Backstrom 0:f6e68b4ce169 11 * #include 'beep.h'
Backstrom 0:f6e68b4ce169 12 *
Backstrom 0:f6e68b4ce169 13 * Beep buzzer(p21);
Backstrom 0:f6e68b4ce169 14 *
Backstrom 0:f6e68b4ce169 15 * int main() {
Backstrom 0:f6e68b4ce169 16 * ...
Backstrom 0:f6e68b4ce169 17 * buzzer.beep(1000,0.5);
Backstrom 0:f6e68b4ce169 18 * ...
Backstrom 0:f6e68b4ce169 19 * }
Backstrom 0:f6e68b4ce169 20 * @endcode
Backstrom 0:f6e68b4ce169 21 */
Backstrom 0:f6e68b4ce169 22
Backstrom 0:f6e68b4ce169 23 //using namespace mbed;
Backstrom 0:f6e68b4ce169 24 // constructor
Backstrom 0:f6e68b4ce169 25 /** Create a Beep object connected to the specified PwmOut pin
Backstrom 0:f6e68b4ce169 26 *
Backstrom 0:f6e68b4ce169 27 * @param pin PwmOut pin to connect to
Backstrom 0:f6e68b4ce169 28 */
Backstrom 0:f6e68b4ce169 29
Backstrom 0:f6e68b4ce169 30 Beep::Beep(PinName pin) : _pwm(pin) {
Backstrom 0:f6e68b4ce169 31 _pwm.write(0.0); // after creating it have to be off
Backstrom 0:f6e68b4ce169 32 }
Backstrom 0:f6e68b4ce169 33
Backstrom 0:f6e68b4ce169 34 /** stop the beep instantaneous
Backstrom 0:f6e68b4ce169 35 * usually not used
Backstrom 0:f6e68b4ce169 36 */
Backstrom 0:f6e68b4ce169 37 void Beep::nobeep() {
Backstrom 0:f6e68b4ce169 38 _pwm.write(0.0);
Backstrom 0:f6e68b4ce169 39 }
Backstrom 0:f6e68b4ce169 40
Backstrom 0:f6e68b4ce169 41 /** Beep with given frequency and duration.
Backstrom 0:f6e68b4ce169 42 *
Backstrom 0:f6e68b4ce169 43 * @param frequency - the frequency of the tone in Hz
Backstrom 0:f6e68b4ce169 44 * @param time - the duration of the tone in seconds
Backstrom 0:f6e68b4ce169 45 */
Backstrom 0:f6e68b4ce169 46
Backstrom 0:f6e68b4ce169 47 void Beep::beep(float freq, float time) {
Backstrom 0:f6e68b4ce169 48
Backstrom 0:f6e68b4ce169 49 _pwm.period(1.0/freq);
Backstrom 0:f6e68b4ce169 50 _pwm.write(0.5); // 50% duty cycle - beep on
Backstrom 0:f6e68b4ce169 51 toff.attach(this,&Beep::nobeep, time); // time to off
Backstrom 0:f6e68b4ce169 52 }
Backstrom 0:f6e68b4ce169 53
Backstrom 0:f6e68b4ce169 54 #define NOTE_GB 0
Backstrom 0:f6e68b4ce169 55 #define NOTE_EB 1
Backstrom 0:f6e68b4ce169 56 #define NOTE_FS 2
Backstrom 0:f6e68b4ce169 57
Backstrom 0:f6e68b4ce169 58 void Beep::play(char note) {
Backstrom 0:f6e68b4ce169 59 if (note=='a') {
Backstrom 0:f6e68b4ce169 60 beep(880,0.1);
Backstrom 0:f6e68b4ce169 61 }
Backstrom 0:f6e68b4ce169 62 if (note=='b') {
Backstrom 0:f6e68b4ce169 63 beep(987,0.1);
Backstrom 0:f6e68b4ce169 64 }
Backstrom 0:f6e68b4ce169 65 if (note=='c') {
Backstrom 0:f6e68b4ce169 66 beep(1024,0.1);
Backstrom 0:f6e68b4ce169 67 }
Backstrom 0:f6e68b4ce169 68 if (note=='d') {
Backstrom 0:f6e68b4ce169 69 beep(1175,0.1);
Backstrom 0:f6e68b4ce169 70 }
Backstrom 0:f6e68b4ce169 71 if (note=='e') {
Backstrom 0:f6e68b4ce169 72 beep(1319,0.1);
Backstrom 0:f6e68b4ce169 73 }
Backstrom 0:f6e68b4ce169 74 if (note=='f') {
Backstrom 0:f6e68b4ce169 75 beep(1397,0.1);
Backstrom 0:f6e68b4ce169 76 }
Backstrom 0:f6e68b4ce169 77 if (note=='g') {
Backstrom 0:f6e68b4ce169 78 beep(1568,0.1);
Backstrom 0:f6e68b4ce169 79 }
Backstrom 0:f6e68b4ce169 80
Backstrom 0:f6e68b4ce169 81 if (note==NOTE_GB) {
Backstrom 0:f6e68b4ce169 82 beep(830,0.1);
Backstrom 0:f6e68b4ce169 83 }
Backstrom 0:f6e68b4ce169 84 if (note==NOTE_EB) {
Backstrom 0:f6e68b4ce169 85 beep(659,0.1);
Backstrom 0:f6e68b4ce169 86 }
Backstrom 0:f6e68b4ce169 87 if (note==NOTE_FS) {
Backstrom 0:f6e68b4ce169 88 beep(1480,0.1);
Backstrom 0:f6e68b4ce169 89 }
Backstrom 0:f6e68b4ce169 90 if (note=='w') {
Backstrom 0:f6e68b4ce169 91 wait(0.05);
Backstrom 0:f6e68b4ce169 92 }
Backstrom 0:f6e68b4ce169 93 wait (0.2); //wait while the note plays.
Backstrom 0:f6e68b4ce169 94 }
Backstrom 0:f6e68b4ce169 95
Backstrom 0:f6e68b4ce169 96 void Beep::popcorn() {
Backstrom 0:f6e68b4ce169 97
Backstrom 0:f6e68b4ce169 98 play('e');
Backstrom 0:f6e68b4ce169 99 play('d');
Backstrom 0:f6e68b4ce169 100 play('e');
Backstrom 0:f6e68b4ce169 101 play('c');
Backstrom 0:f6e68b4ce169 102 play(NOTE_GB);
Backstrom 0:f6e68b4ce169 103 play('c');
Backstrom 0:f6e68b4ce169 104 play(NOTE_EB);
Backstrom 0:f6e68b4ce169 105 play('w');
Backstrom 0:f6e68b4ce169 106 play('e');
Backstrom 0:f6e68b4ce169 107 play('d');
Backstrom 0:f6e68b4ce169 108 play('e');
Backstrom 0:f6e68b4ce169 109 play('c');
Backstrom 0:f6e68b4ce169 110 play(NOTE_GB);
Backstrom 0:f6e68b4ce169 111 play('c');
Backstrom 0:f6e68b4ce169 112 play(NOTE_EB);
Backstrom 0:f6e68b4ce169 113 play('w');
Backstrom 0:f6e68b4ce169 114 play('e');
Backstrom 0:f6e68b4ce169 115 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 116 play('g');
Backstrom 0:f6e68b4ce169 117 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 118 play('g');
Backstrom 0:f6e68b4ce169 119 play('e');
Backstrom 0:f6e68b4ce169 120 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 121 play('e');
Backstrom 0:f6e68b4ce169 122 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 123 play('d');
Backstrom 0:f6e68b4ce169 124 play('e');
Backstrom 0:f6e68b4ce169 125 play('d');
Backstrom 0:f6e68b4ce169 126 play('e');
Backstrom 0:f6e68b4ce169 127 play('d');
Backstrom 0:f6e68b4ce169 128 play('e');
Backstrom 0:f6e68b4ce169 129 play('w');
Backstrom 0:f6e68b4ce169 130 play('e');
Backstrom 0:f6e68b4ce169 131 play('d');
Backstrom 0:f6e68b4ce169 132 play('e');
Backstrom 0:f6e68b4ce169 133 play('c');
Backstrom 0:f6e68b4ce169 134 play(NOTE_GB);
Backstrom 0:f6e68b4ce169 135 play('c');
Backstrom 0:f6e68b4ce169 136 play(NOTE_EB);
Backstrom 0:f6e68b4ce169 137 play('w');
Backstrom 0:f6e68b4ce169 138 play('e');
Backstrom 0:f6e68b4ce169 139 play('d');
Backstrom 0:f6e68b4ce169 140 play('e');
Backstrom 0:f6e68b4ce169 141 play('c');
Backstrom 0:f6e68b4ce169 142 play(NOTE_GB);
Backstrom 0:f6e68b4ce169 143 play('c');
Backstrom 0:f6e68b4ce169 144 play(NOTE_EB);
Backstrom 0:f6e68b4ce169 145 play('w');
Backstrom 0:f6e68b4ce169 146 play('e');
Backstrom 0:f6e68b4ce169 147 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 148 play('g');
Backstrom 0:f6e68b4ce169 149 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 150 play('g');
Backstrom 0:f6e68b4ce169 151 play('e');
Backstrom 0:f6e68b4ce169 152 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 153 play('e');
Backstrom 0:f6e68b4ce169 154 play(NOTE_FS);
Backstrom 0:f6e68b4ce169 155 play('d');
Backstrom 0:f6e68b4ce169 156 play('e');
Backstrom 0:f6e68b4ce169 157 play('d');
Backstrom 0:f6e68b4ce169 158 play('e');
Backstrom 0:f6e68b4ce169 159 play('d');
Backstrom 0:f6e68b4ce169 160 play('g');
Backstrom 0:f6e68b4ce169 161
Backstrom 0:f6e68b4ce169 162 }
Backstrom 0:f6e68b4ce169 163