テスト
Dependencies: C12832 USBHost mbed MMA7660 wave_player
Fork of app-board-Speaker by
main.cpp@19:8762c0a71816, 2016-12-14 (annotated)
- Committer:
- toyohara
- Date:
- Wed Dec 14 09:02:54 2016 +0000
- Revision:
- 19:8762c0a71816
- Parent:
- 18:3a7e3401ada3
cleaned
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
funa0825 | 3:b1115b34e95b | 1 | |
funa0825 | 3:b1115b34e95b | 2 | |
funa0825 | 3:b1115b34e95b | 3 | #define mC 261.626 |
funa0825 | 3:b1115b34e95b | 4 | #define mD 293.665 |
toyohara | 17:9e07a9ff6e91 | 5 | #define mEb 311.13 |
funa0825 | 3:b1115b34e95b | 6 | #define mE 329.628 |
funa0825 | 3:b1115b34e95b | 7 | #define mF 349.228 |
toyohara | 17:9e07a9ff6e91 | 8 | #define mGb 369.99 |
funa0825 | 3:b1115b34e95b | 9 | #define mG 391.995 |
toyohara | 17:9e07a9ff6e91 | 10 | #define mAb 415.3 |
funa0825 | 3:b1115b34e95b | 11 | #define mA 440.000 |
toyohara | 17:9e07a9ff6e91 | 12 | #define mBb 466.16 |
funa0825 | 3:b1115b34e95b | 13 | #define mB 493.883 |
funa0825 | 3:b1115b34e95b | 14 | #define THRESH 80 |
max_0630 | 5:04b0c694b5a1 | 15 | #define COUNT_MAX 15 |
toyohara | 6:82aa8f2c3c47 | 16 | #define DIM 3 |
toyohara | 15:46ca600425fc | 17 | #define NUM_OF_NOTES 21 |
toyohara | 17:9e07a9ff6e91 | 18 | #define NUM_OF_SCALE 5 |
toyohara | 15:46ca600425fc | 19 | |
funa0825 | 3:b1115b34e95b | 20 | #include "C12832.h" |
chris | 0:f86c572491c3 | 21 | #include "mbed.h" |
funa0825 | 3:b1115b34e95b | 22 | #include "rtos.h" |
toyohara | 6:82aa8f2c3c47 | 23 | #include "MMA7660.h" |
toyohara | 10:5fb0cef92e1b | 24 | #include "USBHostMSD.h" |
toyohara | 10:5fb0cef92e1b | 25 | #include "wave_player.h" |
toyohara | 17:9e07a9ff6e91 | 26 | #include <string> |
toyohara | 17:9e07a9ff6e91 | 27 | |
toyohara | 17:9e07a9ff6e91 | 28 | using namespace std; |
chris | 0:f86c572491c3 | 29 | |
funa0825 | 3:b1115b34e95b | 30 | C12832 lcd(p5, p7, p6, p8, p11); |
chris | 1:0a08d26b011a | 31 | DigitalIn fire(p14); |
funa0825 | 3:b1115b34e95b | 32 | //DigitalIn up(p15); |
funa0825 | 3:b1115b34e95b | 33 | //DigitalIn down(p12); |
funa0825 | 3:b1115b34e95b | 34 | PwmOut sp1(p26); |
funa0825 | 3:b1115b34e95b | 35 | InterruptIn myup(p15); |
funa0825 | 3:b1115b34e95b | 36 | InterruptIn mydown(p12); |
funa0825 | 3:b1115b34e95b | 37 | PwmOut led1(p23); |
funa0825 | 3:b1115b34e95b | 38 | PwmOut led2(p24); |
funa0825 | 3:b1115b34e95b | 39 | PwmOut led3(p25); |
funa0825 | 3:b1115b34e95b | 40 | Semaphore three_slots(1); |
funa0825 | 3:b1115b34e95b | 41 | Timer for_chat; |
toyohara | 15:46ca600425fc | 42 | |
chris | 0:f86c572491c3 | 43 | |
toyohara | 6:82aa8f2c3c47 | 44 | //for acceleration sonsor |
toyohara | 6:82aa8f2c3c47 | 45 | MMA7660 acc(p28, p27); |
toyohara | 6:82aa8f2c3c47 | 46 | float r[DIM]; |
toyohara | 6:82aa8f2c3c47 | 47 | |
toyohara | 10:5fb0cef92e1b | 48 | //for music |
toyohara | 10:5fb0cef92e1b | 49 | AnalogOut DACout (p18); |
toyohara | 12:c1148331b151 | 50 | wave_player waver(&DACout); |
toyohara | 15:46ca600425fc | 51 | AnalogIn volumeMeter(p19); |
toyohara | 15:46ca600425fc | 52 | AnalogIn musicSwitch(p20); |
toyohara | 15:46ca600425fc | 53 | InterruptIn push(p14); |
toyohara | 15:46ca600425fc | 54 | InterruptIn right(p16); |
toyohara | 15:46ca600425fc | 55 | InterruptIn left(p13); |
funa0825 | 16:12f30425ba88 | 56 | FILE *wave_file; |
funa0825 | 16:12f30425ba88 | 57 | Mutex stdio_mutex; |
toyohara | 10:5fb0cef92e1b | 58 | |
funa0825 | 3:b1115b34e95b | 59 | int i=0; |
max_0630 | 5:04b0c694b5a1 | 60 | int count = 0; |
toyohara | 15:46ca600425fc | 61 | bool pushed = false; |
toyohara | 15:46ca600425fc | 62 | int i_for20mm; |
toyohara | 15:46ca600425fc | 63 | int i_for20mm_old = 0; |
toyohara | 15:46ca600425fc | 64 | int scale = 0; |
funa0825 | 16:12f30425ba88 | 65 | bool printflag = false; |
max_0630 | 5:04b0c694b5a1 | 66 | |
funa0825 | 3:b1115b34e95b | 67 | float mm[]={mC,mD,mE,mF,mG,mA,mB,mC*2,mD*2,mE*2,mF*2,mG*2,mA*2,mB*2,mC*4}; |
max_0630 | 14:3b123bcdd79c | 68 | |
toyohara | 15:46ca600425fc | 69 | float mmm[]={mC,mD,mE,mF,mG,mA,mB,mC*2,mD*2,mE*2,mF*2,mG*2,mA*2,mB*2,mC*4, mD*4,mE*4,mF*4,mG*4,mA*4,mB*4};//has 21 |
toyohara | 17:9e07a9ff6e91 | 70 | float notes[NUM_OF_SCALE][NUM_OF_NOTES] = { {mC,mD,mE,mF,mG,mA,mB,mC*2,mD*2,mE*2,mF*2,mG*2,mA*2,mB*2,mC*4, mD*4,mE*4,mF*4,mG*4,mA*4,mB*4}, //major |
toyohara | 17:9e07a9ff6e91 | 71 | {mC,mD,mEb,mF,mG,mA,mBb,mC*2,mD*2,mEb*2,mF*2,mG*2,mA*2,mBb*2,mC*4, mD*4,mEb*4,mF*4,mG*4,mA*4,mBb*4}, //Dorian |
toyohara | 17:9e07a9ff6e91 | 72 | {mC,mD,mEb,mF,mG,mAb,mBb,mC*2,mD*2,mEb*2,mF*2,mG*2,mAb*2,mBb*2,mC*4, mD*4,mEb*4,mF*4,mG*4,mAb*4,mBb*4}, //natural minor |
toyohara | 17:9e07a9ff6e91 | 73 | {mC,mEb,mF,mGb,mG,mBb,mC*2,mEb*2,mF*2,mGb*2,mG*2,mBb*2,mC*4,mEb*4,mF*4,mGb*4,mG*4,mBb*4,mC*8,mEb*8,mF*8}, //blues |
toyohara | 17:9e07a9ff6e91 | 74 | {mC,mE,mF,mG,mB,mC*2,mE*2,mF*2,mG*2,mB*2,mC*4,mE*4,mF*4,mG*4,mB*4,mC*8,mE*8,mF*8,mG*8,mB*8,mC*16}}; //Okinawa |
toyohara | 17:9e07a9ff6e91 | 75 | |
toyohara | 17:9e07a9ff6e91 | 76 | string names[] = {"Major", "Dorian", "Minor", "Blues", "Okinawa"}; |
toyohara | 17:9e07a9ff6e91 | 77 | |
funa0825 | 16:12f30425ba88 | 78 | void printlcd(void) |
funa0825 | 16:12f30425ba88 | 79 | { |
toyohara | 17:9e07a9ff6e91 | 80 | lcd.cls(); |
funa0825 | 16:12f30425ba88 | 81 | stdio_mutex.lock(); |
funa0825 | 16:12f30425ba88 | 82 | lcd.locate(10,10); |
toyohara | 17:9e07a9ff6e91 | 83 | lcd.printf("scale %s", names[scale]); |
funa0825 | 16:12f30425ba88 | 84 | printflag=false; |
funa0825 | 16:12f30425ba88 | 85 | stdio_mutex.unlock(); |
funa0825 | 16:12f30425ba88 | 86 | } |
funa0825 | 3:b1115b34e95b | 87 | void intRise(void) |
funa0825 | 3:b1115b34e95b | 88 | { |
funa0825 | 3:b1115b34e95b | 89 | if(for_chat.read_ms()>THRESH); |
funa0825 | 3:b1115b34e95b | 90 | { |
max_0630 | 5:04b0c694b5a1 | 91 | if(i<=13){ |
funa0825 | 3:b1115b34e95b | 92 | i++; |
max_0630 | 5:04b0c694b5a1 | 93 | count ++; |
max_0630 | 5:04b0c694b5a1 | 94 | } |
funa0825 | 3:b1115b34e95b | 95 | sp1.period(1.0/mm[i]); |
funa0825 | 3:b1115b34e95b | 96 | sp1.write(0.5f); |
funa0825 | 3:b1115b34e95b | 97 | for_chat.reset(); |
funa0825 | 3:b1115b34e95b | 98 | } |
funa0825 | 3:b1115b34e95b | 99 | } |
funa0825 | 3:b1115b34e95b | 100 | |
funa0825 | 3:b1115b34e95b | 101 | void intFall(void) |
funa0825 | 3:b1115b34e95b | 102 | { |
funa0825 | 3:b1115b34e95b | 103 | if(for_chat.read_ms()>THRESH); |
funa0825 | 3:b1115b34e95b | 104 | { |
max_0630 | 5:04b0c694b5a1 | 105 | if(i >=1){ |
funa0825 | 3:b1115b34e95b | 106 | i--; |
max_0630 | 5:04b0c694b5a1 | 107 | count --; |
max_0630 | 5:04b0c694b5a1 | 108 | } |
funa0825 | 3:b1115b34e95b | 109 | sp1.period(1.0/mm[i]); |
funa0825 | 3:b1115b34e95b | 110 | sp1.write(0.5f); |
funa0825 | 3:b1115b34e95b | 111 | for_chat.reset(); |
funa0825 | 3:b1115b34e95b | 112 | } |
funa0825 | 3:b1115b34e95b | 113 | } |
funa0825 | 3:b1115b34e95b | 114 | |
toyohara | 15:46ca600425fc | 115 | void On(void) |
toyohara | 15:46ca600425fc | 116 | { |
toyohara | 15:46ca600425fc | 117 | pushed = true; |
toyohara | 15:46ca600425fc | 118 | } |
toyohara | 15:46ca600425fc | 119 | |
toyohara | 15:46ca600425fc | 120 | void Off(void) |
toyohara | 15:46ca600425fc | 121 | { |
toyohara | 15:46ca600425fc | 122 | pushed = false; |
toyohara | 15:46ca600425fc | 123 | } |
toyohara | 15:46ca600425fc | 124 | |
toyohara | 15:46ca600425fc | 125 | void scaleUp(void) |
toyohara | 15:46ca600425fc | 126 | { |
toyohara | 15:46ca600425fc | 127 | if(for_chat.read_ms()>THRESH) ; |
toyohara | 15:46ca600425fc | 128 | { |
toyohara | 15:46ca600425fc | 129 | if (scale == NUM_OF_SCALE - 1) |
toyohara | 15:46ca600425fc | 130 | { |
toyohara | 15:46ca600425fc | 131 | scale = 0; |
toyohara | 15:46ca600425fc | 132 | } else |
toyohara | 15:46ca600425fc | 133 | { |
toyohara | 15:46ca600425fc | 134 | scale++; |
toyohara | 15:46ca600425fc | 135 | } |
toyohara | 17:9e07a9ff6e91 | 136 | printflag=true; |
toyohara | 15:46ca600425fc | 137 | } |
toyohara | 15:46ca600425fc | 138 | for_chat.reset(); |
toyohara | 15:46ca600425fc | 139 | } |
toyohara | 15:46ca600425fc | 140 | |
toyohara | 15:46ca600425fc | 141 | void scaleDown(void) |
toyohara | 15:46ca600425fc | 142 | { |
toyohara | 15:46ca600425fc | 143 | if(for_chat.read_ms()>THRESH) ; |
toyohara | 15:46ca600425fc | 144 | { |
toyohara | 15:46ca600425fc | 145 | if (scale == 0) |
toyohara | 15:46ca600425fc | 146 | { |
toyohara | 15:46ca600425fc | 147 | scale = NUM_OF_SCALE - 1; |
toyohara | 15:46ca600425fc | 148 | } else |
toyohara | 15:46ca600425fc | 149 | { |
toyohara | 15:46ca600425fc | 150 | scale--; |
toyohara | 15:46ca600425fc | 151 | } |
funa0825 | 16:12f30425ba88 | 152 | printflag=true; |
toyohara | 15:46ca600425fc | 153 | } |
toyohara | 15:46ca600425fc | 154 | for_chat.reset(); |
toyohara | 15:46ca600425fc | 155 | } |
toyohara | 15:46ca600425fc | 156 | |
max_0630 | 11:5e85ab7d4176 | 157 | void disp_thread(void const *argument) |
max_0630 | 11:5e85ab7d4176 | 158 | { |
toyohara | 15:46ca600425fc | 159 | float volume; |
toyohara | 15:46ca600425fc | 160 | float mswitch; |
toyohara | 15:46ca600425fc | 161 | |
max_0630 | 11:5e85ab7d4176 | 162 | while (true) |
max_0630 | 11:5e85ab7d4176 | 163 | { |
toyohara | 15:46ca600425fc | 164 | volume = volumeMeter.read(); |
toyohara | 15:46ca600425fc | 165 | mswitch = musicSwitch.read(); |
toyohara | 15:46ca600425fc | 166 | i_for20mm = (int)(mswitch*20);//change 20 param |
max_0630 | 14:3b123bcdd79c | 167 | |
toyohara | 15:46ca600425fc | 168 | if (i_for20mm != i_for20mm_old) |
toyohara | 15:46ca600425fc | 169 | { |
toyohara | 15:46ca600425fc | 170 | sp1.period(1.0/(notes[scale][i_for20mm])); |
toyohara | 15:46ca600425fc | 171 | } |
toyohara | 15:46ca600425fc | 172 | i_for20mm_old = i_for20mm; |
toyohara | 15:46ca600425fc | 173 | |
toyohara | 15:46ca600425fc | 174 | if(pushed) |
toyohara | 15:46ca600425fc | 175 | { |
toyohara | 15:46ca600425fc | 176 | sp1.write(volume); |
toyohara | 15:46ca600425fc | 177 | } |
toyohara | 15:46ca600425fc | 178 | else |
toyohara | 15:46ca600425fc | 179 | { |
toyohara | 15:46ca600425fc | 180 | sp1.write(0.0f); |
toyohara | 15:46ca600425fc | 181 | } |
toyohara | 15:46ca600425fc | 182 | |
toyohara | 15:46ca600425fc | 183 | for_chat.reset(); |
funa0825 | 16:12f30425ba88 | 184 | if(printflag) |
funa0825 | 16:12f30425ba88 | 185 | { |
funa0825 | 16:12f30425ba88 | 186 | printlcd(); |
funa0825 | 16:12f30425ba88 | 187 | } |
max_0630 | 14:3b123bcdd79c | 188 | ///////////////////////////// |
toyohara | 15:46ca600425fc | 189 | |
toyohara | 15:46ca600425fc | 190 | Thread::wait(50); |
max_0630 | 11:5e85ab7d4176 | 191 | } |
max_0630 | 11:5e85ab7d4176 | 192 | } |
funa0825 | 16:12f30425ba88 | 193 | void wave_file_player(void const *argument) |
funa0825 | 16:12f30425ba88 | 194 | { |
funa0825 | 16:12f30425ba88 | 195 | waver.play(wave_file); |
funa0825 | 16:12f30425ba88 | 196 | fclose(wave_file); |
funa0825 | 16:12f30425ba88 | 197 | } |
toyohara | 19:8762c0a71816 | 198 | |
funa0825 | 3:b1115b34e95b | 199 | int main() |
funa0825 | 3:b1115b34e95b | 200 | { |
funa0825 | 3:b1115b34e95b | 201 | wait(0.01); |
funa0825 | 3:b1115b34e95b | 202 | for_chat.start(); |
funa0825 | 3:b1115b34e95b | 203 | myup.fall(&intRise); |
funa0825 | 3:b1115b34e95b | 204 | mydown.fall(&intFall); |
toyohara | 15:46ca600425fc | 205 | push.rise(&On); |
toyohara | 15:46ca600425fc | 206 | push.fall(&Off); |
toyohara | 15:46ca600425fc | 207 | right.rise(&scaleUp); |
toyohara | 15:46ca600425fc | 208 | left.rise(&scaleDown); |
toyohara | 19:8762c0a71816 | 209 | |
funa0825 | 16:12f30425ba88 | 210 | Thread thread6(disp_thread); |
toyohara | 12:c1148331b151 | 211 | |
toyohara | 10:5fb0cef92e1b | 212 | //for USB |
toyohara | 10:5fb0cef92e1b | 213 | USBHostMSD msd("usb"); |
toyohara | 10:5fb0cef92e1b | 214 | while(!msd.connect()) |
toyohara | 10:5fb0cef92e1b | 215 | { |
toyohara | 10:5fb0cef92e1b | 216 | Thread::wait(200); |
toyohara | 10:5fb0cef92e1b | 217 | } |
toyohara | 10:5fb0cef92e1b | 218 | |
toyohara | 10:5fb0cef92e1b | 219 | //for music file |
funa0825 | 16:12f30425ba88 | 220 | stdio_mutex.lock(); |
toyohara | 15:46ca600425fc | 221 | wave_file = fopen("/usb/music/BGMforMBED2.wav", "r"); |
funa0825 | 16:12f30425ba88 | 222 | |
funa0825 | 16:12f30425ba88 | 223 | stdio_mutex.unlock(); |
toyohara | 18:3a7e3401ada3 | 224 | lcd.locate(10,10); |
toyohara | 18:3a7e3401ada3 | 225 | lcd.printf("scale %s", names[0]); |
toyohara | 10:5fb0cef92e1b | 226 | waver.play(wave_file); |
funa0825 | 16:12f30425ba88 | 227 | fclose(wave_file); |
funa0825 | 16:12f30425ba88 | 228 | |
toyohara | 10:5fb0cef92e1b | 229 | |
funa0825 | 3:b1115b34e95b | 230 | lcd.cls(); |
funa0825 | 3:b1115b34e95b | 231 | wait(0.01); |
funa0825 | 3:b1115b34e95b | 232 | while(1) |
funa0825 | 3:b1115b34e95b | 233 | { |
funa0825 | 16:12f30425ba88 | 234 | |
funa0825 | 3:b1115b34e95b | 235 | } |
funa0825 | 3:b1115b34e95b | 236 | } |
funa0825 | 3:b1115b34e95b | 237 |