![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
piano 4 octaves, 8 notes, 3 threads, 2 outputs, no bug, clean code, comments, remove old .h
Fork of Nucleo_piano_CS435 by
Diff: main.cpp
- Revision:
- 6:58bab17100a2
- Parent:
- 5:824785b64822
- Child:
- 7:d2fe1a5e79ed
--- a/main.cpp Tue Jul 03 02:06:44 2018 +0000 +++ b/main.cpp Tue Jul 03 03:29:59 2018 +0000 @@ -35,47 +35,27 @@ void print_note(char *str) { - int i = 0; - int note = 0; - - lcd.cls(); - wait(0.000040f); - while (str[i]) - { - note = str[i]; - lcd.putc(note); - wait(0.000040f); - i++; - } +// lcd.cls(); + // wait(0.000040f); + lcd.printf("%s", str); } -//Thread thread; DigitalOut led1(LED1); -/**void print_thread() -{ - while (true) { - wait(1); - print_char(); - } -}**/ - void refresh_state_button() { - printf("refresh_state_button \n"); + //printf("refresh_state_button \n"); state_buttons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used - printf("state_button [ %d ]\n", state_buttons); + //printf("state_button [ %d ]\n", state_buttons); } void play_music(int notes, double frequency) { - printf("play_music \n"); speaker.period(1.0 / frequency); while (state_buttons == notes) { refresh_state_button(); } - speaker = 0; } void check_buttons() @@ -84,21 +64,24 @@ int i = 0; char bufferOutput[30] = ""; - while (i < 8) + if (state_buttons == 0xFF) { - if (!(state_buttons & noteReference[i].mask)) { - frequency += noteReference[i].frequency * pow(2.0, gamme) + noteReference[i].add[gamme]; - strcat(bufferOutput, noteReference[i].name); - strcat(bufferOutput, " "); + speaker = 0; + lcd.cls(); + } + else { + while (i < 8) + { + if (!(state_buttons & noteReference[i].mask)) { + frequency += noteReference[i].frequency * pow(2.0, gamme) + noteReference[i].add[gamme]; + strcat(bufferOutput, noteReference[i].name); + strcat(bufferOutput, " "); + } + i++; } - strcat(bufferOutput, "\0"); - i++; - } - if (frequency > 0) { - speaker = 0.25; + speaker = 0.5; print_note(bufferOutput); play_music(state_buttons, frequency); - lcd.cls(); } } @@ -113,12 +96,13 @@ { //lcd.printf("%s\n", "CS435 Piano"); //wait(3); - speaker = 0; + while (true) { - printf("loop\n"); + //printf("loop\n"); led1 = !led1; refresh_state_button(); check_buttons(); wait(0.1); + lcd.cls(); } }