Skeleton of solution for transmitting morse code for string of letters entered

Dependencies:   mbed

Committer:
motley
Date:
Sun Oct 01 12:17:14 2017 +0000
Revision:
0:edd3bba01cdd
First commit - letters A to D only

Who changed what in which revision?

UserRevisionLine numberNew contents of line
motley 0:edd3bba01cdd 1 // Suggested partial solution for sending of amessage typed into termianl
motley 0:edd3bba01cdd 2 // by flashing the morse code for each letter in the message on LED
motley 0:edd3bba01cdd 3 // attached to D7 pin
motley 0:edd3bba01cdd 4 // Data entry and debug messages on PC terminal
motley 0:edd3bba01cdd 5 //
motley 0:edd3bba01cdd 6 // Chris Tipney September 2017 for module ELEC143
motley 0:edd3bba01cdd 7 //
motley 0:edd3bba01cdd 8 // Current limitations
motley 0:edd3bba01cdd 9 // (1) Only first 4 alpha characters and no numbers
motley 0:edd3bba01cdd 10 // (2) No warning if message length exceeded
motley 0:edd3bba01cdd 11 // (3) No trapping of non alpha characters (e.g. space or tab)
motley 0:edd3bba01cdd 12 //
motley 0:edd3bba01cdd 13 // Suggested improvements:
motley 0:edd3bba01cdd 14 // Add rest of alphabet and numbers - rest of ASCII table must be trapped
motley 0:edd3bba01cdd 15 // Put suitable areas of code into functions
motley 0:edd3bba01cdd 16
motley 0:edd3bba01cdd 17 #include "mbed.h"
motley 0:edd3bba01cdd 18
motley 0:edd3bba01cdd 19 Serial pc(SERIAL_TX, SERIAL_RX);
motley 0:edd3bba01cdd 20
motley 0:edd3bba01cdd 21 DigitalOut myled(D7);
motley 0:edd3bba01cdd 22
motley 0:edd3bba01cdd 23 const int maxMessageLength = 20; // max message length
motley 0:edd3bba01cdd 24 char message[maxMessageLength]; // Holds the message to be transmitted
motley 0:edd3bba01cdd 25
motley 0:edd3bba01cdd 26 // array of strings to hold the morse codes (only A to D so far)
motley 0:edd3bba01cdd 27 // O = DOT (sound is DIT)
motley 0:edd3bba01cdd 28 // A = DASH (sound is DAA);
motley 0:edd3bba01cdd 29 // S = SPACE after a letter
motley 0:edd3bba01cdd 30 char codes[][6] = { "OAS", // DIT DAA = A
motley 0:edd3bba01cdd 31 "AOOOS", // DAA DIT DIT = B
motley 0:edd3bba01cdd 32 "AOAOS", // DAA DIT DAA DIT = C
motley 0:edd3bba01cdd 33 "AOOS" // DAA DIT DIT = D
motley 0:edd3bba01cdd 34 };
motley 0:edd3bba01cdd 35
motley 0:edd3bba01cdd 36 int main()
motley 0:edd3bba01cdd 37 {
motley 0:edd3bba01cdd 38 int position = 0;
motley 0:edd3bba01cdd 39
motley 0:edd3bba01cdd 40 pc.printf("Morse message start...\n\r");
motley 0:edd3bba01cdd 41
motley 0:edd3bba01cdd 42 while(1) {
motley 0:edd3bba01cdd 43 // diaplay opening message and get message
motley 0:edd3bba01cdd 44 printf("\n\r");
motley 0:edd3bba01cdd 45 pc.printf("Please enter your message and press enter\n\r");
motley 0:edd3bba01cdd 46 scanf("%s", message);
motley 0:edd3bba01cdd 47 pc.printf("Message is %s\n\r", message);
motley 0:edd3bba01cdd 48
motley 0:edd3bba01cdd 49 for (int i = 0; i < strlen(message); i++)
motley 0:edd3bba01cdd 50 {
motley 0:edd3bba01cdd 51 if ((message[i] > 64) && (message[i] < 91)) // upper case letter
motley 0:edd3bba01cdd 52 {
motley 0:edd3bba01cdd 53 position = message[i] - 65;
motley 0:edd3bba01cdd 54 }
motley 0:edd3bba01cdd 55 else if ((message[i] > 96) && (message[i] < 123)) // lower case letter
motley 0:edd3bba01cdd 56 {
motley 0:edd3bba01cdd 57 position = message[i] - 97;
motley 0:edd3bba01cdd 58 }
motley 0:edd3bba01cdd 59
motley 0:edd3bba01cdd 60 printf("%d:%c:%d:%d ", i, message[i], message[i], position);
motley 0:edd3bba01cdd 61 printf("\n\r");
motley 0:edd3bba01cdd 62 printf("%d %s ", position, codes[position]);
motley 0:edd3bba01cdd 63 printf("\n\r");
motley 0:edd3bba01cdd 64 printf("Letter is %c ", message[i]);
motley 0:edd3bba01cdd 65 for (int j=0; j < strlen(codes[position]); j++)
motley 0:edd3bba01cdd 66 {
motley 0:edd3bba01cdd 67 if (codes[position][j] == 'O')
motley 0:edd3bba01cdd 68 {
motley 0:edd3bba01cdd 69 printf("DIT ");
motley 0:edd3bba01cdd 70 myled = 1;
motley 0:edd3bba01cdd 71 wait(0.15);
motley 0:edd3bba01cdd 72 myled = 0;
motley 0:edd3bba01cdd 73 wait(0.15);
motley 0:edd3bba01cdd 74 }
motley 0:edd3bba01cdd 75
motley 0:edd3bba01cdd 76 if (codes[position][j] == 'A')
motley 0:edd3bba01cdd 77 {
motley 0:edd3bba01cdd 78 printf("DAA ");
motley 0:edd3bba01cdd 79 myled = 1;
motley 0:edd3bba01cdd 80 wait(0.45);
motley 0:edd3bba01cdd 81 myled = 0;
motley 0:edd3bba01cdd 82 wait(0.15);
motley 0:edd3bba01cdd 83 }
motley 0:edd3bba01cdd 84
motley 0:edd3bba01cdd 85 if (codes[position][j] == 'S')
motley 0:edd3bba01cdd 86 {
motley 0:edd3bba01cdd 87 printf("<SPACE> ");
motley 0:edd3bba01cdd 88 wait(0.30);
motley 0:edd3bba01cdd 89 }
motley 0:edd3bba01cdd 90 //printf("%c ", codes[position][j]);
motley 0:edd3bba01cdd 91 }
motley 0:edd3bba01cdd 92 printf("\n\r");
motley 0:edd3bba01cdd 93 }
motley 0:edd3bba01cdd 94
motley 0:edd3bba01cdd 95 }
motley 0:edd3bba01cdd 96 }