![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
piano 4 octaves, 8 notes, 3 threads, 2 outputs, no bug, clean code, comments, remove old .h
Fork of Nucleo_piano_CS435 by
main.cpp@6:58bab17100a2, 2018-07-03 (annotated)
- Committer:
- aknin001
- Date:
- Tue Jul 03 03:29:59 2018 +0000
- Revision:
- 6:58bab17100a2
- Parent:
- 5:824785b64822
- Child:
- 7:d2fe1a5e79ed
piano ok 4 octave, 8 notes, bug display better
Who changed what in which revision?
User | Revision | Line number | New 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> |
aknin001 | 5:824785b64822 | 5 | #include <string.h> |
aknin001 | 5:824785b64822 | 6 | #include <math.h> |
Aliened | 3:ca0d5d72f842 | 7 | |
Aliened | 3:ca0d5d72f842 | 8 | // Define screen |
Aliened | 3:ca0d5d72f842 | 9 | TextLCD lcd(PA_8, PA_9, PC_7, PB_6, PA_7, PA_6, PA_5); // RS, RW, E, D4-D7 |
Aliened | 3:ca0d5d72f842 | 10 | |
aknin001 | 5:824785b64822 | 11 | // Define Bus In for Buttons (Do, Re, Mi, Fa, Sol, La, Si, Do) |
aknin001 | 5:824785b64822 | 12 | BusIn Bus_In(PA_0, PA_1, PA_4, PB_0, PA_10, PB_3, PB_5, PB_4); |
bcostm | 0:5701b41769fd | 13 | |
Aliened | 3:ca0d5d72f842 | 14 | // Define the PWM speaker |
Aliened | 3:ca0d5d72f842 | 15 | PwmOut speaker(PB_10); |
Aliened | 3:ca0d5d72f842 | 16 | |
Aliened | 3:ca0d5d72f842 | 17 | //Define variables for sound |
aknin001 | 5:824785b64822 | 18 | struct NoteReference { |
aknin001 | 5:824785b64822 | 19 | char name[4]; |
aknin001 | 5:824785b64822 | 20 | int mask; |
aknin001 | 5:824785b64822 | 21 | double frequency; |
aknin001 | 5:824785b64822 | 22 | double add[4]; |
aknin001 | 5:824785b64822 | 23 | }; |
aknin001 | 5:824785b64822 | 24 | NoteReference noteReference[8] = { {"Do", 0x1, 262.0, {0.0, -1.0, -1.5, -3.0}}, |
aknin001 | 5:824785b64822 | 25 | {"Re", 0x2, 294.0, {0.0, -1.0, -1.0, -3.0 }}, |
aknin001 | 5:824785b64822 | 26 | {"Mi", 0x4, 330.0, {0.0, -1.0, -1.5, -3.0}}, |
aknin001 | 5:824785b64822 | 27 | {"Fa", 0x8, 349.0, {0.0, 0.5, -1.0, 2.0}}, |
aknin001 | 5:824785b64822 | 28 | {"Sol", 0x10, 392.0, {0.0, 0.0, 0.0, 0.0}}, |
aknin001 | 5:824785b64822 | 29 | {"La", 0x20, 440.0, {0.0, 0.0, 0.0, 0.0}}, |
aknin001 | 5:824785b64822 | 30 | {"Si", 0x40, 494.0, {0.0, 0.0, -1.0, -1.0}}, |
aknin001 | 5:824785b64822 | 31 | {"Do", 0x80, 523.0, {0.0, 0.0, 0.0, 0.0}} |
aknin001 | 5:824785b64822 | 32 | }; |
aknin001 | 5:824785b64822 | 33 | volatile int state_buttons = 0; |
aknin001 | 5:824785b64822 | 34 | volatile int gamme = 0; |
Aliened | 3:ca0d5d72f842 | 35 | |
Aliened | 3:ca0d5d72f842 | 36 | void print_note(char *str) |
bcostm | 0:5701b41769fd | 37 | { |
aknin001 | 6:58bab17100a2 | 38 | // lcd.cls(); |
aknin001 | 6:58bab17100a2 | 39 | // wait(0.000040f); |
aknin001 | 6:58bab17100a2 | 40 | lcd.printf("%s", str); |
Aliened | 3:ca0d5d72f842 | 41 | } |
Aliened | 3:ca0d5d72f842 | 42 | |
bcostm | 2:35f13b7f3659 | 43 | |
bcostm | 0:5701b41769fd | 44 | DigitalOut led1(LED1); |
bcostm | 0:5701b41769fd | 45 | |
Aliened | 3:ca0d5d72f842 | 46 | void refresh_state_button() |
Aliened | 3:ca0d5d72f842 | 47 | { |
aknin001 | 6:58bab17100a2 | 48 | //printf("refresh_state_button \n"); |
Aliened | 3:ca0d5d72f842 | 49 | state_buttons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used |
aknin001 | 6:58bab17100a2 | 50 | //printf("state_button [ %d ]\n", state_buttons); |
Aliened | 3:ca0d5d72f842 | 51 | } |
Aliened | 3:ca0d5d72f842 | 52 | |
aknin001 | 5:824785b64822 | 53 | void play_music(int notes, double frequency) |
Aliened | 3:ca0d5d72f842 | 54 | { |
aknin001 | 5:824785b64822 | 55 | speaker.period(1.0 / frequency); |
aknin001 | 5:824785b64822 | 56 | while (state_buttons == notes) { |
Aliened | 3:ca0d5d72f842 | 57 | refresh_state_button(); |
Aliened | 3:ca0d5d72f842 | 58 | } |
Aliened | 3:ca0d5d72f842 | 59 | } |
Aliened | 3:ca0d5d72f842 | 60 | |
Aliened | 3:ca0d5d72f842 | 61 | void check_buttons() |
Aliened | 3:ca0d5d72f842 | 62 | { |
aknin001 | 5:824785b64822 | 63 | double frequency = 0.0; |
aknin001 | 5:824785b64822 | 64 | int i = 0; |
aknin001 | 5:824785b64822 | 65 | char bufferOutput[30] = ""; |
aknin001 | 5:824785b64822 | 66 | |
aknin001 | 6:58bab17100a2 | 67 | if (state_buttons == 0xFF) |
aknin001 | 5:824785b64822 | 68 | { |
aknin001 | 6:58bab17100a2 | 69 | speaker = 0; |
aknin001 | 6:58bab17100a2 | 70 | lcd.cls(); |
aknin001 | 6:58bab17100a2 | 71 | } |
aknin001 | 6:58bab17100a2 | 72 | else { |
aknin001 | 6:58bab17100a2 | 73 | while (i < 8) |
aknin001 | 6:58bab17100a2 | 74 | { |
aknin001 | 6:58bab17100a2 | 75 | if (!(state_buttons & noteReference[i].mask)) { |
aknin001 | 6:58bab17100a2 | 76 | frequency += noteReference[i].frequency * pow(2.0, gamme) + noteReference[i].add[gamme]; |
aknin001 | 6:58bab17100a2 | 77 | strcat(bufferOutput, noteReference[i].name); |
aknin001 | 6:58bab17100a2 | 78 | strcat(bufferOutput, " "); |
aknin001 | 6:58bab17100a2 | 79 | } |
aknin001 | 6:58bab17100a2 | 80 | i++; |
aknin001 | 5:824785b64822 | 81 | } |
aknin001 | 6:58bab17100a2 | 82 | speaker = 0.5; |
aknin001 | 5:824785b64822 | 83 | print_note(bufferOutput); |
aknin001 | 5:824785b64822 | 84 | play_music(state_buttons, frequency); |
Aliened | 3:ca0d5d72f842 | 85 | } |
Aliened | 3:ca0d5d72f842 | 86 | } |
Aliened | 3:ca0d5d72f842 | 87 | |
Aliened | 3:ca0d5d72f842 | 88 | // TODO: perios of notes with ID |
Aliened | 3:ca0d5d72f842 | 89 | // TODO: implement new 4 buttons |
Aliened | 3:ca0d5d72f842 | 90 | // TODO: link new 2 buttons + implementation |
Aliened | 3:ca0d5d72f842 | 91 | // TODO: deal with gamme |
Aliened | 3:ca0d5d72f842 | 92 | |
Aliened | 3:ca0d5d72f842 | 93 | // TODO: PB with DO RE on the screen (board crashes) |
Aliened | 3:ca0d5d72f842 | 94 | |
bcostm | 0:5701b41769fd | 95 | int main() |
bcostm | 0:5701b41769fd | 96 | { |
aknin001 | 5:824785b64822 | 97 | //lcd.printf("%s\n", "CS435 Piano"); |
aknin001 | 5:824785b64822 | 98 | //wait(3); |
aknin001 | 6:58bab17100a2 | 99 | |
bcostm | 0:5701b41769fd | 100 | while (true) { |
aknin001 | 6:58bab17100a2 | 101 | //printf("loop\n"); |
bcostm | 0:5701b41769fd | 102 | led1 = !led1; |
Aliened | 3:ca0d5d72f842 | 103 | refresh_state_button(); |
Aliened | 3:ca0d5d72f842 | 104 | check_buttons(); |
Aliened | 3:ca0d5d72f842 | 105 | wait(0.1); |
aknin001 | 6:58bab17100a2 | 106 | lcd.cls(); |
bcostm | 0:5701b41769fd | 107 | } |
bcostm | 0:5701b41769fd | 108 | } |