コウスケ スズキ
/
sound-piano
piano1-1
main.cpp@0:a2f1b0e043b5, 2022-08-16 (annotated)
- Committer:
- kosukesuzuki
- Date:
- Tue Aug 16 08:08:32 2022 +0000
- Revision:
- 0:a2f1b0e043b5
piano
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kosukesuzuki | 0:a2f1b0e043b5 | 1 | #define mC 261.626//ド |
kosukesuzuki | 0:a2f1b0e043b5 | 2 | #define mD 293.665//レ |
kosukesuzuki | 0:a2f1b0e043b5 | 3 | #define mE 329.628//ミ |
kosukesuzuki | 0:a2f1b0e043b5 | 4 | #define mF 349.228//ファ |
kosukesuzuki | 0:a2f1b0e043b5 | 5 | #define mG 391.995//ソ |
kosukesuzuki | 0:a2f1b0e043b5 | 6 | #define mA 440.000//ラ |
kosukesuzuki | 0:a2f1b0e043b5 | 7 | #define mB 493.883//シ |
kosukesuzuki | 0:a2f1b0e043b5 | 8 | |
kosukesuzuki | 0:a2f1b0e043b5 | 9 | #include "mbed.h" |
kosukesuzuki | 0:a2f1b0e043b5 | 10 | |
kosukesuzuki | 0:a2f1b0e043b5 | 11 | Serial pc(USBTX,USBRX,9600); //UART |
kosukesuzuki | 0:a2f1b0e043b5 | 12 | DigitalOut sp1(A0); |
kosukesuzuki | 0:a2f1b0e043b5 | 13 | Ticker sound; |
kosukesuzuki | 0:a2f1b0e043b5 | 14 | |
kosukesuzuki | 0:a2f1b0e043b5 | 15 | int oto = 0; |
kosukesuzuki | 0:a2f1b0e043b5 | 16 | |
kosukesuzuki | 0:a2f1b0e043b5 | 17 | void tick(void) |
kosukesuzuki | 0:a2f1b0e043b5 | 18 | { |
kosukesuzuki | 0:a2f1b0e043b5 | 19 | sp1.write(oto); |
kosukesuzuki | 0:a2f1b0e043b5 | 20 | oto=!oto; |
kosukesuzuki | 0:a2f1b0e043b5 | 21 | } |
kosukesuzuki | 0:a2f1b0e043b5 | 22 | |
kosukesuzuki | 0:a2f1b0e043b5 | 23 | float so = 0; //sound |
kosukesuzuki | 0:a2f1b0e043b5 | 24 | |
kosukesuzuki | 0:a2f1b0e043b5 | 25 | int t; |
kosukesuzuki | 0:a2f1b0e043b5 | 26 | int ct = 0; |
kosukesuzuki | 0:a2f1b0e043b5 | 27 | float n; |
kosukesuzuki | 0:a2f1b0e043b5 | 28 | |
kosukesuzuki | 0:a2f1b0e043b5 | 29 | float mm[]={mC,mD,mE,mF,mG,mA,mB,mC*2}; |
kosukesuzuki | 0:a2f1b0e043b5 | 30 | |
kosukesuzuki | 0:a2f1b0e043b5 | 31 | int main(){//1 |
kosukesuzuki | 0:a2f1b0e043b5 | 32 | pc.printf("setting, Eenter ple \r\n"); |
kosukesuzuki | 0:a2f1b0e043b5 | 33 | pc.printf("dt[ms] ="); |
kosukesuzuki | 0:a2f1b0e043b5 | 34 | |
kosukesuzuki | 0:a2f1b0e043b5 | 35 | scanf("%f",&n); |
kosukesuzuki | 0:a2f1b0e043b5 | 36 | const float dt = n; |
kosukesuzuki | 0:a2f1b0e043b5 | 37 | pc.printf("\r\n start \r\n"); |
kosukesuzuki | 0:a2f1b0e043b5 | 38 | |
kosukesuzuki | 0:a2f1b0e043b5 | 39 | while(1){//2 |
kosukesuzuki | 0:a2f1b0e043b5 | 40 | t = 1; |
kosukesuzuki | 0:a2f1b0e043b5 | 41 | char cmd; |
kosukesuzuki | 0:a2f1b0e043b5 | 42 | //pc.printf(" count = %d\r\n",ct); |
kosukesuzuki | 0:a2f1b0e043b5 | 43 | |
kosukesuzuki | 0:a2f1b0e043b5 | 44 | if(pc.readable()){ |
kosukesuzuki | 0:a2f1b0e043b5 | 45 | cmd = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 46 | if(cmd == 'q'){ //ド |
kosukesuzuki | 0:a2f1b0e043b5 | 47 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 48 | so = mm[0]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 49 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 50 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 51 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 52 | |
kosukesuzuki | 0:a2f1b0e043b5 | 53 | }else if(cmd == 'w'){ //レ |
kosukesuzuki | 0:a2f1b0e043b5 | 54 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 55 | so = mm[1]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 56 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 57 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 58 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 59 | |
kosukesuzuki | 0:a2f1b0e043b5 | 60 | }else if(cmd == 'e'){ //ミ |
kosukesuzuki | 0:a2f1b0e043b5 | 61 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 62 | so = mm[2]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 63 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 64 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 65 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 66 | |
kosukesuzuki | 0:a2f1b0e043b5 | 67 | }else if(cmd == 't'){ //ファ |
kosukesuzuki | 0:a2f1b0e043b5 | 68 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 69 | so = mm[3]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 70 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 71 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 72 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 73 | |
kosukesuzuki | 0:a2f1b0e043b5 | 74 | }else if(cmd == 'y'){ //ソ |
kosukesuzuki | 0:a2f1b0e043b5 | 75 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 76 | so = mm[4]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 77 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 78 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 79 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 80 | |
kosukesuzuki | 0:a2f1b0e043b5 | 81 | }else if(cmd == 'u'){ //ラ |
kosukesuzuki | 0:a2f1b0e043b5 | 82 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 83 | so = mm[5]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 84 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 85 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 86 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 87 | |
kosukesuzuki | 0:a2f1b0e043b5 | 88 | }else if(cmd == 'i'){ //シ |
kosukesuzuki | 0:a2f1b0e043b5 | 89 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 90 | so = mm[6]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 91 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 92 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 93 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 94 | |
kosukesuzuki | 0:a2f1b0e043b5 | 95 | }else if(cmd == 'o'){ //ド |
kosukesuzuki | 0:a2f1b0e043b5 | 96 | if(pc.readable())t = pc.getc(); |
kosukesuzuki | 0:a2f1b0e043b5 | 97 | so = mm[7]*(float)t; |
kosukesuzuki | 0:a2f1b0e043b5 | 98 | sound.attach(&tick,1.0/so/2.0); |
kosukesuzuki | 0:a2f1b0e043b5 | 99 | wait_us(dt); |
kosukesuzuki | 0:a2f1b0e043b5 | 100 | sound.detach(); |
kosukesuzuki | 0:a2f1b0e043b5 | 101 | |
kosukesuzuki | 0:a2f1b0e043b5 | 102 | }else if(cmd == 'k'){ |
kosukesuzuki | 0:a2f1b0e043b5 | 103 | printf("END"); |
kosukesuzuki | 0:a2f1b0e043b5 | 104 | //return 0; |
kosukesuzuki | 0:a2f1b0e043b5 | 105 | } |
kosukesuzuki | 0:a2f1b0e043b5 | 106 | } |
kosukesuzuki | 0:a2f1b0e043b5 | 107 | ct = ct + 1; |
kosukesuzuki | 0:a2f1b0e043b5 | 108 | }//2 |
kosukesuzuki | 0:a2f1b0e043b5 | 109 | }//1 |