Dummy program to demonstrate problems: working code

Dependencies:   SLCD mbed-rtos mbed

Fork of MNG_TC by Shreesha S

main.cpp

Committer:
shreeshas95
Date:
2015-07-13
Revision:
8:cb93c1d3209a
Parent:
7:e71ecfe3a340
Child:
9:934fdce72b3d
Child:
10:024c2ef51cb1

File content as of revision 8:cb93c1d3209a:

#include "mbed.h"
#define ENDL "\r" << endl

#include "SLCD.h"
SLCD lcd;

#define RX_TIMEOUT_LIMIT 2
#define PASS_TIME_LIMIT 1200

Serial PC(USBTX, USBRX);
Serial rx1m(PTE0, PTE1);

DigitalOut ledr(LED_RED);
DigitalOut ledg(LED_GREEN);

#include "Structures.h"

struct data_list{
   unsigned char val;
   struct data_list* next;
};

namespace VAR_SPACE{
    
    TC_list *Head_node = NULL;
    TC_list *last_node = NULL;
    
    int rx_state = 0;
    /*
    0 : idle
    1 : executing normal
    2 : executing obosc
    3 : idle 2 : obosc received incorrectly
    */
    
    struct data_list *head_data;
    data_list *data_node;
    struct data_list *rx_new_node;
    
    bool new_tc_received = false;
    bool execute_obosc = false;

}

Timeout rx_timeout;
bool pass_over = false;
bool first_time = true;
Timeout pass_time;

#include "crc.h"
#include "SND_TM.h"
#include "COM_RCV_TC.h"
#include "MNG_TC.h"

void after_pass(){
    pass_time.detach();
    pass_over = true;
}

void after_receive(void){
    rx_timeout.detach();
    VAR_SPACE::rx_new_node->val = 0x00;
    
    VAR_SPACE::new_tc_received = true;
    
    if(first_time){
        first_time = false;
        pass_time.attach(&after_pass, PASS_TIME_LIMIT);
    }
}

void rx_read() {
    //~ store value
    VAR_SPACE::rx_new_node->val = rx1m.getc();

    //~ allocate new node
    VAR_SPACE::rx_new_node->next = new data_list;
    VAR_SPACE::rx_new_node = VAR_SPACE::rx_new_node->next;
    VAR_SPACE::rx_new_node->next = NULL;

    rx_timeout.attach(&after_receive, RX_TIMEOUT_LIMIT);
}

void reset_all(){
    printf("resetting all\r\n");
    // reset MNG_TC
    MNG_TC::init();
    TC_list *tcp = VAR_SPACE::Head_node;
    while(tcp != NULL){
        TC_list *temp = tcp->next_TC;
        delete tcp;
        tcp = temp;
    }
    
    // reset COM_RCV_TC
//    handle reset
    
    // reset data linked list
    data_list *dataptr = VAR_SPACE::head_data;
    while( dataptr != NULL ){
        data_list *temp = dataptr->next;
        delete dataptr;
        dataptr = temp;
    }
    VAR_SPACE::head_data = new data_list;
    VAR_SPACE::rx_new_node = VAR_SPACE::head_data;
}

int main(){
    
    printf("welcome to mng_tm_tc\r\n");
    ledr = 1;
    
    PC.baud(9600);
    
    rx1m.baud(1200);
    rx1m.attach(&rx_read);
    VAR_SPACE::head_data = new data_list;
    VAR_SPACE::head_data->next = NULL;
    VAR_SPACE::rx_new_node = VAR_SPACE::head_data;
    
    MNG_TC::init();
    
    lcd.printf("0");
    
    struct data_list *hehe = VAR_SPACE::head_data;
    while( hehe != NULL ){
        printf("%x ", hehe->val);
        hehe = hehe->next;
    }
    printf("\r\n");
    
    while(true){
        ledg = !ledg;
        if( VAR_SPACE::new_tc_received ){
            VAR_SPACE::new_tc_received = false;
            struct data_list *haha = VAR_SPACE::head_data;
            unsigned int count = 0;
            while( haha != NULL ){
                ++count;
//                printf("%x ", haha->val);
                haha = haha->next;
            }
            printf("\t count = %u \r\n", count);
            printf("new tc received : state = %u\r\n", VAR_SPACE::rx_state);
            if( VAR_SPACE::rx_state == 0 ){
                VAR_SPACE::data_node = VAR_SPACE::head_data;
                COM_RCV_TC::rx_rcv_tc();
                MNG_TC::start_with();
                if( MNG_TC::check_for_missing_TC() ){
                    printf("everything pass\r\n");
                    VAR_SPACE::rx_state = 1;
                    MNG_TC::execute_TC();
                }
            }
            else if( VAR_SPACE::rx_state == 3 ){
                VAR_SPACE::data_node = VAR_SPACE::head_data;
                COM_RCV_TC::rx_rcv_tc();
                MNG_TC::start_with();
                if( MNG_TC::check_for_missing_TC() ){
                    VAR_SPACE::execute_obosc = true;
                    VAR_SPACE::rx_state = 2;
                    MNG_TC::execute_TC();
                }
            }
            else{
                // invalid state in main found reset
                reset_all();
            }
        }
        if(pass_over){
            pass_over = false;
            first_time = true;
            // pass got over reset all
            reset_all();
            //also consider frame_no
        }
    }
}