a MIDI-to-Morse code application.

Dependencies:   MIDI REnc button mbed morse

main.cpp

Committer:
ChuckTimber
Date:
2014-07-13
Revision:
4:98b4e2f46789
Parent:
3:993b88218184
Child:
5:decb31e2efa4

File content as of revision 4:98b4e2f46789:

#include "mbed.h"


#define FREQ 800
DigitalOut myled(LED1);

char* codes[] = { ".-", "-...", "-.-.", "-..", ".",
                  "..-.", "--.", "....", "..", ".---",
                  "-.-", ".-..", "--", "-.", "---",
                  ".--.", "--.-", ".-.", "...", "-",
                  "..-", "...-", ".--", "-..-", "-.--", "--..", " "
                };
char* nums[] = { "-----", ".----", "..---", "...--", "....-",
                 ".....", "-....", "--...", "---..", "----."
               };

char* marks[] = { ".-.-.-",  // '.'
                  "--..--",  // ','
                  "..--..",  // '?'
                  "-.-.--",  // '!'
                  "-....-",  // '-'
                  "-..-.",   // '/'
                  ".--.-.",  // '@'
                  "-.--.",   // '('
                  "-.--.-",  // ')'
                  "-...-",   // '='
                  ".-..-.",  // '"'
                  ".-.-.",   // '+'
                  ".----.",  // '''
                  "---...",  // ':'
                  "........" // 'HH'
                };

float cw_tick = 0.1;

PwmOut __pwm( dp24 );

void BeepOFF (void)
{
    __pwm.write(0.0);
}

void BeepON (void)
{
    __pwm.write(0.5);
}

void dot (void)
{
    BeepON();
    myled = 1;
    wait(cw_tick);
    BeepOFF();
    myled = 0;
}

void dash (void)
{
    BeepON();
    myled = 1;
    wait(cw_tick*3);
    BeepOFF();
    myled = 0;
}

void space(int k)
{
    wait(cw_tick * (float)k);
}


class BTN {

public:

    unsigned char CMD;
    unsigned char STAT;
private:
    unsigned char FIL;

    DigitalIn _Pin;
    Ticker _Tick;

    void sample_btn()
    {
        unsigned char a, b;
    
        a = _Pin;
        
        if (a == FIL) {
            b = STAT;
            STAT = a;
            b = (b ^ a) & a;
            if (b) CMD = b;
        }
        FIL = a;
    }
public:
    BTN(PinName pin) : _Pin(pin) {
        _Tick.attach(this, &BTN::sample_btn, 0.01);
    }


};

    BTN btn(dp13);

int main()
{
    int i;
    char* ptr;

    {
        __pwm.period(1.0/FREQ);
        __pwm.write(0.0);
    };

    while(1) {
        for (i = 0; i < 27; ) {
            ptr = codes[i];

            for (; *ptr; ptr++, space(1) ) {
                switch (*ptr) {
                    case '-':
                        dash();
                        break;
                    case '.':
                        dot();
                        break;
                    case ' ':
                        space(7);
                        break;
                    default:
                        continue;
                }
            }
            space(2);

            if(btn.CMD) {
                i++;
                btn.CMD = 0;
            }
        }
    }
}