Adeline Galasso / Mbed OS Nucleo_piano_V1

Dependencies:   TextLCD

Fork of Nucleo_piano_final_clean by karine aknin

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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    run_display()
00112 {
00113     char    old_buffer[30] = "";
00114     int     old_gamme = 0;
00115     
00116     lockBufferOutput.lock();
00117     lockGamme.lock();
00118     lcd.printf("Gamme = %d", gamme);
00119     lockGamme.unlock();
00120     lockBufferOutput.unlock();
00121     while(true)
00122     {
00123         lockBufferOutput.lock();
00124         if (strcmp(old_buffer, bufferOutput))
00125         {
00126             lcd.cls();
00127             lockGamme.lock();
00128             if (strcmp(bufferOutput, ""))
00129             {
00130                 lcd.printf("%s- g[%d]", bufferOutput, gamme);
00131                 pc.printf("Play notes: %s with gamme %d\n", bufferOutput, gamme); 
00132             }
00133             else {
00134                 lcd.printf("Gamme = %d", gamme);
00135                 pc.printf("Release notes\n"); 
00136             }
00137             lockGamme.unlock();
00138             strcpy(old_buffer, bufferOutput);
00139         }
00140         else {
00141             lockGamme.lock();
00142             if (old_gamme != gamme)
00143             {
00144                 lcd.cls();
00145                 lcd.printf("Gamme = %d", gamme);
00146                 pc.printf("Change gamme %d to gamme %d\n", old_gamme, gamme); 
00147                 old_gamme = gamme;
00148             }
00149             lockGamme.unlock();
00150         }
00151         lockBufferOutput.unlock();
00152         Thread::wait(100);
00153     }
00154 }
00155 
00156 void     check_state_button_gamme()
00157 {
00158     stateButtonGamme = Bus_In_Gamme & Bus_In_Gamme.mask();
00159 }
00160 
00161 void    run_gamme()
00162 {
00163     while(true)
00164     {
00165         check_state_button_gamme();
00166         if (stateButtonGamme == 0x0)
00167         {
00168             lockGamme.lock();
00169             gamme = (gamme == 3) ? 0 : gamme + 1;
00170             lockGamme.unlock();
00171             while (stateButtonGamme == 0x0)
00172                 check_state_button_gamme();
00173         }
00174         Thread::wait(100);
00175     }
00176 }
00177 
00178 int main()
00179 {
00180     Thread  music_thread;
00181     Thread  display_thread;
00182     Thread  gamme_thread;
00183     
00184     lcd.printf("CS435 : Piano Project\n");
00185     wait(3);
00186     lcd.cls();
00187  
00188     music_thread.start(run_music);
00189     display_thread.start(run_display);
00190     gamme_thread.start(run_gamme);
00191     while(1);
00192 }