![](/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:
- 8:781b03221397
- Parent:
- 7:d2fe1a5e79ed
- Child:
- 9:9a36b66869fa
--- a/main.cpp Wed Jul 04 21:17:39 2018 +0000 +++ b/main.cpp Wed Jul 04 21:47:32 2018 +0000 @@ -1,7 +1,5 @@ #include "mbed.h" #include "TextLCD.h" -#include "button_value.h" -#include <map> #include <string.h> #include <math.h> @@ -25,20 +23,19 @@ double add[4]; }; -NoteReference noteReference[8] = { {"Do", 0x1, 262.0, {0.0, -1.0, -1.5, -3.0}}, - {"Re", 0x2, 294.0, {0.0, -1.0, -1.0, -3.0 }}, - {"Mi", 0x4, 330.0, {0.0, -1.0, -1.5, -3.0}}, - {"Fa", 0x8, 349.0, {0.0, 0.5, -1.0, 2.0}}, - {"Sol", 0x10, 392.0, {0.0, 0.0, 0.0, 0.0}}, - {"La", 0x20, 440.0, {0.0, 0.0, 0.0, 0.0}}, - {"Si", 0x40, 494.0, {0.0, 0.0, -1.0, -1.0}}, - {"Do", 0x80, 523.0, {0.0, 0.0, 0.0, 0.0}} - }; -volatile int state_buttons = 0; -volatile int state_button_gamme = 0; +NoteReference noteReference[8] = { {"Do", 0x1, 262.0, {0.0, -1.0, -1.5, -3.0}}, + {"Re", 0x2, 294.0, {0.0, -1.0, -1.0, -3.0 }}, + {"Mi", 0x4, 330.0, {0.0, -1.0, -1.5, -3.0}}, + {"Fa", 0x8, 349.0, {0.0, 0.5, -1.0, 2.0}}, + {"Sol", 0x10, 392.0, {0.0, 0.0, 0.0, 0.0}}, + {"La", 0x20, 440.0, {0.0, 0.0, 0.0, 0.0}}, + {"Si", 0x40, 494.0, {0.0, 0.0, -1.0, -1.0}}, + {"Do", 0x80, 523.0, {0.0, 0.0, 0.0, 0.0}} + }; +volatile int stateButtons = 0; +volatile int stateButtonGamme = 0; volatile int gamme = 0; - //Define variable for display char bufferOutput[30] = ""; @@ -48,20 +45,37 @@ void refresh_state_button() { - state_buttons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used + stateButtons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used } void play_music(int notes, double frequency) { speaker.period(1.0 / frequency); - while (state_buttons == notes) { + while (stateButtons == notes) { refresh_state_button(); } } + +double generate_frequency(double frequency, int actualGamme) +{ + frequency = 0.0; + lockBufferOutput.lock(); + strcpy(bufferOutput, ""); + for (int i = 0; i < 8; i++) { + if (!(stateButtons & noteReference[i].mask)) + { + frequency += noteReference[i].frequency * pow(2.0, (double)actualGamme) + noteReference[i].add[actualGamme]; + strcat(bufferOutput, noteReference[i].name); + strcat(bufferOutput, " "); + } + } + lockBufferOutput.unlock(); + return (frequency); +} void check_buttons(double frequency, int actualGamme) { - if (state_buttons == 0xFF) + if (stateButtons == 0xFF) { speaker = 0; lockBufferOutput.lock(); @@ -69,22 +83,12 @@ lockBufferOutput.unlock(); } else { - frequency = 0.0; lockGamme.lock(); actualGamme = gamme; lockGamme.unlock(); - lockBufferOutput.lock(); - strcpy(bufferOutput, ""); - for (int i = 0; i < 8; i++) { - if (!(state_buttons & noteReference[i].mask)) { - frequency += noteReference[i].frequency * pow(2.0, (double)actualGamme) + noteReference[i].add[actualGamme]; - strcat(bufferOutput, noteReference[i].name); - strcat(bufferOutput, " "); - } - } - lockBufferOutput.unlock(); + frequency = generate_frequency(frequency, actualGamme); speaker = 0.5; - play_music(state_buttons, frequency); + play_music(stateButtons, frequency); } } @@ -104,7 +108,7 @@ void run_display() { char old_buffer[30] = ""; - int old_gamme = 0; + int old_gamme = 0; lockBufferOutput.lock(); lockGamme.lock(); @@ -142,7 +146,7 @@ void check_state_button_gamme() { - state_button_gamme = Bus_In_Gamme & Bus_In_Gamme.mask(); + stateButtonGamme = Bus_In_Gamme & Bus_In_Gamme.mask(); } void run_gamme() @@ -150,15 +154,12 @@ while(true) { check_state_button_gamme(); - if (state_button_gamme == 0x0) + if (stateButtonGamme == 0x0) { lockGamme.lock(); - if (gamme == 3) - gamme = 0; - else - gamme++; + gamme = (gamme == 3) ? 0 : gamme + 1; lockGamme.unlock(); - while (state_button_gamme == 0x0) + while (stateButtonGamme == 0x0) check_state_button_gamme(); } Thread::wait(100);