TELECOMMAND MANAGER V1

Dependencies:   mbed SLCD mbed-rtos

Revision:
2:994e741028c7
Parent:
1:df31097c8442
Child:
3:eec1097c0dd6
--- a/main.cpp	Sat Jun 06 04:27:40 2015 +0000
+++ b/main.cpp	Thu Jun 11 11:20:12 2015 +0000
@@ -1,62 +1,131 @@
-#include "mbed.h"
-#include "MNG_TC.h"
+//#define ENDL "\r" << endl
+
 #include <bitset>
 #include <iostream>
 using namespace std;
 
-DigitalOut led(LED_RED);
-Serial PC(USBTX, USBRX);
-DigitalOut led2(LED_GREEN);
+#include "mbed.h"
+#include "MNG_TC.h"
+
+DigitalOut ledr(LED_RED);
+DigitalOut ledg(LED_GREEN);
+
+//Serial PC(USBTX, USBRX);
 //Serial UART_RX(PTE0, PTE1);
 
-int main()
-{
-    UART_TX.baud(38400);
-//    UART_RX.baud(1200);
+namespace ISR{
+/*
+STATE TABLE
+1 = intrerrupt not received & MNG_TC not running
+2 = interrupt received but MNG_TC not running
+3 = interrupt has been handled(no pending interuupts) & MNG_TC running
+4 = new interrupt arrived & MNG_TC running
+*/
+    int state = 1;
+
+/*
+@brief:     handle TC interrupts from the receiver
+@param:     none
+@return:    none
+*/
+    void PC_isr(){
+        cout << "INSIDE PC_isr" << "\r" << endl;
+        PC.attach(NULL);
+        if(state == 1){
+            state = 2;
+        }
+        else if(state == 2){
+            state = 2;
+        }
+        else if(state == 3){
+            state = 4;
+//            read and decode TC here
+            PC.attach(&PC_isr);
+            state = 3;
+        }
+        else if(state == 4){
+            state = 4;
+        }
+    }
+}
+
+
+/*
+@brief:     manage TC
+*/
+void manage_TC(void){
     
-    led = 1;
-    led2 = 0;
     unsigned char str[13];
+/*    Timer time;
+    time.start();
+    float start_time = time.read();*/
     for(int i = 0 ; i < 13 ; ++i){
         str[i] = PC.getc();
+        PC.putc(str[i]);
     }
+    cout << "Successfully read 13 chars\r" << "\r" << endl;
+    PC.attach(&ISR::PC_isr);
     
 //    UART_RX.baud(38400);
     
     TC_list *tc_node = new TC_list;
+//    remove flag
     tc_node->TC_string = &(str[1]);
     
-//    CRC crc_class;
-    uint16_t crc_check = CRC::crcGenerate(&(str[1]), 9);
+    uint16_t crc_check = CRC::crc16_gen(&(str[1]), 9);
     char c1 = (crc_check >> 8) & 0xffff;
     char c2 = (crc_check) & 0xffff;
+//    str[12] is flag
     if(c1 == str[10] && c2 == str[11]){
         tc_node->crc_pass = true;
-//        printf("received tc successfully crc pass\r\n");
+        printf("received tc successfully crc pass\r\n");
     }
     else{
-//        printf("crc fail\r\n");
+        printf("crc fail\r\n");
+        tc_node->crc_pass = false;
     }
     
     tc_node->short_or_long = true;
     tc_node->next_TC = NULL;
     
-    led = 0;
-    led2 = 1;
-    
-    wait(10);
+    ledr = 0;
+    ledg = 1;
     
 //    for(int i = 0 ; i < 13; ++i){
 //        std::bitset<8> b = str[i];
-//        cout << b << "\r" << endl;
+//        cout << b << "\r" << ENDL;
 //    }
     
-    MNG_TC mng_tc(tc_node);
-    mng_tc.TC_list_cleanup();
+    MNG_TC::init(tc_node);
+    MNG_TC::start_with();
+    MNG_TC::decode_TC();
+    MNG_TC::check_for_missing_TC();
+    MNG_TC::execute_TC();
+    
+    
+    cout << "the received tc is\n" << "\r" << endl;
+    for(int i = 0 ; i < 13 ; ++i){
+        cout << tc_node->TC_string[i];
+    }
+    cout << "\n" << "\r" << endl;
     
-    led2 = 0;
-    while (true) {
-        led = !led; // toggle led
+    ISR::state = 1;
+    ledg = 0;
+}
+
+int main(){
+    UART_TX.baud(38400);
+//    UART_RX.baud(1200);
+    
+    PC.baud(9600);
+    PC.attach(&ISR::PC_isr);
+    
+    while(true){
+        ledg = !ledg;
+        if(ISR::state != 1){
+            cout << "Inside while Interrupt received" << "\r" << endl;
+            manage_TC();
+        }
         wait(0.2f);
     }
 }
\ No newline at end of file