a MIDI-to-Morse code application.

Dependencies:   MIDI REnc button mbed morse

Committer:
ChuckTimber
Date:
Sun Jul 13 03:03:51 2014 +0000
Revision:
4:98b4e2f46789
Parent:
3:993b88218184
Child:
5:decb31e2efa4
class expression.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ChuckTimber 0:d0ebbde59857 1 #include "mbed.h"
ChuckTimber 0:d0ebbde59857 2
ChuckTimber 3:993b88218184 3
ChuckTimber 0:d0ebbde59857 4 #define FREQ 800
ChuckTimber 0:d0ebbde59857 5 DigitalOut myled(LED1);
ChuckTimber 0:d0ebbde59857 6
ChuckTimber 3:993b88218184 7 char* codes[] = { ".-", "-...", "-.-.", "-..", ".",
ChuckTimber 0:d0ebbde59857 8 "..-.", "--.", "....", "..", ".---",
ChuckTimber 3:993b88218184 9 "-.-", ".-..", "--", "-.", "---",
ChuckTimber 0:d0ebbde59857 10 ".--.", "--.-", ".-.", "...", "-",
ChuckTimber 3:993b88218184 11 "..-", "...-", ".--", "-..-", "-.--", "--..", " "
ChuckTimber 3:993b88218184 12 };
ChuckTimber 0:d0ebbde59857 13 char* nums[] = { "-----", ".----", "..---", "...--", "....-",
ChuckTimber 3:993b88218184 14 ".....", "-....", "--...", "---..", "----."
ChuckTimber 3:993b88218184 15 };
ChuckTimber 3:993b88218184 16
ChuckTimber 3:993b88218184 17 char* marks[] = { ".-.-.-", // '.'
ChuckTimber 3:993b88218184 18 "--..--", // ','
ChuckTimber 3:993b88218184 19 "..--..", // '?'
ChuckTimber 3:993b88218184 20 "-.-.--", // '!'
ChuckTimber 3:993b88218184 21 "-....-", // '-'
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 "........" // 'HH'
ChuckTimber 3:993b88218184 32 };
ChuckTimber 0:d0ebbde59857 33
ChuckTimber 0:d0ebbde59857 34 float cw_tick = 0.1;
ChuckTimber 0:d0ebbde59857 35
ChuckTimber 0:d0ebbde59857 36 PwmOut __pwm( dp24 );
ChuckTimber 0:d0ebbde59857 37
ChuckTimber 0:d0ebbde59857 38 void BeepOFF (void)
ChuckTimber 0:d0ebbde59857 39 {
ChuckTimber 0:d0ebbde59857 40 __pwm.write(0.0);
ChuckTimber 0:d0ebbde59857 41 }
ChuckTimber 0:d0ebbde59857 42
ChuckTimber 0:d0ebbde59857 43 void BeepON (void)
ChuckTimber 0:d0ebbde59857 44 {
ChuckTimber 0:d0ebbde59857 45 __pwm.write(0.5);
ChuckTimber 0:d0ebbde59857 46 }
ChuckTimber 0:d0ebbde59857 47
ChuckTimber 0:d0ebbde59857 48 void dot (void)
ChuckTimber 0:d0ebbde59857 49 {
ChuckTimber 3:993b88218184 50 BeepON();
ChuckTimber 3:993b88218184 51 myled = 1;
ChuckTimber 0:d0ebbde59857 52 wait(cw_tick);
ChuckTimber 3:993b88218184 53 BeepOFF();
ChuckTimber 3:993b88218184 54 myled = 0;
ChuckTimber 0:d0ebbde59857 55 }
ChuckTimber 0:d0ebbde59857 56
ChuckTimber 0:d0ebbde59857 57 void dash (void)
ChuckTimber 0:d0ebbde59857 58 {
ChuckTimber 3:993b88218184 59 BeepON();
ChuckTimber 3:993b88218184 60 myled = 1;
ChuckTimber 0:d0ebbde59857 61 wait(cw_tick*3);
ChuckTimber 3:993b88218184 62 BeepOFF();
ChuckTimber 3:993b88218184 63 myled = 0;
ChuckTimber 0:d0ebbde59857 64 }
ChuckTimber 0:d0ebbde59857 65
ChuckTimber 2:5fe4a1431347 66 void space(int k)
ChuckTimber 2:5fe4a1431347 67 {
ChuckTimber 2:5fe4a1431347 68 wait(cw_tick * (float)k);
ChuckTimber 2:5fe4a1431347 69 }
ChuckTimber 2:5fe4a1431347 70
ChuckTimber 2:5fe4a1431347 71
ChuckTimber 4:98b4e2f46789 72 class BTN {
ChuckTimber 4:98b4e2f46789 73
ChuckTimber 4:98b4e2f46789 74 public:
ChuckTimber 4:98b4e2f46789 75
ChuckTimber 3:993b88218184 76 unsigned char CMD;
ChuckTimber 3:993b88218184 77 unsigned char STAT;
ChuckTimber 4:98b4e2f46789 78 private:
ChuckTimber 3:993b88218184 79 unsigned char FIL;
ChuckTimber 3:993b88218184 80
ChuckTimber 4:98b4e2f46789 81 DigitalIn _Pin;
ChuckTimber 3:993b88218184 82 Ticker _Tick;
ChuckTimber 3:993b88218184 83
ChuckTimber 4:98b4e2f46789 84 void sample_btn()
ChuckTimber 4:98b4e2f46789 85 {
ChuckTimber 4:98b4e2f46789 86 unsigned char a, b;
ChuckTimber 3:993b88218184 87
ChuckTimber 4:98b4e2f46789 88 a = _Pin;
ChuckTimber 4:98b4e2f46789 89
ChuckTimber 4:98b4e2f46789 90 if (a == FIL) {
ChuckTimber 4:98b4e2f46789 91 b = STAT;
ChuckTimber 4:98b4e2f46789 92 STAT = a;
ChuckTimber 4:98b4e2f46789 93 b = (b ^ a) & a;
ChuckTimber 4:98b4e2f46789 94 if (b) CMD = b;
ChuckTimber 4:98b4e2f46789 95 }
ChuckTimber 4:98b4e2f46789 96 FIL = a;
ChuckTimber 3:993b88218184 97 }
ChuckTimber 4:98b4e2f46789 98 public:
ChuckTimber 4:98b4e2f46789 99 BTN(PinName pin) : _Pin(pin) {
ChuckTimber 4:98b4e2f46789 100 _Tick.attach(this, &BTN::sample_btn, 0.01);
ChuckTimber 4:98b4e2f46789 101 }
ChuckTimber 4:98b4e2f46789 102
ChuckTimber 4:98b4e2f46789 103
ChuckTimber 4:98b4e2f46789 104 };
ChuckTimber 4:98b4e2f46789 105
ChuckTimber 4:98b4e2f46789 106 BTN btn(dp13);
ChuckTimber 0:d0ebbde59857 107
ChuckTimber 3:993b88218184 108 int main()
ChuckTimber 0:d0ebbde59857 109 {
ChuckTimber 0:d0ebbde59857 110 int i;
ChuckTimber 0:d0ebbde59857 111 char* ptr;
ChuckTimber 0:d0ebbde59857 112
ChuckTimber 0:d0ebbde59857 113 {
ChuckTimber 0:d0ebbde59857 114 __pwm.period(1.0/FREQ);
ChuckTimber 0:d0ebbde59857 115 __pwm.write(0.0);
ChuckTimber 0:d0ebbde59857 116 };
ChuckTimber 0:d0ebbde59857 117
ChuckTimber 0:d0ebbde59857 118 while(1) {
ChuckTimber 3:993b88218184 119 for (i = 0; i < 27; ) {
ChuckTimber 1:d1e13b283169 120 ptr = codes[i];
ChuckTimber 2:5fe4a1431347 121
ChuckTimber 2:5fe4a1431347 122 for (; *ptr; ptr++, space(1) ) {
ChuckTimber 0:d0ebbde59857 123 switch (*ptr) {
ChuckTimber 3:993b88218184 124 case '-':
ChuckTimber 3:993b88218184 125 dash();
ChuckTimber 3:993b88218184 126 break;
ChuckTimber 3:993b88218184 127 case '.':
ChuckTimber 3:993b88218184 128 dot();
ChuckTimber 3:993b88218184 129 break;
ChuckTimber 3:993b88218184 130 case ' ':
ChuckTimber 3:993b88218184 131 space(7);
ChuckTimber 3:993b88218184 132 break;
ChuckTimber 3:993b88218184 133 default:
ChuckTimber 3:993b88218184 134 continue;
ChuckTimber 0:d0ebbde59857 135 }
ChuckTimber 0:d0ebbde59857 136 }
ChuckTimber 2:5fe4a1431347 137 space(2);
ChuckTimber 2:5fe4a1431347 138
ChuckTimber 4:98b4e2f46789 139 if(btn.CMD) {
ChuckTimber 3:993b88218184 140 i++;
ChuckTimber 4:98b4e2f46789 141 btn.CMD = 0;
ChuckTimber 3:993b88218184 142 }
ChuckTimber 0:d0ebbde59857 143 }
ChuckTimber 0:d0ebbde59857 144 }
ChuckTimber 0:d0ebbde59857 145 }