daniel moser / Mbed 2 deprecated program1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <string>
00003 
00004 DigitalOut myled1(LED1);
00005 DigitalOut myled2(LED2);
00006 DigitalOut myled3(LED3);
00007 DigitalOut myled4(LED4);
00008 
00009 LocalFileSystem local("local");               // Create the local filesystem under the name "local"
00010 
00011 //function protypes
00012 char encrypt(char c);
00013 void flashLights();
00014 
00015 int main() {
00016     flashLights();
00017     //print to a file saved on the mbed mass storage device
00018     FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
00019     fprintf(fp, "Hello text doc World!");
00020     fclose(fp);
00021 
00022     //print some more
00023     fp = fopen("/local/out.txt", "a");  // Open "out.txt" on the local file system for writing, appending
00024     fprintf(fp, " log begins here: /n");
00025     fclose(fp);
00026 
00027     //print to the serial port, that requres an extra driver for xp
00028     Serial pc(USBTX, USBRX); // tx, rx
00029     pc.printf("Hello serial comms World!\n");
00030 
00031     bool commandIncoming=false;          //using a singal char to then expect a command char
00032     bool echoOn=true;                    //echo will eventually be output to the other USB port, and will be renamed pass through
00033     bool loggingOn=true;                 //log all the chars pressed to a text file
00034     bool encrypting=false;               //encrypting using the one time pad - exclude output chars * and others(todo: make a output map)
00035     char cOut;                           //the char being echod (or put out to the PC), is the cyphertext if encrypting is true
00036     char c;                              //the char coming in from the keyboard
00037 
00038     while (1) {
00039 
00040         //get a char from the keyboard (USB input)
00041         c = pc.getc();
00042 
00043         if (loggingOn) {
00044             fp = fopen("/local/out.txt", "a");  // Open "out.txt" on the local file system for writing, appending
00045             //todo: find a nicer way of doing this
00046             string s;
00047             s = c;
00048             fprintf(fp,"%s",s);
00049             fclose(fp);
00050         }
00051         //responsible for the output (at the moment to the serial comms, but eventually to the PC (USB output))
00052         if (c!='*'&&!commandIncoming && echoOn) {
00053             if (encrypting) {
00054                 cOut = encrypt(c);
00055             } else {
00056                 cOut = c;
00057             }
00058             pc.putc(cOut);
00059         }
00060         if (commandIncoming) {
00061             commandIncoming = false;
00062             //password recall
00063             if (c=='p') {
00064                 pc.puts("default password");
00065             }
00066             //username recall
00067             if (c=='u') {
00068                 pc.puts("default username");
00069             }
00070             //echo on/ off - debug (should be on all the time)
00071             if (c=='e') {
00072                 if (echoOn==true) {
00073                     pc.puts("key echo off");
00074                     echoOn=false;
00075                     myled1 = 1;
00076                 } else {
00077                     //when passing through myled1 is dim
00078                     pc.puts("key echo on");
00079                     echoOn=true;
00080                     myled1 = 0;
00081                 }
00082             }
00083             //dump keylogged data (paste anywhere functionality)
00084             if (c=='d') {
00085                 pc.puts("the data");
00086                 //recall the last x keys pressed or pull data from the txt file
00087             }
00088             if (c=='l') {
00089                 if (loggingOn==true) {
00090                     pc.puts("key logging off");
00091                     loggingOn=false;
00092                     //when loggingOn myled1 is dim
00093                     myled2 = 1;
00094                 } else {
00095                     pc.puts("key logging on");
00096                     loggingOn=true;
00097                     myled2 = 0;
00098                 }
00099             }
00100             if (c=='c') {
00101                 if (encrypting==true) {
00102                     pc.puts("exiting the encryption mode");
00103                     encrypting = false;
00104                     //when encrypting myled3 is bright
00105                     myled3 = 0;
00106                 } else {
00107                     pc.puts("entering the encryption mode - command keys are the same as before");
00108                     encrypting = true;
00109                     myled3 = 1;
00110                 }
00111             }
00112         }
00113         //if the key is the start of a command, read the next key as a command (ie *p means output password)
00114         if (c== '*') {
00115             commandIncoming = true;
00116         }
00117     }
00118 }
00119 
00120 //eventually have this a one time pad, drawing chars from another file,
00121 //deleting them as they go, and somehow having them not readable while in storage
00122 char encrypt(char c) {
00123     return (c+1);
00124 }
00125 
00126 void flashLights() {
00127     //blink some lights
00128     myled1 = 1;
00129     wait(0.2);
00130     myled1 = 0;
00131     wait(0.2);
00132 
00133     myled1 = 1;
00134     myled2 = 1;
00135     myled3 = 1;
00136     myled4 = 1;
00137 
00138     wait(0.2);
00139     myled1 = 0;
00140     myled2 = 0;
00141     myled3 = 0;
00142     myled4 = 0;
00143 }