Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Nucleo_piano_V1 by
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include <string.h> 00004 #include <math.h> 00005 00006 // Define screen 00007 TextLCD lcd(PA_8, PA_9, PC_7, PB_6, PA_7, PA_6, PA_5); // RS, RW, E, D4-D7 00008 00009 // Define Bus In for Buttons (Do, Re, Mi, Fa, Sol, La, Si, Do) 00010 BusIn Bus_In(PA_0, PA_1, PA_4, PB_0, PA_10, PB_3, PB_5, PB_4); 00011 00012 // Define the PWM speaker 00013 PwmOut speaker(PB_10); 00014 00015 // Define Bus In for Gamme Button 00016 BusIn Bus_In_Gamme(PB_9); 00017 00018 // Define Serial 00019 Serial pc(PA_2, PA_3); 00020 00021 //Define variables for sound 00022 struct NoteReference { 00023 char name[4]; 00024 int mask; 00025 double frequency; 00026 double add[4]; 00027 }; 00028 00029 NoteReference noteReference[8] = { {"Do", 0x1, 262.0, {0.0, -1.0, -1.5, -3.0}}, 00030 {"Re", 0x2, 294.0, {0.0, -1.0, -1.0, -3.0 }}, 00031 {"Mi", 0x4, 330.0, {0.0, -1.0, -1.5, -3.0}}, 00032 {"Fa", 0x8, 349.0, {0.0, 0.5, -1.0, 2.0}}, 00033 {"Sol", 0x10, 392.0, {0.0, 0.0, 0.0, 0.0}}, 00034 {"La", 0x20, 440.0, {0.0, 0.0, 0.0, 0.0}}, 00035 {"Si", 0x40, 494.0, {0.0, 0.0, -1.0, -1.0}}, 00036 {"Do", 0x80, 523.0, {0.0, 0.0, 0.0, 0.0}} 00037 }; 00038 volatile int stateButtons = 0; 00039 volatile int stateButtonGamme = 0; 00040 volatile int gamme = 0; 00041 00042 //Define variable for display 00043 char bufferOutput[30] = ""; 00044 00045 //Define variables for synchronization 00046 Mutex lockBufferOutput; 00047 Mutex lockGamme; 00048 00049 void refresh_state_button() 00050 { 00051 stateButtons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used 00052 } 00053 00054 void play_music(int notes, double frequency) 00055 { 00056 speaker.period(1.0 / frequency); 00057 while (stateButtons == notes) { 00058 refresh_state_button(); 00059 } 00060 } 00061 00062 double generate_frequency(double frequency, int actualGamme) 00063 { 00064 frequency = 0.0; 00065 lockBufferOutput.lock(); 00066 strcpy(bufferOutput, ""); 00067 for (int i = 0; i < 8; i++) { 00068 if (!(stateButtons & noteReference[i].mask)) 00069 { 00070 frequency += noteReference[i].frequency * pow(2.0, (double)actualGamme) + noteReference[i].add[actualGamme]; 00071 strcat(bufferOutput, noteReference[i].name); 00072 strcat(bufferOutput, " "); 00073 } 00074 } 00075 lockBufferOutput.unlock(); 00076 return (frequency); 00077 } 00078 00079 void check_buttons(double frequency, int actualGamme) 00080 { 00081 if (stateButtons == 0xFF) 00082 { 00083 speaker = 0; 00084 lockBufferOutput.lock(); 00085 strcpy(bufferOutput, ""); 00086 lockBufferOutput.unlock(); 00087 } 00088 else { 00089 lockGamme.lock(); 00090 actualGamme = gamme; 00091 lockGamme.unlock(); 00092 frequency = generate_frequency(frequency, actualGamme); 00093 speaker = 0.5; 00094 play_music(stateButtons, frequency); 00095 } 00096 } 00097 00098 void run_music() 00099 { 00100 double frequency = 0.0; 00101 int actualGamme = 0; 00102 00103 while (true) 00104 { 00105 refresh_state_button(); 00106 check_buttons(frequency, actualGamme); 00107 Thread::wait(100); 00108 } 00109 } 00110 00111 void refresh_display_notes(char *old_buffer) 00112 { 00113 lcd.cls(); 00114 lockGamme.lock(); 00115 if (strcmp(bufferOutput, "")) 00116 { 00117 lcd.printf("%s- g[%d]", bufferOutput, gamme); 00118 pc.printf("Play notes: %s with gamme %d\n", bufferOutput, gamme); 00119 } 00120 else 00121 { 00122 lcd.printf("Gamme = %d", gamme); 00123 pc.printf("Release notes\n"); 00124 } 00125 lockGamme.unlock(); 00126 strcpy(old_buffer, bufferOutput); 00127 } 00128 00129 int refresh_display_gamme(int old_gamme) 00130 { 00131 lockGamme.lock(); 00132 if (old_gamme != gamme) 00133 { 00134 lcd.cls(); 00135 lcd.printf("Gamme = %d", gamme); 00136 pc.printf("Change gamme %d to gamme %d\n", old_gamme, gamme); 00137 old_gamme = gamme; 00138 } 00139 lockGamme.unlock(); 00140 return old_gamme; 00141 } 00142 00143 void run_display() 00144 { 00145 char old_buffer[30] = ""; 00146 int old_gamme = 0; 00147 00148 lockBufferOutput.lock(); 00149 lockGamme.lock(); 00150 lcd.printf("Gamme = %d", gamme); 00151 lockGamme.unlock(); 00152 lockBufferOutput.unlock(); 00153 while(true) 00154 { 00155 lockBufferOutput.lock(); 00156 if (strcmp(old_buffer, bufferOutput)) 00157 refresh_display_notes(old_buffer); 00158 else 00159 old_gamme = refresh_display_gamme(old_gamme); 00160 lockBufferOutput.unlock(); 00161 Thread::wait(100); 00162 } 00163 } 00164 00165 void check_state_button_gamme() 00166 { 00167 stateButtonGamme = Bus_In_Gamme & Bus_In_Gamme.mask(); 00168 } 00169 00170 void run_gamme() 00171 { 00172 while(true) 00173 { 00174 check_state_button_gamme(); 00175 if (stateButtonGamme == 0x0) 00176 { 00177 lockGamme.lock(); 00178 gamme = (gamme == 3) ? 0 : gamme + 1; 00179 lockGamme.unlock(); 00180 while (stateButtonGamme == 0x0) 00181 check_state_button_gamme(); 00182 } 00183 Thread::wait(100); 00184 } 00185 } 00186 00187 int main() 00188 { 00189 Thread music_thread; 00190 Thread display_thread; 00191 Thread gamme_thread; 00192 00193 lcd.printf("CS435 : Piano Project\n"); 00194 wait(3); 00195 lcd.cls(); 00196 00197 music_thread.start(run_music); 00198 display_thread.start(run_display); 00199 gamme_thread.start(run_gamme); 00200 while(1); 00201 }
Generated on Wed Jul 13 2022 09:23:36 by
1.7.2
