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 by
Revision 7:d2fe1a5e79ed, committed 2018-07-04
- Comitter:
- aknin001
- Date:
- Wed Jul 04 21:17:39 2018 +0000
- Parent:
- 6:58bab17100a2
- Commit message:
- project working need clean code and comments
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Jul 03 03:29:59 2018 +0000
+++ b/main.cpp Wed Jul 04 21:17:39 2018 +0000
@@ -4,7 +4,7 @@
#include <map>
#include <string.h>
#include <math.h>
-
+
// Define screen
TextLCD lcd(PA_8, PA_9, PC_7, PB_6, PA_7, PA_6, PA_5); // RS, RW, E, D4-D7
@@ -13,7 +13,10 @@
// Define the PWM speaker
PwmOut speaker(PB_10);
-
+
+//Define Bus In for Gamme Button
+BusIn Bus_In_Gamme(PB_9);
+
//Define variables for sound
struct NoteReference {
char name[4];
@@ -21,6 +24,7 @@
double frequency;
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}},
@@ -31,78 +35,148 @@
{"Do", 0x80, 523.0, {0.0, 0.0, 0.0, 0.0}}
};
volatile int state_buttons = 0;
+volatile int state_button_gamme = 0;
volatile int gamme = 0;
-void print_note(char *str)
-{
-// lcd.cls();
- // wait(0.000040f);
- lcd.printf("%s", str);
-}
-
-DigitalOut led1(LED1);
+//Define variable for display
+char bufferOutput[30] = "";
+//Define variables for synchronization
+Mutex lockBufferOutput;
+Mutex lockGamme;
+
void refresh_state_button()
{
- //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);
+ state_buttons = 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);
+ speaker.period(1.0 / frequency);
while (state_buttons == notes) {
refresh_state_button();
}
}
-
-void check_buttons()
+
+void check_buttons(double frequency, int actualGamme)
{
- double frequency = 0.0;
- int i = 0;
- char bufferOutput[30] = "";
-
if (state_buttons == 0xFF)
{
speaker = 0;
- lcd.cls();
+ lockBufferOutput.lock();
+ strcpy(bufferOutput, "");
+ lockBufferOutput.unlock();
}
else {
- while (i < 8)
- {
+ 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, gamme) + noteReference[i].add[gamme];
+ frequency += noteReference[i].frequency * pow(2.0, (double)actualGamme) + noteReference[i].add[actualGamme];
strcat(bufferOutput, noteReference[i].name);
strcat(bufferOutput, " ");
}
- i++;
}
+ lockBufferOutput.unlock();
speaker = 0.5;
- print_note(bufferOutput);
play_music(state_buttons, frequency);
}
}
-// TODO: perios of notes with ID
-// TODO: implement new 4 buttons
-// TODO: link new 2 buttons + implementation
-// TODO: deal with gamme
+void run_music()
+{
+ double frequency = 0.0;
+ int actualGamme = 0;
+
+ while (true)
+ {
+ refresh_state_button();
+ check_buttons(frequency, actualGamme);
+ Thread::wait(100);
+ }
+}
-// TODO: PB with DO RE on the screen (board crashes)
+void run_display()
+{
+ char old_buffer[30] = "";
+ int old_gamme = 0;
+
+ lockBufferOutput.lock();
+ lockGamme.lock();
+ lcd.printf("Gamme = %d", gamme);
+ lockGamme.unlock();
+ lockBufferOutput.unlock();
+ while(true)
+ {
+ lockBufferOutput.lock();
+ if (strcmp(old_buffer, bufferOutput))
+ {
+ lcd.cls();
+ lockGamme.lock();
+ if (strcmp(bufferOutput, ""))
+ lcd.printf("%s- g[%d]", bufferOutput, gamme);
+ else
+ lcd.printf("Gamme = %d", gamme);
+ lockGamme.unlock();
+ strcpy(old_buffer, bufferOutput);
+ }
+ else {
+ lockGamme.lock();
+ if (old_gamme != gamme)
+ {
+ lcd.cls();
+ lcd.printf("Gamme = %d", gamme);
+ old_gamme = gamme;
+ }
+ lockGamme.unlock();
+ }
+ lockBufferOutput.unlock();
+ Thread::wait(100);
+ }
+}
+
+void check_state_button_gamme()
+{
+ state_button_gamme = Bus_In_Gamme & Bus_In_Gamme.mask();
+}
+
+void run_gamme()
+{
+ while(true)
+ {
+ check_state_button_gamme();
+ if (state_button_gamme == 0x0)
+ {
+ lockGamme.lock();
+ if (gamme == 3)
+ gamme = 0;
+ else
+ gamme++;
+ lockGamme.unlock();
+ while (state_button_gamme == 0x0)
+ check_state_button_gamme();
+ }
+ Thread::wait(100);
+ }
+}
int main()
{
- //lcd.printf("%s\n", "CS435 Piano");
- //wait(3);
-
- while (true) {
- //printf("loop\n");
- led1 = !led1;
- refresh_state_button();
- check_buttons();
- wait(0.1);
- lcd.cls();
- }
-}
+ Thread music_thread;
+ Thread display_thread;
+ Thread gamme_thread;
+
+ lcd.printf("CS435 : Piano Project\n");
+ wait(3);
+ lcd.cls();
+
+ music_thread.start(run_music);
+ display_thread.start(run_display);
+ gamme_thread.start(run_gamme);
+ while(1);
+}
\ No newline at end of file
