Add Serial UART communication to display action messages (Play notes X, release notes, change gamme X to gamme Y)

Dependencies:   TextLCD

Fork of Nucleo_piano_final_clean by karine aknin

Committer:
Aliened
Date:
Wed Jul 04 22:17:46 2018 +0000
Revision:
9:653ee515b0ad
Parent:
8:781b03221397
Add Serial UART communication to print actions messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 0:5701b41769fd 1 #include "mbed.h"
Aliened 3:ca0d5d72f842 2 #include "TextLCD.h"
aknin001 5:824785b64822 3 #include <string.h>
aknin001 5:824785b64822 4 #include <math.h>
aknin001 7:d2fe1a5e79ed 5
Aliened 3:ca0d5d72f842 6 // Define screen
Aliened 3:ca0d5d72f842 7 TextLCD lcd(PA_8, PA_9, PC_7, PB_6, PA_7, PA_6, PA_5); // RS, RW, E, D4-D7
Aliened 3:ca0d5d72f842 8
aknin001 5:824785b64822 9 // Define Bus In for Buttons (Do, Re, Mi, Fa, Sol, La, Si, Do)
aknin001 5:824785b64822 10 BusIn Bus_In(PA_0, PA_1, PA_4, PB_0, PA_10, PB_3, PB_5, PB_4);
bcostm 0:5701b41769fd 11
Aliened 3:ca0d5d72f842 12 // Define the PWM speaker
Aliened 3:ca0d5d72f842 13 PwmOut speaker(PB_10);
aknin001 7:d2fe1a5e79ed 14
Aliened 9:653ee515b0ad 15 // Define Bus In for Gamme Button
aknin001 7:d2fe1a5e79ed 16 BusIn Bus_In_Gamme(PB_9);
Aliened 9:653ee515b0ad 17
Aliened 9:653ee515b0ad 18 // Define Serial
Aliened 9:653ee515b0ad 19 Serial pc(PA_2, PA_3);
aknin001 7:d2fe1a5e79ed 20
Aliened 3:ca0d5d72f842 21 //Define variables for sound
aknin001 5:824785b64822 22 struct NoteReference {
aknin001 5:824785b64822 23 char name[4];
aknin001 5:824785b64822 24 int mask;
aknin001 5:824785b64822 25 double frequency;
aknin001 5:824785b64822 26 double add[4];
aknin001 5:824785b64822 27 };
aknin001 7:d2fe1a5e79ed 28
aknin001 8:781b03221397 29 NoteReference noteReference[8] = { {"Do", 0x1, 262.0, {0.0, -1.0, -1.5, -3.0}},
aknin001 8:781b03221397 30 {"Re", 0x2, 294.0, {0.0, -1.0, -1.0, -3.0 }},
aknin001 8:781b03221397 31 {"Mi", 0x4, 330.0, {0.0, -1.0, -1.5, -3.0}},
aknin001 8:781b03221397 32 {"Fa", 0x8, 349.0, {0.0, 0.5, -1.0, 2.0}},
aknin001 8:781b03221397 33 {"Sol", 0x10, 392.0, {0.0, 0.0, 0.0, 0.0}},
aknin001 8:781b03221397 34 {"La", 0x20, 440.0, {0.0, 0.0, 0.0, 0.0}},
aknin001 8:781b03221397 35 {"Si", 0x40, 494.0, {0.0, 0.0, -1.0, -1.0}},
aknin001 8:781b03221397 36 {"Do", 0x80, 523.0, {0.0, 0.0, 0.0, 0.0}}
aknin001 8:781b03221397 37 };
aknin001 8:781b03221397 38 volatile int stateButtons = 0;
aknin001 8:781b03221397 39 volatile int stateButtonGamme = 0;
aknin001 5:824785b64822 40 volatile int gamme = 0;
Aliened 3:ca0d5d72f842 41
aknin001 7:d2fe1a5e79ed 42 //Define variable for display
aknin001 7:d2fe1a5e79ed 43 char bufferOutput[30] = "";
bcostm 0:5701b41769fd 44
aknin001 7:d2fe1a5e79ed 45 //Define variables for synchronization
aknin001 7:d2fe1a5e79ed 46 Mutex lockBufferOutput;
aknin001 7:d2fe1a5e79ed 47 Mutex lockGamme;
aknin001 7:d2fe1a5e79ed 48
Aliened 3:ca0d5d72f842 49 void refresh_state_button()
Aliened 3:ca0d5d72f842 50 {
aknin001 8:781b03221397 51 stateButtons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used
Aliened 3:ca0d5d72f842 52 }
aknin001 7:d2fe1a5e79ed 53
aknin001 5:824785b64822 54 void play_music(int notes, double frequency)
Aliened 3:ca0d5d72f842 55 {
aknin001 7:d2fe1a5e79ed 56 speaker.period(1.0 / frequency);
aknin001 8:781b03221397 57 while (stateButtons == notes) {
Aliened 3:ca0d5d72f842 58 refresh_state_button();
Aliened 3:ca0d5d72f842 59 }
Aliened 3:ca0d5d72f842 60 }
aknin001 8:781b03221397 61
aknin001 8:781b03221397 62 double generate_frequency(double frequency, int actualGamme)
aknin001 8:781b03221397 63 {
aknin001 8:781b03221397 64 frequency = 0.0;
aknin001 8:781b03221397 65 lockBufferOutput.lock();
aknin001 8:781b03221397 66 strcpy(bufferOutput, "");
aknin001 8:781b03221397 67 for (int i = 0; i < 8; i++) {
aknin001 8:781b03221397 68 if (!(stateButtons & noteReference[i].mask))
aknin001 8:781b03221397 69 {
aknin001 8:781b03221397 70 frequency += noteReference[i].frequency * pow(2.0, (double)actualGamme) + noteReference[i].add[actualGamme];
aknin001 8:781b03221397 71 strcat(bufferOutput, noteReference[i].name);
aknin001 8:781b03221397 72 strcat(bufferOutput, " ");
aknin001 8:781b03221397 73 }
aknin001 8:781b03221397 74 }
aknin001 8:781b03221397 75 lockBufferOutput.unlock();
aknin001 8:781b03221397 76 return (frequency);
aknin001 8:781b03221397 77 }
aknin001 7:d2fe1a5e79ed 78
aknin001 7:d2fe1a5e79ed 79 void check_buttons(double frequency, int actualGamme)
Aliened 3:ca0d5d72f842 80 {
aknin001 8:781b03221397 81 if (stateButtons == 0xFF)
aknin001 5:824785b64822 82 {
aknin001 6:58bab17100a2 83 speaker = 0;
aknin001 7:d2fe1a5e79ed 84 lockBufferOutput.lock();
aknin001 7:d2fe1a5e79ed 85 strcpy(bufferOutput, "");
aknin001 7:d2fe1a5e79ed 86 lockBufferOutput.unlock();
aknin001 6:58bab17100a2 87 }
aknin001 6:58bab17100a2 88 else {
aknin001 7:d2fe1a5e79ed 89 lockGamme.lock();
aknin001 7:d2fe1a5e79ed 90 actualGamme = gamme;
aknin001 7:d2fe1a5e79ed 91 lockGamme.unlock();
aknin001 8:781b03221397 92 frequency = generate_frequency(frequency, actualGamme);
aknin001 6:58bab17100a2 93 speaker = 0.5;
aknin001 8:781b03221397 94 play_music(stateButtons, frequency);
Aliened 3:ca0d5d72f842 95 }
Aliened 3:ca0d5d72f842 96 }
Aliened 3:ca0d5d72f842 97
aknin001 7:d2fe1a5e79ed 98 void run_music()
aknin001 7:d2fe1a5e79ed 99 {
aknin001 7:d2fe1a5e79ed 100 double frequency = 0.0;
aknin001 7:d2fe1a5e79ed 101 int actualGamme = 0;
aknin001 7:d2fe1a5e79ed 102
aknin001 7:d2fe1a5e79ed 103 while (true)
aknin001 7:d2fe1a5e79ed 104 {
aknin001 7:d2fe1a5e79ed 105 refresh_state_button();
aknin001 7:d2fe1a5e79ed 106 check_buttons(frequency, actualGamme);
aknin001 7:d2fe1a5e79ed 107 Thread::wait(100);
aknin001 7:d2fe1a5e79ed 108 }
aknin001 7:d2fe1a5e79ed 109 }
Aliened 3:ca0d5d72f842 110
aknin001 7:d2fe1a5e79ed 111 void run_display()
aknin001 7:d2fe1a5e79ed 112 {
aknin001 7:d2fe1a5e79ed 113 char old_buffer[30] = "";
aknin001 8:781b03221397 114 int old_gamme = 0;
aknin001 7:d2fe1a5e79ed 115
aknin001 7:d2fe1a5e79ed 116 lockBufferOutput.lock();
aknin001 7:d2fe1a5e79ed 117 lockGamme.lock();
aknin001 7:d2fe1a5e79ed 118 lcd.printf("Gamme = %d", gamme);
aknin001 7:d2fe1a5e79ed 119 lockGamme.unlock();
aknin001 7:d2fe1a5e79ed 120 lockBufferOutput.unlock();
aknin001 7:d2fe1a5e79ed 121 while(true)
aknin001 7:d2fe1a5e79ed 122 {
aknin001 7:d2fe1a5e79ed 123 lockBufferOutput.lock();
aknin001 7:d2fe1a5e79ed 124 if (strcmp(old_buffer, bufferOutput))
aknin001 7:d2fe1a5e79ed 125 {
aknin001 7:d2fe1a5e79ed 126 lcd.cls();
aknin001 7:d2fe1a5e79ed 127 lockGamme.lock();
aknin001 7:d2fe1a5e79ed 128 if (strcmp(bufferOutput, ""))
Aliened 9:653ee515b0ad 129 {
aknin001 7:d2fe1a5e79ed 130 lcd.printf("%s- g[%d]", bufferOutput, gamme);
Aliened 9:653ee515b0ad 131 pc.printf("Play notes: %s with gamme %d\n", bufferOutput, gamme);
Aliened 9:653ee515b0ad 132 }
Aliened 9:653ee515b0ad 133 else {
aknin001 7:d2fe1a5e79ed 134 lcd.printf("Gamme = %d", gamme);
Aliened 9:653ee515b0ad 135 pc.printf("Release notes\n");
Aliened 9:653ee515b0ad 136 }
aknin001 7:d2fe1a5e79ed 137 lockGamme.unlock();
aknin001 7:d2fe1a5e79ed 138 strcpy(old_buffer, bufferOutput);
aknin001 7:d2fe1a5e79ed 139 }
aknin001 7:d2fe1a5e79ed 140 else {
aknin001 7:d2fe1a5e79ed 141 lockGamme.lock();
aknin001 7:d2fe1a5e79ed 142 if (old_gamme != gamme)
aknin001 7:d2fe1a5e79ed 143 {
aknin001 7:d2fe1a5e79ed 144 lcd.cls();
aknin001 7:d2fe1a5e79ed 145 lcd.printf("Gamme = %d", gamme);
Aliened 9:653ee515b0ad 146 pc.printf("Change gamme %d to gamme %d\n", old_gamme, gamme);
aknin001 7:d2fe1a5e79ed 147 old_gamme = gamme;
aknin001 7:d2fe1a5e79ed 148 }
aknin001 7:d2fe1a5e79ed 149 lockGamme.unlock();
aknin001 7:d2fe1a5e79ed 150 }
aknin001 7:d2fe1a5e79ed 151 lockBufferOutput.unlock();
aknin001 7:d2fe1a5e79ed 152 Thread::wait(100);
aknin001 7:d2fe1a5e79ed 153 }
aknin001 7:d2fe1a5e79ed 154 }
aknin001 7:d2fe1a5e79ed 155
aknin001 7:d2fe1a5e79ed 156 void check_state_button_gamme()
aknin001 7:d2fe1a5e79ed 157 {
aknin001 8:781b03221397 158 stateButtonGamme = Bus_In_Gamme & Bus_In_Gamme.mask();
aknin001 7:d2fe1a5e79ed 159 }
aknin001 7:d2fe1a5e79ed 160
aknin001 7:d2fe1a5e79ed 161 void run_gamme()
aknin001 7:d2fe1a5e79ed 162 {
aknin001 7:d2fe1a5e79ed 163 while(true)
aknin001 7:d2fe1a5e79ed 164 {
aknin001 7:d2fe1a5e79ed 165 check_state_button_gamme();
aknin001 8:781b03221397 166 if (stateButtonGamme == 0x0)
aknin001 7:d2fe1a5e79ed 167 {
aknin001 7:d2fe1a5e79ed 168 lockGamme.lock();
aknin001 8:781b03221397 169 gamme = (gamme == 3) ? 0 : gamme + 1;
aknin001 7:d2fe1a5e79ed 170 lockGamme.unlock();
aknin001 8:781b03221397 171 while (stateButtonGamme == 0x0)
aknin001 7:d2fe1a5e79ed 172 check_state_button_gamme();
aknin001 7:d2fe1a5e79ed 173 }
aknin001 7:d2fe1a5e79ed 174 Thread::wait(100);
aknin001 7:d2fe1a5e79ed 175 }
aknin001 7:d2fe1a5e79ed 176 }
Aliened 3:ca0d5d72f842 177
bcostm 0:5701b41769fd 178 int main()
bcostm 0:5701b41769fd 179 {
aknin001 7:d2fe1a5e79ed 180 Thread music_thread;
aknin001 7:d2fe1a5e79ed 181 Thread display_thread;
aknin001 7:d2fe1a5e79ed 182 Thread gamme_thread;
aknin001 7:d2fe1a5e79ed 183
aknin001 7:d2fe1a5e79ed 184 lcd.printf("CS435 : Piano Project\n");
aknin001 7:d2fe1a5e79ed 185 wait(3);
aknin001 7:d2fe1a5e79ed 186 lcd.cls();
aknin001 7:d2fe1a5e79ed 187
aknin001 7:d2fe1a5e79ed 188 music_thread.start(run_music);
aknin001 7:d2fe1a5e79ed 189 display_thread.start(run_display);
aknin001 7:d2fe1a5e79ed 190 gamme_thread.start(run_gamme);
aknin001 7:d2fe1a5e79ed 191 while(1);
aknin001 7:d2fe1a5e79ed 192 }