sqefqsdf
Dependencies: C12832 EthernetInterface LM75B mbed-rtos mbed
Fork of app-board-LM75B by
Speaker.cpp@6:77a4c45f6416, 2017-03-23 (annotated)
- Committer:
- gimohd
- Date:
- Thu Mar 23 12:51:27 2017 +0000
- Revision:
- 6:77a4c45f6416
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gimohd | 6:77a4c45f6416 | 1 | #include "Speaker.h" |
gimohd | 6:77a4c45f6416 | 2 | |
gimohd | 6:77a4c45f6416 | 3 | |
gimohd | 6:77a4c45f6416 | 4 | float Speaker::soundValues[17][3] = { |
gimohd | 6:77a4c45f6416 | 5 | {110, 480, 100} ,//1 |
gimohd | 6:77a4c45f6416 | 6 | {110, 480, 100} ,//2 |
gimohd | 6:77a4c45f6416 | 7 | {110, 480, 100} ,//3 |
gimohd | 6:77a4c45f6416 | 8 | {98, 360, 75} ,//4 |
gimohd | 6:77a4c45f6416 | 9 | {130, 120, 100} ,//5 |
gimohd | 6:77a4c45f6416 | 10 | {110, 480, 100} ,//6 |
gimohd | 6:77a4c45f6416 | 11 | {98, 360, 75} ,//7 |
gimohd | 6:77a4c45f6416 | 12 | {130, 120, 100} ,//8 |
gimohd | 6:77a4c45f6416 | 13 | {110, 960, 100} ,//9 |
gimohd | 6:77a4c45f6416 | 14 | {165, 480, 100} ,//10 |
gimohd | 6:77a4c45f6416 | 15 | {165, 480, 100} ,//11 |
gimohd | 6:77a4c45f6416 | 16 | {165, 480, 100} ,//12 |
gimohd | 6:77a4c45f6416 | 17 | {175, 360, 75} ,//13 |
gimohd | 6:77a4c45f6416 | 18 | {130, 120, 100} ,//14 |
gimohd | 6:77a4c45f6416 | 19 | {104, 480, 100} ,//15 |
gimohd | 6:77a4c45f6416 | 20 | {98, 360, 75} ,//16 |
gimohd | 6:77a4c45f6416 | 21 | {130, 120, 100} //17 |
gimohd | 6:77a4c45f6416 | 22 | }; |
gimohd | 6:77a4c45f6416 | 23 | |
gimohd | 6:77a4c45f6416 | 24 | Speaker::Speaker(PinName pin) :PwmOut(pin) |
gimohd | 6:77a4c45f6416 | 25 | { |
gimohd | 6:77a4c45f6416 | 26 | |
gimohd | 6:77a4c45f6416 | 27 | } |
gimohd | 6:77a4c45f6416 | 28 | |
gimohd | 6:77a4c45f6416 | 29 | Speaker::~Speaker() |
gimohd | 6:77a4c45f6416 | 30 | { |
gimohd | 6:77a4c45f6416 | 31 | delete this; |
gimohd | 6:77a4c45f6416 | 32 | } |
gimohd | 6:77a4c45f6416 | 33 | |
gimohd | 6:77a4c45f6416 | 34 | void Speaker::play(int number) |
gimohd | 6:77a4c45f6416 | 35 | { |
gimohd | 6:77a4c45f6416 | 36 | float freq = 0; |
gimohd | 6:77a4c45f6416 | 37 | float duration = 0; |
gimohd | 6:77a4c45f6416 | 38 | float delay = 0; |
gimohd | 6:77a4c45f6416 | 39 | for (int i = 1; i < number; i++) { |
gimohd | 6:77a4c45f6416 | 40 | freq = soundValues[i-1][0]; |
gimohd | 6:77a4c45f6416 | 41 | duration = soundValues[i-1][1]; |
gimohd | 6:77a4c45f6416 | 42 | delay = soundValues[i-1][2]; |
gimohd | 6:77a4c45f6416 | 43 | playSound(freq ,duration, delay); |
gimohd | 6:77a4c45f6416 | 44 | } |
gimohd | 6:77a4c45f6416 | 45 | } |
gimohd | 6:77a4c45f6416 | 46 | |
gimohd | 6:77a4c45f6416 | 47 | void Speaker::playSound(float frequency, float duration, float delay) |
gimohd | 6:77a4c45f6416 | 48 | { |
gimohd | 6:77a4c45f6416 | 49 | this->period(1.0/frequency); |
gimohd | 6:77a4c45f6416 | 50 | this->write(0.5); |
gimohd | 6:77a4c45f6416 | 51 | wait_ms(duration); |
gimohd | 6:77a4c45f6416 | 52 | this->write(0.0); |
gimohd | 6:77a4c45f6416 | 53 | this->period(0.0005); |
gimohd | 6:77a4c45f6416 | 54 | wait_ms(delay); |
gimohd | 6:77a4c45f6416 | 55 | } |