Nucleo piano project with base template nucleo rtos basic

Dependencies:   TextLCD_piano

Committer:
Aliened
Date:
Mon Jul 02 19:47:31 2018 +0000
Revision:
4:1f3add98272b
Parent:
3:ca0d5d72f842
Nucleo_Piano_V1

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"
Aliened 3:ca0d5d72f842 3 #include "button_value.h"
Aliened 3:ca0d5d72f842 4 #include <map>
Aliened 3:ca0d5d72f842 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
Aliened 3:ca0d5d72f842 9 // Define Bus In for Buttons (Do, Re, Mi, Fa)
Aliened 3:ca0d5d72f842 10 BusIn Bus_In(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);
Aliened 3:ca0d5d72f842 14
Aliened 3:ca0d5d72f842 15 //Define variables for sound
Aliened 3:ca0d5d72f842 16 volatile int state_buttons = 0;
Aliened 3:ca0d5d72f842 17 std::map<int, double> period;
Aliened 3:ca0d5d72f842 18
Aliened 3:ca0d5d72f842 19 void print_note(char *str)
bcostm 0:5701b41769fd 20 {
Aliened 3:ca0d5d72f842 21 lcd.cls();
Aliened 3:ca0d5d72f842 22 lcd.printf(str);
Aliened 3:ca0d5d72f842 23 }
Aliened 3:ca0d5d72f842 24
Aliened 3:ca0d5d72f842 25 void print_char()
Aliened 3:ca0d5d72f842 26 {
Aliened 3:ca0d5d72f842 27 lcd.printf("Hello world!");
bcostm 0:5701b41769fd 28 fflush(stdout);
bcostm 0:5701b41769fd 29 }
bcostm 0:5701b41769fd 30
bcostm 2:35f13b7f3659 31 Thread thread;
bcostm 2:35f13b7f3659 32
bcostm 0:5701b41769fd 33 DigitalOut led1(LED1);
bcostm 0:5701b41769fd 34
bcostm 2:35f13b7f3659 35 void print_thread()
bcostm 0:5701b41769fd 36 {
bcostm 0:5701b41769fd 37 while (true) {
bcostm 2:35f13b7f3659 38 wait(1);
bcostm 0:5701b41769fd 39 print_char();
bcostm 0:5701b41769fd 40 }
bcostm 0:5701b41769fd 41 }
bcostm 0:5701b41769fd 42
Aliened 3:ca0d5d72f842 43 void refresh_state_button()
Aliened 3:ca0d5d72f842 44 {
Aliened 3:ca0d5d72f842 45 printf("refresh_state_button \n");
Aliened 3:ca0d5d72f842 46 state_buttons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used
Aliened 3:ca0d5d72f842 47 printf("state_button [ %d ]\n", state_buttons);
Aliened 3:ca0d5d72f842 48 }
Aliened 3:ca0d5d72f842 49
Aliened 3:ca0d5d72f842 50 void play_music(int key)
Aliened 3:ca0d5d72f842 51 {
Aliened 3:ca0d5d72f842 52 printf("play_music \n");
Aliened 3:ca0d5d72f842 53 printf("key [%x], value[%f]\n", key, period[key]);
Aliened 3:ca0d5d72f842 54 speaker.period(period[key]);
Aliened 3:ca0d5d72f842 55 while (state_buttons == key) {
Aliened 3:ca0d5d72f842 56 refresh_state_button();
Aliened 3:ca0d5d72f842 57 }
Aliened 3:ca0d5d72f842 58 check_buttons();
Aliened 3:ca0d5d72f842 59 }
Aliened 3:ca0d5d72f842 60
Aliened 3:ca0d5d72f842 61 void check_buttons()
Aliened 3:ca0d5d72f842 62 {
Aliened 3:ca0d5d72f842 63 printf("check_buttons \n");
Aliened 3:ca0d5d72f842 64 // check bits set in bus
Aliened 3:ca0d5d72f842 65 switch(state_buttons) {
Aliened 3:ca0d5d72f842 66 case 0x0:
Aliened 3:ca0d5d72f842 67 printf("0b0000, All buttons pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 68 speaker = 0.25;
Aliened 3:ca0d5d72f842 69 print_note("DO RE MI FA");
Aliened 3:ca0d5d72f842 70 play_music(0x0);
Aliened 3:ca0d5d72f842 71 break;
Aliened 3:ca0d5d72f842 72 case 0x1:
Aliened 3:ca0d5d72f842 73 printf("0b0001, Button 2 & 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 74 speaker = 0.25;
Aliened 3:ca0d5d72f842 75 print_note("RE MI FA");
Aliened 3:ca0d5d72f842 76 play_music(0x1);
Aliened 3:ca0d5d72f842 77 break;
Aliened 3:ca0d5d72f842 78 case 0x2:
Aliened 3:ca0d5d72f842 79 printf("0b0010, Button 1 & 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 80 print_note("DO MI FA");
Aliened 3:ca0d5d72f842 81 speaker = 0.25;
Aliened 3:ca0d5d72f842 82 play_music(0x2);
Aliened 3:ca0d5d72f842 83 break;
Aliened 3:ca0d5d72f842 84 case 0x3:
Aliened 3:ca0d5d72f842 85 printf("0b0011, Button 3 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 86 print_note("MI FA");
Aliened 3:ca0d5d72f842 87 speaker = 0.25;
Aliened 3:ca0d5d72f842 88 play_music(0x3);
Aliened 3:ca0d5d72f842 89 break;
Aliened 3:ca0d5d72f842 90 case 0x4:
Aliened 3:ca0d5d72f842 91 printf("0b0100, Button 1 & 2 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 92 print_note("DO RE FA");
Aliened 3:ca0d5d72f842 93 speaker = 0.25;
Aliened 3:ca0d5d72f842 94 play_music(0x4);
Aliened 3:ca0d5d72f842 95 break;
Aliened 3:ca0d5d72f842 96 case 0x5:
Aliened 3:ca0d5d72f842 97 printf("0b0101, Button 2 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 98 print_note("RE FA");
Aliened 3:ca0d5d72f842 99 speaker = 0.25;
Aliened 3:ca0d5d72f842 100 play_music(0x5);
Aliened 3:ca0d5d72f842 101 break;
Aliened 3:ca0d5d72f842 102 case 0x6:
Aliened 3:ca0d5d72f842 103 printf("0b0110, Button 1 & 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 104 print_note("DO FA");
Aliened 3:ca0d5d72f842 105 speaker = 0.25;
Aliened 3:ca0d5d72f842 106 play_music(0x6);
Aliened 3:ca0d5d72f842 107 break;
Aliened 3:ca0d5d72f842 108 case 0x7:
Aliened 3:ca0d5d72f842 109 printf("0b0111, Button 4 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 110 print_note("FA");
Aliened 3:ca0d5d72f842 111 speaker = 0.5;
Aliened 3:ca0d5d72f842 112 play_music(0x7);
Aliened 3:ca0d5d72f842 113 break;
Aliened 3:ca0d5d72f842 114 case 0x8:
Aliened 3:ca0d5d72f842 115 printf("0b1000, Button 1 & 2 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 116 print_note("DO RE MI");
Aliened 3:ca0d5d72f842 117 speaker = 0.25;
Aliened 3:ca0d5d72f842 118 play_music(0x8);
Aliened 3:ca0d5d72f842 119 break;
Aliened 3:ca0d5d72f842 120 case 0x9:
Aliened 3:ca0d5d72f842 121 printf("0b1001, Button 2 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 122 print_note("RE MI");
Aliened 3:ca0d5d72f842 123 speaker = 0.25;
Aliened 3:ca0d5d72f842 124 play_music(0x9);
Aliened 3:ca0d5d72f842 125 break;
Aliened 3:ca0d5d72f842 126 case 0xA:
Aliened 3:ca0d5d72f842 127 printf("0b1010, Button 1 & 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 128 print_note("DO MI");
Aliened 3:ca0d5d72f842 129 speaker = 0.25;
Aliened 3:ca0d5d72f842 130 play_music(0xA);
Aliened 3:ca0d5d72f842 131 break;
Aliened 3:ca0d5d72f842 132 case 0xB:
Aliened 3:ca0d5d72f842 133 printf("0b1011, Button 3 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 134 print_note("MI");
Aliened 3:ca0d5d72f842 135 speaker = 0.5;
Aliened 3:ca0d5d72f842 136 play_music(0xB);
Aliened 3:ca0d5d72f842 137 break;
Aliened 3:ca0d5d72f842 138 case 0xC:
Aliened 3:ca0d5d72f842 139 printf("0b1100, Button 1 & 2 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 140 speaker = 0.25;
Aliened 3:ca0d5d72f842 141 print_note("DO RE");
Aliened 3:ca0d5d72f842 142 play_music(0xC);
Aliened 3:ca0d5d72f842 143 break;
Aliened 3:ca0d5d72f842 144 case 0xD:
Aliened 3:ca0d5d72f842 145 printf("0b1101, Button 2 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 146 print_note("RE");
Aliened 3:ca0d5d72f842 147 speaker = 0.5;
Aliened 3:ca0d5d72f842 148 play_music(0xD);
Aliened 3:ca0d5d72f842 149 break;
Aliened 3:ca0d5d72f842 150 case 0xE:
Aliened 3:ca0d5d72f842 151 printf("0b1110, Button 1 pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 152 print_note("DO");
Aliened 3:ca0d5d72f842 153 speaker = 0.5;
Aliened 3:ca0d5d72f842 154 play_music(0xE);
Aliened 3:ca0d5d72f842 155 break;
Aliened 3:ca0d5d72f842 156 case 0xF:
Aliened 3:ca0d5d72f842 157 printf("0b1111, No button pressed [%d] \n\r", Bus_In & Bus_In.mask());
Aliened 3:ca0d5d72f842 158 speaker = 0;
Aliened 3:ca0d5d72f842 159 lcd.cls();
Aliened 3:ca0d5d72f842 160 break;
Aliened 3:ca0d5d72f842 161 }
Aliened 3:ca0d5d72f842 162 }
Aliened 3:ca0d5d72f842 163
Aliened 3:ca0d5d72f842 164 // TODO: perios of notes with ID
Aliened 3:ca0d5d72f842 165 // TODO: implement new 4 buttons
Aliened 3:ca0d5d72f842 166 // TODO: link new 2 buttons + implementation
Aliened 3:ca0d5d72f842 167 // TODO: deal with gamme
Aliened 3:ca0d5d72f842 168
Aliened 3:ca0d5d72f842 169 // TODO: PB with DO RE on the screen (board crashes)
Aliened 3:ca0d5d72f842 170
bcostm 0:5701b41769fd 171 int main()
bcostm 0:5701b41769fd 172 {
Aliened 3:ca0d5d72f842 173 printf("\n\n*** CS 435 - Piano ***\n");
Aliened 3:ca0d5d72f842 174
Aliened 3:ca0d5d72f842 175 period[0xE] = 1.0 / 262.0; // DO
Aliened 3:ca0d5d72f842 176 period[0xD] = 1.0 / 294.0; // RE
Aliened 3:ca0d5d72f842 177 period[0xB] = 1.0 / 330.0; // MI
Aliened 3:ca0d5d72f842 178 period[0x7] = 1.0 / 349.0; // FA
Aliened 3:ca0d5d72f842 179 period[0xC] = 1.0 / (262.0 + 294.0); // DO + RE
Aliened 3:ca0d5d72f842 180 period[0x8] = 1.0 / (262.0 + 294.0 + 330.0)
Aliened 3:ca0d5d72f842 181 lcd.printf("CS 435 - Piano");
bcostm 2:35f13b7f3659 182
Aliened 3:ca0d5d72f842 183 //thread.start(print_thread);
bcostm 2:35f13b7f3659 184
bcostm 0:5701b41769fd 185 while (true) {
Aliened 3:ca0d5d72f842 186 printf("loop\n");
bcostm 0:5701b41769fd 187 led1 = !led1;
Aliened 3:ca0d5d72f842 188 refresh_state_button();
Aliened 3:ca0d5d72f842 189 check_buttons();
Aliened 3:ca0d5d72f842 190 wait(0.1);
bcostm 0:5701b41769fd 191 }
bcostm 0:5701b41769fd 192 }