piano 4 octaves, 8 notes, 3 threads, 2 outputs, no bug, clean code, comments, remove old .h

Dependencies:   TextLCD

Fork of Nucleo_piano_CS435 by karine aknin

Revision:
5:824785b64822
Parent:
3:ca0d5d72f842
Child:
6:58bab17100a2
--- a/main.cpp	Mon Jul 02 19:47:31 2018 +0000
+++ b/main.cpp	Tue Jul 03 02:06:44 2018 +0000
@@ -2,43 +2,64 @@
 #include "TextLCD.h"
 #include "button_value.h"
 #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
  
-// Define Bus In for Buttons (Do, Re, Mi, Fa)
-BusIn Bus_In(PA_10, PB_3, PB_5, PB_4);
+// Define Bus In for Buttons (Do, Re, Mi, Fa, Sol, La, Si, Do)
+BusIn Bus_In(PA_0, PA_1, PA_4, PB_0, PA_10, PB_3, PB_5, PB_4);
 
 // Define the PWM speaker
 PwmOut speaker(PB_10);
     
 //Define variables for sound
-volatile int state_buttons = 0;
-std::map<int, double> period;
+struct  NoteReference {
+    char    name[4];
+    int     mask;
+    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}},
+                                                {"Fa", 0x8, 349.0, {0.0, 0.5, -1.0, 2.0}},
+                                                {"Sol", 0x10, 392.0, {0.0, 0.0, 0.0, 0.0}},
+                                                {"La", 0x20, 440.0, {0.0, 0.0, 0.0, 0.0}},
+                                                {"Si", 0x40, 494.0, {0.0, 0.0, -1.0, -1.0}},
+                                                {"Do", 0x80, 523.0, {0.0, 0.0, 0.0, 0.0}}
+                                            };
+volatile int    state_buttons = 0;
+volatile int    gamme = 0;
 
 void print_note(char *str)
 {
+    int i = 0;
+    int note = 0;
+    
     lcd.cls();
-    lcd.printf(str);
+    wait(0.000040f);
+    while (str[i])
+    {   
+        note = str[i];    
+        lcd.putc(note);
+        wait(0.000040f);
+        i++;
+    }
 }
 
-void print_char()
-{
-    lcd.printf("Hello world!");
-    fflush(stdout);
-}
-
-Thread thread;
+//Thread thread;
 
 DigitalOut led1(LED1);
 
-void print_thread()
+/**void print_thread()
 {
     while (true) {
         wait(1);
         print_char();
     }
-}
+}**/
 
 void    refresh_state_button()
 {
@@ -47,117 +68,37 @@
    printf("state_button [ %d ]\n", state_buttons);
 }
 
-void        play_music(int key)
+void        play_music(int notes, double frequency)
 {
    printf("play_music \n");
-   printf("key [%x], value[%f]\n", key, period[key]);
-   speaker.period(period[key]);            
-   while (state_buttons == key) {
+   speaker.period(1.0 / frequency);            
+   while (state_buttons == notes) {
      refresh_state_button();
    }
-   check_buttons();
+   speaker = 0;
 }
 
 void        check_buttons()
 {
-   printf("check_buttons \n");
-    // check bits set in bus
-    switch(state_buttons) { 
-        case 0x0: 
-            printf("0b0000, All buttons pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            speaker = 0.25;
-            print_note("DO RE MI FA");
-            play_music(0x0);
-            break;
-        case 0x1: 
-            printf("0b0001, Button 2 & 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            speaker = 0.25;
-            print_note("RE MI FA");
-            play_music(0x1);
-            break;
-        case 0x2: 
-            printf("0b0010, Button 1 & 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("DO MI FA");
-            speaker = 0.25;
-            play_music(0x2);
-            break; 
-        case 0x3: 
-            printf("0b0011, Button 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("MI FA");
-            speaker = 0.25;
-            play_music(0x3);
-            break;
-        case 0x4: 
-            printf("0b0100, Button 1 & 2 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("DO RE FA");
-            speaker = 0.25;
-            play_music(0x4);
-            break;
-        case 0x5: 
-            printf("0b0101, Button 2 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("RE FA");
-            speaker = 0.25;
-            play_music(0x5);
-            break;
-        case 0x6: 
-            printf("0b0110, Button 1 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("DO FA");
-            speaker = 0.25;
-            play_music(0x6);
-            break;
-        case 0x7: 
-            printf("0b0111, Button 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("FA");
-            speaker = 0.5;
-            play_music(0x7);
-            break;
-        case 0x8: 
-            printf("0b1000, Button 1 & 2 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("DO RE MI");
-            speaker = 0.25;
-            play_music(0x8);
-            break; 
-        case 0x9: 
-            printf("0b1001, Button 2 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("RE MI");
-            speaker = 0.25;
-            play_music(0x9);
-            break; 
-        case 0xA: 
-            printf("0b1010, Button 1 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("DO MI");
-            speaker = 0.25;
-            play_music(0xA);
-            break;
-        case 0xB: 
-            printf("0b1011, Button 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("MI");
-            speaker = 0.5;
-            play_music(0xB);
-            break;
-        case 0xC: 
-            printf("0b1100, Button 1 & 2 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            speaker = 0.25;
-            print_note("DO RE");
-            play_music(0xC);
-            break; 
-        case 0xD: 
-            printf("0b1101, Button 2 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("RE");
-            speaker = 0.5;
-            play_music(0xD);
-            break; 
-        case 0xE: 
-            printf("0b1110, Button 1 pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            print_note("DO");
-            speaker = 0.5;
-            play_music(0xE);
-            break; 
-        case 0xF: 
-            printf("0b1111, No button pressed [%d] \n\r", Bus_In & Bus_In.mask());
-            speaker = 0;
-            lcd.cls();
-            break;
+    double   frequency = 0.0;
+    int      i = 0;
+    char     bufferOutput[30] = "";
+    
+    while (i < 8)
+    {
+        if (!(state_buttons & noteReference[i].mask)) {
+            frequency += noteReference[i].frequency * pow(2.0, gamme) + noteReference[i].add[gamme];
+            strcat(bufferOutput, noteReference[i].name);
+            strcat(bufferOutput, " ");
+        }
+        strcat(bufferOutput, "\0");
+        i++;
+    }
+    if (frequency > 0) {
+        speaker = 0.25;
+        print_note(bufferOutput);
+        play_music(state_buttons, frequency);
+        lcd.cls();
     }
 }
 
@@ -170,18 +111,9 @@
 
 int main()
 {
-    printf("\n\n*** CS 435 - Piano ***\n");
-    
-    period[0xE] = 1.0 / 262.0; // DO
-    period[0xD] = 1.0 / 294.0; // RE
-    period[0xB] = 1.0 / 330.0; // MI
-    period[0x7] = 1.0 / 349.0; // FA
-    period[0xC] = 1.0 / (262.0 + 294.0); // DO + RE
-    period[0x8] = 1.0 / (262.0 + 294.0 + 330.0) 
-    lcd.printf("CS 435 - Piano");
-
-    //thread.start(print_thread);
-
+    //lcd.printf("%s\n", "CS435 Piano");
+    //wait(3);
+    speaker = 0;
     while (true) {
         printf("loop\n");
         led1 = !led1;