![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Final Project for ECE 4180. Morse Code Encoder and Transmission
Dependencies: 4DGL-uLCD-SE EthernetInterface mbed-rtos mbed
main.cpp@0:50e84db663a3, 2015-12-08 (annotated)
- Committer:
- doubster
- Date:
- Tue Dec 08 09:15:15 2015 +0000
- Revision:
- 0:50e84db663a3
Final Push for Morse Code Encoder
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
doubster | 0:50e84db663a3 | 1 | #include "mbed.h" |
doubster | 0:50e84db663a3 | 2 | #include "uLCD_4DGL.h" |
doubster | 0:50e84db663a3 | 3 | #include <time.h> |
doubster | 0:50e84db663a3 | 4 | #include <string> |
doubster | 0:50e84db663a3 | 5 | #include "EthernetInterface.h" |
doubster | 0:50e84db663a3 | 6 | |
doubster | 0:50e84db663a3 | 7 | // Definitions for networking |
doubster | 0:50e84db663a3 | 8 | const char* ECHO_SERVER_ADDRESS = "10.0.0.32"; |
doubster | 0:50e84db663a3 | 9 | const int ECHO_SERVER_PORT = 7; |
doubster | 0:50e84db663a3 | 10 | |
doubster | 0:50e84db663a3 | 11 | // uLCD |
doubster | 0:50e84db663a3 | 12 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
doubster | 0:50e84db663a3 | 13 | |
doubster | 0:50e84db663a3 | 14 | // Mbed Leds |
doubster | 0:50e84db663a3 | 15 | DigitalOut myled1(LED1); |
doubster | 0:50e84db663a3 | 16 | DigitalOut myled2(LED2); |
doubster | 0:50e84db663a3 | 17 | DigitalOut myled3(LED3); |
doubster | 0:50e84db663a3 | 18 | DigitalOut myled4(LED4); |
doubster | 0:50e84db663a3 | 19 | |
doubster | 0:50e84db663a3 | 20 | // Push Buttons |
doubster | 0:50e84db663a3 | 21 | DigitalIn pb1(p23); |
doubster | 0:50e84db663a3 | 22 | DigitalIn pb2(p22); |
doubster | 0:50e84db663a3 | 23 | DigitalIn pb3(p21); |
doubster | 0:50e84db663a3 | 24 | |
doubster | 0:50e84db663a3 | 25 | // Function declarations |
doubster | 0:50e84db663a3 | 26 | // Screen Declarations |
doubster | 0:50e84db663a3 | 27 | void StartScreen(); |
doubster | 0:50e84db663a3 | 28 | void ClientServerScreen(); |
doubster | 0:50e84db663a3 | 29 | void HelpScreen(); |
doubster | 0:50e84db663a3 | 30 | void InputScreen(); |
doubster | 0:50e84db663a3 | 31 | void EncryptionScreen(); |
doubster | 0:50e84db663a3 | 32 | void DecryptionScreen(); |
doubster | 0:50e84db663a3 | 33 | void CaesarShiftScreen(); |
doubster | 0:50e84db663a3 | 34 | // Actual Functions |
doubster | 0:50e84db663a3 | 35 | string MorseCode(string a); |
doubster | 0:50e84db663a3 | 36 | |
doubster | 0:50e84db663a3 | 37 | // UDP Transmission Functions |
doubster | 0:50e84db663a3 | 38 | void Client(string message); |
doubster | 0:50e84db663a3 | 39 | string Server(); |
doubster | 0:50e84db663a3 | 40 | // Cipher declarations |
doubster | 0:50e84db663a3 | 41 | // opt => 0 Encrypt opt => 1 Decrypt |
doubster | 0:50e84db663a3 | 42 | string CaesarCipher(string word,int opt, int shift); |
doubster | 0:50e84db663a3 | 43 | string PolybiusSquareCipher(string a,int opt); |
doubster | 0:50e84db663a3 | 44 | string PolybiusTableTranslation(char letter); |
doubster | 0:50e84db663a3 | 45 | string PolybiusTableLookup(string number); |
doubster | 0:50e84db663a3 | 46 | |
doubster | 0:50e84db663a3 | 47 | int main() { |
doubster | 0:50e84db663a3 | 48 | |
doubster | 0:50e84db663a3 | 49 | // Create pullups |
doubster | 0:50e84db663a3 | 50 | pb1.mode(PullUp); |
doubster | 0:50e84db663a3 | 51 | pb2.mode(PullUp); |
doubster | 0:50e84db663a3 | 52 | pb3.mode(PullUp); |
doubster | 0:50e84db663a3 | 53 | |
doubster | 0:50e84db663a3 | 54 | // Constants |
doubster | 0:50e84db663a3 | 55 | time_t start,end; |
doubster | 0:50e84db663a3 | 56 | double dif; |
doubster | 0:50e84db663a3 | 57 | string morseinput = ""; |
doubster | 0:50e84db663a3 | 58 | string morsetranslation = ""; |
doubster | 0:50e84db663a3 | 59 | string cipheredinput; |
doubster | 0:50e84db663a3 | 60 | string morsereceived = ""; |
doubster | 0:50e84db663a3 | 61 | string cipherreceived = ""; |
doubster | 0:50e84db663a3 | 62 | int clientserver; |
doubster | 0:50e84db663a3 | 63 | int firsttime = 0; |
doubster | 0:50e84db663a3 | 64 | int firsttimeserver = 0; |
doubster | 0:50e84db663a3 | 65 | string chosencipher = ""; |
doubster | 0:50e84db663a3 | 66 | int shiftvalue = 0; |
doubster | 0:50e84db663a3 | 67 | bool inserting = true; |
doubster | 0:50e84db663a3 | 68 | EthernetInterface ethclient; |
doubster | 0:50e84db663a3 | 69 | EthernetInterface ethserver; |
doubster | 0:50e84db663a3 | 70 | const char * IPaddress = "10.0.0.32"; // 10.10.0.65 |
doubster | 0:50e84db663a3 | 71 | const char * NetworkMask = "255.255.255.0"; |
doubster | 0:50e84db663a3 | 72 | const char * Gateway = "10.0.0.1"; // 10.10.0.1 |
doubster | 0:50e84db663a3 | 73 | |
doubster | 0:50e84db663a3 | 74 | // Welcome Screen |
doubster | 0:50e84db663a3 | 75 | StartScreen(); |
doubster | 0:50e84db663a3 | 76 | |
doubster | 0:50e84db663a3 | 77 | // pb1 => up |
doubster | 0:50e84db663a3 | 78 | // pb2 => down |
doubster | 0:50e84db663a3 | 79 | // pb3 => right |
doubster | 0:50e84db663a3 | 80 | // 0 - Client |
doubster | 0:50e84db663a3 | 81 | // 1 - Server |
doubster | 0:50e84db663a3 | 82 | while(true) // Infinite Loop. Restart at ClientServerScreen. |
doubster | 0:50e84db663a3 | 83 | { |
doubster | 0:50e84db663a3 | 84 | morseinput = ""; |
doubster | 0:50e84db663a3 | 85 | morsetranslation = ""; |
doubster | 0:50e84db663a3 | 86 | morsereceived = ""; |
doubster | 0:50e84db663a3 | 87 | cipherreceived = ""; |
doubster | 0:50e84db663a3 | 88 | clientserver = 0; |
doubster | 0:50e84db663a3 | 89 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 90 | ClientServerScreen(); |
doubster | 0:50e84db663a3 | 91 | shiftvalue = 0; |
doubster | 0:50e84db663a3 | 92 | while(pb2 && pb1) |
doubster | 0:50e84db663a3 | 93 | { |
doubster | 0:50e84db663a3 | 94 | } |
doubster | 0:50e84db663a3 | 95 | |
doubster | 0:50e84db663a3 | 96 | if(!pb1) // pb1 was pressed, do Client Network |
doubster | 0:50e84db663a3 | 97 | { |
doubster | 0:50e84db663a3 | 98 | clientserver = 0; |
doubster | 0:50e84db663a3 | 99 | } |
doubster | 0:50e84db663a3 | 100 | else if(!pb2) |
doubster | 0:50e84db663a3 | 101 | { |
doubster | 0:50e84db663a3 | 102 | clientserver = 1; // pb2 was pressed, do Server Network |
doubster | 0:50e84db663a3 | 103 | } |
doubster | 0:50e84db663a3 | 104 | |
doubster | 0:50e84db663a3 | 105 | if(clientserver == 0) |
doubster | 0:50e84db663a3 | 106 | { |
doubster | 0:50e84db663a3 | 107 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 108 | if(firsttime==0) |
doubster | 0:50e84db663a3 | 109 | { |
doubster | 0:50e84db663a3 | 110 | HelpScreen(); |
doubster | 0:50e84db663a3 | 111 | } |
doubster | 0:50e84db663a3 | 112 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 113 | InputScreen(); |
doubster | 0:50e84db663a3 | 114 | // Input Morse Code below. pb1 to input. |
doubster | 0:50e84db663a3 | 115 | // pb2 to move to the next step |
doubster | 0:50e84db663a3 | 116 | start = 0; |
doubster | 0:50e84db663a3 | 117 | end = 0; |
doubster | 0:50e84db663a3 | 118 | while(pb3) |
doubster | 0:50e84db663a3 | 119 | { |
doubster | 0:50e84db663a3 | 120 | |
doubster | 0:50e84db663a3 | 121 | if(!pb1) // Means a dot was inserted |
doubster | 0:50e84db663a3 | 122 | { |
doubster | 0:50e84db663a3 | 123 | while(!pb1) // Need to wait until the button is released |
doubster | 0:50e84db663a3 | 124 | { |
doubster | 0:50e84db663a3 | 125 | } |
doubster | 0:50e84db663a3 | 126 | morseinput.append("."); |
doubster | 0:50e84db663a3 | 127 | inserting = true; |
doubster | 0:50e84db663a3 | 128 | time(&start); |
doubster | 0:50e84db663a3 | 129 | } |
doubster | 0:50e84db663a3 | 130 | else if(!pb2) |
doubster | 0:50e84db663a3 | 131 | { |
doubster | 0:50e84db663a3 | 132 | while(!pb2) // Need to wait till release |
doubster | 0:50e84db663a3 | 133 | { |
doubster | 0:50e84db663a3 | 134 | } |
doubster | 0:50e84db663a3 | 135 | morseinput.append("-"); |
doubster | 0:50e84db663a3 | 136 | inserting = true; |
doubster | 0:50e84db663a3 | 137 | time(&start); |
doubster | 0:50e84db663a3 | 138 | } |
doubster | 0:50e84db663a3 | 139 | |
doubster | 0:50e84db663a3 | 140 | if(start !=0.0) |
doubster | 0:50e84db663a3 | 141 | { |
doubster | 0:50e84db663a3 | 142 | time(&end); |
doubster | 0:50e84db663a3 | 143 | dif = difftime(end,start); |
doubster | 0:50e84db663a3 | 144 | if(dif>1) |
doubster | 0:50e84db663a3 | 145 | { |
doubster | 0:50e84db663a3 | 146 | string e = MorseCode(morseinput); |
doubster | 0:50e84db663a3 | 147 | if(e!="error") |
doubster | 0:50e84db663a3 | 148 | { |
doubster | 0:50e84db663a3 | 149 | morsetranslation.append(e); |
doubster | 0:50e84db663a3 | 150 | uLCD.printf("%s",e); |
doubster | 0:50e84db663a3 | 151 | } |
doubster | 0:50e84db663a3 | 152 | start = 0; |
doubster | 0:50e84db663a3 | 153 | end = 0; |
doubster | 0:50e84db663a3 | 154 | morseinput = ""; |
doubster | 0:50e84db663a3 | 155 | } |
doubster | 0:50e84db663a3 | 156 | } |
doubster | 0:50e84db663a3 | 157 | } |
doubster | 0:50e84db663a3 | 158 | |
doubster | 0:50e84db663a3 | 159 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 160 | EncryptionScreen(); |
doubster | 0:50e84db663a3 | 161 | |
doubster | 0:50e84db663a3 | 162 | while(pb2 && pb1 && pb3) |
doubster | 0:50e84db663a3 | 163 | { |
doubster | 0:50e84db663a3 | 164 | } |
doubster | 0:50e84db663a3 | 165 | |
doubster | 0:50e84db663a3 | 166 | if(!pb1) // pb1 was pressed, do caesar cipher |
doubster | 0:50e84db663a3 | 167 | { |
doubster | 0:50e84db663a3 | 168 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 169 | CaesarShiftScreen(); |
doubster | 0:50e84db663a3 | 170 | shiftvalue = 0; |
doubster | 0:50e84db663a3 | 171 | uLCD.locate(0,4); |
doubster | 0:50e84db663a3 | 172 | uLCD.printf("%02d",shiftvalue); |
doubster | 0:50e84db663a3 | 173 | while(pb3) |
doubster | 0:50e84db663a3 | 174 | { |
doubster | 0:50e84db663a3 | 175 | if(!pb1) // Means go up one value |
doubster | 0:50e84db663a3 | 176 | { |
doubster | 0:50e84db663a3 | 177 | while(!pb1) // Need to wait until the button is released |
doubster | 0:50e84db663a3 | 178 | { |
doubster | 0:50e84db663a3 | 179 | } |
doubster | 0:50e84db663a3 | 180 | shiftvalue = shiftvalue + 1; |
doubster | 0:50e84db663a3 | 181 | if(shiftvalue>25) |
doubster | 0:50e84db663a3 | 182 | { |
doubster | 0:50e84db663a3 | 183 | shiftvalue = 25; |
doubster | 0:50e84db663a3 | 184 | } |
doubster | 0:50e84db663a3 | 185 | uLCD.locate(0,4); |
doubster | 0:50e84db663a3 | 186 | uLCD.printf("%02d",shiftvalue); |
doubster | 0:50e84db663a3 | 187 | } |
doubster | 0:50e84db663a3 | 188 | if(!pb2) // Means go up one value |
doubster | 0:50e84db663a3 | 189 | { |
doubster | 0:50e84db663a3 | 190 | while(!pb2) // Need to wait until the button is released |
doubster | 0:50e84db663a3 | 191 | { |
doubster | 0:50e84db663a3 | 192 | } |
doubster | 0:50e84db663a3 | 193 | shiftvalue = shiftvalue - 1; |
doubster | 0:50e84db663a3 | 194 | if(shiftvalue<0) |
doubster | 0:50e84db663a3 | 195 | { |
doubster | 0:50e84db663a3 | 196 | shiftvalue = 0; |
doubster | 0:50e84db663a3 | 197 | } |
doubster | 0:50e84db663a3 | 198 | uLCD.locate(0,4); |
doubster | 0:50e84db663a3 | 199 | uLCD.printf("%02d",shiftvalue); |
doubster | 0:50e84db663a3 | 200 | } |
doubster | 0:50e84db663a3 | 201 | } |
doubster | 0:50e84db663a3 | 202 | cipheredinput = CaesarCipher(morsetranslation,0,shiftvalue); |
doubster | 0:50e84db663a3 | 203 | chosencipher = "Caesar Cipher"; |
doubster | 0:50e84db663a3 | 204 | } |
doubster | 0:50e84db663a3 | 205 | else if(!pb2) // pb2 was pressed, do Polybius Square cipher |
doubster | 0:50e84db663a3 | 206 | { |
doubster | 0:50e84db663a3 | 207 | cipheredinput = PolybiusSquareCipher(morsetranslation,0); |
doubster | 0:50e84db663a3 | 208 | chosencipher = "Polybius Sqre"; |
doubster | 0:50e84db663a3 | 209 | } |
doubster | 0:50e84db663a3 | 210 | else if(!pb3) // No Cipher |
doubster | 0:50e84db663a3 | 211 | { |
doubster | 0:50e84db663a3 | 212 | cipheredinput = morsetranslation; |
doubster | 0:50e84db663a3 | 213 | chosencipher = "No Cipher"; |
doubster | 0:50e84db663a3 | 214 | } |
doubster | 0:50e84db663a3 | 215 | wait(2); |
doubster | 0:50e84db663a3 | 216 | |
doubster | 0:50e84db663a3 | 217 | // Once the morse input comes in through here. |
doubster | 0:50e84db663a3 | 218 | // Tell the user that we are about to send it all. |
doubster | 0:50e84db663a3 | 219 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 220 | uLCD.printf("%s\n",chosencipher); |
doubster | 0:50e84db663a3 | 221 | uLCD.printf("Sending Value\n\n"); |
doubster | 0:50e84db663a3 | 222 | uLCD.printf("\n%s\n",morsetranslation); |
doubster | 0:50e84db663a3 | 223 | uLCD.printf("\n%s\n",cipheredinput); |
doubster | 0:50e84db663a3 | 224 | wait(3); |
doubster | 0:50e84db663a3 | 225 | |
doubster | 0:50e84db663a3 | 226 | if(firsttime == 0) |
doubster | 0:50e84db663a3 | 227 | { |
doubster | 0:50e84db663a3 | 228 | ethclient.init(); |
doubster | 0:50e84db663a3 | 229 | firsttime = 1; |
doubster | 0:50e84db663a3 | 230 | } |
doubster | 0:50e84db663a3 | 231 | |
doubster | 0:50e84db663a3 | 232 | ethclient.connect(); |
doubster | 0:50e84db663a3 | 233 | UDPSocket sock; |
doubster | 0:50e84db663a3 | 234 | sock.init(); |
doubster | 0:50e84db663a3 | 235 | Endpoint echo_server; |
doubster | 0:50e84db663a3 | 236 | echo_server.set_address(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT); |
doubster | 0:50e84db663a3 | 237 | char out_buffer[1024]; |
doubster | 0:50e84db663a3 | 238 | strncpy(out_buffer, cipheredinput.c_str(), sizeof(out_buffer)); |
doubster | 0:50e84db663a3 | 239 | out_buffer[sizeof(out_buffer) - 1] = 0; |
doubster | 0:50e84db663a3 | 240 | //uLCD.printf("\Client IP Address is %s\n", ethclient.getIPAddress()); |
doubster | 0:50e84db663a3 | 241 | //wait(4); |
doubster | 0:50e84db663a3 | 242 | sock.sendTo(echo_server, out_buffer, sizeof(out_buffer)); |
doubster | 0:50e84db663a3 | 243 | sock.close(); |
doubster | 0:50e84db663a3 | 244 | ethclient.disconnect(); |
doubster | 0:50e84db663a3 | 245 | // technically the eth should still be connected |
doubster | 0:50e84db663a3 | 246 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 247 | uLCD.printf("Message has been sent"); |
doubster | 0:50e84db663a3 | 248 | wait(2); |
doubster | 0:50e84db663a3 | 249 | } |
doubster | 0:50e84db663a3 | 250 | else |
doubster | 0:50e84db663a3 | 251 | { |
doubster | 0:50e84db663a3 | 252 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 253 | uLCD.printf("Receiving..."); |
doubster | 0:50e84db663a3 | 254 | |
doubster | 0:50e84db663a3 | 255 | if(firsttimeserver == 0) |
doubster | 0:50e84db663a3 | 256 | { |
doubster | 0:50e84db663a3 | 257 | //uLCD.printf("first time server"); |
doubster | 0:50e84db663a3 | 258 | ethserver.init(IPaddress, NetworkMask, Gateway); |
doubster | 0:50e84db663a3 | 259 | firsttimeserver = 1; |
doubster | 0:50e84db663a3 | 260 | } |
doubster | 0:50e84db663a3 | 261 | //uLCD.printf("initialized"); |
doubster | 0:50e84db663a3 | 262 | ethserver.connect(); |
doubster | 0:50e84db663a3 | 263 | UDPSocket server; |
doubster | 0:50e84db663a3 | 264 | server.bind(ECHO_SERVER_PORT); |
doubster | 0:50e84db663a3 | 265 | Endpoint client; |
doubster | 0:50e84db663a3 | 266 | char buffer[256]; |
doubster | 0:50e84db663a3 | 267 | //uLCD.printf("\Server IP Address is %s\n", ethserver.getIPAddress()); |
doubster | 0:50e84db663a3 | 268 | string message; |
doubster | 0:50e84db663a3 | 269 | int n = server.receiveFrom(client, buffer, sizeof(buffer)); |
doubster | 0:50e84db663a3 | 270 | buffer[n] = '\0'; |
doubster | 0:50e84db663a3 | 271 | |
doubster | 0:50e84db663a3 | 272 | string message2(buffer); |
doubster | 0:50e84db663a3 | 273 | server.close(); |
doubster | 0:50e84db663a3 | 274 | ethserver.disconnect(); |
doubster | 0:50e84db663a3 | 275 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 276 | uLCD.printf("Message %s",message2); |
doubster | 0:50e84db663a3 | 277 | wait(3); |
doubster | 0:50e84db663a3 | 278 | morsereceived = message2; |
doubster | 0:50e84db663a3 | 279 | // string morsereceived = ""; |
doubster | 0:50e84db663a3 | 280 | // string cipherreceived = ""; |
doubster | 0:50e84db663a3 | 281 | |
doubster | 0:50e84db663a3 | 282 | // After Reception, User will have 2 options to decipher |
doubster | 0:50e84db663a3 | 283 | // Engima or Caesar. |
doubster | 0:50e84db663a3 | 284 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 285 | DecryptionScreen(); |
doubster | 0:50e84db663a3 | 286 | while(pb2 && pb1 && pb3) |
doubster | 0:50e84db663a3 | 287 | { |
doubster | 0:50e84db663a3 | 288 | } |
doubster | 0:50e84db663a3 | 289 | |
doubster | 0:50e84db663a3 | 290 | if(!pb1) // pb1 was pressed, do caesar cipher |
doubster | 0:50e84db663a3 | 291 | { |
doubster | 0:50e84db663a3 | 292 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 293 | CaesarShiftScreen(); |
doubster | 0:50e84db663a3 | 294 | shiftvalue = 0; |
doubster | 0:50e84db663a3 | 295 | uLCD.locate(0,4); |
doubster | 0:50e84db663a3 | 296 | uLCD.printf("%02d",shiftvalue); |
doubster | 0:50e84db663a3 | 297 | while(pb3) |
doubster | 0:50e84db663a3 | 298 | { |
doubster | 0:50e84db663a3 | 299 | if(!pb1) // Means go up one value |
doubster | 0:50e84db663a3 | 300 | { |
doubster | 0:50e84db663a3 | 301 | while(!pb1) // Need to wait until the button is released |
doubster | 0:50e84db663a3 | 302 | { |
doubster | 0:50e84db663a3 | 303 | } |
doubster | 0:50e84db663a3 | 304 | shiftvalue = shiftvalue + 1; |
doubster | 0:50e84db663a3 | 305 | if(shiftvalue>25) |
doubster | 0:50e84db663a3 | 306 | { |
doubster | 0:50e84db663a3 | 307 | shiftvalue = 25; |
doubster | 0:50e84db663a3 | 308 | } |
doubster | 0:50e84db663a3 | 309 | uLCD.locate(0,4); |
doubster | 0:50e84db663a3 | 310 | uLCD.printf("%02d",shiftvalue); |
doubster | 0:50e84db663a3 | 311 | } |
doubster | 0:50e84db663a3 | 312 | if(!pb2) // Means go up one value |
doubster | 0:50e84db663a3 | 313 | { |
doubster | 0:50e84db663a3 | 314 | while(!pb2) // Need to wait until the button is released |
doubster | 0:50e84db663a3 | 315 | { |
doubster | 0:50e84db663a3 | 316 | } |
doubster | 0:50e84db663a3 | 317 | shiftvalue = shiftvalue - 1; |
doubster | 0:50e84db663a3 | 318 | if(shiftvalue<0) |
doubster | 0:50e84db663a3 | 319 | { |
doubster | 0:50e84db663a3 | 320 | shiftvalue = 0; |
doubster | 0:50e84db663a3 | 321 | } |
doubster | 0:50e84db663a3 | 322 | uLCD.locate(0,4); |
doubster | 0:50e84db663a3 | 323 | uLCD.printf("%02d",shiftvalue); |
doubster | 0:50e84db663a3 | 324 | } |
doubster | 0:50e84db663a3 | 325 | } |
doubster | 0:50e84db663a3 | 326 | cipherreceived = CaesarCipher(morsereceived,1,shiftvalue); // Second input = 1, for decryption |
doubster | 0:50e84db663a3 | 327 | } |
doubster | 0:50e84db663a3 | 328 | else if(!pb2) // pb2 was pressed, do Polybius Square Cipher |
doubster | 0:50e84db663a3 | 329 | { |
doubster | 0:50e84db663a3 | 330 | cipherreceived = PolybiusSquareCipher(morsereceived,1); // Second input = 1, for decryption |
doubster | 0:50e84db663a3 | 331 | } |
doubster | 0:50e84db663a3 | 332 | else if(!pb3) |
doubster | 0:50e84db663a3 | 333 | { |
doubster | 0:50e84db663a3 | 334 | cipherreceived = morsereceived; |
doubster | 0:50e84db663a3 | 335 | } |
doubster | 0:50e84db663a3 | 336 | uLCD.cls(); |
doubster | 0:50e84db663a3 | 337 | uLCD.printf("Decryption\n\n"); |
doubster | 0:50e84db663a3 | 338 | uLCD.printf("%s\n",morsereceived); |
doubster | 0:50e84db663a3 | 339 | uLCD.printf("%s\n",cipherreceived); |
doubster | 0:50e84db663a3 | 340 | wait(5); |
doubster | 0:50e84db663a3 | 341 | } |
doubster | 0:50e84db663a3 | 342 | } |
doubster | 0:50e84db663a3 | 343 | } |
doubster | 0:50e84db663a3 | 344 | |
doubster | 0:50e84db663a3 | 345 | |
doubster | 0:50e84db663a3 | 346 | // Start Screen to show Welcome Page |
doubster | 0:50e84db663a3 | 347 | void StartScreen() |
doubster | 0:50e84db663a3 | 348 | { |
doubster | 0:50e84db663a3 | 349 | uLCD.printf("Welcome to the Morse Code Enconder and Transmitter.\n\n\n"); |
doubster | 0:50e84db663a3 | 350 | wait(4); |
doubster | 0:50e84db663a3 | 351 | } |
doubster | 0:50e84db663a3 | 352 | |
doubster | 0:50e84db663a3 | 353 | // Screen to choose between Client and Servers |
doubster | 0:50e84db663a3 | 354 | void ClientServerScreen() |
doubster | 0:50e84db663a3 | 355 | { |
doubster | 0:50e84db663a3 | 356 | uLCD.printf("Choose an option:\n\n"); |
doubster | 0:50e84db663a3 | 357 | uLCD.printf("1 - Transmit\n"); |
doubster | 0:50e84db663a3 | 358 | uLCD.printf("2 - Receive\n"); |
doubster | 0:50e84db663a3 | 359 | //uLCD.printf("3 - Checkers\n"); |
doubster | 0:50e84db663a3 | 360 | } |
doubster | 0:50e84db663a3 | 361 | |
doubster | 0:50e84db663a3 | 362 | // Screen 2 |
doubster | 0:50e84db663a3 | 363 | void HelpScreen() |
doubster | 0:50e84db663a3 | 364 | { |
doubster | 0:50e84db663a3 | 365 | uLCD.printf("Up => . \n\n"); |
doubster | 0:50e84db663a3 | 366 | uLCD.printf("Down => - \n\n"); |
doubster | 0:50e84db663a3 | 367 | uLCD.printf("Right => Submit\n"); |
doubster | 0:50e84db663a3 | 368 | wait(2); |
doubster | 0:50e84db663a3 | 369 | } |
doubster | 0:50e84db663a3 | 370 | |
doubster | 0:50e84db663a3 | 371 | // Screen 3 |
doubster | 0:50e84db663a3 | 372 | void InputScreen() |
doubster | 0:50e84db663a3 | 373 | { |
doubster | 0:50e84db663a3 | 374 | uLCD.printf("Input Morse Code\n"); |
doubster | 0:50e84db663a3 | 375 | } |
doubster | 0:50e84db663a3 | 376 | |
doubster | 0:50e84db663a3 | 377 | // Screen 4 |
doubster | 0:50e84db663a3 | 378 | void EncryptionScreen() |
doubster | 0:50e84db663a3 | 379 | { |
doubster | 0:50e84db663a3 | 380 | uLCD.printf("Choose encryption:\n\n"); |
doubster | 0:50e84db663a3 | 381 | uLCD.printf("1 - Caesar Cipher\n"); |
doubster | 0:50e84db663a3 | 382 | uLCD.printf("2 - Polybius Sqre\n"); |
doubster | 0:50e84db663a3 | 383 | uLCD.printf("3 - No cipher\n"); |
doubster | 0:50e84db663a3 | 384 | } |
doubster | 0:50e84db663a3 | 385 | |
doubster | 0:50e84db663a3 | 386 | void DecryptionScreen() |
doubster | 0:50e84db663a3 | 387 | { |
doubster | 0:50e84db663a3 | 388 | uLCD.printf("Choose decryption:\n\n"); |
doubster | 0:50e84db663a3 | 389 | uLCD.printf("1 - Caesar Cipher\n"); |
doubster | 0:50e84db663a3 | 390 | uLCD.printf("2 - Polybius Sqre\n"); |
doubster | 0:50e84db663a3 | 391 | uLCD.printf("3 - No cipher\n"); |
doubster | 0:50e84db663a3 | 392 | } |
doubster | 0:50e84db663a3 | 393 | |
doubster | 0:50e84db663a3 | 394 | void CaesarShiftScreen() { |
doubster | 0:50e84db663a3 | 395 | uLCD.printf("Choose a shift value\n"); |
doubster | 0:50e84db663a3 | 396 | uLCD.printf("UP and DOWN"); |
doubster | 0:50e84db663a3 | 397 | } |
doubster | 0:50e84db663a3 | 398 | |
doubster | 0:50e84db663a3 | 399 | // Returns the proper character based on the inputed morse code |
doubster | 0:50e84db663a3 | 400 | string MorseCode(string a) |
doubster | 0:50e84db663a3 | 401 | { |
doubster | 0:50e84db663a3 | 402 | //uLCD.printf("%s",a); |
doubster | 0:50e84db663a3 | 403 | if(a == ".-") { return "A";} |
doubster | 0:50e84db663a3 | 404 | if(a == "-...") { return "B";} |
doubster | 0:50e84db663a3 | 405 | if(a == "-.-.") { return "C";} |
doubster | 0:50e84db663a3 | 406 | if(a == "-..") { return "D";} |
doubster | 0:50e84db663a3 | 407 | if(a == ".") { return "E";} |
doubster | 0:50e84db663a3 | 408 | if(a == "..-.") { return "F";} |
doubster | 0:50e84db663a3 | 409 | if(a == "--.") { return "G";} |
doubster | 0:50e84db663a3 | 410 | if(a == "....") { return "H";} |
doubster | 0:50e84db663a3 | 411 | if(a == "..") { return "I";} |
doubster | 0:50e84db663a3 | 412 | if(a == ".---") { return "J";} |
doubster | 0:50e84db663a3 | 413 | if(a == "-.-") { return "K";} |
doubster | 0:50e84db663a3 | 414 | if(a == ".-..") { return "L";} |
doubster | 0:50e84db663a3 | 415 | if(a == "--") { return "M";} |
doubster | 0:50e84db663a3 | 416 | if(a == "-.") { return "N";} |
doubster | 0:50e84db663a3 | 417 | if(a == "---") { return "O";} |
doubster | 0:50e84db663a3 | 418 | if(a == ".--.") { return "P";} |
doubster | 0:50e84db663a3 | 419 | if(a == "--.-") { return "Q";} |
doubster | 0:50e84db663a3 | 420 | if(a == ".-.") { return "R";} |
doubster | 0:50e84db663a3 | 421 | if(a == "...") { return "S";} |
doubster | 0:50e84db663a3 | 422 | if(a == "-") { return "T";} |
doubster | 0:50e84db663a3 | 423 | if(a == "..-") { return "U";} |
doubster | 0:50e84db663a3 | 424 | if(a == "...-") { return "V";} |
doubster | 0:50e84db663a3 | 425 | if(a == ".--") { return "W";} |
doubster | 0:50e84db663a3 | 426 | if(a == "-..-") { return "X";} |
doubster | 0:50e84db663a3 | 427 | if(a == "-.--") { return "Y";} |
doubster | 0:50e84db663a3 | 428 | if(a == "--..") { return "Z";} |
doubster | 0:50e84db663a3 | 429 | if(a == ".----") { return "1";} |
doubster | 0:50e84db663a3 | 430 | if(a == "..---") { return "2";} |
doubster | 0:50e84db663a3 | 431 | if(a == "...--") { return "3";} |
doubster | 0:50e84db663a3 | 432 | if(a == "....-") { return "4";} |
doubster | 0:50e84db663a3 | 433 | if(a == ".....") { return "5";} |
doubster | 0:50e84db663a3 | 434 | if(a == "-....") { return "6";} |
doubster | 0:50e84db663a3 | 435 | if(a == "--...") { return "7";} |
doubster | 0:50e84db663a3 | 436 | if(a == "---..") { return "8";} |
doubster | 0:50e84db663a3 | 437 | if(a == "----.") { return "9";} |
doubster | 0:50e84db663a3 | 438 | if(a == "-----") { return "0";} |
doubster | 0:50e84db663a3 | 439 | return "error"; |
doubster | 0:50e84db663a3 | 440 | } |
doubster | 0:50e84db663a3 | 441 | |
doubster | 0:50e84db663a3 | 442 | // Cipher Codes by Ishita |
doubster | 0:50e84db663a3 | 443 | |
doubster | 0:50e84db663a3 | 444 | |
doubster | 0:50e84db663a3 | 445 | |
doubster | 0:50e84db663a3 | 446 | // Find the length of the string |
doubster | 0:50e84db663a3 | 447 | // Loop through it, and replace each a[i] with its equivalent that is 3 letters later. |
doubster | 0:50e84db663a3 | 448 | // For that will have to loop through Alphabet and find the position of said letter. |
doubster | 0:50e84db663a3 | 449 | // Then increment. So hopefully there is a isin(alphabet,"A) -> that returns the index, position etc. |
doubster | 0:50e84db663a3 | 450 | string CaesarCipher(string word, int opt, int shift) |
doubster | 0:50e84db663a3 | 451 | { |
doubster | 0:50e84db663a3 | 452 | string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
doubster | 0:50e84db663a3 | 453 | if(opt == 0) // Encrypt |
doubster | 0:50e84db663a3 | 454 | { |
doubster | 0:50e84db663a3 | 455 | string encrypted = ""; |
doubster | 0:50e84db663a3 | 456 | for (int x = 0; x < word.length(); x++) { |
doubster | 0:50e84db663a3 | 457 | char c = word[x]; |
doubster | 0:50e84db663a3 | 458 | int ind = alphabet.find(c); |
doubster | 0:50e84db663a3 | 459 | ind = (ind + shift) % 26; |
doubster | 0:50e84db663a3 | 460 | encrypted += alphabet[ind]; |
doubster | 0:50e84db663a3 | 461 | } |
doubster | 0:50e84db663a3 | 462 | return encrypted; |
doubster | 0:50e84db663a3 | 463 | } else if (opt == 1) { //Decrypt |
doubster | 0:50e84db663a3 | 464 | string decrypted = ""; |
doubster | 0:50e84db663a3 | 465 | for (int x = 0; x < word.length(); x++) { |
doubster | 0:50e84db663a3 | 466 | char c = word[x]; |
doubster | 0:50e84db663a3 | 467 | int ind = alphabet.find(c); |
doubster | 0:50e84db663a3 | 468 | ind = (ind - shift) % 26; |
doubster | 0:50e84db663a3 | 469 | decrypted += alphabet[ind]; |
doubster | 0:50e84db663a3 | 470 | } |
doubster | 0:50e84db663a3 | 471 | return decrypted; |
doubster | 0:50e84db663a3 | 472 | } else { |
doubster | 0:50e84db663a3 | 473 | return "Error"; |
doubster | 0:50e84db663a3 | 474 | } |
doubster | 0:50e84db663a3 | 475 | } |
doubster | 0:50e84db663a3 | 476 | |
doubster | 0:50e84db663a3 | 477 | //Polybius Square cipher |
doubster | 0:50e84db663a3 | 478 | string PolybiusSquareCipher(string word,int opt) |
doubster | 0:50e84db663a3 | 479 | { |
doubster | 0:50e84db663a3 | 480 | string number = ""; |
doubster | 0:50e84db663a3 | 481 | string numbers = ""; |
doubster | 0:50e84db663a3 | 482 | if(opt == 0) // Encrypt |
doubster | 0:50e84db663a3 | 483 | { |
doubster | 0:50e84db663a3 | 484 | string encrypted = ""; |
doubster | 0:50e84db663a3 | 485 | string rows = ""; |
doubster | 0:50e84db663a3 | 486 | string cols = ""; |
doubster | 0:50e84db663a3 | 487 | for (int x = 0; x < word.length(); x++) { |
doubster | 0:50e84db663a3 | 488 | char c = word[x]; |
doubster | 0:50e84db663a3 | 489 | number = PolybiusTableTranslation(c); |
doubster | 0:50e84db663a3 | 490 | rows += number[0]; |
doubster | 0:50e84db663a3 | 491 | cols += number[1]; |
doubster | 0:50e84db663a3 | 492 | } |
doubster | 0:50e84db663a3 | 493 | numbers = rows + cols; |
doubster | 0:50e84db663a3 | 494 | for (int y = 0; y < numbers.length(); y = y + 2) { |
doubster | 0:50e84db663a3 | 495 | encrypted += PolybiusTableLookup(numbers.substr(y,2)); |
doubster | 0:50e84db663a3 | 496 | } |
doubster | 0:50e84db663a3 | 497 | return encrypted; |
doubster | 0:50e84db663a3 | 498 | } else if (opt == 1) { //Decrypt |
doubster | 0:50e84db663a3 | 499 | string decrypted = ""; |
doubster | 0:50e84db663a3 | 500 | for (int x = 0; x < word.length(); x++) { |
doubster | 0:50e84db663a3 | 501 | char c = word[x]; |
doubster | 0:50e84db663a3 | 502 | number = PolybiusTableTranslation(c); |
doubster | 0:50e84db663a3 | 503 | numbers += number; |
doubster | 0:50e84db663a3 | 504 | } |
doubster | 0:50e84db663a3 | 505 | int length = numbers.length()/2; |
doubster | 0:50e84db663a3 | 506 | for (int y = 0; y < length; y++) { |
doubster | 0:50e84db663a3 | 507 | string newNum = ""; |
doubster | 0:50e84db663a3 | 508 | newNum += numbers[y]; |
doubster | 0:50e84db663a3 | 509 | newNum += numbers[y + length]; |
doubster | 0:50e84db663a3 | 510 | decrypted += PolybiusTableLookup(newNum); |
doubster | 0:50e84db663a3 | 511 | } |
doubster | 0:50e84db663a3 | 512 | return decrypted; |
doubster | 0:50e84db663a3 | 513 | } else { |
doubster | 0:50e84db663a3 | 514 | return "Error"; |
doubster | 0:50e84db663a3 | 515 | } |
doubster | 0:50e84db663a3 | 516 | } |
doubster | 0:50e84db663a3 | 517 | |
doubster | 0:50e84db663a3 | 518 | |
doubster | 0:50e84db663a3 | 519 | |
doubster | 0:50e84db663a3 | 520 | string PolybiusTableTranslation(char letter) { |
doubster | 0:50e84db663a3 | 521 | if (letter == 'A') {return "11";} |
doubster | 0:50e84db663a3 | 522 | if (letter == 'B') {return "12";} |
doubster | 0:50e84db663a3 | 523 | if (letter == 'C') {return "13";} |
doubster | 0:50e84db663a3 | 524 | if (letter == 'D') {return "14";} |
doubster | 0:50e84db663a3 | 525 | if (letter == 'E') {return "15";} |
doubster | 0:50e84db663a3 | 526 | if (letter == 'F') {return "21";} |
doubster | 0:50e84db663a3 | 527 | if (letter == 'G') {return "22";} |
doubster | 0:50e84db663a3 | 528 | if (letter == 'H') {return "23";} |
doubster | 0:50e84db663a3 | 529 | if (letter == 'I') {return "24";} |
doubster | 0:50e84db663a3 | 530 | if (letter == 'J') {return "24";} |
doubster | 0:50e84db663a3 | 531 | if (letter == 'K') {return "25";} |
doubster | 0:50e84db663a3 | 532 | if (letter == 'L') {return "31";} |
doubster | 0:50e84db663a3 | 533 | if (letter == 'M') {return "32";} |
doubster | 0:50e84db663a3 | 534 | if (letter == 'N') {return "33";} |
doubster | 0:50e84db663a3 | 535 | if (letter == 'O') {return "34";} |
doubster | 0:50e84db663a3 | 536 | if (letter == 'P') {return "35";} |
doubster | 0:50e84db663a3 | 537 | if (letter == 'Q') {return "41";} |
doubster | 0:50e84db663a3 | 538 | if (letter == 'R') {return "42";} |
doubster | 0:50e84db663a3 | 539 | if (letter == 'S') {return "43";} |
doubster | 0:50e84db663a3 | 540 | if (letter == 'T') {return "44";} |
doubster | 0:50e84db663a3 | 541 | if (letter == 'U') {return "45";} |
doubster | 0:50e84db663a3 | 542 | if (letter == 'V') {return "51";} |
doubster | 0:50e84db663a3 | 543 | if (letter == 'W') {return "52";} |
doubster | 0:50e84db663a3 | 544 | if (letter == 'X') {return "53";} |
doubster | 0:50e84db663a3 | 545 | if (letter == 'Y') {return "54";} |
doubster | 0:50e84db663a3 | 546 | if (letter == 'Z') {return "55";} |
doubster | 0:50e84db663a3 | 547 | |
doubster | 0:50e84db663a3 | 548 | return "error"; |
doubster | 0:50e84db663a3 | 549 | } |
doubster | 0:50e84db663a3 | 550 | |
doubster | 0:50e84db663a3 | 551 | string PolybiusTableLookup(string number) { |
doubster | 0:50e84db663a3 | 552 | if (number == "11") {return "A";} |
doubster | 0:50e84db663a3 | 553 | if (number == "12") {return "B";} |
doubster | 0:50e84db663a3 | 554 | if (number == "13") {return "C";} |
doubster | 0:50e84db663a3 | 555 | if (number == "14") {return "D";} |
doubster | 0:50e84db663a3 | 556 | if (number == "15") {return "E";} |
doubster | 0:50e84db663a3 | 557 | if (number == "21") {return "F";} |
doubster | 0:50e84db663a3 | 558 | if (number == "22") {return "G";} |
doubster | 0:50e84db663a3 | 559 | if (number == "23") {return "H";} |
doubster | 0:50e84db663a3 | 560 | if (number == "24") {return "I/J";} |
doubster | 0:50e84db663a3 | 561 | if (number == "25") {return "K";} |
doubster | 0:50e84db663a3 | 562 | if (number == "31") {return "L";} |
doubster | 0:50e84db663a3 | 563 | if (number == "32") {return "M";} |
doubster | 0:50e84db663a3 | 564 | if (number == "33") {return "N";} |
doubster | 0:50e84db663a3 | 565 | if (number == "34") {return "O";} |
doubster | 0:50e84db663a3 | 566 | if (number =="35") {return "P";} |
doubster | 0:50e84db663a3 | 567 | if (number == "41") {return "Q";} |
doubster | 0:50e84db663a3 | 568 | if (number == "42") {return "R";} |
doubster | 0:50e84db663a3 | 569 | if (number == "43") {return "S";} |
doubster | 0:50e84db663a3 | 570 | if (number == "44") {return "T";} |
doubster | 0:50e84db663a3 | 571 | if (number == "45") {return "U";} |
doubster | 0:50e84db663a3 | 572 | if (number == "51") {return "V";} |
doubster | 0:50e84db663a3 | 573 | if (number == "52") {return "W";} |
doubster | 0:50e84db663a3 | 574 | if (number == "53") {return "X";} |
doubster | 0:50e84db663a3 | 575 | if (number == "54") {return "Y";} |
doubster | 0:50e84db663a3 | 576 | if (number == "55") {return "Z";} |
doubster | 0:50e84db663a3 | 577 | |
doubster | 0:50e84db663a3 | 578 | return "error"; |
doubster | 0:50e84db663a3 | 579 | } |
doubster | 0:50e84db663a3 | 580 | |
doubster | 0:50e84db663a3 | 581 | |
doubster | 0:50e84db663a3 | 582 | |
doubster | 0:50e84db663a3 | 583 |