We made use of TextLCD library.

Dependencies:   PS2Keyboard SDFileSystem TSI TextLCD mbed

Fork of TextLCD_HelloWorld by Simon Ford

Committer:
Ceyhun_Emre
Date:
Sat Jun 02 09:42:25 2018 +0000
Revision:
3:7c22a2f849c6
Parent:
2:ad0b044d0a10
This code is written for a Morse Encoder-Decoder Device that uses a FRDM-KL25Z board. It can encode the text taken from the keyboard, decode the Morse code taken from the touch slider of the board and transmit the Morse code using LED and buzzer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:334327d1a416 1 #include "mbed.h"
simon 0:334327d1a416 2 #include "TextLCD.h"
Ceyhun_Emre 3:7c22a2f849c6 3 #include "PS2ASCII.h"
Ceyhun_Emre 3:7c22a2f849c6 4 #include "TSISensor.h"
Ceyhun_Emre 3:7c22a2f849c6 5 #include "SDFileSystem.h"
Ceyhun_Emre 3:7c22a2f849c6 6 #include <string> //to define string
Ceyhun_Emre 3:7c22a2f849c6 7 #include <vector>
simon 0:334327d1a416 8
Ceyhun_Emre 3:7c22a2f849c6 9 // Peripheral IO initializations, the Ticker Object and global variables
Ceyhun_Emre 3:7c22a2f849c6 10 TextLCD lcd(D0, D1, D2, D3, D4, D5, TextLCD::LCD20x4);
Ceyhun_Emre 3:7c22a2f849c6 11 PS2ASCII ps2kb(D6, D7);
Ceyhun_Emre 3:7c22a2f849c6 12 DigitalOut led(LED1);
Ceyhun_Emre 3:7c22a2f849c6 13 DigitalOut buzzer(D8);
Ceyhun_Emre 3:7c22a2f849c6 14 TSISensor touchSlider;
Ceyhun_Emre 3:7c22a2f849c6 15 Ticker callCheckSOS;
Ceyhun_Emre 3:7c22a2f849c6 16 bool stopSOS;
Ceyhun_Emre 3:7c22a2f849c6 17 bool percentageNonZero;
Ceyhun_Emre 3:7c22a2f849c6 18
Ceyhun_Emre 3:7c22a2f849c6 19 // Screen function declarations. These functions are created to provide modularity to the code.
Ceyhun_Emre 3:7c22a2f849c6 20 // They take references as inputs to modify variables declared in the main function.
Ceyhun_Emre 3:7c22a2f849c6 21 void MenuScreen(int& screenValue, int& cursorIndex);
Ceyhun_Emre 3:7c22a2f849c6 22 void EncodeScreen(int& screenValue, string& textToEncode, string& encodedText, vector<string>& pairToWrite);
Ceyhun_Emre 3:7c22a2f849c6 23 void morseInputScreen(int& screenValue, string& morseInputText, vector<string>& pairToWrite);
Ceyhun_Emre 3:7c22a2f849c6 24 void EmergencySOS(int& screenValue);
Ceyhun_Emre 3:7c22a2f849c6 25 void AfterEncodeOptions(int& screenValue, int& cursorIndex, int& selectedCheckboxes, string& encodedText);
Ceyhun_Emre 3:7c22a2f849c6 26 void afterMorseInputOptions(int& screenValue, int& cursorIndex, int& selectedCheckboxes, string& morseInputText, vector<string>& pairToWrite);
Ceyhun_Emre 3:7c22a2f849c6 27 void writeToSDScreen(int& screenValue, vector<string>& pairToWrite, string& fileName);
Ceyhun_Emre 3:7c22a2f849c6 28
Ceyhun_Emre 3:7c22a2f849c6 29 // Algorithm function declarations. These functions are the backbone of the project, as they perform encode, decode & transmission tasks.
Ceyhun_Emre 3:7c22a2f849c6 30 string encode (string textToEncode);
Ceyhun_Emre 3:7c22a2f849c6 31 string morseInput();
Ceyhun_Emre 3:7c22a2f849c6 32 string decode( string textToDecode);
Ceyhun_Emre 3:7c22a2f849c6 33 char findChar( string code);
Ceyhun_Emre 3:7c22a2f849c6 34 void checkSOS();
Ceyhun_Emre 3:7c22a2f849c6 35 void transmitLED (string encodedText);
Ceyhun_Emre 3:7c22a2f849c6 36 void transmitBuzzer (string encodedText);
Ceyhun_Emre 3:7c22a2f849c6 37 void transmitLEDAndBuzzer (string encodedText);
Ceyhun_Emre 3:7c22a2f849c6 38
Ceyhun_Emre 3:7c22a2f849c6 39 // The main function. After performing initial tasks, it enters an infinite loop which only calls screen functions.
Ceyhun_Emre 3:7c22a2f849c6 40 int main() {
Ceyhun_Emre 3:7c22a2f849c6 41 led = 1; // Turning off the led (active low)
Ceyhun_Emre 3:7c22a2f849c6 42
Ceyhun_Emre 3:7c22a2f849c6 43 // Setting user defined characters (udc) for LCD
Ceyhun_Emre 3:7c22a2f849c6 44 const char udc_rightCursor[] = {8, 12, 14, 15, 14, 12, 8, 0};
Ceyhun_Emre 3:7c22a2f849c6 45 const char udc_emptyCheckBox[] = {0, 31, 17, 17, 17, 17, 31, 0};
Ceyhun_Emre 3:7c22a2f849c6 46 const char udc_fullCheckBox[] = {0, 31, 31, 31, 31, 31, 31, 0};
Ceyhun_Emre 3:7c22a2f849c6 47
Ceyhun_Emre 3:7c22a2f849c6 48 lcd.setUDC(0, (char *) udc_rightCursor);
Ceyhun_Emre 3:7c22a2f849c6 49 lcd.setUDC(1, (char *) udc_emptyCheckBox);
Ceyhun_Emre 3:7c22a2f849c6 50 lcd.setUDC(2, (char *) udc_fullCheckBox);
Ceyhun_Emre 3:7c22a2f849c6 51
Ceyhun_Emre 3:7c22a2f849c6 52 // Intro Screen
Ceyhun_Emre 3:7c22a2f849c6 53 lcd.setCursor(TextLCD::CurOff_BlkOff);
Ceyhun_Emre 3:7c22a2f849c6 54 lcd.printf(" Morse Code\n");
Ceyhun_Emre 3:7c22a2f849c6 55 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 56 lcd.printf(" Encoder - Decoder\n");
Ceyhun_Emre 3:7c22a2f849c6 57 lcd.locate(0, 2);
Ceyhun_Emre 3:7c22a2f849c6 58 lcd.printf(" Device by\n");
Ceyhun_Emre 3:7c22a2f849c6 59 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 60 lcd.printf(" CS & CEO\n");
Ceyhun_Emre 3:7c22a2f849c6 61 wait(3);
Ceyhun_Emre 3:7c22a2f849c6 62
Ceyhun_Emre 3:7c22a2f849c6 63 // Variable declarations which are passed between screens (they work similar to a global variable)
Ceyhun_Emre 3:7c22a2f849c6 64 int screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 65 int cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 66 int selectedCheckboxes = 0;
Ceyhun_Emre 3:7c22a2f849c6 67
Ceyhun_Emre 3:7c22a2f849c6 68 string textToEncode = "";
Ceyhun_Emre 3:7c22a2f849c6 69 string morseInputText = "";
Ceyhun_Emre 3:7c22a2f849c6 70 string encodedText = "";
Ceyhun_Emre 3:7c22a2f849c6 71 string decodedText = "";
Ceyhun_Emre 3:7c22a2f849c6 72 string fileName = "";
Ceyhun_Emre 3:7c22a2f849c6 73 vector<string> pairToWrite(2);
Ceyhun_Emre 3:7c22a2f849c6 74 pairToWrite[0] = "";
Ceyhun_Emre 3:7c22a2f849c6 75 pairToWrite[1] = "";
Ceyhun_Emre 3:7c22a2f849c6 76
Ceyhun_Emre 3:7c22a2f849c6 77 // The infinite loop with only a simple switch-case block that calls screens according to the screenValue variable.
Ceyhun_Emre 3:7c22a2f849c6 78 // This variable is modified at the end of any screen function so that main program calls in appropriate screen.
Ceyhun_Emre 3:7c22a2f849c6 79 while(1) {
Ceyhun_Emre 3:7c22a2f849c6 80 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 81
Ceyhun_Emre 3:7c22a2f849c6 82 switch (screenValue) {
Ceyhun_Emre 3:7c22a2f849c6 83 case 0 :
Ceyhun_Emre 3:7c22a2f849c6 84 MenuScreen(screenValue, cursorIndex);
Ceyhun_Emre 3:7c22a2f849c6 85 break;
Ceyhun_Emre 3:7c22a2f849c6 86 case 1 :
Ceyhun_Emre 3:7c22a2f849c6 87 EncodeScreen(screenValue, textToEncode, encodedText, pairToWrite);
Ceyhun_Emre 3:7c22a2f849c6 88 break;
Ceyhun_Emre 3:7c22a2f849c6 89 case 2 :
Ceyhun_Emre 3:7c22a2f849c6 90 morseInputScreen(screenValue, morseInputText, pairToWrite);
Ceyhun_Emre 3:7c22a2f849c6 91 break;
Ceyhun_Emre 3:7c22a2f849c6 92 case 3 :
Ceyhun_Emre 3:7c22a2f849c6 93 EmergencySOS(screenValue);
Ceyhun_Emre 3:7c22a2f849c6 94 break;
Ceyhun_Emre 3:7c22a2f849c6 95 case 4 :
Ceyhun_Emre 3:7c22a2f849c6 96 AfterEncodeOptions(screenValue, cursorIndex, selectedCheckboxes, encodedText);
Ceyhun_Emre 3:7c22a2f849c6 97 break;
Ceyhun_Emre 3:7c22a2f849c6 98 case 5 :
Ceyhun_Emre 3:7c22a2f849c6 99 afterMorseInputOptions(screenValue, cursorIndex, selectedCheckboxes, morseInputText, pairToWrite);
Ceyhun_Emre 3:7c22a2f849c6 100 break;
Ceyhun_Emre 3:7c22a2f849c6 101 case 6 :
Ceyhun_Emre 3:7c22a2f849c6 102 writeToSDScreen(screenValue, pairToWrite, fileName);
Ceyhun_Emre 3:7c22a2f849c6 103 break;
Ceyhun_Emre 3:7c22a2f849c6 104 default :
Ceyhun_Emre 3:7c22a2f849c6 105 MenuScreen(screenValue, cursorIndex);
Ceyhun_Emre 3:7c22a2f849c6 106 }
Ceyhun_Emre 3:7c22a2f849c6 107
Ceyhun_Emre 3:7c22a2f849c6 108 }
Ceyhun_Emre 3:7c22a2f849c6 109
Ceyhun_Emre 3:7c22a2f849c6 110 }
Ceyhun_Emre 3:7c22a2f849c6 111
Ceyhun_Emre 3:7c22a2f849c6 112 // This function constructs the main menu screen. Here, the user can select one of the four options to perform through a cursor interface.
Ceyhun_Emre 3:7c22a2f849c6 113 // The cursor can be moved with PS/2 keyboard arrow keys and the option can be selected by pressing the Enter key.
Ceyhun_Emre 3:7c22a2f849c6 114 void MenuScreen(int& screenValue, int& cursorIndex) {
Ceyhun_Emre 3:7c22a2f849c6 115 const int CURSOR_MAX_INDEX = 3; // The constant showing the max. location of the cursor, starting from index 0.
Ceyhun_Emre 3:7c22a2f849c6 116
Ceyhun_Emre 3:7c22a2f849c6 117 lcd.locate(0, 0);
Ceyhun_Emre 3:7c22a2f849c6 118 lcd.printf(" Encode Text\n"); // Launchs the encode screen.
Ceyhun_Emre 3:7c22a2f849c6 119 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 120 lcd.printf(" Input Morse Code\n"); // Launchs the Morse input screen
Ceyhun_Emre 3:7c22a2f849c6 121 lcd.locate(0, 2);
Ceyhun_Emre 3:7c22a2f849c6 122 lcd.printf(" Emergency SOS!\n"); // Launchs the emergency SOS (distress signal) screen.
Ceyhun_Emre 3:7c22a2f849c6 123 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 124 lcd.printf(" Write to SD Card\n"); // Launchs the SD Card screen
Ceyhun_Emre 3:7c22a2f849c6 125
Ceyhun_Emre 3:7c22a2f849c6 126 // Places the cursor to the appropriate screen. The cursor character is in the 0th custom character RAM location.
Ceyhun_Emre 3:7c22a2f849c6 127 lcd.locate(0, cursorIndex);
Ceyhun_Emre 3:7c22a2f849c6 128 lcd.putc(0x00);
Ceyhun_Emre 3:7c22a2f849c6 129
Ceyhun_Emre 3:7c22a2f849c6 130 // Asks for keypress from the keyboard and sets the arrowValue variable to 0.
Ceyhun_Emre 3:7c22a2f849c6 131 // Followingly, this variable is given a value of 1 or -1 according to the arrow (or 0 for other keys).
Ceyhun_Emre 3:7c22a2f849c6 132 unsigned char pressedChar = ps2kb.getChar();
Ceyhun_Emre 3:7c22a2f849c6 133 int arrowValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 134
Ceyhun_Emre 3:7c22a2f849c6 135 if (ps2kb.E0flag() || !ps2kb.numlock()) {
Ceyhun_Emre 3:7c22a2f849c6 136 switch (pressedChar) {
Ceyhun_Emre 3:7c22a2f849c6 137 case 0x72 :
Ceyhun_Emre 3:7c22a2f849c6 138 arrowValue = 1;
Ceyhun_Emre 3:7c22a2f849c6 139 break;
Ceyhun_Emre 3:7c22a2f849c6 140 case 0x74 :
Ceyhun_Emre 3:7c22a2f849c6 141 arrowValue = 1;
Ceyhun_Emre 3:7c22a2f849c6 142 break;
Ceyhun_Emre 3:7c22a2f849c6 143 case 0x75 :
Ceyhun_Emre 3:7c22a2f849c6 144 arrowValue = -1;
Ceyhun_Emre 3:7c22a2f849c6 145 break;
Ceyhun_Emre 3:7c22a2f849c6 146 case 0x6B:
Ceyhun_Emre 3:7c22a2f849c6 147 arrowValue = -1;
Ceyhun_Emre 3:7c22a2f849c6 148 break;
Ceyhun_Emre 3:7c22a2f849c6 149 default:
Ceyhun_Emre 3:7c22a2f849c6 150 arrowValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 151 }
Ceyhun_Emre 3:7c22a2f849c6 152
Ceyhun_Emre 3:7c22a2f849c6 153 cursorIndex += arrowValue; // arrowValue is added to the cursorIndex to move the cursor on the next iteration of the screen.
Ceyhun_Emre 3:7c22a2f849c6 154
Ceyhun_Emre 3:7c22a2f849c6 155 // Algorithm to prevent the overflow of the cursorIndex and keep it in the interval [0, CURSOR_MAX_INDEX]
Ceyhun_Emre 3:7c22a2f849c6 156 if (cursorIndex == -1)
Ceyhun_Emre 3:7c22a2f849c6 157 cursorIndex = CURSOR_MAX_INDEX;
Ceyhun_Emre 3:7c22a2f849c6 158
Ceyhun_Emre 3:7c22a2f849c6 159 else if (cursorIndex == (CURSOR_MAX_INDEX + 1))
Ceyhun_Emre 3:7c22a2f849c6 160 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 161
Ceyhun_Emre 3:7c22a2f849c6 162 }
Ceyhun_Emre 3:7c22a2f849c6 163
Ceyhun_Emre 3:7c22a2f849c6 164 // If Enter is pressed, this block sets the screenValue and clears the cursorIndex for the execution of the chosen screen.
Ceyhun_Emre 3:7c22a2f849c6 165 if (pressedChar == 0x5A) {
Ceyhun_Emre 3:7c22a2f849c6 166 switch (cursorIndex) {
Ceyhun_Emre 3:7c22a2f849c6 167 case 0 :
Ceyhun_Emre 3:7c22a2f849c6 168 screenValue = 1;
Ceyhun_Emre 3:7c22a2f849c6 169 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 170
Ceyhun_Emre 3:7c22a2f849c6 171 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 172 lcd.printf("Please enter the\n");
Ceyhun_Emre 3:7c22a2f849c6 173 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 174 lcd.printf("text to be encoded.\n");
Ceyhun_Emre 3:7c22a2f849c6 175 lcd.locate(0, 2);
Ceyhun_Emre 3:7c22a2f849c6 176 lcd.printf("\"Enter\": Encode\n");
Ceyhun_Emre 3:7c22a2f849c6 177 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 178 lcd.printf("\"Esc\": Cancel\n");
Ceyhun_Emre 3:7c22a2f849c6 179 wait(3);
Ceyhun_Emre 3:7c22a2f849c6 180
Ceyhun_Emre 3:7c22a2f849c6 181 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 182 lcd.setCursor(TextLCD::CurOn_BlkOn);
Ceyhun_Emre 3:7c22a2f849c6 183
Ceyhun_Emre 3:7c22a2f849c6 184 break;
Ceyhun_Emre 3:7c22a2f849c6 185
Ceyhun_Emre 3:7c22a2f849c6 186 case 1 :
Ceyhun_Emre 3:7c22a2f849c6 187 screenValue = 2;
Ceyhun_Emre 3:7c22a2f849c6 188 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 189 break;
Ceyhun_Emre 3:7c22a2f849c6 190
Ceyhun_Emre 3:7c22a2f849c6 191 case 2 :
Ceyhun_Emre 3:7c22a2f849c6 192 screenValue = 3;
Ceyhun_Emre 3:7c22a2f849c6 193 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 194 break;
Ceyhun_Emre 3:7c22a2f849c6 195
Ceyhun_Emre 3:7c22a2f849c6 196 case 3:
Ceyhun_Emre 3:7c22a2f849c6 197 screenValue = 6;
Ceyhun_Emre 3:7c22a2f849c6 198 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 199
Ceyhun_Emre 3:7c22a2f849c6 200 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 201 lcd.printf("Please enter the\n");
Ceyhun_Emre 3:7c22a2f849c6 202 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 203 lcd.printf("file name.\n");
Ceyhun_Emre 3:7c22a2f849c6 204 lcd.locate(0, 2);
Ceyhun_Emre 3:7c22a2f849c6 205 lcd.printf("\"Enter\": Proceed\n");
Ceyhun_Emre 3:7c22a2f849c6 206 wait(3);
Ceyhun_Emre 3:7c22a2f849c6 207
Ceyhun_Emre 3:7c22a2f849c6 208 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 209 lcd.setCursor(TextLCD::CurOn_BlkOn);
Ceyhun_Emre 3:7c22a2f849c6 210
Ceyhun_Emre 3:7c22a2f849c6 211 break;
Ceyhun_Emre 3:7c22a2f849c6 212
Ceyhun_Emre 3:7c22a2f849c6 213 default :
Ceyhun_Emre 3:7c22a2f849c6 214 screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 215 }
Ceyhun_Emre 3:7c22a2f849c6 216 }
Ceyhun_Emre 3:7c22a2f849c6 217 }
Ceyhun_Emre 3:7c22a2f849c6 218
Ceyhun_Emre 3:7c22a2f849c6 219 // This function launchs the encode screen, where the user is promped to enter a text to encode, through the keyboard.
Ceyhun_Emre 3:7c22a2f849c6 220 void EncodeScreen(int& screenValue, string& textToEncode, string& encodedText, vector<string>& pairToWrite) {
Ceyhun_Emre 3:7c22a2f849c6 221 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 222 lcd.printf(textToEncode.c_str());
Ceyhun_Emre 3:7c22a2f849c6 223 unsigned char pressedChar = ps2kb.getChar();
Ceyhun_Emre 3:7c22a2f849c6 224
Ceyhun_Emre 3:7c22a2f849c6 225 // The Esc keypress sends the user back to the menu screen.
Ceyhun_Emre 3:7c22a2f849c6 226 if (pressedChar == 0x76) {
Ceyhun_Emre 3:7c22a2f849c6 227 lcd.setCursor(TextLCD::CurOff_BlkOff);
Ceyhun_Emre 3:7c22a2f849c6 228 textToEncode = "";
Ceyhun_Emre 3:7c22a2f849c6 229 screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 230 }
Ceyhun_Emre 3:7c22a2f849c6 231
Ceyhun_Emre 3:7c22a2f849c6 232 // The Enter key prompts the device to take the text as the input for the encode algorithm.
Ceyhun_Emre 3:7c22a2f849c6 233 else if (pressedChar == 0x5A) {
Ceyhun_Emre 3:7c22a2f849c6 234
Ceyhun_Emre 3:7c22a2f849c6 235 // If nothing is entered, the user is warned to do so.
Ceyhun_Emre 3:7c22a2f849c6 236 if (textToEncode.compare("") == 0) {
Ceyhun_Emre 3:7c22a2f849c6 237 lcd.setCursor(TextLCD::CurOff_BlkOff);
Ceyhun_Emre 3:7c22a2f849c6 238 lcd.printf("Please enter a text first.\n");
Ceyhun_Emre 3:7c22a2f849c6 239 wait(2);
Ceyhun_Emre 3:7c22a2f849c6 240 lcd.setCursor(TextLCD::CurOn_BlkOn);
Ceyhun_Emre 3:7c22a2f849c6 241 }
Ceyhun_Emre 3:7c22a2f849c6 242
Ceyhun_Emre 3:7c22a2f849c6 243 // If the user enters a text, this block calls in the encode function and moves to post-encode screen.
Ceyhun_Emre 3:7c22a2f849c6 244 else {
Ceyhun_Emre 3:7c22a2f849c6 245 screenValue = 4;
Ceyhun_Emre 3:7c22a2f849c6 246
Ceyhun_Emre 3:7c22a2f849c6 247 encodedText = encode(textToEncode);
Ceyhun_Emre 3:7c22a2f849c6 248 pairToWrite[0] = textToEncode;
Ceyhun_Emre 3:7c22a2f849c6 249 pairToWrite[1] = encodedText;
Ceyhun_Emre 3:7c22a2f849c6 250 textToEncode = "";
Ceyhun_Emre 3:7c22a2f849c6 251 lcd.setCursor(TextLCD::CurOff_BlkOff);
Ceyhun_Emre 3:7c22a2f849c6 252 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 253 lcd.printf("Encode Successful!\n");
Ceyhun_Emre 3:7c22a2f849c6 254 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 255 lcd.printf("Please check display\n");
Ceyhun_Emre 3:7c22a2f849c6 256 lcd.locate(0, 2);
Ceyhun_Emre 3:7c22a2f849c6 257 lcd.printf("and transmit options\n");
Ceyhun_Emre 3:7c22a2f849c6 258 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 259 lcd.printf("you want.\n");
Ceyhun_Emre 3:7c22a2f849c6 260 wait(1);
Ceyhun_Emre 3:7c22a2f849c6 261 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 262 }
Ceyhun_Emre 3:7c22a2f849c6 263 }
Ceyhun_Emre 3:7c22a2f849c6 264
Ceyhun_Emre 3:7c22a2f849c6 265 // If Backspace is pressed, a character is erased, unless there is nothing to erase.
Ceyhun_Emre 3:7c22a2f849c6 266 else if (pressedChar == 0x66) {
Ceyhun_Emre 3:7c22a2f849c6 267 if (textToEncode.compare("") != 0)
Ceyhun_Emre 3:7c22a2f849c6 268 textToEncode = textToEncode.substr(0, textToEncode.length() - 1);
Ceyhun_Emre 3:7c22a2f849c6 269 }
Ceyhun_Emre 3:7c22a2f849c6 270
Ceyhun_Emre 3:7c22a2f849c6 271 // If something else is pressed, it is appended to the textToEncode screen.
Ceyhun_Emre 3:7c22a2f849c6 272 else
Ceyhun_Emre 3:7c22a2f849c6 273 textToEncode += ps2kb.getASCII(pressedChar);
Ceyhun_Emre 3:7c22a2f849c6 274
Ceyhun_Emre 3:7c22a2f849c6 275 }
Ceyhun_Emre 3:7c22a2f849c6 276
Ceyhun_Emre 3:7c22a2f849c6 277 // This function launches the post-encode screen where the user can choose any combination of
Ceyhun_Emre 3:7c22a2f849c6 278 // transmission by LED and/or buzzer and displaying the encoded text.
Ceyhun_Emre 3:7c22a2f849c6 279 void AfterEncodeOptions(int& screenValue, int& cursorIndex, int& selectedCheckboxes, string& encodedText) {
Ceyhun_Emre 3:7c22a2f849c6 280 const int CURSOR_MAX_INDEX = 2;
Ceyhun_Emre 3:7c22a2f849c6 281
Ceyhun_Emre 3:7c22a2f849c6 282 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 283 lcd.printf(" Transmit Mode:\n");
Ceyhun_Emre 3:7c22a2f849c6 284 lcd.locate(2, 1);
Ceyhun_Emre 3:7c22a2f849c6 285 lcd.printf(" LED\n");
Ceyhun_Emre 3:7c22a2f849c6 286 lcd.locate(2, 2);
Ceyhun_Emre 3:7c22a2f849c6 287 lcd.printf(" Buzzer\n");
Ceyhun_Emre 3:7c22a2f849c6 288 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 289 lcd.printf(" Display\n");
Ceyhun_Emre 3:7c22a2f849c6 290
Ceyhun_Emre 3:7c22a2f849c6 291 // Cursor is placed into the appropriate position, which is moved by the arrow keys.
Ceyhun_Emre 3:7c22a2f849c6 292 lcd.locate(0, cursorIndex + 1);
Ceyhun_Emre 3:7c22a2f849c6 293 lcd.putc(0x00);
Ceyhun_Emre 3:7c22a2f849c6 294
Ceyhun_Emre 3:7c22a2f849c6 295 // This block prints the checkboxes next to the options, either empty or full according to user's choice.
Ceyhun_Emre 3:7c22a2f849c6 296 lcd.locate(11, 1);
Ceyhun_Emre 3:7c22a2f849c6 297 if ((selectedCheckboxes & 0x01) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 298 lcd.putc(0x01);
Ceyhun_Emre 3:7c22a2f849c6 299 else
Ceyhun_Emre 3:7c22a2f849c6 300 lcd.putc(0x02);
Ceyhun_Emre 3:7c22a2f849c6 301
Ceyhun_Emre 3:7c22a2f849c6 302 lcd.locate(11, 2);
Ceyhun_Emre 3:7c22a2f849c6 303 if ((selectedCheckboxes & 0x02) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 304 lcd.putc(0x01);
Ceyhun_Emre 3:7c22a2f849c6 305 else
Ceyhun_Emre 3:7c22a2f849c6 306 lcd.putc(0x02);
Ceyhun_Emre 3:7c22a2f849c6 307
Ceyhun_Emre 3:7c22a2f849c6 308 lcd.locate(11, 3);
Ceyhun_Emre 3:7c22a2f849c6 309 if ((selectedCheckboxes & 0x04) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 310 lcd.putc(0x01);
Ceyhun_Emre 3:7c22a2f849c6 311 else
Ceyhun_Emre 3:7c22a2f849c6 312 lcd.putc(0x02);
Ceyhun_Emre 3:7c22a2f849c6 313
Ceyhun_Emre 3:7c22a2f849c6 314 unsigned char pressedChar = ps2kb.getChar();
Ceyhun_Emre 3:7c22a2f849c6 315 int arrowValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 316
Ceyhun_Emre 3:7c22a2f849c6 317 // Same arrowValue algorithm from the main menu.
Ceyhun_Emre 3:7c22a2f849c6 318 if (ps2kb.E0flag() || !ps2kb.numlock()) {
Ceyhun_Emre 3:7c22a2f849c6 319 switch (pressedChar) {
Ceyhun_Emre 3:7c22a2f849c6 320 case 0x72 :
Ceyhun_Emre 3:7c22a2f849c6 321 arrowValue = 1;
Ceyhun_Emre 3:7c22a2f849c6 322 break;
Ceyhun_Emre 3:7c22a2f849c6 323 case 0x74 :
Ceyhun_Emre 3:7c22a2f849c6 324 arrowValue = 1;
Ceyhun_Emre 3:7c22a2f849c6 325 break;
Ceyhun_Emre 3:7c22a2f849c6 326 case 0x75 :
Ceyhun_Emre 3:7c22a2f849c6 327 arrowValue = -1;
Ceyhun_Emre 3:7c22a2f849c6 328 break;
Ceyhun_Emre 3:7c22a2f849c6 329 case 0x6B:
Ceyhun_Emre 3:7c22a2f849c6 330 arrowValue = -1;
Ceyhun_Emre 3:7c22a2f849c6 331 break;
Ceyhun_Emre 3:7c22a2f849c6 332 default:
Ceyhun_Emre 3:7c22a2f849c6 333 arrowValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 334 }
Ceyhun_Emre 3:7c22a2f849c6 335
Ceyhun_Emre 3:7c22a2f849c6 336 cursorIndex += arrowValue;
Ceyhun_Emre 3:7c22a2f849c6 337
Ceyhun_Emre 3:7c22a2f849c6 338 if (cursorIndex == -1)
Ceyhun_Emre 3:7c22a2f849c6 339 cursorIndex = 2;
Ceyhun_Emre 3:7c22a2f849c6 340
Ceyhun_Emre 3:7c22a2f849c6 341 else if (cursorIndex == (CURSOR_MAX_INDEX + 1))
Ceyhun_Emre 3:7c22a2f849c6 342 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 343
Ceyhun_Emre 3:7c22a2f849c6 344 }
Ceyhun_Emre 3:7c22a2f849c6 345
Ceyhun_Emre 3:7c22a2f849c6 346 // If Space is pressed, the device either selects or deselects the checkbox
Ceyhun_Emre 3:7c22a2f849c6 347 // depending on the previous state. This is done through the selectedCheckboxes
Ceyhun_Emre 3:7c22a2f849c6 348 // variable, which stores the states of the checkboxes in its first 3 bits.
Ceyhun_Emre 3:7c22a2f849c6 349 // The complement operation is done through bitwise AND comparison in if-else blocks
Ceyhun_Emre 3:7c22a2f849c6 350 // inside a switch-case block.
Ceyhun_Emre 3:7c22a2f849c6 351 if (pressedChar == 0x29) {
Ceyhun_Emre 3:7c22a2f849c6 352 switch (cursorIndex) {
Ceyhun_Emre 3:7c22a2f849c6 353 case 0 :
Ceyhun_Emre 3:7c22a2f849c6 354 if ((selectedCheckboxes & 0x01) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 355 selectedCheckboxes += 1;
Ceyhun_Emre 3:7c22a2f849c6 356 else
Ceyhun_Emre 3:7c22a2f849c6 357 selectedCheckboxes -= 1;
Ceyhun_Emre 3:7c22a2f849c6 358 break;
Ceyhun_Emre 3:7c22a2f849c6 359 case 1 :
Ceyhun_Emre 3:7c22a2f849c6 360 if ((selectedCheckboxes & 0x02) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 361 selectedCheckboxes += 2;
Ceyhun_Emre 3:7c22a2f849c6 362 else
Ceyhun_Emre 3:7c22a2f849c6 363 selectedCheckboxes -= 2;
Ceyhun_Emre 3:7c22a2f849c6 364 break;
Ceyhun_Emre 3:7c22a2f849c6 365 case 2 :
Ceyhun_Emre 3:7c22a2f849c6 366 if ((selectedCheckboxes & 0x04) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 367 selectedCheckboxes += 4;
Ceyhun_Emre 3:7c22a2f849c6 368 else
Ceyhun_Emre 3:7c22a2f849c6 369 selectedCheckboxes -= 4;
Ceyhun_Emre 3:7c22a2f849c6 370 break;
Ceyhun_Emre 3:7c22a2f849c6 371 }
Ceyhun_Emre 3:7c22a2f849c6 372 }
Ceyhun_Emre 3:7c22a2f849c6 373
Ceyhun_Emre 3:7c22a2f849c6 374 // When Enter is pressed, the options chosen are executed.
Ceyhun_Emre 3:7c22a2f849c6 375 else if (pressedChar == 0x5A) {
Ceyhun_Emre 3:7c22a2f849c6 376
Ceyhun_Emre 3:7c22a2f849c6 377 // If no checkbox is selected, the user is warned.
Ceyhun_Emre 3:7c22a2f849c6 378 if (selectedCheckboxes == 0) {
Ceyhun_Emre 3:7c22a2f849c6 379 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 380 lcd.printf("Please select at least 1 checkbox.\n");
Ceyhun_Emre 3:7c22a2f849c6 381 wait(2);
Ceyhun_Emre 3:7c22a2f849c6 382 }
Ceyhun_Emre 3:7c22a2f849c6 383
Ceyhun_Emre 3:7c22a2f849c6 384 else {
Ceyhun_Emre 3:7c22a2f849c6 385 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 386 // If display option is chosen, it is executed first.
Ceyhun_Emre 3:7c22a2f849c6 387 if (selectedCheckboxes >= 4)
Ceyhun_Emre 3:7c22a2f849c6 388 lcd.printf(encodedText.c_str());
Ceyhun_Emre 3:7c22a2f849c6 389
Ceyhun_Emre 3:7c22a2f849c6 390 // Transmit algorithms are checked and executed if selected.
Ceyhun_Emre 3:7c22a2f849c6 391 if ((selectedCheckboxes & 0x03) == 0x01)
Ceyhun_Emre 3:7c22a2f849c6 392 transmitLED(encodedText);
Ceyhun_Emre 3:7c22a2f849c6 393 else if ((selectedCheckboxes & 0x03) == 0x02)
Ceyhun_Emre 3:7c22a2f849c6 394 transmitBuzzer(encodedText);
Ceyhun_Emre 3:7c22a2f849c6 395 else if ((selectedCheckboxes & 0x03) == 0x03)
Ceyhun_Emre 3:7c22a2f849c6 396 transmitLEDAndBuzzer(encodedText);
Ceyhun_Emre 3:7c22a2f849c6 397 else
Ceyhun_Emre 3:7c22a2f849c6 398 wait(5); // Display duration when no transmit is selected.
Ceyhun_Emre 3:7c22a2f849c6 399
Ceyhun_Emre 3:7c22a2f849c6 400 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 401 screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 402 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 403 selectedCheckboxes = 0;
Ceyhun_Emre 3:7c22a2f849c6 404 encodedText = "";
Ceyhun_Emre 3:7c22a2f849c6 405 }
Ceyhun_Emre 3:7c22a2f849c6 406 }
Ceyhun_Emre 3:7c22a2f849c6 407 }
Ceyhun_Emre 3:7c22a2f849c6 408
Ceyhun_Emre 3:7c22a2f849c6 409 // This function launchs the encode screen, where the user is promped to enter a text to decode, through the
Ceyhun_Emre 3:7c22a2f849c6 410 // capacitive touch slider.
Ceyhun_Emre 3:7c22a2f849c6 411 void morseInputScreen(int& screenValue , string& morseInputText, vector<string>& pairToWrite) {
Ceyhun_Emre 3:7c22a2f849c6 412 // The instructions are displayed to the user, since the Morse input procedure is more
Ceyhun_Emre 3:7c22a2f849c6 413 // sophisticated than the text input.
Ceyhun_Emre 3:7c22a2f849c6 414 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 415 lcd.printf( "Press for each\n");
Ceyhun_Emre 3:7c22a2f849c6 416 lcd.locate(0,1);
Ceyhun_Emre 3:7c22a2f849c6 417 lcd.printf( "time red led is on\n");
Ceyhun_Emre 3:7c22a2f849c6 418 wait( 3);
Ceyhun_Emre 3:7c22a2f849c6 419 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 420
Ceyhun_Emre 3:7c22a2f849c6 421 lcd.printf( "bottom* of slider: -\n");
Ceyhun_Emre 3:7c22a2f849c6 422 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 423 lcd.printf( "top* of slider: .\n");
Ceyhun_Emre 3:7c22a2f849c6 424 lcd.locate(0, 2);
Ceyhun_Emre 3:7c22a2f849c6 425 lcd.printf( ".-.-.: END\n"); // End of message sequence to end the input.
Ceyhun_Emre 3:7c22a2f849c6 426 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 427 lcd.printf( "*when USB is on left");
Ceyhun_Emre 3:7c22a2f849c6 428 wait( 3);
Ceyhun_Emre 3:7c22a2f849c6 429 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 430
Ceyhun_Emre 3:7c22a2f849c6 431 lcd.printf( "Missing 1 period:\n");
Ceyhun_Emre 3:7c22a2f849c6 432 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 433 lcd.printf( "space between char.s\n");
Ceyhun_Emre 3:7c22a2f849c6 434 lcd.locate(0, 2);
Ceyhun_Emre 3:7c22a2f849c6 435 lcd.printf( "Missing 2 periods:\n");
Ceyhun_Emre 3:7c22a2f849c6 436 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 437 lcd.printf( "space between words\n");
Ceyhun_Emre 3:7c22a2f849c6 438 wait( 3);
Ceyhun_Emre 3:7c22a2f849c6 439 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 440
Ceyhun_Emre 3:7c22a2f849c6 441 // Morse input algorithm is called.
Ceyhun_Emre 3:7c22a2f849c6 442 morseInputText = morseInput();
Ceyhun_Emre 3:7c22a2f849c6 443 pairToWrite[0] = morseInputText;
Ceyhun_Emre 3:7c22a2f849c6 444 screenValue = 5;
Ceyhun_Emre 3:7c22a2f849c6 445 }
simon 0:334327d1a416 446
Ceyhun_Emre 3:7c22a2f849c6 447 // This function launches the post-morse input screen where the user can choose any combination of
Ceyhun_Emre 3:7c22a2f849c6 448 // transmission by LED and/or buzzer and decoding & displaying the decodec text.
Ceyhun_Emre 3:7c22a2f849c6 449 void afterMorseInputOptions(int& screenValue, int& cursorIndex, int& selectedCheckboxes, string& morseInputText, vector<string>& pairToWrite) {
Ceyhun_Emre 3:7c22a2f849c6 450 const int CURSOR_MAX_INDEX = 2;
Ceyhun_Emre 3:7c22a2f849c6 451
Ceyhun_Emre 3:7c22a2f849c6 452 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 453 lcd.printf(" Transmit Mode:\n");
Ceyhun_Emre 3:7c22a2f849c6 454 lcd.locate(2, 1);
Ceyhun_Emre 3:7c22a2f849c6 455 lcd.printf(" LED\n");
Ceyhun_Emre 3:7c22a2f849c6 456 lcd.locate(2, 2);
Ceyhun_Emre 3:7c22a2f849c6 457 lcd.printf(" Buzzer\n");
Ceyhun_Emre 3:7c22a2f849c6 458 lcd.locate(0, 3);
Ceyhun_Emre 3:7c22a2f849c6 459 lcd.printf(" Decode & Display\n");
Ceyhun_Emre 3:7c22a2f849c6 460
Ceyhun_Emre 3:7c22a2f849c6 461 // Cursor is placed into the appropriate position, which is moved by the arrow keys.
Ceyhun_Emre 3:7c22a2f849c6 462 lcd.locate(0, cursorIndex + 1);
Ceyhun_Emre 3:7c22a2f849c6 463 lcd.putc(0x00);
Ceyhun_Emre 3:7c22a2f849c6 464
Ceyhun_Emre 3:7c22a2f849c6 465 // This block prints the checkboxes next to the options, either empty or full according to user's choice.
Ceyhun_Emre 3:7c22a2f849c6 466 lcd.locate(11, 1);
Ceyhun_Emre 3:7c22a2f849c6 467 if ((selectedCheckboxes & 0x01) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 468 lcd.putc(0x01);
Ceyhun_Emre 3:7c22a2f849c6 469 else
Ceyhun_Emre 3:7c22a2f849c6 470 lcd.putc(0x02);
Ceyhun_Emre 3:7c22a2f849c6 471
Ceyhun_Emre 3:7c22a2f849c6 472 lcd.locate(11, 2);
Ceyhun_Emre 3:7c22a2f849c6 473 if ((selectedCheckboxes & 0x02) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 474 lcd.putc(0x01);
Ceyhun_Emre 3:7c22a2f849c6 475 else
Ceyhun_Emre 3:7c22a2f849c6 476 lcd.putc(0x02);
Ceyhun_Emre 3:7c22a2f849c6 477
Ceyhun_Emre 3:7c22a2f849c6 478 lcd.locate(20, 3);
Ceyhun_Emre 3:7c22a2f849c6 479 if ((selectedCheckboxes & 0x04) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 480 lcd.putc(0x01);
Ceyhun_Emre 3:7c22a2f849c6 481 else
Ceyhun_Emre 3:7c22a2f849c6 482 lcd.putc(0x02);
Ceyhun_Emre 3:7c22a2f849c6 483
Ceyhun_Emre 3:7c22a2f849c6 484 unsigned char pressedChar = ps2kb.getChar();
Ceyhun_Emre 3:7c22a2f849c6 485 int arrowValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 486
Ceyhun_Emre 3:7c22a2f849c6 487 // Same arrowValue algorithm from the main menu.
Ceyhun_Emre 3:7c22a2f849c6 488 if (ps2kb.E0flag() || !ps2kb.numlock()) {
Ceyhun_Emre 3:7c22a2f849c6 489 switch (pressedChar) {
Ceyhun_Emre 3:7c22a2f849c6 490 case 0x72 :
Ceyhun_Emre 3:7c22a2f849c6 491 arrowValue = 1;
Ceyhun_Emre 3:7c22a2f849c6 492 break;
Ceyhun_Emre 3:7c22a2f849c6 493 case 0x74 :
Ceyhun_Emre 3:7c22a2f849c6 494 arrowValue = 1;
Ceyhun_Emre 3:7c22a2f849c6 495 break;
Ceyhun_Emre 3:7c22a2f849c6 496 case 0x75 :
Ceyhun_Emre 3:7c22a2f849c6 497 arrowValue = -1;
Ceyhun_Emre 3:7c22a2f849c6 498 break;
Ceyhun_Emre 3:7c22a2f849c6 499 case 0x6B:
Ceyhun_Emre 3:7c22a2f849c6 500 arrowValue = -1;
Ceyhun_Emre 3:7c22a2f849c6 501 break;
Ceyhun_Emre 3:7c22a2f849c6 502 default:
Ceyhun_Emre 3:7c22a2f849c6 503 arrowValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 504 }
Ceyhun_Emre 3:7c22a2f849c6 505
Ceyhun_Emre 3:7c22a2f849c6 506 cursorIndex += arrowValue;
Ceyhun_Emre 3:7c22a2f849c6 507
Ceyhun_Emre 3:7c22a2f849c6 508 if (cursorIndex == -1)
Ceyhun_Emre 3:7c22a2f849c6 509 cursorIndex = 2;
Ceyhun_Emre 3:7c22a2f849c6 510
Ceyhun_Emre 3:7c22a2f849c6 511 else if (cursorIndex == (CURSOR_MAX_INDEX + 1))
Ceyhun_Emre 3:7c22a2f849c6 512 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 513
Ceyhun_Emre 3:7c22a2f849c6 514 }
Ceyhun_Emre 3:7c22a2f849c6 515
Ceyhun_Emre 3:7c22a2f849c6 516 // Same checkbox algorithm from the post-encode screen.
Ceyhun_Emre 3:7c22a2f849c6 517 if (pressedChar == 0x29) {
Ceyhun_Emre 3:7c22a2f849c6 518 switch (cursorIndex) {
Ceyhun_Emre 3:7c22a2f849c6 519 case 0 :
Ceyhun_Emre 3:7c22a2f849c6 520 if ((selectedCheckboxes & 0x01) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 521 selectedCheckboxes += 1;
Ceyhun_Emre 3:7c22a2f849c6 522 else
Ceyhun_Emre 3:7c22a2f849c6 523 selectedCheckboxes -= 1;
Ceyhun_Emre 3:7c22a2f849c6 524 break;
Ceyhun_Emre 3:7c22a2f849c6 525 case 1 :
Ceyhun_Emre 3:7c22a2f849c6 526 if ((selectedCheckboxes & 0x02) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 527 selectedCheckboxes += 2;
Ceyhun_Emre 3:7c22a2f849c6 528 else
Ceyhun_Emre 3:7c22a2f849c6 529 selectedCheckboxes -= 2;
Ceyhun_Emre 3:7c22a2f849c6 530 break;
Ceyhun_Emre 3:7c22a2f849c6 531 case 2 :
Ceyhun_Emre 3:7c22a2f849c6 532 if ((selectedCheckboxes & 0x04) == 0x00)
Ceyhun_Emre 3:7c22a2f849c6 533 selectedCheckboxes += 4;
Ceyhun_Emre 3:7c22a2f849c6 534 else
Ceyhun_Emre 3:7c22a2f849c6 535 selectedCheckboxes -= 4;
Ceyhun_Emre 3:7c22a2f849c6 536 break;
Ceyhun_Emre 3:7c22a2f849c6 537 }
Ceyhun_Emre 3:7c22a2f849c6 538 }
Ceyhun_Emre 3:7c22a2f849c6 539
Ceyhun_Emre 3:7c22a2f849c6 540 // When Enter is pressed, the options chosen are executed.
Ceyhun_Emre 3:7c22a2f849c6 541 else if (pressedChar == 0x5A) {
Ceyhun_Emre 3:7c22a2f849c6 542
Ceyhun_Emre 3:7c22a2f849c6 543 // If no checkbox is selected, the user is warned.
Ceyhun_Emre 3:7c22a2f849c6 544 if (selectedCheckboxes == 0) {
Ceyhun_Emre 3:7c22a2f849c6 545 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 546 lcd.printf("Please select at least 1 checkbox.\n");
Ceyhun_Emre 3:7c22a2f849c6 547 wait(1);
Ceyhun_Emre 3:7c22a2f849c6 548 }
Ceyhun_Emre 3:7c22a2f849c6 549
Ceyhun_Emre 3:7c22a2f849c6 550 else {
Ceyhun_Emre 3:7c22a2f849c6 551 // If decode & display option is chosen, it is executed first.
Ceyhun_Emre 3:7c22a2f849c6 552 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 553 if (selectedCheckboxes >= 4) {
Ceyhun_Emre 3:7c22a2f849c6 554 string decodedText = decode( morseInputText);
Ceyhun_Emre 3:7c22a2f849c6 555 pairToWrite[1] = decodedText;
Ceyhun_Emre 3:7c22a2f849c6 556 lcd.printf(decodedText.c_str());
Ceyhun_Emre 3:7c22a2f849c6 557 }
Ceyhun_Emre 3:7c22a2f849c6 558 else
Ceyhun_Emre 3:7c22a2f849c6 559 pairToWrite[1] = ""; // Empty string is written to the SD card variable, if decode is not selected
Ceyhun_Emre 3:7c22a2f849c6 560
Ceyhun_Emre 3:7c22a2f849c6 561 // Transmit algorithms are checked and executed if selected.
Ceyhun_Emre 3:7c22a2f849c6 562 if ((selectedCheckboxes & 0x03) == 0x01)
Ceyhun_Emre 3:7c22a2f849c6 563 transmitLED(morseInputText);
Ceyhun_Emre 3:7c22a2f849c6 564 else if ((selectedCheckboxes & 0x03) == 0x02)
Ceyhun_Emre 3:7c22a2f849c6 565 transmitBuzzer(morseInputText);
Ceyhun_Emre 3:7c22a2f849c6 566 else if ((selectedCheckboxes & 0x03) == 0x03)
Ceyhun_Emre 3:7c22a2f849c6 567 transmitLEDAndBuzzer(morseInputText);
Ceyhun_Emre 3:7c22a2f849c6 568 else
Ceyhun_Emre 3:7c22a2f849c6 569 wait(5); // Display duration when no transmit is selected.
Ceyhun_Emre 3:7c22a2f849c6 570
Ceyhun_Emre 3:7c22a2f849c6 571 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 572 screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 573 cursorIndex = 0;
Ceyhun_Emre 3:7c22a2f849c6 574 selectedCheckboxes = 0;
Ceyhun_Emre 3:7c22a2f849c6 575 morseInputText = "";
Ceyhun_Emre 3:7c22a2f849c6 576 }
Ceyhun_Emre 3:7c22a2f849c6 577 }
Ceyhun_Emre 3:7c22a2f849c6 578 }
Ceyhun_Emre 3:7c22a2f849c6 579
Ceyhun_Emre 3:7c22a2f849c6 580 // This function is called when emergency SOS screen is chosen. This screen simply transmits
Ceyhun_Emre 3:7c22a2f849c6 581 // an SOS signal through LED and buzzer in an infinite loop until being interrupted by the user.
Ceyhun_Emre 3:7c22a2f849c6 582 void EmergencySOS(int& screenValue){
Ceyhun_Emre 3:7c22a2f849c6 583 const string SOS_MORSE_CODE = "...---..."; // SOS Morse code string
Ceyhun_Emre 3:7c22a2f849c6 584 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 585 lcd.printf( "Press touch slider\n");
Ceyhun_Emre 3:7c22a2f849c6 586 lcd.locate(0, 1);
Ceyhun_Emre 3:7c22a2f849c6 587 lcd.printf( "to cancel SOS signal\n");
Ceyhun_Emre 3:7c22a2f849c6 588
Ceyhun_Emre 3:7c22a2f849c6 589 percentageNonZero = false;
Ceyhun_Emre 3:7c22a2f849c6 590 callCheckSOS.attach( &checkSOS, 0.05); // Attachs the callCheckSOS Ticker to checkSOS function
Ceyhun_Emre 3:7c22a2f849c6 591 do {
Ceyhun_Emre 3:7c22a2f849c6 592 transmitLEDAndBuzzer( SOS_MORSE_CODE);
Ceyhun_Emre 3:7c22a2f849c6 593 wait(0.75);
Ceyhun_Emre 3:7c22a2f849c6 594 } while ( !stopSOS);
Ceyhun_Emre 3:7c22a2f849c6 595
Ceyhun_Emre 3:7c22a2f849c6 596 callCheckSOS.detach();
Ceyhun_Emre 3:7c22a2f849c6 597 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 598 screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 599 }
Ceyhun_Emre 3:7c22a2f849c6 600
Ceyhun_Emre 3:7c22a2f849c6 601 // This function is called whenever the interrupt timer overflows in 50 ms. It checks the capacitive touchslider
Ceyhun_Emre 3:7c22a2f849c6 602 // output. If it is different than 0 (in other words, it it is touched at least once), the stopSOS boolean is
Ceyhun_Emre 3:7c22a2f849c6 603 // set true and the do while loop above is exited, which terminates the distress signal.
Ceyhun_Emre 3:7c22a2f849c6 604 void checkSOS() {
Ceyhun_Emre 3:7c22a2f849c6 605 if ( !percentageNonZero && touchSlider.readPercentage() == 0) {
Ceyhun_Emre 3:7c22a2f849c6 606 stopSOS = false;
Ceyhun_Emre 3:7c22a2f849c6 607 }
Ceyhun_Emre 3:7c22a2f849c6 608 else {
Ceyhun_Emre 3:7c22a2f849c6 609 stopSOS = true;
Ceyhun_Emre 3:7c22a2f849c6 610 percentageNonZero = true;
Ceyhun_Emre 3:7c22a2f849c6 611 }
Ceyhun_Emre 3:7c22a2f849c6 612 }
Ceyhun_Emre 3:7c22a2f849c6 613
Ceyhun_Emre 3:7c22a2f849c6 614 // This function calls in the write-to-SD-card screen where the user can write the last
Ceyhun_Emre 3:7c22a2f849c6 615 // text-Morse code pair (either encoded or decoded into the microSD card in the adapter connected to the device.
Ceyhun_Emre 3:7c22a2f849c6 616 void writeToSDScreen(int& screenValue, vector<string>& pairToWrite, string& fileName) {
Ceyhun_Emre 3:7c22a2f849c6 617 SDFileSystem sdCard(D11, D12, D13, D10, "SD CARD");
Ceyhun_Emre 3:7c22a2f849c6 618 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 619 lcd.printf(fileName.c_str());
Ceyhun_Emre 3:7c22a2f849c6 620 unsigned char pressedChar = ps2kb.getChar();
Ceyhun_Emre 3:7c22a2f849c6 621
Ceyhun_Emre 3:7c22a2f849c6 622 // The user is promped to enter the file name.
Ceyhun_Emre 3:7c22a2f849c6 623 if (pressedChar == 0x5A) {
Ceyhun_Emre 3:7c22a2f849c6 624
Ceyhun_Emre 3:7c22a2f849c6 625 // The user is warned if no text is entered.
Ceyhun_Emre 3:7c22a2f849c6 626 if (fileName.compare("") == 0) {
Ceyhun_Emre 3:7c22a2f849c6 627 lcd.setCursor(TextLCD::CurOff_BlkOff);
Ceyhun_Emre 3:7c22a2f849c6 628 lcd.printf("Please enter a text first.\n");
Ceyhun_Emre 3:7c22a2f849c6 629 wait(2);
Ceyhun_Emre 3:7c22a2f849c6 630 lcd.setCursor(TextLCD::CurOn_BlkOn);
Ceyhun_Emre 3:7c22a2f849c6 631 }
Ceyhun_Emre 3:7c22a2f849c6 632
Ceyhun_Emre 3:7c22a2f849c6 633 // Functions of the SD card library are called to write the text and morse code
Ceyhun_Emre 3:7c22a2f849c6 634 // texts into the card.
Ceyhun_Emre 3:7c22a2f849c6 635 else {
Ceyhun_Emre 3:7c22a2f849c6 636 lcd.setCursor(TextLCD::CurOff_BlkOff);
Ceyhun_Emre 3:7c22a2f849c6 637 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 638 screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 639
Ceyhun_Emre 3:7c22a2f849c6 640 string fileDirectory = "/SD CARD/morse_enc_dec_dev/"; // Pre-defined directory.
Ceyhun_Emre 3:7c22a2f849c6 641 mkdir("/SD CARD/morse_enc_dec_dev", 0777);
Ceyhun_Emre 3:7c22a2f849c6 642 fileDirectory += fileName; // fileName is added to the fileDirectory.
Ceyhun_Emre 3:7c22a2f849c6 643 fileDirectory += ".txt";
Ceyhun_Emre 3:7c22a2f849c6 644 FILE *fp = fopen(fileDirectory.c_str(), "w");
Ceyhun_Emre 3:7c22a2f849c6 645 if (fp == NULL) {
Ceyhun_Emre 3:7c22a2f849c6 646 error("Could not open file for write\n");
Ceyhun_Emre 3:7c22a2f849c6 647 }
Ceyhun_Emre 3:7c22a2f849c6 648 string textToWrite = pairToWrite[0] + " " + pairToWrite[1];
Ceyhun_Emre 3:7c22a2f849c6 649 fprintf(fp, textToWrite.c_str());
Ceyhun_Emre 3:7c22a2f849c6 650 fclose(fp);
Ceyhun_Emre 3:7c22a2f849c6 651
Ceyhun_Emre 3:7c22a2f849c6 652 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 653 lcd.printf("Write to SD is successful!"); // Success message
Ceyhun_Emre 3:7c22a2f849c6 654 wait(2);
Ceyhun_Emre 3:7c22a2f849c6 655 lcd.cls();
Ceyhun_Emre 3:7c22a2f849c6 656
Ceyhun_Emre 3:7c22a2f849c6 657 screenValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 658 fileName = "";
Ceyhun_Emre 3:7c22a2f849c6 659 lcd.setCursor(TextLCD::CurOff_BlkOff);
Ceyhun_Emre 3:7c22a2f849c6 660
Ceyhun_Emre 3:7c22a2f849c6 661 }
Ceyhun_Emre 3:7c22a2f849c6 662 }
Ceyhun_Emre 3:7c22a2f849c6 663
Ceyhun_Emre 3:7c22a2f849c6 664 else if (pressedChar == 0x66) {
Ceyhun_Emre 3:7c22a2f849c6 665 if (fileName.compare("") != 0)
Ceyhun_Emre 3:7c22a2f849c6 666 fileName = fileName.substr(0, fileName.length() - 1);
Ceyhun_Emre 3:7c22a2f849c6 667 }
Ceyhun_Emre 3:7c22a2f849c6 668
Ceyhun_Emre 3:7c22a2f849c6 669 else
Ceyhun_Emre 3:7c22a2f849c6 670 fileName += ps2kb.getASCII(pressedChar);
Ceyhun_Emre 3:7c22a2f849c6 671
simon 0:334327d1a416 672 }
Ceyhun_Emre 3:7c22a2f849c6 673
Ceyhun_Emre 3:7c22a2f849c6 674 // This function takes text input as string and uses the binary Morse tree, coded
Ceyhun_Emre 3:7c22a2f849c6 675 // in if-else blocks, to convert it to Morse code. It outputs the result as a string.
Ceyhun_Emre 3:7c22a2f849c6 676 string encode( string textToEncode) {
Ceyhun_Emre 3:7c22a2f849c6 677 string encodedText = "";
Ceyhun_Emre 3:7c22a2f849c6 678 char checkedLetter;
Ceyhun_Emre 3:7c22a2f849c6 679
Ceyhun_Emre 3:7c22a2f849c6 680 for ( short int counter = 0; counter < textToEncode.length(); counter++) {
Ceyhun_Emre 3:7c22a2f849c6 681 checkedLetter = textToEncode.at( counter);
Ceyhun_Emre 3:7c22a2f849c6 682 if ( checkedLetter == ' ') {
Ceyhun_Emre 3:7c22a2f849c6 683 encodedText += '/'; // '/' for the space between words ( 4 unit
Ceyhun_Emre 3:7c22a2f849c6 684 // times for this char => 7 unit times for space between words)
Ceyhun_Emre 3:7c22a2f849c6 685 }
Ceyhun_Emre 3:7c22a2f849c6 686 else if ( checkedLetter == 'e' || checkedLetter == 'i' || checkedLetter == 'a'
Ceyhun_Emre 3:7c22a2f849c6 687 || checkedLetter == 's' || checkedLetter == 'u' || checkedLetter == 'r'
Ceyhun_Emre 3:7c22a2f849c6 688 || checkedLetter == 'w' || checkedLetter == 'h' || checkedLetter == 'v'
Ceyhun_Emre 3:7c22a2f849c6 689 || checkedLetter == 'f' || checkedLetter == 'l' || checkedLetter == 'p'
Ceyhun_Emre 3:7c22a2f849c6 690 || checkedLetter == 'j' || checkedLetter == '5' || checkedLetter == '4'
Ceyhun_Emre 3:7c22a2f849c6 691 || checkedLetter == '3' || checkedLetter == '2' || checkedLetter == '1' ) {
Ceyhun_Emre 3:7c22a2f849c6 692 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 693 if ( checkedLetter == 'i' || checkedLetter == 's' || checkedLetter == 'u'
Ceyhun_Emre 3:7c22a2f849c6 694 || checkedLetter == 'h' || checkedLetter == 'v' || checkedLetter == 'f'
Ceyhun_Emre 3:7c22a2f849c6 695 || checkedLetter == '5' || checkedLetter == '4' || checkedLetter == '3'
Ceyhun_Emre 3:7c22a2f849c6 696 || checkedLetter == '2') {
Ceyhun_Emre 3:7c22a2f849c6 697 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 698 if ( checkedLetter == 's' || checkedLetter == 'h' || checkedLetter == 'v'
Ceyhun_Emre 3:7c22a2f849c6 699 || checkedLetter == '5' || checkedLetter == '4' || checkedLetter == '3') {
Ceyhun_Emre 3:7c22a2f849c6 700 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 701 if ( checkedLetter == 'h' || checkedLetter == '5'
Ceyhun_Emre 3:7c22a2f849c6 702 || checkedLetter == '4') {
Ceyhun_Emre 3:7c22a2f849c6 703 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 704 if ( checkedLetter == '5') {
Ceyhun_Emre 3:7c22a2f849c6 705 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 706 }
Ceyhun_Emre 3:7c22a2f849c6 707 else if ( checkedLetter != 'h') { // checkedLetter = '4'
Ceyhun_Emre 3:7c22a2f849c6 708 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 709 }
Ceyhun_Emre 3:7c22a2f849c6 710 }
Ceyhun_Emre 3:7c22a2f849c6 711 else if ( checkedLetter != 's') {
Ceyhun_Emre 3:7c22a2f849c6 712 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 713 if ( checkedLetter == '3') {
Ceyhun_Emre 3:7c22a2f849c6 714 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 715 }
Ceyhun_Emre 3:7c22a2f849c6 716 }
Ceyhun_Emre 3:7c22a2f849c6 717 }
Ceyhun_Emre 3:7c22a2f849c6 718 else if ( checkedLetter != 'i'){
Ceyhun_Emre 3:7c22a2f849c6 719 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 720 if ( checkedLetter == 'f') {
Ceyhun_Emre 3:7c22a2f849c6 721 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 722 }
Ceyhun_Emre 3:7c22a2f849c6 723 else if ( checkedLetter != 'u'){ // checkedLetter = '2'
Ceyhun_Emre 3:7c22a2f849c6 724 encodedText += "--";
Ceyhun_Emre 3:7c22a2f849c6 725 }
Ceyhun_Emre 3:7c22a2f849c6 726 }
Ceyhun_Emre 3:7c22a2f849c6 727 }
Ceyhun_Emre 3:7c22a2f849c6 728 else if ( checkedLetter != 'e') {
Ceyhun_Emre 3:7c22a2f849c6 729 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 730 if ( checkedLetter == 'r' || checkedLetter == 'l') {
Ceyhun_Emre 3:7c22a2f849c6 731 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 732 if ( checkedLetter == 'l') {
Ceyhun_Emre 3:7c22a2f849c6 733 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 734 }
Ceyhun_Emre 3:7c22a2f849c6 735 }
Ceyhun_Emre 3:7c22a2f849c6 736 else if ( checkedLetter != 'a'){
Ceyhun_Emre 3:7c22a2f849c6 737 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 738 if ( checkedLetter == 'p') {
Ceyhun_Emre 3:7c22a2f849c6 739 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 740 }
Ceyhun_Emre 3:7c22a2f849c6 741 else if ( checkedLetter != 'w'){
Ceyhun_Emre 3:7c22a2f849c6 742 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 743 if ( checkedLetter == '1') {
Ceyhun_Emre 3:7c22a2f849c6 744 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 745 }
Ceyhun_Emre 3:7c22a2f849c6 746 }
Ceyhun_Emre 3:7c22a2f849c6 747 }
Ceyhun_Emre 3:7c22a2f849c6 748 }
Ceyhun_Emre 3:7c22a2f849c6 749 }
Ceyhun_Emre 3:7c22a2f849c6 750 else {
Ceyhun_Emre 3:7c22a2f849c6 751 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 752 if ( checkedLetter == 'n' || checkedLetter == 'd' || checkedLetter == 'k'
Ceyhun_Emre 3:7c22a2f849c6 753 || checkedLetter == 'b' || checkedLetter == 'x' || checkedLetter == 'c'
Ceyhun_Emre 3:7c22a2f849c6 754 || checkedLetter == 'y' || checkedLetter == '6') {
Ceyhun_Emre 3:7c22a2f849c6 755 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 756 if ( checkedLetter == 'd' || checkedLetter == 'b'
Ceyhun_Emre 3:7c22a2f849c6 757 || checkedLetter == 'x' || checkedLetter == '6') {
Ceyhun_Emre 3:7c22a2f849c6 758 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 759 if ( checkedLetter == 'b' || checkedLetter == '6') {
Ceyhun_Emre 3:7c22a2f849c6 760 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 761 if ( checkedLetter == '6') {
Ceyhun_Emre 3:7c22a2f849c6 762 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 763 }
Ceyhun_Emre 3:7c22a2f849c6 764 }
Ceyhun_Emre 3:7c22a2f849c6 765 else if ( checkedLetter != 'd') { //checkedLetter = 'x'
Ceyhun_Emre 3:7c22a2f849c6 766 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 767 }
Ceyhun_Emre 3:7c22a2f849c6 768 }
Ceyhun_Emre 3:7c22a2f849c6 769 else if ( checkedLetter != 'n'){
Ceyhun_Emre 3:7c22a2f849c6 770 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 771 if ( checkedLetter == 'c') {
Ceyhun_Emre 3:7c22a2f849c6 772 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 773 }
Ceyhun_Emre 3:7c22a2f849c6 774 else if ( checkedLetter != 'k') { // checkedLetter = 'y'
Ceyhun_Emre 3:7c22a2f849c6 775 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 776 }
Ceyhun_Emre 3:7c22a2f849c6 777 }
Ceyhun_Emre 3:7c22a2f849c6 778 }
Ceyhun_Emre 3:7c22a2f849c6 779 else if ( checkedLetter != 't'){
Ceyhun_Emre 3:7c22a2f849c6 780 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 781 if ( checkedLetter == 'g' || checkedLetter == 'z' || checkedLetter == 'q'
Ceyhun_Emre 3:7c22a2f849c6 782 || checkedLetter == '7') {
Ceyhun_Emre 3:7c22a2f849c6 783 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 784 if ( checkedLetter == 'z' || checkedLetter == '7') {
Ceyhun_Emre 3:7c22a2f849c6 785 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 786 if ( checkedLetter == '7') {
Ceyhun_Emre 3:7c22a2f849c6 787 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 788 }
Ceyhun_Emre 3:7c22a2f849c6 789 }
Ceyhun_Emre 3:7c22a2f849c6 790 else if ( checkedLetter != 'g') { // checkedLetter = 'q'
Ceyhun_Emre 3:7c22a2f849c6 791 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 792 }
Ceyhun_Emre 3:7c22a2f849c6 793 }
Ceyhun_Emre 3:7c22a2f849c6 794 else if ( checkedLetter != 'm') {
Ceyhun_Emre 3:7c22a2f849c6 795 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 796 if ( checkedLetter == '8') {
Ceyhun_Emre 3:7c22a2f849c6 797 encodedText += "..";
Ceyhun_Emre 3:7c22a2f849c6 798 }
Ceyhun_Emre 3:7c22a2f849c6 799 else if ( checkedLetter != 'o'){
Ceyhun_Emre 3:7c22a2f849c6 800 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 801 if ( checkedLetter == '9') {
Ceyhun_Emre 3:7c22a2f849c6 802 encodedText += '.';
Ceyhun_Emre 3:7c22a2f849c6 803 }
Ceyhun_Emre 3:7c22a2f849c6 804 else { //checkedLetter = 0
Ceyhun_Emre 3:7c22a2f849c6 805 encodedText += '-';
Ceyhun_Emre 3:7c22a2f849c6 806 }
Ceyhun_Emre 3:7c22a2f849c6 807 }
Ceyhun_Emre 3:7c22a2f849c6 808 }
Ceyhun_Emre 3:7c22a2f849c6 809 }
Ceyhun_Emre 3:7c22a2f849c6 810 }
Ceyhun_Emre 3:7c22a2f849c6 811 encodedText += ' '; // ' ' for the space between letters ( 3 unit
Ceyhun_Emre 3:7c22a2f849c6 812 //times for this char)
Ceyhun_Emre 3:7c22a2f849c6 813 }
Ceyhun_Emre 3:7c22a2f849c6 814 return encodedText;
Ceyhun_Emre 3:7c22a2f849c6 815 }
Ceyhun_Emre 3:7c22a2f849c6 816
Ceyhun_Emre 3:7c22a2f849c6 817 // This function is called to take Morse code input from the capacitive touchslider.
Ceyhun_Emre 3:7c22a2f849c6 818 string morseInput() {
Ceyhun_Emre 3:7c22a2f849c6 819 //for taking input
Ceyhun_Emre 3:7c22a2f849c6 820 //constants
Ceyhun_Emre 3:7c22a2f849c6 821 const double unitTimeForDecoding = 1; // 250 miliseconds
Ceyhun_Emre 3:7c22a2f849c6 822
Ceyhun_Emre 3:7c22a2f849c6 823 //variables
Ceyhun_Emre 3:7c22a2f849c6 824 string textToDecode;
Ceyhun_Emre 3:7c22a2f849c6 825 short int numberOfSpaces; // to check if a word has ended
Ceyhun_Emre 3:7c22a2f849c6 826 float currentValue;
Ceyhun_Emre 3:7c22a2f849c6 827 float pressedValue;
Ceyhun_Emre 3:7c22a2f849c6 828 string addLetter;
Ceyhun_Emre 3:7c22a2f849c6 829 bool change;
Ceyhun_Emre 3:7c22a2f849c6 830 bool takeMorseInput;
Ceyhun_Emre 3:7c22a2f849c6 831
Ceyhun_Emre 3:7c22a2f849c6 832 //initialization
Ceyhun_Emre 3:7c22a2f849c6 833 textToDecode = "";
Ceyhun_Emre 3:7c22a2f849c6 834 numberOfSpaces = 0;
Ceyhun_Emre 3:7c22a2f849c6 835 change = true;
Ceyhun_Emre 3:7c22a2f849c6 836 takeMorseInput = true;
Ceyhun_Emre 3:7c22a2f849c6 837
Ceyhun_Emre 3:7c22a2f849c6 838 wait( unitTimeForDecoding);
Ceyhun_Emre 3:7c22a2f849c6 839 while( takeMorseInput) {
Ceyhun_Emre 3:7c22a2f849c6 840 led = 0; //indicating that input can be taken now
Ceyhun_Emre 3:7c22a2f849c6 841 for ( short int counter = 0; counter < 200; counter++) {
Ceyhun_Emre 3:7c22a2f849c6 842 wait( unitTimeForDecoding / 200);
Ceyhun_Emre 3:7c22a2f849c6 843 if ( ( currentValue = touchSlider.readPercentage()) > 0 && change) {
Ceyhun_Emre 3:7c22a2f849c6 844 change = false;
Ceyhun_Emre 3:7c22a2f849c6 845 pressedValue = currentValue;
Ceyhun_Emre 3:7c22a2f849c6 846 }
Ceyhun_Emre 3:7c22a2f849c6 847
Ceyhun_Emre 3:7c22a2f849c6 848 if ( touchSlider.readPercentage() == 0 && change) {
Ceyhun_Emre 3:7c22a2f849c6 849 pressedValue = 0;
Ceyhun_Emre 3:7c22a2f849c6 850 }
Ceyhun_Emre 3:7c22a2f849c6 851 }
Ceyhun_Emre 3:7c22a2f849c6 852 change = true;
Ceyhun_Emre 3:7c22a2f849c6 853 led = 1; //indicating pause
Ceyhun_Emre 3:7c22a2f849c6 854
Ceyhun_Emre 3:7c22a2f849c6 855 if ( pressedValue <= 0) {
Ceyhun_Emre 3:7c22a2f849c6 856 addLetter = " ";
Ceyhun_Emre 3:7c22a2f849c6 857 numberOfSpaces++;
Ceyhun_Emre 3:7c22a2f849c6 858 if ( numberOfSpaces >= 2) { // seperate words
Ceyhun_Emre 3:7c22a2f849c6 859 addLetter = "/ ";
Ceyhun_Emre 3:7c22a2f849c6 860 }
Ceyhun_Emre 3:7c22a2f849c6 861 }
Ceyhun_Emre 3:7c22a2f849c6 862 else if ( 0.5 < pressedValue && pressedValue <= 1) {
Ceyhun_Emre 3:7c22a2f849c6 863 addLetter = "-";
Ceyhun_Emre 3:7c22a2f849c6 864 numberOfSpaces = 0;
Ceyhun_Emre 3:7c22a2f849c6 865 }
Ceyhun_Emre 3:7c22a2f849c6 866 else {
Ceyhun_Emre 3:7c22a2f849c6 867 addLetter = ".";
Ceyhun_Emre 3:7c22a2f849c6 868 numberOfSpaces = 0;
Ceyhun_Emre 3:7c22a2f849c6 869 }
Ceyhun_Emre 3:7c22a2f849c6 870 textToDecode += addLetter;
Ceyhun_Emre 3:7c22a2f849c6 871 wait( unitTimeForDecoding);
Ceyhun_Emre 3:7c22a2f849c6 872
Ceyhun_Emre 3:7c22a2f849c6 873 if ( textToDecode.length() > 6) {
Ceyhun_Emre 3:7c22a2f849c6 874 if ( ( textToDecode.substr( textToDecode.length() - 6, 6)).compare(" .-.-.") == 0) {
Ceyhun_Emre 3:7c22a2f849c6 875 takeMorseInput = false;
Ceyhun_Emre 3:7c22a2f849c6 876 }
Ceyhun_Emre 3:7c22a2f849c6 877 }
Ceyhun_Emre 3:7c22a2f849c6 878 }
Ceyhun_Emre 3:7c22a2f849c6 879
Ceyhun_Emre 3:7c22a2f849c6 880 if ( textToDecode.at( 0) == ' ') {
Ceyhun_Emre 3:7c22a2f849c6 881 textToDecode = textToDecode.substr( (int) textToDecode.find_first_not_of( " /"),
Ceyhun_Emre 3:7c22a2f849c6 882 textToDecode.length() - 6 - (int) textToDecode.find_first_not_of( " /"));
Ceyhun_Emre 3:7c22a2f849c6 883 }
Ceyhun_Emre 3:7c22a2f849c6 884 else {
Ceyhun_Emre 3:7c22a2f849c6 885 textToDecode = textToDecode.substr( 0, textToDecode.length() - 6);
Ceyhun_Emre 3:7c22a2f849c6 886 }
Ceyhun_Emre 3:7c22a2f849c6 887 return textToDecode;
Ceyhun_Emre 3:7c22a2f849c6 888 }
Ceyhun_Emre 3:7c22a2f849c6 889
Ceyhun_Emre 3:7c22a2f849c6 890 // This function decodes the Morse code taken as a string and returns the text result as
Ceyhun_Emre 3:7c22a2f849c6 891 // a string. It uses again the binary Morse code tree, coded inside the findChar function.
Ceyhun_Emre 3:7c22a2f849c6 892 string decode( string textToDecode) {
Ceyhun_Emre 3:7c22a2f849c6 893 //for decoding
Ceyhun_Emre 3:7c22a2f849c6 894 //variables
Ceyhun_Emre 3:7c22a2f849c6 895 string decodedText;
Ceyhun_Emre 3:7c22a2f849c6 896 string codeForEachLetter;
Ceyhun_Emre 3:7c22a2f849c6 897
Ceyhun_Emre 3:7c22a2f849c6 898 //initialization
Ceyhun_Emre 3:7c22a2f849c6 899 decodedText = "";
Ceyhun_Emre 3:7c22a2f849c6 900 codeForEachLetter = "";
Ceyhun_Emre 3:7c22a2f849c6 901
Ceyhun_Emre 3:7c22a2f849c6 902 // decodes all characters other than the last one
Ceyhun_Emre 3:7c22a2f849c6 903 while ( textToDecode.find( " ") != std::string::npos) {
Ceyhun_Emre 3:7c22a2f849c6 904 codeForEachLetter = textToDecode.substr(0, textToDecode.find( " "));
Ceyhun_Emre 3:7c22a2f849c6 905 decodedText += findChar( codeForEachLetter);
Ceyhun_Emre 3:7c22a2f849c6 906 textToDecode = textToDecode.substr( textToDecode.find( " ") + 1,
Ceyhun_Emre 3:7c22a2f849c6 907 textToDecode.length()
Ceyhun_Emre 3:7c22a2f849c6 908 - ( textToDecode.find( " ") + 1));
Ceyhun_Emre 3:7c22a2f849c6 909 }
Ceyhun_Emre 3:7c22a2f849c6 910 //decodes the last character
Ceyhun_Emre 3:7c22a2f849c6 911 decodedText += findChar( textToDecode);
Ceyhun_Emre 3:7c22a2f849c6 912
Ceyhun_Emre 3:7c22a2f849c6 913 return decodedText;
Ceyhun_Emre 3:7c22a2f849c6 914
Ceyhun_Emre 3:7c22a2f849c6 915 }
Ceyhun_Emre 3:7c22a2f849c6 916
Ceyhun_Emre 3:7c22a2f849c6 917 //This function performs the conversion to character while decoding. It is based
Ceyhun_Emre 3:7c22a2f849c6 918 //on binary Morse code tree.
Ceyhun_Emre 3:7c22a2f849c6 919 char findChar( string code) {
Ceyhun_Emre 3:7c22a2f849c6 920 if ( code.at( 0) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 921 if ( code.length() != 1) {
Ceyhun_Emre 3:7c22a2f849c6 922 if ( code.at( 1) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 923 if ( code.length() != 2) {
Ceyhun_Emre 3:7c22a2f849c6 924 if ( code.at( 2) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 925 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 926 if ( code.at( 3) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 927 if ( code.length() != 4) {
Ceyhun_Emre 3:7c22a2f849c6 928 if ( code.at( 4) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 929 return '5';
Ceyhun_Emre 3:7c22a2f849c6 930 }
Ceyhun_Emre 3:7c22a2f849c6 931 return '4';
Ceyhun_Emre 3:7c22a2f849c6 932 }
Ceyhun_Emre 3:7c22a2f849c6 933 return 'H';
Ceyhun_Emre 3:7c22a2f849c6 934 }
Ceyhun_Emre 3:7c22a2f849c6 935 else {
Ceyhun_Emre 3:7c22a2f849c6 936 if ( code.length() != 4) {
Ceyhun_Emre 3:7c22a2f849c6 937 return '3';
Ceyhun_Emre 3:7c22a2f849c6 938 }
Ceyhun_Emre 3:7c22a2f849c6 939 return 'V';
Ceyhun_Emre 3:7c22a2f849c6 940 }
Ceyhun_Emre 3:7c22a2f849c6 941 }
Ceyhun_Emre 3:7c22a2f849c6 942 return 'S';
Ceyhun_Emre 3:7c22a2f849c6 943 }
Ceyhun_Emre 3:7c22a2f849c6 944 else {
Ceyhun_Emre 3:7c22a2f849c6 945 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 946 if ( code.at( 3) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 947 return 'F';
Ceyhun_Emre 3:7c22a2f849c6 948 }
Ceyhun_Emre 3:7c22a2f849c6 949 return '2';
Ceyhun_Emre 3:7c22a2f849c6 950 }
Ceyhun_Emre 3:7c22a2f849c6 951 return 'U';
Ceyhun_Emre 3:7c22a2f849c6 952 }
Ceyhun_Emre 3:7c22a2f849c6 953 }
Ceyhun_Emre 3:7c22a2f849c6 954 return 'I';
Ceyhun_Emre 3:7c22a2f849c6 955 }
Ceyhun_Emre 3:7c22a2f849c6 956 else {
Ceyhun_Emre 3:7c22a2f849c6 957 if ( code.length() != 2) {
Ceyhun_Emre 3:7c22a2f849c6 958 if ( code.at( 2) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 959 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 960 return 'L';
Ceyhun_Emre 3:7c22a2f849c6 961 }
Ceyhun_Emre 3:7c22a2f849c6 962 return 'R';
Ceyhun_Emre 3:7c22a2f849c6 963 }
Ceyhun_Emre 3:7c22a2f849c6 964 else {
Ceyhun_Emre 3:7c22a2f849c6 965 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 966 if ( code.at( 3) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 967 return 'P';
Ceyhun_Emre 3:7c22a2f849c6 968 }
Ceyhun_Emre 3:7c22a2f849c6 969 if ( code.length() != 4) {
Ceyhun_Emre 3:7c22a2f849c6 970 return '1';
Ceyhun_Emre 3:7c22a2f849c6 971 }
Ceyhun_Emre 3:7c22a2f849c6 972 return 'J';
Ceyhun_Emre 3:7c22a2f849c6 973 }
Ceyhun_Emre 3:7c22a2f849c6 974 return 'W';
Ceyhun_Emre 3:7c22a2f849c6 975 }
Ceyhun_Emre 3:7c22a2f849c6 976 }
Ceyhun_Emre 3:7c22a2f849c6 977 return 'A';
Ceyhun_Emre 3:7c22a2f849c6 978 }
Ceyhun_Emre 3:7c22a2f849c6 979 }
Ceyhun_Emre 3:7c22a2f849c6 980 return 'E';
Ceyhun_Emre 3:7c22a2f849c6 981 }
Ceyhun_Emre 3:7c22a2f849c6 982 else if ( code.at( 0) == '-') {
Ceyhun_Emre 3:7c22a2f849c6 983 if ( code.length() != 1) {
Ceyhun_Emre 3:7c22a2f849c6 984 if ( code.at( 1) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 985 if ( code.length() != 2) {
Ceyhun_Emre 3:7c22a2f849c6 986 if ( code.at( 2) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 987 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 988 if ( code.at( 3) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 989 if ( code.length() != 4) {
Ceyhun_Emre 3:7c22a2f849c6 990 return '6';
Ceyhun_Emre 3:7c22a2f849c6 991 }
Ceyhun_Emre 3:7c22a2f849c6 992 return 'B';
Ceyhun_Emre 3:7c22a2f849c6 993 }
Ceyhun_Emre 3:7c22a2f849c6 994 return 'X';
Ceyhun_Emre 3:7c22a2f849c6 995 }
Ceyhun_Emre 3:7c22a2f849c6 996 return 'D';
Ceyhun_Emre 3:7c22a2f849c6 997 }
Ceyhun_Emre 3:7c22a2f849c6 998 else {
Ceyhun_Emre 3:7c22a2f849c6 999 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 1000 if ( code.at( 3) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1001 return 'C';
Ceyhun_Emre 3:7c22a2f849c6 1002 }
Ceyhun_Emre 3:7c22a2f849c6 1003 return 'Y';
Ceyhun_Emre 3:7c22a2f849c6 1004 }
Ceyhun_Emre 3:7c22a2f849c6 1005 return 'K';
Ceyhun_Emre 3:7c22a2f849c6 1006 }
Ceyhun_Emre 3:7c22a2f849c6 1007 }
Ceyhun_Emre 3:7c22a2f849c6 1008 return 'N';
Ceyhun_Emre 3:7c22a2f849c6 1009 }
Ceyhun_Emre 3:7c22a2f849c6 1010 else {
Ceyhun_Emre 3:7c22a2f849c6 1011 if ( code.length() != 2) {
Ceyhun_Emre 3:7c22a2f849c6 1012 if ( code.at( 2) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1013 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 1014 if ( code.at( 3) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1015 if ( code.length() != 4) {
Ceyhun_Emre 3:7c22a2f849c6 1016 return '7';
Ceyhun_Emre 3:7c22a2f849c6 1017 }
Ceyhun_Emre 3:7c22a2f849c6 1018 return 'Z';
Ceyhun_Emre 3:7c22a2f849c6 1019 }
Ceyhun_Emre 3:7c22a2f849c6 1020 return 'Q';
Ceyhun_Emre 3:7c22a2f849c6 1021 }
Ceyhun_Emre 3:7c22a2f849c6 1022 return 'G';
Ceyhun_Emre 3:7c22a2f849c6 1023 }
Ceyhun_Emre 3:7c22a2f849c6 1024 else {
Ceyhun_Emre 3:7c22a2f849c6 1025 if ( code.length() != 3) {
Ceyhun_Emre 3:7c22a2f849c6 1026 if ( code.at( 3) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1027 return '8';
Ceyhun_Emre 3:7c22a2f849c6 1028 }
Ceyhun_Emre 3:7c22a2f849c6 1029 if ( code.at( 4) == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1030 return '9';
Ceyhun_Emre 3:7c22a2f849c6 1031 }
Ceyhun_Emre 3:7c22a2f849c6 1032 return '0';
Ceyhun_Emre 3:7c22a2f849c6 1033 }
Ceyhun_Emre 3:7c22a2f849c6 1034 return 'O';
Ceyhun_Emre 3:7c22a2f849c6 1035 }
Ceyhun_Emre 3:7c22a2f849c6 1036 }
Ceyhun_Emre 3:7c22a2f849c6 1037 return 'M';
Ceyhun_Emre 3:7c22a2f849c6 1038 }
Ceyhun_Emre 3:7c22a2f849c6 1039 }
Ceyhun_Emre 3:7c22a2f849c6 1040 return 'T';
Ceyhun_Emre 3:7c22a2f849c6 1041 }
Ceyhun_Emre 3:7c22a2f849c6 1042 return ' '; // codeForEachLetter.at( 0) = '/'
Ceyhun_Emre 3:7c22a2f849c6 1043 }
Ceyhun_Emre 3:7c22a2f849c6 1044
Ceyhun_Emre 3:7c22a2f849c6 1045 // This function transmit the inputted Morse code through LED and buzzer simultaneously.
Ceyhun_Emre 3:7c22a2f849c6 1046 void transmitLEDAndBuzzer (string encodedText) {
Ceyhun_Emre 3:7c22a2f849c6 1047 const double unitTime = 0.25; // 250 miliseconds
Ceyhun_Emre 3:7c22a2f849c6 1048
Ceyhun_Emre 3:7c22a2f849c6 1049 // code to send encodedText to LED and buzzer appropriately
Ceyhun_Emre 3:7c22a2f849c6 1050 for ( short int counter = 0; counter < encodedText.length(); counter++) {
Ceyhun_Emre 3:7c22a2f849c6 1051 char checkedChar = encodedText.at( counter);
Ceyhun_Emre 3:7c22a2f849c6 1052 if ( checkedChar == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1053 led = 0;
Ceyhun_Emre 3:7c22a2f849c6 1054 buzzer = 1;
Ceyhun_Emre 3:7c22a2f849c6 1055 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1056 led = 1;
Ceyhun_Emre 3:7c22a2f849c6 1057 buzzer = 0;
Ceyhun_Emre 3:7c22a2f849c6 1058 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1059 }
Ceyhun_Emre 3:7c22a2f849c6 1060 else if ( checkedChar == '-') {
Ceyhun_Emre 3:7c22a2f849c6 1061 led = 0;
Ceyhun_Emre 3:7c22a2f849c6 1062 buzzer = 1;
Ceyhun_Emre 3:7c22a2f849c6 1063 wait( unitTime * 3);
Ceyhun_Emre 3:7c22a2f849c6 1064 led = 1;
Ceyhun_Emre 3:7c22a2f849c6 1065 buzzer = 0;
Ceyhun_Emre 3:7c22a2f849c6 1066 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1067 }
Ceyhun_Emre 3:7c22a2f849c6 1068 else if ( checkedChar == ' ') {
Ceyhun_Emre 3:7c22a2f849c6 1069 wait( unitTime * 3);
Ceyhun_Emre 3:7c22a2f849c6 1070 }
Ceyhun_Emre 3:7c22a2f849c6 1071 else { // checkedChar = '/'
Ceyhun_Emre 3:7c22a2f849c6 1072 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1073 }
Ceyhun_Emre 3:7c22a2f849c6 1074 }
Ceyhun_Emre 3:7c22a2f849c6 1075 }
Ceyhun_Emre 3:7c22a2f849c6 1076
Ceyhun_Emre 3:7c22a2f849c6 1077 // This function transmit the inputted Morse code through only LED.
Ceyhun_Emre 3:7c22a2f849c6 1078 void transmitLED (string encodedText) {
Ceyhun_Emre 3:7c22a2f849c6 1079 const double unitTime = 0.25; // 250 miliseconds
Ceyhun_Emre 3:7c22a2f849c6 1080
Ceyhun_Emre 3:7c22a2f849c6 1081 // code to send encodedText to LED appropriately
Ceyhun_Emre 3:7c22a2f849c6 1082 for ( short int counter = 0; counter < encodedText.length(); counter++) {
Ceyhun_Emre 3:7c22a2f849c6 1083 char checkedChar = encodedText.at( counter);
Ceyhun_Emre 3:7c22a2f849c6 1084 if ( checkedChar == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1085 led = 0;
Ceyhun_Emre 3:7c22a2f849c6 1086 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1087 led = 1;
Ceyhun_Emre 3:7c22a2f849c6 1088 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1089 }
Ceyhun_Emre 3:7c22a2f849c6 1090 else if ( checkedChar == '-') {
Ceyhun_Emre 3:7c22a2f849c6 1091 led = 0;
Ceyhun_Emre 3:7c22a2f849c6 1092 wait( unitTime * 3);
Ceyhun_Emre 3:7c22a2f849c6 1093 led = 1;
Ceyhun_Emre 3:7c22a2f849c6 1094 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1095 }
Ceyhun_Emre 3:7c22a2f849c6 1096 else if ( checkedChar == ' ') {
Ceyhun_Emre 3:7c22a2f849c6 1097 wait( unitTime * 3);
Ceyhun_Emre 3:7c22a2f849c6 1098 }
Ceyhun_Emre 3:7c22a2f849c6 1099 else { // checkedChar = '/'
Ceyhun_Emre 3:7c22a2f849c6 1100 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1101 }
Ceyhun_Emre 3:7c22a2f849c6 1102 }
Ceyhun_Emre 3:7c22a2f849c6 1103 }
Ceyhun_Emre 3:7c22a2f849c6 1104
Ceyhun_Emre 3:7c22a2f849c6 1105 // This function transmit the inputted Morse code through only buzzer.
Ceyhun_Emre 3:7c22a2f849c6 1106 void transmitBuzzer (string encodedText) {
Ceyhun_Emre 3:7c22a2f849c6 1107 const double unitTime = 0.25; // 250 miliseconds
Ceyhun_Emre 3:7c22a2f849c6 1108
Ceyhun_Emre 3:7c22a2f849c6 1109 // code to send encodedText to buzzer appropriately
Ceyhun_Emre 3:7c22a2f849c6 1110 for ( short int counter = 0; counter < encodedText.length(); counter++) {
Ceyhun_Emre 3:7c22a2f849c6 1111 char checkedChar = encodedText.at( counter);
Ceyhun_Emre 3:7c22a2f849c6 1112 if ( checkedChar == '.') {
Ceyhun_Emre 3:7c22a2f849c6 1113 buzzer = 1;
Ceyhun_Emre 3:7c22a2f849c6 1114 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1115 buzzer = 0;
Ceyhun_Emre 3:7c22a2f849c6 1116 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1117 }
Ceyhun_Emre 3:7c22a2f849c6 1118 else if ( checkedChar == '-') {
Ceyhun_Emre 3:7c22a2f849c6 1119 buzzer = 1;
Ceyhun_Emre 3:7c22a2f849c6 1120 wait( unitTime * 3);
Ceyhun_Emre 3:7c22a2f849c6 1121 buzzer = 0;
Ceyhun_Emre 3:7c22a2f849c6 1122 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1123 }
Ceyhun_Emre 3:7c22a2f849c6 1124 else if ( checkedChar == ' ') {
Ceyhun_Emre 3:7c22a2f849c6 1125 wait( unitTime * 3);
Ceyhun_Emre 3:7c22a2f849c6 1126 }
Ceyhun_Emre 3:7c22a2f849c6 1127 else { // checkedChar = '/'
Ceyhun_Emre 3:7c22a2f849c6 1128 wait( unitTime);
Ceyhun_Emre 3:7c22a2f849c6 1129 }
Ceyhun_Emre 3:7c22a2f849c6 1130 }
Ceyhun_Emre 3:7c22a2f849c6 1131 }