テスト
Dependencies: C12832 USBHost mbed MMA7660 wave_player
Fork of app-board-Speaker by
main.cpp@5:04b0c694b5a1, 2016-12-10 (annotated)
- Committer:
- max_0630
- Date:
- Sat Dec 10 11:30:44 2016 +0000
- Revision:
- 5:04b0c694b5a1
- Parent:
- 3:b1115b34e95b
- Child:
- 6:82aa8f2c3c47
sound : LED = 1;1; param " i" never go upper 15,under 0;
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 |
funa0825 | 3:b1115b34e95b | 5 | #define mE 329.628 |
funa0825 | 3:b1115b34e95b | 6 | #define mF 349.228 |
funa0825 | 3:b1115b34e95b | 7 | #define mG 391.995 |
funa0825 | 3:b1115b34e95b | 8 | #define mA 440.000 |
funa0825 | 3:b1115b34e95b | 9 | #define mB 493.883 |
funa0825 | 3:b1115b34e95b | 10 | #define THRESH 80 |
max_0630 | 5:04b0c694b5a1 | 11 | #define COUNT_MAX 15 |
funa0825 | 3:b1115b34e95b | 12 | #include "C12832.h" |
chris | 0:f86c572491c3 | 13 | #include "mbed.h" |
funa0825 | 3:b1115b34e95b | 14 | #include "rtos.h" |
chris | 0:f86c572491c3 | 15 | |
funa0825 | 3:b1115b34e95b | 16 | C12832 lcd(p5, p7, p6, p8, p11); |
chris | 1:0a08d26b011a | 17 | DigitalIn fire(p14); |
funa0825 | 3:b1115b34e95b | 18 | //DigitalIn up(p15); |
funa0825 | 3:b1115b34e95b | 19 | //DigitalIn down(p12); |
funa0825 | 3:b1115b34e95b | 20 | PwmOut sp1(p26); |
funa0825 | 3:b1115b34e95b | 21 | InterruptIn myup(p15); |
funa0825 | 3:b1115b34e95b | 22 | InterruptIn mydown(p12); |
funa0825 | 3:b1115b34e95b | 23 | PwmOut led1(p23); |
funa0825 | 3:b1115b34e95b | 24 | PwmOut led2(p24); |
funa0825 | 3:b1115b34e95b | 25 | PwmOut led3(p25); |
funa0825 | 3:b1115b34e95b | 26 | Semaphore three_slots(1); |
funa0825 | 3:b1115b34e95b | 27 | Timer for_chat; |
chris | 0:f86c572491c3 | 28 | |
funa0825 | 3:b1115b34e95b | 29 | int i=0; |
max_0630 | 5:04b0c694b5a1 | 30 | int count = 0; |
max_0630 | 5:04b0c694b5a1 | 31 | |
funa0825 | 3:b1115b34e95b | 32 | 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}; |
funa0825 | 3:b1115b34e95b | 33 | void intRise(void) |
funa0825 | 3:b1115b34e95b | 34 | { |
funa0825 | 3:b1115b34e95b | 35 | if(for_chat.read_ms()>THRESH); |
funa0825 | 3:b1115b34e95b | 36 | { |
max_0630 | 5:04b0c694b5a1 | 37 | if(i<=13){ |
funa0825 | 3:b1115b34e95b | 38 | i++; |
max_0630 | 5:04b0c694b5a1 | 39 | count ++; |
max_0630 | 5:04b0c694b5a1 | 40 | } |
funa0825 | 3:b1115b34e95b | 41 | sp1.period(1.0/mm[i]); |
funa0825 | 3:b1115b34e95b | 42 | sp1.write(0.5f); |
funa0825 | 3:b1115b34e95b | 43 | for_chat.reset(); |
funa0825 | 3:b1115b34e95b | 44 | } |
funa0825 | 3:b1115b34e95b | 45 | } |
funa0825 | 3:b1115b34e95b | 46 | |
funa0825 | 3:b1115b34e95b | 47 | void intFall(void) |
funa0825 | 3:b1115b34e95b | 48 | { |
funa0825 | 3:b1115b34e95b | 49 | if(for_chat.read_ms()>THRESH); |
funa0825 | 3:b1115b34e95b | 50 | { |
max_0630 | 5:04b0c694b5a1 | 51 | if(i >=1){ |
funa0825 | 3:b1115b34e95b | 52 | i--; |
max_0630 | 5:04b0c694b5a1 | 53 | count --; |
max_0630 | 5:04b0c694b5a1 | 54 | } |
funa0825 | 3:b1115b34e95b | 55 | sp1.period(1.0/mm[i]); |
funa0825 | 3:b1115b34e95b | 56 | sp1.write(0.5f); |
funa0825 | 3:b1115b34e95b | 57 | for_chat.reset(); |
funa0825 | 3:b1115b34e95b | 58 | } |
funa0825 | 3:b1115b34e95b | 59 | } |
funa0825 | 3:b1115b34e95b | 60 | |
funa0825 | 3:b1115b34e95b | 61 | |
funa0825 | 3:b1115b34e95b | 62 | void r_thread(void const *argument) |
funa0825 | 3:b1115b34e95b | 63 | { |
funa0825 | 3:b1115b34e95b | 64 | float r = 0; |
funa0825 | 3:b1115b34e95b | 65 | while (true) |
funa0825 | 3:b1115b34e95b | 66 | { |
funa0825 | 3:b1115b34e95b | 67 | r=0; |
funa0825 | 3:b1115b34e95b | 68 | while(r<=1) |
funa0825 | 3:b1115b34e95b | 69 | { |
funa0825 | 3:b1115b34e95b | 70 | three_slots.wait(); |
max_0630 | 5:04b0c694b5a1 | 71 | switch(count){ |
max_0630 | 5:04b0c694b5a1 | 72 | case 0:r = 0.0;break; |
max_0630 | 5:04b0c694b5a1 | 73 | case 1:r=0.07;break; |
max_0630 | 5:04b0c694b5a1 | 74 | case 2:r=0.13;break; |
max_0630 | 5:04b0c694b5a1 | 75 | case 3:r=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 76 | case 4:r=0.27;break; |
max_0630 | 5:04b0c694b5a1 | 77 | case 5:r=0.34;break; |
max_0630 | 5:04b0c694b5a1 | 78 | case 6:r=0.41;break; |
max_0630 | 5:04b0c694b5a1 | 79 | case 7:r=0.48;break; |
max_0630 | 5:04b0c694b5a1 | 80 | case 8:r=0.55;break; |
max_0630 | 5:04b0c694b5a1 | 81 | case 9:r=0.62;break; |
max_0630 | 5:04b0c694b5a1 | 82 | case 10:r=0.69;break; |
max_0630 | 5:04b0c694b5a1 | 83 | case 11:r=0.76;break; |
max_0630 | 5:04b0c694b5a1 | 84 | case 12:r=0.83;break; |
max_0630 | 5:04b0c694b5a1 | 85 | case 13:r=0.90;break; |
max_0630 | 5:04b0c694b5a1 | 86 | case 14:r=0.97;break; |
max_0630 | 5:04b0c694b5a1 | 87 | } |
max_0630 | 5:04b0c694b5a1 | 88 | //r = (float)(count%COUNT_MAX)/COUNT_MAX; |
funa0825 | 3:b1115b34e95b | 89 | led1 = r; |
max_0630 | 5:04b0c694b5a1 | 90 | //r += 0.01; |
funa0825 | 3:b1115b34e95b | 91 | lcd.locate(0,16); |
funa0825 | 3:b1115b34e95b | 92 | lcd.printf("R: %3.2f",r); |
funa0825 | 3:b1115b34e95b | 93 | three_slots.release(); |
funa0825 | 3:b1115b34e95b | 94 | Thread::wait(100); |
funa0825 | 3:b1115b34e95b | 95 | |
funa0825 | 3:b1115b34e95b | 96 | } |
funa0825 | 3:b1115b34e95b | 97 | while(r>0) |
funa0825 | 3:b1115b34e95b | 98 | { |
funa0825 | 3:b1115b34e95b | 99 | three_slots.wait(); |
max_0630 | 5:04b0c694b5a1 | 100 | switch(count){ |
max_0630 | 5:04b0c694b5a1 | 101 | case 0:r = 0.0;break; |
max_0630 | 5:04b0c694b5a1 | 102 | case 1:r=0.07;break; |
max_0630 | 5:04b0c694b5a1 | 103 | case 2:r=0.13;break; |
max_0630 | 5:04b0c694b5a1 | 104 | case 3:r=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 105 | case 4:r=0.27;break; |
max_0630 | 5:04b0c694b5a1 | 106 | case 5:r=0.34;break; |
max_0630 | 5:04b0c694b5a1 | 107 | case 6:r=0.41;break; |
max_0630 | 5:04b0c694b5a1 | 108 | case 7:r=0.48;break; |
max_0630 | 5:04b0c694b5a1 | 109 | case 8:r=0.55;break; |
max_0630 | 5:04b0c694b5a1 | 110 | case 9:r=0.62;break; |
max_0630 | 5:04b0c694b5a1 | 111 | case 10:r=0.69;break; |
max_0630 | 5:04b0c694b5a1 | 112 | case 11:r=0.76;break; |
max_0630 | 5:04b0c694b5a1 | 113 | case 12:r=0.83;break; |
max_0630 | 5:04b0c694b5a1 | 114 | case 13:r=0.90;break; |
max_0630 | 5:04b0c694b5a1 | 115 | case 14:r=0.97;break; |
max_0630 | 5:04b0c694b5a1 | 116 | } |
max_0630 | 5:04b0c694b5a1 | 117 | //r = (float)(count%COUNT_MAX)/COUNT_MAX; |
funa0825 | 3:b1115b34e95b | 118 | led1 = r; |
max_0630 | 5:04b0c694b5a1 | 119 | //r -= 0.01; |
funa0825 | 3:b1115b34e95b | 120 | lcd.locate(0,16); |
funa0825 | 3:b1115b34e95b | 121 | lcd.printf("R: %3.2f",r); |
funa0825 | 3:b1115b34e95b | 122 | three_slots.release(); |
funa0825 | 3:b1115b34e95b | 123 | Thread::wait(100); |
funa0825 | 3:b1115b34e95b | 124 | |
funa0825 | 3:b1115b34e95b | 125 | } |
funa0825 | 3:b1115b34e95b | 126 | } |
funa0825 | 3:b1115b34e95b | 127 | } |
funa0825 | 3:b1115b34e95b | 128 | void g_thread(void const *argument) |
chris | 0:f86c572491c3 | 129 | { |
funa0825 | 3:b1115b34e95b | 130 | float g = 0; |
funa0825 | 3:b1115b34e95b | 131 | while (true) |
funa0825 | 3:b1115b34e95b | 132 | { |
funa0825 | 3:b1115b34e95b | 133 | g=0; |
funa0825 | 3:b1115b34e95b | 134 | while(g<=1) |
funa0825 | 3:b1115b34e95b | 135 | { |
funa0825 | 3:b1115b34e95b | 136 | three_slots.wait(); |
max_0630 | 5:04b0c694b5a1 | 137 | switch(count){ |
max_0630 | 5:04b0c694b5a1 | 138 | case 0:g = 0.50;break; |
max_0630 | 5:04b0c694b5a1 | 139 | case 1:g = 0.50;break; |
max_0630 | 5:04b0c694b5a1 | 140 | case 2:g=0.63;break; |
max_0630 | 5:04b0c694b5a1 | 141 | case 3:g=0.63;break; |
max_0630 | 5:04b0c694b5a1 | 142 | case 4:g=0.77;break; |
max_0630 | 5:04b0c694b5a1 | 143 | case 5:g=0.77;break; |
max_0630 | 5:04b0c694b5a1 | 144 | case 6:g=0.90;break; |
max_0630 | 5:04b0c694b5a1 | 145 | case 7:g=0.90;break; |
max_0630 | 5:04b0c694b5a1 | 146 | case 8:g=0.47;break; |
max_0630 | 5:04b0c694b5a1 | 147 | case 9:g=0.47;break; |
max_0630 | 5:04b0c694b5a1 | 148 | case 10:g=0.33;break; |
max_0630 | 5:04b0c694b5a1 | 149 | case 11:g=0.33;break; |
max_0630 | 5:04b0c694b5a1 | 150 | case 12:g=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 151 | case 13:g=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 152 | case 14:g=0.07;break; |
max_0630 | 5:04b0c694b5a1 | 153 | } |
funa0825 | 3:b1115b34e95b | 154 | led2 = g; |
max_0630 | 5:04b0c694b5a1 | 155 | //g += 0.01; |
funa0825 | 3:b1115b34e95b | 156 | lcd.locate(40,16); |
funa0825 | 3:b1115b34e95b | 157 | lcd.printf("G: %3.2f",g); |
funa0825 | 3:b1115b34e95b | 158 | three_slots.release(); |
funa0825 | 3:b1115b34e95b | 159 | Thread::wait(200); |
funa0825 | 3:b1115b34e95b | 160 | } |
funa0825 | 3:b1115b34e95b | 161 | while(g>0) |
funa0825 | 3:b1115b34e95b | 162 | { |
funa0825 | 3:b1115b34e95b | 163 | three_slots.wait(); |
max_0630 | 5:04b0c694b5a1 | 164 | switch(count){ |
max_0630 | 5:04b0c694b5a1 | 165 | case 0:g = 0.50;break; |
max_0630 | 5:04b0c694b5a1 | 166 | case 1:g = 0.50;break; |
max_0630 | 5:04b0c694b5a1 | 167 | case 2:g=0.63;break; |
max_0630 | 5:04b0c694b5a1 | 168 | case 3:g=0.63;break; |
max_0630 | 5:04b0c694b5a1 | 169 | case 4:g=0.77;break; |
max_0630 | 5:04b0c694b5a1 | 170 | case 5:g=0.77;break; |
max_0630 | 5:04b0c694b5a1 | 171 | case 6:g=0.90;break; |
max_0630 | 5:04b0c694b5a1 | 172 | case 7:g=0.90;break; |
max_0630 | 5:04b0c694b5a1 | 173 | case 8:g=0.47;break; |
max_0630 | 5:04b0c694b5a1 | 174 | case 9:g=0.47;break; |
max_0630 | 5:04b0c694b5a1 | 175 | case 10:g=0.33;break; |
max_0630 | 5:04b0c694b5a1 | 176 | case 11:g=0.33;break; |
max_0630 | 5:04b0c694b5a1 | 177 | case 12:g=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 178 | case 13:g=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 179 | case 14:g=0.07;break; |
max_0630 | 5:04b0c694b5a1 | 180 | } |
funa0825 | 3:b1115b34e95b | 181 | led2 = g; |
max_0630 | 5:04b0c694b5a1 | 182 | //g -= 0.01; |
funa0825 | 3:b1115b34e95b | 183 | lcd.locate(40,16); |
funa0825 | 3:b1115b34e95b | 184 | lcd.printf("G: %3.2f",g); |
funa0825 | 3:b1115b34e95b | 185 | three_slots.release(); |
funa0825 | 3:b1115b34e95b | 186 | Thread::wait(200); |
funa0825 | 3:b1115b34e95b | 187 | } |
funa0825 | 3:b1115b34e95b | 188 | } |
funa0825 | 3:b1115b34e95b | 189 | } |
funa0825 | 3:b1115b34e95b | 190 | void b_thread(void const *argument) |
funa0825 | 3:b1115b34e95b | 191 | { |
funa0825 | 3:b1115b34e95b | 192 | float b = 0; |
funa0825 | 3:b1115b34e95b | 193 | while (true) |
funa0825 | 3:b1115b34e95b | 194 | { |
funa0825 | 3:b1115b34e95b | 195 | b=0; |
funa0825 | 3:b1115b34e95b | 196 | while(b<=1) |
funa0825 | 3:b1115b34e95b | 197 | { |
funa0825 | 3:b1115b34e95b | 198 | three_slots.wait(); |
max_0630 | 5:04b0c694b5a1 | 199 | switch(count){ |
max_0630 | 5:04b0c694b5a1 | 200 | case 0:b = 1.00;break; |
max_0630 | 5:04b0c694b5a1 | 201 | case 1:b = 1.00;break; |
max_0630 | 5:04b0c694b5a1 | 202 | case 2:b=1.00;break; |
max_0630 | 5:04b0c694b5a1 | 203 | case 3:b=0.80;break; |
max_0630 | 5:04b0c694b5a1 | 204 | case 4:b=0.80;break; |
max_0630 | 5:04b0c694b5a1 | 205 | case 5:b=0.80;break; |
max_0630 | 5:04b0c694b5a1 | 206 | case 6:b=0.60;break; |
max_0630 | 5:04b0c694b5a1 | 207 | case 7:b=0.60;break; |
max_0630 | 5:04b0c694b5a1 | 208 | case 8:b=0.60;break; |
max_0630 | 5:04b0c694b5a1 | 209 | case 9:b=0.40;break; |
max_0630 | 5:04b0c694b5a1 | 210 | case 10:b=0.40;break; |
max_0630 | 5:04b0c694b5a1 | 211 | case 11:b=0.40;break; |
max_0630 | 5:04b0c694b5a1 | 212 | case 12:b=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 213 | case 13:b=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 214 | case 14:b=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 215 | } |
funa0825 | 3:b1115b34e95b | 216 | led3 = b; |
max_0630 | 5:04b0c694b5a1 | 217 | //b += 0.01; |
funa0825 | 3:b1115b34e95b | 218 | lcd.locate(80,16); |
funa0825 | 3:b1115b34e95b | 219 | lcd.printf("B: %3.2f",b); |
funa0825 | 3:b1115b34e95b | 220 | three_slots.release(); |
funa0825 | 3:b1115b34e95b | 221 | Thread::wait(300); |
chris | 2:cfd337edff08 | 222 | } |
funa0825 | 3:b1115b34e95b | 223 | while(b>0) |
funa0825 | 3:b1115b34e95b | 224 | { |
funa0825 | 3:b1115b34e95b | 225 | three_slots.wait(); |
max_0630 | 5:04b0c694b5a1 | 226 | switch(count){ |
max_0630 | 5:04b0c694b5a1 | 227 | case 0:b = 1.00;break; |
max_0630 | 5:04b0c694b5a1 | 228 | case 1:b = 1.00;break; |
max_0630 | 5:04b0c694b5a1 | 229 | case 2:b=1.00;break; |
max_0630 | 5:04b0c694b5a1 | 230 | case 3:b=0.80;break; |
max_0630 | 5:04b0c694b5a1 | 231 | case 4:b=0.80;break; |
max_0630 | 5:04b0c694b5a1 | 232 | case 5:b=0.80;break; |
max_0630 | 5:04b0c694b5a1 | 233 | case 6:b=0.60;break; |
max_0630 | 5:04b0c694b5a1 | 234 | case 7:b=0.60;break; |
max_0630 | 5:04b0c694b5a1 | 235 | case 8:b=0.60;break; |
max_0630 | 5:04b0c694b5a1 | 236 | case 9:b=0.40;break; |
max_0630 | 5:04b0c694b5a1 | 237 | case 10:b=0.40;break; |
max_0630 | 5:04b0c694b5a1 | 238 | case 11:b=0.40;break; |
max_0630 | 5:04b0c694b5a1 | 239 | case 12:b=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 240 | case 13:b=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 241 | case 14:b=0.20;break; |
max_0630 | 5:04b0c694b5a1 | 242 | } |
funa0825 | 3:b1115b34e95b | 243 | led3 = b; |
max_0630 | 5:04b0c694b5a1 | 244 | //b -= 0.01; |
funa0825 | 3:b1115b34e95b | 245 | lcd.locate(80,16); |
funa0825 | 3:b1115b34e95b | 246 | lcd.printf("B: %3.2f",b); |
funa0825 | 3:b1115b34e95b | 247 | three_slots.release(); |
funa0825 | 3:b1115b34e95b | 248 | Thread::wait(300); |
funa0825 | 3:b1115b34e95b | 249 | } |
funa0825 | 3:b1115b34e95b | 250 | } |
funa0825 | 3:b1115b34e95b | 251 | } |
funa0825 | 3:b1115b34e95b | 252 | void count_thread(void const *argument) |
funa0825 | 3:b1115b34e95b | 253 | { |
funa0825 | 3:b1115b34e95b | 254 | while (true) |
funa0825 | 3:b1115b34e95b | 255 | { |
funa0825 | 3:b1115b34e95b | 256 | three_slots.wait(); |
funa0825 | 3:b1115b34e95b | 257 | lcd.locate(0,3); |
max_0630 | 5:04b0c694b5a1 | 258 | lcd.printf("Counting : %d ",i); |
funa0825 | 3:b1115b34e95b | 259 | three_slots.release(); |
funa0825 | 3:b1115b34e95b | 260 | Thread::wait(100); |
chris | 0:f86c572491c3 | 261 | } |
funa0825 | 3:b1115b34e95b | 262 | } |
funa0825 | 3:b1115b34e95b | 263 | int main() |
funa0825 | 3:b1115b34e95b | 264 | { |
funa0825 | 3:b1115b34e95b | 265 | wait(0.01); |
funa0825 | 3:b1115b34e95b | 266 | for_chat.start(); |
funa0825 | 3:b1115b34e95b | 267 | myup.fall(&intRise); |
funa0825 | 3:b1115b34e95b | 268 | mydown.fall(&intFall); |
funa0825 | 3:b1115b34e95b | 269 | Thread thread1(r_thread,(void *)"Th 1"); |
funa0825 | 3:b1115b34e95b | 270 | Thread thread2(g_thread,(void *)"Th 2"); |
funa0825 | 3:b1115b34e95b | 271 | Thread thread3(b_thread,(void *)"Th 3"); |
funa0825 | 3:b1115b34e95b | 272 | Thread thread4(count_thread,(void *)"Th 4"); |
funa0825 | 3:b1115b34e95b | 273 | |
funa0825 | 3:b1115b34e95b | 274 | lcd.cls(); |
funa0825 | 3:b1115b34e95b | 275 | wait(0.01); |
funa0825 | 3:b1115b34e95b | 276 | while(1) |
funa0825 | 3:b1115b34e95b | 277 | { |
funa0825 | 3:b1115b34e95b | 278 | //lcd.cls(); |
funa0825 | 3:b1115b34e95b | 279 | |
funa0825 | 3:b1115b34e95b | 280 | } |
funa0825 | 3:b1115b34e95b | 281 | } |
funa0825 | 3:b1115b34e95b | 282 |