Refacto function run_display Add call to function refresh_display_notes Add call to function refresh_display_gamme

Dependencies:   TextLCD

Fork of Nucleo_piano_V1 by Adeline Galasso

Committer:
Aliened
Date:
Wed Jul 04 23:06:52 2018 +0000
Revision:
10:09b4203b5ae9
Parent:
9:653ee515b0ad
Refacto function run_display; Add call to refresh_display_notes; Add call to refresh_display_gamme

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
Aliened 10:09b4203b5ae9 111 void refresh_display_notes(char *old_buffer)
Aliened 10:09b4203b5ae9 112 {
Aliened 10:09b4203b5ae9 113 lcd.cls();
Aliened 10:09b4203b5ae9 114 lockGamme.lock();
Aliened 10:09b4203b5ae9 115 if (strcmp(bufferOutput, ""))
Aliened 10:09b4203b5ae9 116 {
Aliened 10:09b4203b5ae9 117 lcd.printf("%s- g[%d]", bufferOutput, gamme);
Aliened 10:09b4203b5ae9 118 pc.printf("Play notes: %s with gamme %d\n", bufferOutput, gamme);
Aliened 10:09b4203b5ae9 119 }
Aliened 10:09b4203b5ae9 120 else
Aliened 10:09b4203b5ae9 121 {
Aliened 10:09b4203b5ae9 122 lcd.printf("Gamme = %d", gamme);
Aliened 10:09b4203b5ae9 123 pc.printf("Release notes\n");
Aliened 10:09b4203b5ae9 124 }
Aliened 10:09b4203b5ae9 125 lockGamme.unlock();
Aliened 10:09b4203b5ae9 126 strcpy(old_buffer, bufferOutput);
Aliened 10:09b4203b5ae9 127 }
Aliened 10:09b4203b5ae9 128
Aliened 10:09b4203b5ae9 129 int refresh_display_gamme(int old_gamme)
Aliened 10:09b4203b5ae9 130 {
Aliened 10:09b4203b5ae9 131 lockGamme.lock();
Aliened 10:09b4203b5ae9 132 if (old_gamme != gamme)
Aliened 10:09b4203b5ae9 133 {
Aliened 10:09b4203b5ae9 134 lcd.cls();
Aliened 10:09b4203b5ae9 135 lcd.printf("Gamme = %d", gamme);
Aliened 10:09b4203b5ae9 136 pc.printf("Change gamme %d to gamme %d\n", old_gamme, gamme);
Aliened 10:09b4203b5ae9 137 old_gamme = gamme;
Aliened 10:09b4203b5ae9 138 }
Aliened 10:09b4203b5ae9 139 lockGamme.unlock();
Aliened 10:09b4203b5ae9 140 return old_gamme;
Aliened 10:09b4203b5ae9 141 }
Aliened 10:09b4203b5ae9 142
aknin001 7:d2fe1a5e79ed 143 void run_display()
aknin001 7:d2fe1a5e79ed 144 {
aknin001 7:d2fe1a5e79ed 145 char old_buffer[30] = "";
aknin001 8:781b03221397 146 int old_gamme = 0;
aknin001 7:d2fe1a5e79ed 147
aknin001 7:d2fe1a5e79ed 148 lockBufferOutput.lock();
aknin001 7:d2fe1a5e79ed 149 lockGamme.lock();
aknin001 7:d2fe1a5e79ed 150 lcd.printf("Gamme = %d", gamme);
aknin001 7:d2fe1a5e79ed 151 lockGamme.unlock();
aknin001 7:d2fe1a5e79ed 152 lockBufferOutput.unlock();
aknin001 7:d2fe1a5e79ed 153 while(true)
aknin001 7:d2fe1a5e79ed 154 {
aknin001 7:d2fe1a5e79ed 155 lockBufferOutput.lock();
aknin001 7:d2fe1a5e79ed 156 if (strcmp(old_buffer, bufferOutput))
Aliened 10:09b4203b5ae9 157 refresh_display_notes(old_buffer);
Aliened 10:09b4203b5ae9 158 else
Aliened 10:09b4203b5ae9 159 old_gamme = refresh_display_gamme(old_gamme);
aknin001 7:d2fe1a5e79ed 160 lockBufferOutput.unlock();
aknin001 7:d2fe1a5e79ed 161 Thread::wait(100);
aknin001 7:d2fe1a5e79ed 162 }
aknin001 7:d2fe1a5e79ed 163 }
aknin001 7:d2fe1a5e79ed 164
aknin001 7:d2fe1a5e79ed 165 void check_state_button_gamme()
aknin001 7:d2fe1a5e79ed 166 {
aknin001 8:781b03221397 167 stateButtonGamme = Bus_In_Gamme & Bus_In_Gamme.mask();
aknin001 7:d2fe1a5e79ed 168 }
aknin001 7:d2fe1a5e79ed 169
aknin001 7:d2fe1a5e79ed 170 void run_gamme()
aknin001 7:d2fe1a5e79ed 171 {
aknin001 7:d2fe1a5e79ed 172 while(true)
aknin001 7:d2fe1a5e79ed 173 {
aknin001 7:d2fe1a5e79ed 174 check_state_button_gamme();
aknin001 8:781b03221397 175 if (stateButtonGamme == 0x0)
aknin001 7:d2fe1a5e79ed 176 {
aknin001 7:d2fe1a5e79ed 177 lockGamme.lock();
aknin001 8:781b03221397 178 gamme = (gamme == 3) ? 0 : gamme + 1;
aknin001 7:d2fe1a5e79ed 179 lockGamme.unlock();
aknin001 8:781b03221397 180 while (stateButtonGamme == 0x0)
aknin001 7:d2fe1a5e79ed 181 check_state_button_gamme();
aknin001 7:d2fe1a5e79ed 182 }
aknin001 7:d2fe1a5e79ed 183 Thread::wait(100);
aknin001 7:d2fe1a5e79ed 184 }
aknin001 7:d2fe1a5e79ed 185 }
Aliened 3:ca0d5d72f842 186
bcostm 0:5701b41769fd 187 int main()
bcostm 0:5701b41769fd 188 {
aknin001 7:d2fe1a5e79ed 189 Thread music_thread;
aknin001 7:d2fe1a5e79ed 190 Thread display_thread;
aknin001 7:d2fe1a5e79ed 191 Thread gamme_thread;
aknin001 7:d2fe1a5e79ed 192
aknin001 7:d2fe1a5e79ed 193 lcd.printf("CS435 : Piano Project\n");
aknin001 7:d2fe1a5e79ed 194 wait(3);
aknin001 7:d2fe1a5e79ed 195 lcd.cls();
aknin001 7:d2fe1a5e79ed 196
aknin001 7:d2fe1a5e79ed 197 music_thread.start(run_music);
aknin001 7:d2fe1a5e79ed 198 display_thread.start(run_display);
aknin001 7:d2fe1a5e79ed 199 gamme_thread.start(run_gamme);
aknin001 7:d2fe1a5e79ed 200 while(1);
aknin001 7:d2fe1a5e79ed 201 }