Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: kiokuryoku_game escapeFromYou junirobo_sample001 ROBOX_Sample_IRcon ... more
beep_sound.cpp@0:c9832831c73f, 2016-06-04 (annotated)
- Committer:
- RBH
- Date:
- Sat Jun 04 12:45:10 2016 +0000
- Revision:
- 0:c9832831c73f
- Child:
- 2:259899b723ab
First version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| RBH | 0:c9832831c73f | 1 | #include "beep_sound.h" |
| RBH | 0:c9832831c73f | 2 | #include "math.h" |
| RBH | 0:c9832831c73f | 3 | beep_sound::beep_sound(PinName pwm) |
| RBH | 0:c9832831c73f | 4 | { |
| RBH | 0:c9832831c73f | 5 | m_pwm = new PwmOut(pwm); |
| RBH | 0:c9832831c73f | 6 | m_pwm->write(0.0f); |
| RBH | 0:c9832831c73f | 7 | } |
| RBH | 0:c9832831c73f | 8 | |
| RBH | 0:c9832831c73f | 9 | beep_sound::~beep_sound() |
| RBH | 0:c9832831c73f | 10 | { |
| RBH | 0:c9832831c73f | 11 | delete m_pwm; |
| RBH | 0:c9832831c73f | 12 | } |
| RBH | 0:c9832831c73f | 13 | |
| RBH | 0:c9832831c73f | 14 | void beep_sound::SetFrequency(int octave, int note, bool on_off) |
| RBH | 0:c9832831c73f | 15 | { |
| RBH | 0:c9832831c73f | 16 | static int preFrequency = 0; |
| RBH | 0:c9832831c73f | 17 | if(on_off == true){ |
| RBH | 0:c9832831c73f | 18 | int frequency = frequencyTable[note] << octave; |
| RBH | 0:c9832831c73f | 19 | if(frequency != preFrequency){ |
| RBH | 0:c9832831c73f | 20 | m_pwm->period(1.0f/(float)frequency); |
| RBH | 0:c9832831c73f | 21 | m_pwm->write(0.5f); |
| RBH | 0:c9832831c73f | 22 | } |
| RBH | 0:c9832831c73f | 23 | preFrequency = frequency; |
| RBH | 0:c9832831c73f | 24 | }else{ |
| RBH | 0:c9832831c73f | 25 | m_pwm->write(0.0f); |
| RBH | 0:c9832831c73f | 26 | preFrequency = 0; |
| RBH | 0:c9832831c73f | 27 | } |
| RBH | 0:c9832831c73f | 28 | } |
| RBH | 0:c9832831c73f | 29 | |
| RBH | 0:c9832831c73f | 30 | void beep_sound::onpu(int octave, int note, float time_s) |
| RBH | 0:c9832831c73f | 31 | { |
| RBH | 0:c9832831c73f | 32 | beep_sound::SetFrequency(octave, note, true); |
| RBH | 0:c9832831c73f | 33 | wait(time_s); |
| RBH | 0:c9832831c73f | 34 | beep_sound::SetFrequency(octave, note, false); |
| RBH | 0:c9832831c73f | 35 | /* |
| RBH | 0:c9832831c73f | 36 | if(octave >= 10){ |
| RBH | 0:c9832831c73f | 37 | octave -= 10; |
| RBH | 0:c9832831c73f | 38 | beep_sound::SetFrequency(octave, note, true); |
| RBH | 0:c9832831c73f | 39 | wait(time_s); |
| RBH | 0:c9832831c73f | 40 | beep_sound::SetFrequency(octave, note, false); |
| RBH | 0:c9832831c73f | 41 | }else{ |
| RBH | 0:c9832831c73f | 42 | beep_sound::SetFrequency(octave, note, true); |
| RBH | 0:c9832831c73f | 43 | } |
| RBH | 0:c9832831c73f | 44 | */ |
| RBH | 0:c9832831c73f | 45 | } |
| RBH | 0:c9832831c73f | 46 | void beep_sound::sinwave(float center,int speed,int width) |
| RBH | 0:c9832831c73f | 47 | { |
| RBH | 0:c9832831c73f | 48 | for(float i=0.0;i<6.28;i+=0.01*speed) |
| RBH | 0:c9832831c73f | 49 | { |
| RBH | 0:c9832831c73f | 50 | m_pwm->period(1.0f/(center+width*(float)sin(i))); |
| RBH | 0:c9832831c73f | 51 | m_pwm->write(0.5f); |
| RBH | 0:c9832831c73f | 52 | wait_ms(2); |
| RBH | 0:c9832831c73f | 53 | } |
| RBH | 0:c9832831c73f | 54 | } |
| RBH | 0:c9832831c73f | 55 | void beep_sound::setwave(float frequency,float duty,float wait) |
| RBH | 0:c9832831c73f | 56 | { |
| RBH | 0:c9832831c73f | 57 | m_pwm->period(1.0f/(frequency)); |
| RBH | 0:c9832831c73f | 58 | m_pwm->write(duty); |
| RBH | 0:c9832831c73f | 59 | wait_ms(wait); |
| RBH | 0:c9832831c73f | 60 | } |
| RBH | 0:c9832831c73f | 61 | //引数の回数だけ鳴らす |
| RBH | 0:c9832831c73f | 62 | void beep_sound::beep_right(int nTimes) |
| RBH | 0:c9832831c73f | 63 | { |
| RBH | 0:c9832831c73f | 64 | for(int i=0; i<nTimes; i++){ |
| RBH | 0:c9832831c73f | 65 | beep_sound::onpu(3,11,0.1); |
| RBH | 0:c9832831c73f | 66 | beep_sound::onpu(3,7,0.35); |
| RBH | 0:c9832831c73f | 67 | wait(0.1); |
| RBH | 0:c9832831c73f | 68 | } |
| RBH | 0:c9832831c73f | 69 | } |
| RBH | 0:c9832831c73f | 70 | void beep_sound::beep_wrong(int nTimes) |
| RBH | 0:c9832831c73f | 71 | { |
| RBH | 0:c9832831c73f | 72 | for (int i = 0; i < nTimes; i++) { |
| RBH | 0:c9832831c73f | 73 | m_pwm->period(0.01f); |
| RBH | 0:c9832831c73f | 74 | m_pwm->write(0.5f); |
| RBH | 0:c9832831c73f | 75 | wait(0.1); |
| RBH | 0:c9832831c73f | 76 | m_pwm->write(0.0f); |
| RBH | 0:c9832831c73f | 77 | wait(0.1); |
| RBH | 0:c9832831c73f | 78 | m_pwm->period(0.01f); |
| RBH | 0:c9832831c73f | 79 | m_pwm->write(0.5f); |
| RBH | 0:c9832831c73f | 80 | wait(0.35); |
| RBH | 0:c9832831c73f | 81 | m_pwm->write(0.0f); |
| RBH | 0:c9832831c73f | 82 | wait(0.1); |
| RBH | 0:c9832831c73f | 83 | } |
| RBH | 0:c9832831c73f | 84 | } |
| RBH | 0:c9832831c73f | 85 | void beep_sound::beep_1up(int nTimes) |
| RBH | 0:c9832831c73f | 86 | { |
| RBH | 0:c9832831c73f | 87 | for (int i = 0; i < nTimes; i++) { |
| RBH | 0:c9832831c73f | 88 | beep_sound::onpu(3,4,0.1); |
| RBH | 0:c9832831c73f | 89 | beep_sound::onpu(3,7,0.1); |
| RBH | 0:c9832831c73f | 90 | beep_sound::onpu(4,4,0.1); |
| RBH | 0:c9832831c73f | 91 | beep_sound::onpu(4,0,0.1); |
| RBH | 0:c9832831c73f | 92 | beep_sound::onpu(4,2,0.1); |
| RBH | 0:c9832831c73f | 93 | beep_sound::onpu(4,7,0.1); |
| RBH | 0:c9832831c73f | 94 | wait(0.1); |
| RBH | 0:c9832831c73f | 95 | } |
| RBH | 0:c9832831c73f | 96 | } |
| RBH | 0:c9832831c73f | 97 | void beep_sound::beep_konbini(int nTimes) |
| RBH | 0:c9832831c73f | 98 | { |
| RBH | 0:c9832831c73f | 99 | for (int i = 0; i < nTimes; i++) { |
| RBH | 0:c9832831c73f | 100 | beep_sound::onpu(2,6,0.2); |
| RBH | 0:c9832831c73f | 101 | beep_sound::onpu(2,2,0.2); |
| RBH | 0:c9832831c73f | 102 | beep_sound::onpu(1,9,0.2); |
| RBH | 0:c9832831c73f | 103 | beep_sound::onpu(2,2,0.2); |
| RBH | 0:c9832831c73f | 104 | beep_sound::onpu(2,4,0.2); |
| RBH | 0:c9832831c73f | 105 | beep_sound::onpu(2,9,0.4); |
| RBH | 0:c9832831c73f | 106 | beep_sound::onpu(1,9,0.2); |
| RBH | 0:c9832831c73f | 107 | beep_sound::onpu(2,4,0.2); |
| RBH | 0:c9832831c73f | 108 | beep_sound::onpu(2,6,0.2); |
| RBH | 0:c9832831c73f | 109 | beep_sound::onpu(2,4,0.2); |
| RBH | 0:c9832831c73f | 110 | beep_sound::onpu(1,9,0.2); |
| RBH | 0:c9832831c73f | 111 | beep_sound::onpu(2,2,0.6); |
| RBH | 0:c9832831c73f | 112 | wait(0.1); |
| RBH | 0:c9832831c73f | 113 | } |
| RBH | 0:c9832831c73f | 114 | } |
| RBH | 0:c9832831c73f | 115 | void beep_sound::beep_encount(int nTimes) |
| RBH | 0:c9832831c73f | 116 | { |
| RBH | 0:c9832831c73f | 117 | for (int i = 0; i < nTimes; i++) { |
| RBH | 0:c9832831c73f | 118 | beep_sound::onpu(4,0,0.08); |
| RBH | 0:c9832831c73f | 119 | beep_sound::onpu(3,11,0.08); |
| RBH | 0:c9832831c73f | 120 | beep_sound::onpu(3,10,0.08); |
| RBH | 0:c9832831c73f | 121 | beep_sound::onpu(3,9,0.08); |
| RBH | 0:c9832831c73f | 122 | |
| RBH | 0:c9832831c73f | 123 | beep_sound::onpu(3,10,0.08); |
| RBH | 0:c9832831c73f | 124 | beep_sound::onpu(3,9,0.08); |
| RBH | 0:c9832831c73f | 125 | beep_sound::onpu(3,8,0.08); |
| RBH | 0:c9832831c73f | 126 | beep_sound::onpu(3,7,0.08); |
| RBH | 0:c9832831c73f | 127 | |
| RBH | 0:c9832831c73f | 128 | beep_sound::onpu(3,8,0.08); |
| RBH | 0:c9832831c73f | 129 | beep_sound::onpu(3,7,0.08); |
| RBH | 0:c9832831c73f | 130 | beep_sound::onpu(3,6,0.08); |
| RBH | 0:c9832831c73f | 131 | beep_sound::onpu(3,5,0.08); |
| RBH | 0:c9832831c73f | 132 | |
| RBH | 0:c9832831c73f | 133 | beep_sound::onpu(3,6,0.08); |
| RBH | 0:c9832831c73f | 134 | beep_sound::onpu(3,5,0.08); |
| RBH | 0:c9832831c73f | 135 | beep_sound::onpu(3,4,0.08); |
| RBH | 0:c9832831c73f | 136 | beep_sound::onpu(3,3,0.08); |
| RBH | 0:c9832831c73f | 137 | |
| RBH | 0:c9832831c73f | 138 | beep_sound::onpu(3,4,0.08); |
| RBH | 0:c9832831c73f | 139 | beep_sound::onpu(3,3,0.08); |
| RBH | 0:c9832831c73f | 140 | beep_sound::onpu(3,2,0.08); |
| RBH | 0:c9832831c73f | 141 | beep_sound::onpu(3,1,0.08); |
| RBH | 0:c9832831c73f | 142 | |
| RBH | 0:c9832831c73f | 143 | beep_sound::onpu(3,2,0.08); |
| RBH | 0:c9832831c73f | 144 | beep_sound::onpu(3,1,0.08); |
| RBH | 0:c9832831c73f | 145 | beep_sound::onpu(3,0,0.08); |
| RBH | 0:c9832831c73f | 146 | beep_sound::onpu(2,11,0.08); |
| RBH | 0:c9832831c73f | 147 | |
| RBH | 0:c9832831c73f | 148 | beep_sound::onpu(3,0,0.08); |
| RBH | 0:c9832831c73f | 149 | beep_sound::onpu(2,11,0.08); |
| RBH | 0:c9832831c73f | 150 | beep_sound::onpu(2,10,0.08); |
| RBH | 0:c9832831c73f | 151 | beep_sound::onpu(2,9,0.08); |
| RBH | 0:c9832831c73f | 152 | |
| RBH | 0:c9832831c73f | 153 | beep_sound::onpu(2,10,0.08); |
| RBH | 0:c9832831c73f | 154 | beep_sound::onpu(2,9,0.08); |
| RBH | 0:c9832831c73f | 155 | beep_sound::onpu(2,8,0.08); |
| RBH | 0:c9832831c73f | 156 | beep_sound::onpu(2,7,0.08); |
| RBH | 0:c9832831c73f | 157 | } |
| RBH | 0:c9832831c73f | 158 | wait(0.1); |
| RBH | 0:c9832831c73f | 159 | } |