This program converts Morse input to Letters and prints them

Dependencies:   mbed

Committer:
samanvay
Date:
Mon Sep 14 16:25:18 2015 +0000
Revision:
0:c2959226a08e
MorseCode

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samanvay 0:c2959226a08e 1 #include "mbed.h"
samanvay 0:c2959226a08e 2 #include "PinDetect.h"
samanvay 0:c2959226a08e 3
samanvay 0:c2959226a08e 4 PinDetect pin1(D2, PullUp);
samanvay 0:c2959226a08e 5 PinDetect pin2(D3, PullUp);
samanvay 0:c2959226a08e 6 PinDetect pin3(D4, PullUp);
samanvay 0:c2959226a08e 7 DigitalOut myled(LED1);
samanvay 0:c2959226a08e 8
samanvay 0:c2959226a08e 9 Serial pc(USBTX, USBRX);
samanvay 0:c2959226a08e 10
samanvay 0:c2959226a08e 11 int pinbus[5] = {0, 0, 0, 0, 0};
samanvay 0:c2959226a08e 12
samanvay 0:c2959226a08e 13 char letters[1][27] = {
samanvay 0:c2959226a08e 14 {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' '},
samanvay 0:c2959226a08e 15 };
samanvay 0:c2959226a08e 16
samanvay 0:c2959226a08e 17 int pinbusvalues[27][4] = {
samanvay 0:c2959226a08e 18 {0, 1, 2, 2}, {1, 0, 0, 0}, {1, 0, 1, 0}, {2, 0, 0, 2}, {0, 2, 2, 2},
samanvay 0:c2959226a08e 19 {0, 0, 1, 0}, { 1, 1, 0, 2}, {0, 0, 0, 0}, { 0, 0, 2, 2}, {0, 1, 1, 1},
samanvay 0:c2959226a08e 20 { 1, 0, 1, 2}, { 2, 2, 2, 2}, { 1, 1, 2, 2}, {1, 0, 2, 2}, {2, 0, 0, 0},
samanvay 0:c2959226a08e 21 {0, 1, 1, 0}, { 1, 1, 0, 1}, {0, 2, 0, 2}, {0, 0, 0, 2}, {1, 2, 2, 2},
samanvay 0:c2959226a08e 22 {0, 0, 1, 2}, {0, 0, 0, 1}, {0, 2, 2, 0}, {1, 0, 0, 1}, {1, 0, 1, 1},
samanvay 0:c2959226a08e 23 {1, 1, 0, 0}, {0, 0, 0, 2}
samanvay 0:c2959226a08e 24 };
samanvay 0:c2959226a08e 25
samanvay 0:c2959226a08e 26 int j=0;
samanvay 0:c2959226a08e 27 void b1(void){
samanvay 0:c2959226a08e 28 // pc.printf("From F1: %d", j);
samanvay 0:c2959226a08e 29 pinbus[j] = 0;
samanvay 0:c2959226a08e 30 j=j+1;
samanvay 0:c2959226a08e 31 }
samanvay 0:c2959226a08e 32 void b2(void){
samanvay 0:c2959226a08e 33 // pc.printf("From F2: %d", j);
samanvay 0:c2959226a08e 34 pinbus[j] = 1;
samanvay 0:c2959226a08e 35 j=j+1;
samanvay 0:c2959226a08e 36 }
samanvay 0:c2959226a08e 37 void b3(void){
samanvay 0:c2959226a08e 38 // pc.printf("From F3: %d", j);
samanvay 0:c2959226a08e 39 pinbus[j] = 2;
samanvay 0:c2959226a08e 40 j=j+1;
samanvay 0:c2959226a08e 41 }
samanvay 0:c2959226a08e 42
samanvay 0:c2959226a08e 43 void keyPressed(void){
samanvay 0:c2959226a08e 44 // pc.printf("From keypressed: %d", j);
samanvay 0:c2959226a08e 45 if (j < 4)
samanvay 0:c2959226a08e 46 return;
samanvay 0:c2959226a08e 47 // pc.printf("%d %d %d %d", pinbus[0], pinbus[1], pinbus[2], pinbus[3]);
samanvay 0:c2959226a08e 48 for (int i=0; i<27; i++) {
samanvay 0:c2959226a08e 49 if (pinbus[0] == pinbusvalues[i][0]&&pinbus[1] == pinbusvalues[i][1]&&pinbus[2] == pinbusvalues[i][2]&&pinbus[3] == pinbusvalues[i][3]) {
samanvay 0:c2959226a08e 50 pc.printf("%c", letters[0][i]);
samanvay 0:c2959226a08e 51 }
samanvay 0:c2959226a08e 52 }
samanvay 0:c2959226a08e 53 j=0;
samanvay 0:c2959226a08e 54 }
samanvay 0:c2959226a08e 55
samanvay 0:c2959226a08e 56 int main() {
samanvay 0:c2959226a08e 57 pc.baud(9600);
samanvay 0:c2959226a08e 58
samanvay 0:c2959226a08e 59 pin1.attach_deasserted(&b1);
samanvay 0:c2959226a08e 60 pin2.attach_deasserted(&b2);
samanvay 0:c2959226a08e 61 pin3.attach_deasserted(&b3);
samanvay 0:c2959226a08e 62
samanvay 0:c2959226a08e 63
samanvay 0:c2959226a08e 64 pin1.attach_asserted(&keyPressed);
samanvay 0:c2959226a08e 65 pin2.attach_asserted(&keyPressed);
samanvay 0:c2959226a08e 66 pin3.attach_asserted(&keyPressed);
samanvay 0:c2959226a08e 67 //set frequency
samanvay 0:c2959226a08e 68 pin1.setSampleFrequency();
samanvay 0:c2959226a08e 69 pin2.setSampleFrequency();
samanvay 0:c2959226a08e 70 pin3.setSampleFrequency();
samanvay 0:c2959226a08e 71
samanvay 0:c2959226a08e 72 while(1){
samanvay 0:c2959226a08e 73 myled = !myled;
samanvay 0:c2959226a08e 74 wait(0.2);
samanvay 0:c2959226a08e 75 }
samanvay 0:c2959226a08e 76 }