piano1-1

main.cpp

Committer:
kosukesuzuki
Date:
2022-08-16
Revision:
0:a2f1b0e043b5

File content as of revision 0:a2f1b0e043b5:

#define mC 261.626//ド
#define mD 293.665//レ
#define mE 329.628//ミ
#define mF 349.228//ファ
#define mG 391.995//ソ
#define mA 440.000//ラ
#define mB 493.883//シ

#include "mbed.h"

Serial pc(USBTX,USBRX,9600); //UART
DigitalOut sp1(A0);
Ticker sound;

int oto = 0;

void tick(void)
{
    sp1.write(oto);
    oto=!oto;
}

float so = 0; //sound

int t;
int ct = 0;
float n;

float mm[]={mC,mD,mE,mF,mG,mA,mB,mC*2};

int main(){//1
    pc.printf("setting, Eenter ple \r\n");
    pc.printf("dt[ms] =");
    
    scanf("%f",&n);
    const float dt = n;
    pc.printf("\r\n start \r\n");
    
    while(1){//2
        t = 1;
        char cmd;
        //pc.printf(" count = %d\r\n",ct);
        
        if(pc.readable()){
             cmd = pc.getc();
             if(cmd == 'q'){    //ド
                if(pc.readable())t = pc.getc();
                so = mm[0]*(float)t;
                sound.attach(&tick,1.0/so/2.0);
                wait_us(dt);
                sound.detach();
                
                }else if(cmd == 'w'){   //レ
                    if(pc.readable())t = pc.getc();
                    so = mm[1]*(float)t;
                    sound.attach(&tick,1.0/so/2.0);
                    wait_us(dt);
                    sound.detach();
                    
                    }else if(cmd == 'e'){   //ミ
                        if(pc.readable())t = pc.getc();
                        so = mm[2]*(float)t;
                        sound.attach(&tick,1.0/so/2.0);
                        wait_us(dt);
                        sound.detach();
                        
                        }else if(cmd == 't'){   //ファ
                            if(pc.readable())t = pc.getc();
                            so = mm[3]*(float)t;
                            sound.attach(&tick,1.0/so/2.0);
                            wait_us(dt);
                            sound.detach();
                            
                            }else if(cmd == 'y'){   //ソ
                                if(pc.readable())t = pc.getc();
                                so = mm[4]*(float)t;
                                sound.attach(&tick,1.0/so/2.0);
                                wait_us(dt);
                                sound.detach();
                                
                                }else if(cmd == 'u'){   //ラ
                                    if(pc.readable())t = pc.getc();
                                    so = mm[5]*(float)t;
                                    sound.attach(&tick,1.0/so/2.0);
                                    wait_us(dt);
                                    sound.detach();
                                    
                                    }else if(cmd == 'i'){   //シ
                                        if(pc.readable())t = pc.getc();
                                        so = mm[6]*(float)t;
                                        sound.attach(&tick,1.0/so/2.0);
                                        wait_us(dt);
                                        sound.detach();
                                        
                                        }else if(cmd == 'o'){   //ド
                                            if(pc.readable())t = pc.getc();
                                            so = mm[7]*(float)t;
                                            sound.attach(&tick,1.0/so/2.0);
                                            wait_us(dt);
                                            sound.detach();
                                            
                                            }else if(cmd == 'k'){
                                                printf("END");
                                                //return 0;
                                                }
            }
            ct = ct + 1;
        }//2
    }//1