Li Ruofan 201199450
Dependencies: mbed Gamepad Joystick
bgm/bgm.cpp@8:b4a2954dd74f, 2020-05-16 (annotated)
- Committer:
- DannyLee
- Date:
- Sat May 16 17:26:10 2020 +0000
- Revision:
- 8:b4a2954dd74f
- Parent:
- 6:cbd9e1f26a10
Debugging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DannyLee | 3:cf9fead9c3f4 | 1 | #include "bgm.h" |
DannyLee | 3:cf9fead9c3f4 | 2 | |
DannyLee | 3:cf9fead9c3f4 | 3 | // construct & destruct/ |
DannyLee | 8:b4a2954dd74f | 4 | Bgm::Bgm() |
DannyLee | 6:cbd9e1f26a10 | 5 | : |
DannyLee | 6:cbd9e1f26a10 | 6 | _timeout(new Timeout()), |
DannyLee | 6:cbd9e1f26a10 | 7 | _buzzer(new PwmOut(PTC10)) |
DannyLee | 6:cbd9e1f26a10 | 8 | {} |
DannyLee | 6:cbd9e1f26a10 | 9 | |
DannyLee | 8:b4a2954dd74f | 10 | Bgm::~Bgm() |
DannyLee | 3:cf9fead9c3f4 | 11 | { |
DannyLee | 6:cbd9e1f26a10 | 12 | |
DannyLee | 3:cf9fead9c3f4 | 13 | } |
DannyLee | 3:cf9fead9c3f4 | 14 | |
DannyLee | 8:b4a2954dd74f | 15 | void Bgm::tone(float frequency, float duration) |
DannyLee | 6:cbd9e1f26a10 | 16 | { |
DannyLee | 6:cbd9e1f26a10 | 17 | _buzzer->period(1.0f/frequency); |
DannyLee | 6:cbd9e1f26a10 | 18 | _buzzer->write(0.5); // 50% duty cycle - square wave |
DannyLee | 8:b4a2954dd74f | 19 | _timeout->attach(callback(this, &Bgm::tone_off), duration ); |
DannyLee | 6:cbd9e1f26a10 | 20 | } |
DannyLee | 3:cf9fead9c3f4 | 21 | // The number "1,2,3,4,5,6,7" means each number note in the notation |
DannyLee | 3:cf9fead9c3f4 | 22 | |
DannyLee | 3:cf9fead9c3f4 | 23 | // Excerpted from famous Chinese and English songs |
DannyLee | 8:b4a2954dd74f | 24 | void Bgm::welcome(){ |
DannyLee | 3:cf9fead9c3f4 | 25 | tone(784.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 26 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 27 | tone(784.0f,0.1f); |
DannyLee | 3:cf9fead9c3f4 | 28 | wait(0.1f); |
DannyLee | 3:cf9fead9c3f4 | 29 | tone(880.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 30 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 31 | tone(587.0f,0.8f); |
DannyLee | 3:cf9fead9c3f4 | 32 | wait(0.8f); |
DannyLee | 3:cf9fead9c3f4 | 33 | tone(523.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 34 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 35 | tone(523.0f,0.1f); |
DannyLee | 3:cf9fead9c3f4 | 36 | wait(0.1f); |
DannyLee | 3:cf9fead9c3f4 | 37 | tone(440.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 38 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 39 | tone(587.0f,0.8f); |
DannyLee | 3:cf9fead9c3f4 | 40 | wait(0.8f); |
DannyLee | 3:cf9fead9c3f4 | 41 | tone(784.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 42 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 43 | tone(784.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 44 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 45 | tone(880.0f,0.1f); |
DannyLee | 3:cf9fead9c3f4 | 46 | wait(0.1f); |
DannyLee | 3:cf9fead9c3f4 | 47 | tone(1046.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 48 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 49 | tone(880.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 50 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 51 | tone(784.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 52 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 53 | tone(523.0f,0.4f); |
DannyLee | 3:cf9fead9c3f4 | 54 | wait(0.4f); |
DannyLee | 3:cf9fead9c3f4 | 55 | tone(523.0f,0.1f); |
DannyLee | 3:cf9fead9c3f4 | 56 | wait(0.1f); |
DannyLee | 3:cf9fead9c3f4 | 57 | tone(440.0f,0.1f); |
DannyLee | 3:cf9fead9c3f4 | 58 | wait(0.1f); |
DannyLee | 3:cf9fead9c3f4 | 59 | tone(587.0f,0.8f); |
DannyLee | 3:cf9fead9c3f4 | 60 | wait(0.8f); |
DannyLee | 3:cf9fead9c3f4 | 61 | |
DannyLee | 3:cf9fead9c3f4 | 62 | } |
DannyLee | 3:cf9fead9c3f4 | 63 | |
DannyLee | 8:b4a2954dd74f | 64 | void Bgm::battlefield(){ |
DannyLee | 3:cf9fead9c3f4 | 65 | tone(659.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 66 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 67 | tone(659.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 68 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 69 | tone(587.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 70 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 71 | tone(523.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 72 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 73 | tone(523.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 74 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 75 | tone(659.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 76 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 77 | tone(784.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 78 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 79 | tone(1046.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 80 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 81 | tone(1046.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 82 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 83 | tone(1046.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 84 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 85 | tone(880.0f,0.5f); |
DannyLee | 3:cf9fead9c3f4 | 86 | wait(0.5f); |
DannyLee | 3:cf9fead9c3f4 | 87 | tone(784.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 88 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 89 | tone(784.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 90 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 91 | tone(880.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 92 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 93 | tone(784.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 94 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 95 | tone(659.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 96 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 97 | tone(523.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 98 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 99 | tone(587.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 100 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 101 | tone(587.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 102 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 103 | tone(659.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 104 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 105 | tone(587.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 106 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 107 | |
DannyLee | 3:cf9fead9c3f4 | 108 | } |
DannyLee | 3:cf9fead9c3f4 | 109 | |
DannyLee | 8:b4a2954dd74f | 110 | void Bgm::died(){ |
DannyLee | 3:cf9fead9c3f4 | 111 | tone(784.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 112 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 113 | tone(880.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 114 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 115 | tone(784.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 116 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 117 | tone(698.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 118 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 119 | tone(659.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 120 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 121 | tone(698.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 122 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 123 | tone(784.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 124 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 125 | tone(587.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 126 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 127 | tone(784.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 128 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 129 | tone(784.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 130 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 131 | tone(659.0f,0.2f); |
DannyLee | 3:cf9fead9c3f4 | 132 | wait(0.2f); |
DannyLee | 3:cf9fead9c3f4 | 133 | tone(523.0f,0.3f); |
DannyLee | 3:cf9fead9c3f4 | 134 | wait(0.3f); |
DannyLee | 3:cf9fead9c3f4 | 135 | |
DannyLee | 3:cf9fead9c3f4 | 136 | } |
DannyLee | 3:cf9fead9c3f4 | 137 | |
DannyLee | 8:b4a2954dd74f | 138 | void Bgm::tone_off() |
DannyLee | 3:cf9fead9c3f4 | 139 | { |
DannyLee | 3:cf9fead9c3f4 | 140 | // called after timeout |
DannyLee | 3:cf9fead9c3f4 | 141 | _buzzer->write(0.0); |
DannyLee | 3:cf9fead9c3f4 | 142 | } |