HIMBED_3AHELI / Mbed 2 deprecated CHAP_ForstnerAndreas

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut led(p20);//P1_13);
00004 Serial pc(USBTX,USBRX);
00005 BusOut leds(LED1,LED2,LED3,LED4);
00006 Timer tim;
00007 Ticker tick;
00008 char strRnd[5];
00009 char strIn[50];
00010 int tries=0,cnt=0;
00011 
00012 // States
00013 const int ST_BOOT = 0;
00014 const int ST_AUTH = 1;
00015 const int ST_STRING = 2;
00016 const int ST_INPUT = 3;
00017 const int ST_COMP = 4;
00018 const int ST_SUCC = 5;
00019 
00020 void blink();
00021 // ---------------- Read String from Serial --------------------------
00022 void ReadFromSerial(char *s, const int MAX_SIZE=50)
00023 {
00024     for (int i = 0; i < 50; i++) {s[i] = '\0';}    //set entire string to NULL
00025     for (int i = 0; i < 50; i++) {
00026         char c = pc.getc();
00027         if (c == '\n')
00028             break;
00029         s[i] = c;
00030     }
00031 }
00032 // ---------------- Random String Gen --------------------------
00033 void gen_random(char *s, const int len)
00034 {
00035     static const char alphanum[] =
00036         "0123456789"
00037         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
00038         "abcdefghijklmnopqrstuvwxyz";
00039 
00040     for (int i = 0; i < len; ++i) {
00041         s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
00042     }
00043 
00044     s[len] = 0;
00045 }
00046 // ---------------- Event Klasse --------------------------
00047 class SwEvent
00048 {
00049     InterruptIn _isr;
00050     bool _pressed;
00051     void _RisingISR();
00052 
00053 public:
00054     SwEvent(PinName pin) : _isr(pin) {
00055         _pressed = false;
00056     }
00057     int CheckFlag();    // das muss im do-Zweig (while(true) Schleife) ständig abgefragt werden
00058     void InitIsr();
00059 };
00060 
00061 int SwEvent::CheckFlag()
00062 {
00063     if( _pressed ) {
00064         _pressed = false;
00065         return 1;
00066     }
00067     return 0;
00068 }
00069 
00070 void SwEvent::InitIsr()
00071 {
00072     _isr.rise(this, &SwEvent::_RisingISR);
00073 }
00074 
00075 void SwEvent::_RisingISR()
00076 {
00077     if( _isr.read() )
00078         _pressed = true;
00079 }
00080 
00081 SwEvent sw2(P0_23);
00082 
00083 // ----------------- Stm Klasse -----------------------------
00084 class Stm
00085 {
00086 public:
00087     Stm() {
00088         state=ST_BOOT;
00089     }
00090 
00091     void Boot();
00092     void Auth();
00093     void GenString();
00094     void Input();
00095     void Comp();
00096     void Succ();
00097 
00098     uint8_t state;
00099 };
00100 
00101 
00102 void Stm::Boot()  //ST-0 -> Boot Message
00103 {
00104     leds = 0x0;
00105     pc.printf("HELLO!\n");
00106     tim.start();
00107     while(true) {
00108         if(tim.read()>1) {
00109             tim.stop();
00110             tim.reset();
00111             state = ST_AUTH;
00112             return;
00113         }
00114     }
00115 }
00116 
00117 void Stm::Auth()  //ST-1 -> Authentication
00118 {
00119     tick.attach(&blink,0.5);
00120     tim.reset();
00121     pc.printf("Bitte betaetigen Sie die Taste <SW2> um fortzufahren\n");
00122     while(true) {
00123         led=1;
00124         if(sw2.CheckFlag()) {
00125             tick.detach();
00126             state = ST_STRING;
00127             return;
00128         }
00129     }
00130 }
00131 
00132 void Stm::GenString()  //ST-2 -> Generate Random String
00133 {
00134     gen_random(strRnd, 5);
00135     pc.printf("%s\n",strRnd);
00136     tim.start();
00137     leds=0x9;
00138     tick.attach(&blink,0.2);
00139     while(1) {
00140         state = ST_INPUT;
00141         return;
00142     }
00143 }
00144 
00145 
00146 void Stm::Input()  //ST-3 -> Input from Serial
00147 {
00148     pc.printf("String eingeben!\n");
00149     while(1) {
00150         strIn[0]='\0';
00151         //pc.scanf("%s", strIn);
00152         ReadFromSerial(strIn);
00153         pc.printf("%s\n",strIn);
00154         tick.detach();leds = 0x0;
00155         state = ST_COMP;
00156         return;
00157     }
00158 }
00159 
00160 
00161 void Stm::Comp()  //ST-4 -> Compare the Strings
00162 {
00163     if (strncmp(strRnd,strIn,5)==0) { //Strings sind gleich
00164         tim.stop();
00165         leds=0xF;
00166         tries = 0;
00167         pc.printf("Erfolgreiche Eingabe in %.2f Sekunden\n",tim.read());
00168         while(1) {
00169             state = ST_SUCC;
00170             return;
00171         }
00172     } else if (strncmp(strRnd,strIn,5)!=0 && tries == 2) { //3x falsch
00173         tim.reset();
00174         tries +=1;
00175         pc.printf("3-fach Falsche Eingabe\n");
00176         leds=0xF;
00177         while(1) {
00178             if (tim.read_ms()>200) {
00179                 leds = ~leds;
00180                 tim.reset();
00181             }
00182         }
00183     } else { //falsch
00184         pc.printf("Falsche Eingabe\n");
00185         tries +=1;
00186         leds=0x2;
00187         state=ST_INPUT;
00188     }
00189 }
00190 void Stm::Succ()  //ST-5 -> Successful Input
00191 {
00192     while(1) {
00193         ReadFromSerial(strIn);
00194         if (strIn[0] == '.') {
00195             leds = 0x0;
00196             state = ST_AUTH;
00197             return;
00198         }
00199         if (strIn[0] != '\0') {
00200             cnt++;
00201             pc.printf("%i:%i> %s\n",cnt,strlen(strIn),strIn);
00202         }
00203     }
00204 }
00205 
00206 
00207 Stm stm;
00208 
00209 void stateMachine()
00210 {
00211     switch (stm.state) {
00212         case ST_BOOT:
00213             stm.Boot();
00214             break;
00215         case ST_AUTH:
00216             stm.Auth();
00217             break;
00218         case ST_STRING:
00219             stm.GenString();
00220             break;
00221         case ST_INPUT:
00222             stm.Input();
00223             break;
00224         case ST_COMP:
00225             stm.Comp();
00226             break;
00227         case ST_SUCC:
00228             stm.Succ();
00229             break;
00230         default:
00231             stm.Boot();
00232             break;
00233     }
00234 }
00235 
00236 void blink(){
00237     if (stm.state == ST_AUTH){
00238         leds[0] = !leds[0];}
00239     if (stm.state == ST_INPUT){
00240         leds = ~leds;}
00241 }
00242 
00243 int main()
00244 {
00245     sw2.InitIsr();
00246     while(1) {
00247         stateMachine();
00248     }
00249 }