![](/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@5:824785b64822, 2018-07-03 (annotated)
- Committer:
- aknin001
- Date:
- Tue Jul 03 02:06:44 2018 +0000
- Revision:
- 5:824785b64822
- Parent:
- 3:ca0d5d72f842
- Child:
- 6:58bab17100a2
piano 8 notes, 4 octave, bug display
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 | 5:824785b64822 | 38 | int i = 0; |
aknin001 | 5:824785b64822 | 39 | int note = 0; |
aknin001 | 5:824785b64822 | 40 | |
Aliened | 3:ca0d5d72f842 | 41 | lcd.cls(); |
aknin001 | 5:824785b64822 | 42 | wait(0.000040f); |
aknin001 | 5:824785b64822 | 43 | while (str[i]) |
aknin001 | 5:824785b64822 | 44 | { |
aknin001 | 5:824785b64822 | 45 | note = str[i]; |
aknin001 | 5:824785b64822 | 46 | lcd.putc(note); |
aknin001 | 5:824785b64822 | 47 | wait(0.000040f); |
aknin001 | 5:824785b64822 | 48 | i++; |
aknin001 | 5:824785b64822 | 49 | } |
Aliened | 3:ca0d5d72f842 | 50 | } |
Aliened | 3:ca0d5d72f842 | 51 | |
aknin001 | 5:824785b64822 | 52 | //Thread thread; |
bcostm | 2:35f13b7f3659 | 53 | |
bcostm | 0:5701b41769fd | 54 | DigitalOut led1(LED1); |
bcostm | 0:5701b41769fd | 55 | |
aknin001 | 5:824785b64822 | 56 | /**void print_thread() |
bcostm | 0:5701b41769fd | 57 | { |
bcostm | 0:5701b41769fd | 58 | while (true) { |
bcostm | 2:35f13b7f3659 | 59 | wait(1); |
bcostm | 0:5701b41769fd | 60 | print_char(); |
bcostm | 0:5701b41769fd | 61 | } |
aknin001 | 5:824785b64822 | 62 | }**/ |
bcostm | 0:5701b41769fd | 63 | |
Aliened | 3:ca0d5d72f842 | 64 | void refresh_state_button() |
Aliened | 3:ca0d5d72f842 | 65 | { |
Aliened | 3:ca0d5d72f842 | 66 | printf("refresh_state_button \n"); |
Aliened | 3:ca0d5d72f842 | 67 | state_buttons = Bus_In & Bus_In.mask(); // read the bus and mask out bits not being used |
Aliened | 3:ca0d5d72f842 | 68 | printf("state_button [ %d ]\n", state_buttons); |
Aliened | 3:ca0d5d72f842 | 69 | } |
Aliened | 3:ca0d5d72f842 | 70 | |
aknin001 | 5:824785b64822 | 71 | void play_music(int notes, double frequency) |
Aliened | 3:ca0d5d72f842 | 72 | { |
Aliened | 3:ca0d5d72f842 | 73 | printf("play_music \n"); |
aknin001 | 5:824785b64822 | 74 | speaker.period(1.0 / frequency); |
aknin001 | 5:824785b64822 | 75 | while (state_buttons == notes) { |
Aliened | 3:ca0d5d72f842 | 76 | refresh_state_button(); |
Aliened | 3:ca0d5d72f842 | 77 | } |
aknin001 | 5:824785b64822 | 78 | speaker = 0; |
Aliened | 3:ca0d5d72f842 | 79 | } |
Aliened | 3:ca0d5d72f842 | 80 | |
Aliened | 3:ca0d5d72f842 | 81 | void check_buttons() |
Aliened | 3:ca0d5d72f842 | 82 | { |
aknin001 | 5:824785b64822 | 83 | double frequency = 0.0; |
aknin001 | 5:824785b64822 | 84 | int i = 0; |
aknin001 | 5:824785b64822 | 85 | char bufferOutput[30] = ""; |
aknin001 | 5:824785b64822 | 86 | |
aknin001 | 5:824785b64822 | 87 | while (i < 8) |
aknin001 | 5:824785b64822 | 88 | { |
aknin001 | 5:824785b64822 | 89 | if (!(state_buttons & noteReference[i].mask)) { |
aknin001 | 5:824785b64822 | 90 | frequency += noteReference[i].frequency * pow(2.0, gamme) + noteReference[i].add[gamme]; |
aknin001 | 5:824785b64822 | 91 | strcat(bufferOutput, noteReference[i].name); |
aknin001 | 5:824785b64822 | 92 | strcat(bufferOutput, " "); |
aknin001 | 5:824785b64822 | 93 | } |
aknin001 | 5:824785b64822 | 94 | strcat(bufferOutput, "\0"); |
aknin001 | 5:824785b64822 | 95 | i++; |
aknin001 | 5:824785b64822 | 96 | } |
aknin001 | 5:824785b64822 | 97 | if (frequency > 0) { |
aknin001 | 5:824785b64822 | 98 | speaker = 0.25; |
aknin001 | 5:824785b64822 | 99 | print_note(bufferOutput); |
aknin001 | 5:824785b64822 | 100 | play_music(state_buttons, frequency); |
aknin001 | 5:824785b64822 | 101 | lcd.cls(); |
Aliened | 3:ca0d5d72f842 | 102 | } |
Aliened | 3:ca0d5d72f842 | 103 | } |
Aliened | 3:ca0d5d72f842 | 104 | |
Aliened | 3:ca0d5d72f842 | 105 | // TODO: perios of notes with ID |
Aliened | 3:ca0d5d72f842 | 106 | // TODO: implement new 4 buttons |
Aliened | 3:ca0d5d72f842 | 107 | // TODO: link new 2 buttons + implementation |
Aliened | 3:ca0d5d72f842 | 108 | // TODO: deal with gamme |
Aliened | 3:ca0d5d72f842 | 109 | |
Aliened | 3:ca0d5d72f842 | 110 | // TODO: PB with DO RE on the screen (board crashes) |
Aliened | 3:ca0d5d72f842 | 111 | |
bcostm | 0:5701b41769fd | 112 | int main() |
bcostm | 0:5701b41769fd | 113 | { |
aknin001 | 5:824785b64822 | 114 | //lcd.printf("%s\n", "CS435 Piano"); |
aknin001 | 5:824785b64822 | 115 | //wait(3); |
aknin001 | 5:824785b64822 | 116 | speaker = 0; |
bcostm | 0:5701b41769fd | 117 | while (true) { |
Aliened | 3:ca0d5d72f842 | 118 | printf("loop\n"); |
bcostm | 0:5701b41769fd | 119 | led1 = !led1; |
Aliened | 3:ca0d5d72f842 | 120 | refresh_state_button(); |
Aliened | 3:ca0d5d72f842 | 121 | check_buttons(); |
Aliened | 3:ca0d5d72f842 | 122 | wait(0.1); |
bcostm | 0:5701b41769fd | 123 | } |
bcostm | 0:5701b41769fd | 124 | } |