a MIDI-to-Morse code application.
Dependencies: MIDI REnc button mbed morse
main.cpp@12:9d6544cefd81, 2014-12-06 (annotated)
- Committer:
- ChuckTimber
- Date:
- Sat Dec 06 00:00:58 2014 +0000
- Revision:
- 12:9d6544cefd81
- Parent:
- 11:51fdd053c7c2
Analog Synth builders summit 2014 version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ChuckTimber | 0:d0ebbde59857 | 1 | #include "mbed.h" |
ChuckTimber | 5:decb31e2efa4 | 2 | #include "button.h" |
ChuckTimber | 9:09fc839da416 | 3 | #include "REnc.h" |
ChuckTimber | 6:7405805be717 | 4 | #include "morse.h" |
ChuckTimber | 8:96f4614a46ba | 5 | #include "MIDI.h" |
ChuckTimber | 0:d0ebbde59857 | 6 | |
ChuckTimber | 9:09fc839da416 | 7 | // alphabet 26-charactor + 1-space |
ChuckTimber | 3:993b88218184 | 8 | char* codes[] = { ".-", "-...", "-.-.", "-..", ".", |
ChuckTimber | 0:d0ebbde59857 | 9 | "..-.", "--.", "....", "..", ".---", |
ChuckTimber | 3:993b88218184 | 10 | "-.-", ".-..", "--", "-.", "---", |
ChuckTimber | 0:d0ebbde59857 | 11 | ".--.", "--.-", ".-.", "...", "-", |
ChuckTimber | 3:993b88218184 | 12 | "..-", "...-", ".--", "-..-", "-.--", "--..", " " |
ChuckTimber | 3:993b88218184 | 13 | }; |
ChuckTimber | 9:09fc839da416 | 14 | |
ChuckTimber | 9:09fc839da416 | 15 | // 10-number |
ChuckTimber | 0:d0ebbde59857 | 16 | char* nums[] = { "-----", ".----", "..---", "...--", "....-", |
ChuckTimber | 3:993b88218184 | 17 | ".....", "-....", "--...", "---..", "----." |
ChuckTimber | 3:993b88218184 | 18 | }; |
ChuckTimber | 3:993b88218184 | 19 | |
ChuckTimber | 9:09fc839da416 | 20 | // 15-marks |
ChuckTimber | 3:993b88218184 | 21 | char* marks[] = { ".-.-.-", // '.' |
ChuckTimber | 3:993b88218184 | 22 | "--..--", // ',' |
ChuckTimber | 3:993b88218184 | 23 | "..--..", // '?' |
ChuckTimber | 3:993b88218184 | 24 | "-.-.--", // '!' |
ChuckTimber | 3:993b88218184 | 25 | "-....-", // '-' |
ChuckTimber | 3:993b88218184 | 26 | "-..-.", // '/' |
ChuckTimber | 3:993b88218184 | 27 | ".--.-.", // '@' |
ChuckTimber | 3:993b88218184 | 28 | "-.--.", // '(' |
ChuckTimber | 3:993b88218184 | 29 | "-.--.-", // ')' |
ChuckTimber | 3:993b88218184 | 30 | "-...-", // '=' |
ChuckTimber | 3:993b88218184 | 31 | ".-..-.", // '"' |
ChuckTimber | 3:993b88218184 | 32 | ".-.-.", // '+' |
ChuckTimber | 3:993b88218184 | 33 | ".----.", // ''' |
ChuckTimber | 3:993b88218184 | 34 | "---...", // ':' |
ChuckTimber | 3:993b88218184 | 35 | "........" // 'HH' |
ChuckTimber | 3:993b88218184 | 36 | }; |
ChuckTimber | 0:d0ebbde59857 | 37 | |
ChuckTimber | 9:09fc839da416 | 38 | // 6-hybrid |
ChuckTimber | 9:09fc839da416 | 39 | char* hybrid[] = { ".-...", // 'AS' |
ChuckTimber | 9:09fc839da416 | 40 | ".-.-.", // 'AR' |
ChuckTimber | 9:09fc839da416 | 41 | "-...-.-", // 'BK' |
ChuckTimber | 9:09fc839da416 | 42 | "-...-", // 'BT' |
ChuckTimber | 9:09fc839da416 | 43 | "-.--.", // 'KN' |
ChuckTimber | 9:09fc839da416 | 44 | "...-.-" // 'VA' |
ChuckTimber | 9:09fc839da416 | 45 | }; |
ChuckTimber | 9:09fc839da416 | 46 | |
ChuckTimber | 8:96f4614a46ba | 47 | |
ChuckTimber | 8:96f4614a46ba | 48 | |
ChuckTimber | 8:96f4614a46ba | 49 | void proc_NoteOn(byte channel, byte note, byte velocity); |
ChuckTimber | 8:96f4614a46ba | 50 | void proc_ControlChange(byte channel, byte number, byte value); |
ChuckTimber | 9:09fc839da416 | 51 | void proc_REnc_right(void); |
ChuckTimber | 9:09fc839da416 | 52 | void proc_REnc_left(void); |
ChuckTimber | 2:5fe4a1431347 | 53 | |
ChuckTimber | 2:5fe4a1431347 | 54 | |
ChuckTimber | 6:7405805be717 | 55 | BTN btn(dp13); |
ChuckTimber | 10:57bbdc516590 | 56 | Morse morse; // LED1, dp24 |
ChuckTimber | 8:96f4614a46ba | 57 | MIDI midi(dp16, dp15); |
ChuckTimber | 9:09fc839da416 | 58 | REnc renc(dp1, dp2); |
ChuckTimber | 12:9d6544cefd81 | 59 | DigitalOut isMidi(dp17); |
ChuckTimber | 12:9d6544cefd81 | 60 | DigitalOut isNotMidi(dp18); |
ChuckTimber | 12:9d6544cefd81 | 61 | |
ChuckTimber | 0:d0ebbde59857 | 62 | |
ChuckTimber | 3:993b88218184 | 63 | int main() |
ChuckTimber | 0:d0ebbde59857 | 64 | { |
ChuckTimber | 0:d0ebbde59857 | 65 | int i; |
ChuckTimber | 7:82910f3806b3 | 66 | int mode = 0; |
ChuckTimber | 7:82910f3806b3 | 67 | int value; |
ChuckTimber | 0:d0ebbde59857 | 68 | |
ChuckTimber | 8:96f4614a46ba | 69 | //Morse morse(dp24, LED1); |
ChuckTimber | 8:96f4614a46ba | 70 | //Morse morse(dp24, LED1, 0.08, 1000); |
ChuckTimber | 8:96f4614a46ba | 71 | |
ChuckTimber | 8:96f4614a46ba | 72 | midi.setHandleNoteOn(&proc_NoteOn); |
ChuckTimber | 8:96f4614a46ba | 73 | midi.setHandleControlChange(&proc_ControlChange); |
ChuckTimber | 9:09fc839da416 | 74 | |
ChuckTimber | 12:9d6544cefd81 | 75 | renc.setHandleCC(&proc_REnc_right); |
ChuckTimber | 12:9d6544cefd81 | 76 | renc.setHandleCCW(&proc_REnc_left); |
ChuckTimber | 8:96f4614a46ba | 77 | |
ChuckTimber | 8:96f4614a46ba | 78 | midi.begin(); |
ChuckTimber | 8:96f4614a46ba | 79 | wait(1.0); |
ChuckTimber | 5:decb31e2efa4 | 80 | btn.CMD = 0; |
ChuckTimber | 8:96f4614a46ba | 81 | |
ChuckTimber | 0:d0ebbde59857 | 82 | while(1) { |
ChuckTimber | 8:96f4614a46ba | 83 | midi.read(); |
ChuckTimber | 9:09fc839da416 | 84 | /* |
ChuckTimber | 11:51fdd053c7c2 | 85 | if (renc.CMD == CLOCKWISE) { |
ChuckTimber | 9:09fc839da416 | 86 | morse.incidx(); |
ChuckTimber | 11:51fdd053c7c2 | 87 | renc.CMD = STOP |
ChuckTimber | 11:51fdd053c7c2 | 88 | } else if (renc.CMD == COUNTERCLOCKWISE) { |
ChuckTimber | 9:09fc839da416 | 89 | morse.decidx(); |
ChuckTimber | 11:51fdd053c7c2 | 90 | renc.CMD = STOP; |
ChuckTimber | 9:09fc839da416 | 91 | } |
ChuckTimber | 9:09fc839da416 | 92 | */ |
ChuckTimber | 7:82910f3806b3 | 93 | if(btn.CMD) { |
ChuckTimber | 7:82910f3806b3 | 94 | mode++; |
ChuckTimber | 7:82910f3806b3 | 95 | btn.CMD = 0; |
ChuckTimber | 8:96f4614a46ba | 96 | srand( time(NULL) ); |
ChuckTimber | 7:82910f3806b3 | 97 | } |
ChuckTimber | 7:82910f3806b3 | 98 | if (mode % 2) { |
ChuckTimber | 12:9d6544cefd81 | 99 | isNotMidi = 1; |
ChuckTimber | 12:9d6544cefd81 | 100 | isMidi = 0; |
ChuckTimber | 7:82910f3806b3 | 101 | for (i = 0; i < 5; i++) { |
ChuckTimber | 7:82910f3806b3 | 102 | value = rand(); |
ChuckTimber | 7:82910f3806b3 | 103 | morse.code(codes[value % 26]); |
ChuckTimber | 5:decb31e2efa4 | 104 | } |
ChuckTimber | 7:82910f3806b3 | 105 | morse.code(codes[26]); |
ChuckTimber | 12:9d6544cefd81 | 106 | } else { |
ChuckTimber | 12:9d6544cefd81 | 107 | isNotMidi = 0; |
ChuckTimber | 12:9d6544cefd81 | 108 | isMidi = 1; |
ChuckTimber | 0:d0ebbde59857 | 109 | } |
ChuckTimber | 0:d0ebbde59857 | 110 | } |
ChuckTimber | 0:d0ebbde59857 | 111 | } |
ChuckTimber | 8:96f4614a46ba | 112 | |
ChuckTimber | 8:96f4614a46ba | 113 | void proc_NoteOn(byte channel, byte note, byte velocity) |
ChuckTimber | 8:96f4614a46ba | 114 | { |
ChuckTimber | 8:96f4614a46ba | 115 | if ((36 <= note) && (note <= 36+26)) { |
ChuckTimber | 8:96f4614a46ba | 116 | morse.code(codes[note - 36]); |
ChuckTimber | 8:96f4614a46ba | 117 | } |
ChuckTimber | 8:96f4614a46ba | 118 | } |
ChuckTimber | 8:96f4614a46ba | 119 | |
ChuckTimber | 8:96f4614a46ba | 120 | void proc_ControlChange(byte channel, byte number, byte value) |
ChuckTimber | 8:96f4614a46ba | 121 | { |
ChuckTimber | 8:96f4614a46ba | 122 | if (number == 16) { // General Purpose Controller 1 |
ChuckTimber | 9:09fc839da416 | 123 | morse.setidx(value); |
ChuckTimber | 8:96f4614a46ba | 124 | } |
ChuckTimber | 9:09fc839da416 | 125 | } |
ChuckTimber | 9:09fc839da416 | 126 | |
ChuckTimber | 9:09fc839da416 | 127 | void proc_REnc_right(void) |
ChuckTimber | 9:09fc839da416 | 128 | { |
ChuckTimber | 9:09fc839da416 | 129 | morse.incidx(); |
ChuckTimber | 9:09fc839da416 | 130 | } |
ChuckTimber | 9:09fc839da416 | 131 | |
ChuckTimber | 9:09fc839da416 | 132 | void proc_REnc_left(void) |
ChuckTimber | 9:09fc839da416 | 133 | { |
ChuckTimber | 9:09fc839da416 | 134 | morse.decidx(); |
ChuckTimber | 9:09fc839da416 | 135 | } |