Nucleo piano project with base template nucleo rtos basic
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include "button_value.h" 00004 #include <map> 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) 00010 BusIn Bus_In(PA_10, PB_3, PB_5, PB_4); 00011 00012 // Define the PWM speaker 00013 PwmOut speaker(PB_10); 00014 00015 //Define variables for sound 00016 volatile int state_buttons = 0; 00017 std::map<int, double> period; 00018 00019 void print_note(char *str) 00020 { 00021 lcd.cls(); 00022 lcd.printf(str); 00023 } 00024 00025 void print_char() 00026 { 00027 lcd.printf("Hello world!"); 00028 fflush(stdout); 00029 } 00030 00031 Thread thread; 00032 00033 DigitalOut led1(LED1); 00034 00035 void print_thread() 00036 { 00037 while (true) { 00038 wait(1); 00039 print_char(); 00040 } 00041 } 00042 00043 void refresh_state_button() 00044 { 00045 printf("refresh_state_button \n"); 00046 state_buttons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used 00047 printf("state_button [ %d ]\n", state_buttons); 00048 } 00049 00050 void play_music(int key) 00051 { 00052 printf("play_music \n"); 00053 printf("key [%x], value[%f]\n", key, period[key]); 00054 speaker.period(period[key]); 00055 while (state_buttons == key) { 00056 refresh_state_button(); 00057 } 00058 check_buttons(); 00059 } 00060 00061 void check_buttons() 00062 { 00063 printf("check_buttons \n"); 00064 // check bits set in bus 00065 switch(state_buttons) { 00066 case 0x0: 00067 printf("0b0000, All buttons pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00068 speaker = 0.25; 00069 print_note("DO RE MI FA"); 00070 play_music(0x0); 00071 break; 00072 case 0x1: 00073 printf("0b0001, Button 2 & 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00074 speaker = 0.25; 00075 print_note("RE MI FA"); 00076 play_music(0x1); 00077 break; 00078 case 0x2: 00079 printf("0b0010, Button 1 & 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00080 print_note("DO MI FA"); 00081 speaker = 0.25; 00082 play_music(0x2); 00083 break; 00084 case 0x3: 00085 printf("0b0011, Button 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00086 print_note("MI FA"); 00087 speaker = 0.25; 00088 play_music(0x3); 00089 break; 00090 case 0x4: 00091 printf("0b0100, Button 1 & 2 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00092 print_note("DO RE FA"); 00093 speaker = 0.25; 00094 play_music(0x4); 00095 break; 00096 case 0x5: 00097 printf("0b0101, Button 2 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00098 print_note("RE FA"); 00099 speaker = 0.25; 00100 play_music(0x5); 00101 break; 00102 case 0x6: 00103 printf("0b0110, Button 1 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00104 print_note("DO FA"); 00105 speaker = 0.25; 00106 play_music(0x6); 00107 break; 00108 case 0x7: 00109 printf("0b0111, Button 4 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00110 print_note("FA"); 00111 speaker = 0.5; 00112 play_music(0x7); 00113 break; 00114 case 0x8: 00115 printf("0b1000, Button 1 & 2 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00116 print_note("DO RE MI"); 00117 speaker = 0.25; 00118 play_music(0x8); 00119 break; 00120 case 0x9: 00121 printf("0b1001, Button 2 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00122 print_note("RE MI"); 00123 speaker = 0.25; 00124 play_music(0x9); 00125 break; 00126 case 0xA: 00127 printf("0b1010, Button 1 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00128 print_note("DO MI"); 00129 speaker = 0.25; 00130 play_music(0xA); 00131 break; 00132 case 0xB: 00133 printf("0b1011, Button 3 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00134 print_note("MI"); 00135 speaker = 0.5; 00136 play_music(0xB); 00137 break; 00138 case 0xC: 00139 printf("0b1100, Button 1 & 2 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00140 speaker = 0.25; 00141 print_note("DO RE"); 00142 play_music(0xC); 00143 break; 00144 case 0xD: 00145 printf("0b1101, Button 2 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00146 print_note("RE"); 00147 speaker = 0.5; 00148 play_music(0xD); 00149 break; 00150 case 0xE: 00151 printf("0b1110, Button 1 pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00152 print_note("DO"); 00153 speaker = 0.5; 00154 play_music(0xE); 00155 break; 00156 case 0xF: 00157 printf("0b1111, No button pressed [%d] \n\r", Bus_In & Bus_In.mask()); 00158 speaker = 0; 00159 lcd.cls(); 00160 break; 00161 } 00162 } 00163 00164 // TODO: perios of notes with ID 00165 // TODO: implement new 4 buttons 00166 // TODO: link new 2 buttons + implementation 00167 // TODO: deal with gamme 00168 00169 // TODO: PB with DO RE on the screen (board crashes) 00170 00171 int main() 00172 { 00173 printf("\n\n*** CS 435 - Piano ***\n"); 00174 00175 period[0xE] = 1.0 / 262.0; // DO 00176 period[0xD] = 1.0 / 294.0; // RE 00177 period[0xB] = 1.0 / 330.0; // MI 00178 period[0x7] = 1.0 / 349.0; // FA 00179 period[0xC] = 1.0 / (262.0 + 294.0); // DO + RE 00180 period[0x8] = 1.0 / (262.0 + 294.0 + 330.0) 00181 lcd.printf("CS 435 - Piano"); 00182 00183 //thread.start(print_thread); 00184 00185 while (true) { 00186 printf("loop\n"); 00187 led1 = !led1; 00188 refresh_state_button(); 00189 check_buttons(); 00190 wait(0.1); 00191 } 00192 }
Generated on Wed Jul 13 2022 01:05:40 by
1.7.2