serial_extend library for array data transmission and reception

Dependents:   receives_robot_wheel

serial_extend.cpp

Committer:
bousiya03
Date:
2014-10-05
Revision:
1:e808dd01f134
Parent:
0:fbf5705f90cc

File content as of revision 1:e808dd01f134:

/* 
 *mbed Multibyte Serial Library
 */

#include "mbed.h"
#include "serial_extend.h"
#include "RawSerial.h"

    
        serial_extend::serial_extend(PinName tx,PinName rx) : __serial__(tx,rx){
            
            /*
            switch(state){
            
            case read:
            {
            __serial__.attach(this,&serial_extend::RX,Serial::RxIrq);
            uint8_t k = __serial__.getc();
            break;
            }
            
            case write:
            {
            __serial__.attach(this,&serial_extend::TX,Serial::TxIrq);
            __serial__.putc(1);
            break;
            }
            
            case both:
            {
             __serial__.attach(this,&serial_extend::RX,Serial::RxIrq);
            uint8_t m = __serial__.getc();
            __serial__.attach(this,&serial_extend::TX,Serial::TxIrq);
            __serial__.putc(1);
            }
            
        }
        */
    }
    
        void serial_extend::start_write(){
            
            __stop_write = 0;
            __serial__.attach(this,&serial_extend::TX,RawSerial::TxIrq);
            __serial__.putc(1);
            
            }
            
        void serial_extend::stop_write(){
            
            __stop_write  = 1;
            
            }
            
            
        void serial_extend::start_read(){
            
            __stop_read = 0;
            __serial__.attach(this,&serial_extend::RX,RawSerial::RxIrq);
            uint8_t k = __serial__.getc();
            
            }
            
        void serial_extend::stop_read(){
            
            __stop_read = 1;
            
            }

        void serial_extend::read_data(uint8_t* readData,uint8_t readKey){

            __readData = readData;
            __readSize = __SIZE(__readData);
            __readKey = readKey;
        }


        void serial_extend::write_data(uint8_t* writeData,uint8_t writeKey){

            __writeData=writeData;
            __writeSize = __SIZE(__writeData);
            __writeKey = writeKey;
        }


        void serial_extend::TX(void){

            if(__stop_write==1){return;} 

            static uint8_t tx=__writeSize+2, i;
            static uint8_t txData[MAX_DATA_NUM]={__writeKey};   
            static uint8_t tx_checkcode=0;
            if(tx >= __writeSize+2){
                txData[KEY] = __writeKey;

                for(int k=1;k<=__writeSize;k++){
                    txData[k] = __writeData[k-1];
                }

          
                for(i=KEY+1, tx_checkcode=0; i<__writeSize+1; i++){
                    tx_checkcode ^= txData[i];
                }               //CHECKCODE
                txData[__writeSize+1] = tx_checkcode;
                tx=0;
            }
            __serial__.putc(txData[tx]);
            tx++;
        }

        void serial_extend::RX(void){
            
            if(__stop_read==1){return;}
            
            static uint8_t rx=0, i;
            static uint8_t rxData[MAX_DATA_NUM]={__readKey};
            static uint8_t rx_checkcode=0;

            rxData[rx] = __serial__.getc();

            if(rxData[KEY]==__readKey){
                rx++;
            }

            if(rx==__readSize+1){
                for(i=KEY+1, rx_checkcode=0; i<__readSize+1; i++){
                    rx_checkcode ^= rxData[i];
                }       //CHECKCODE
            }

            if(rx >= __readSize+2){
                if(rxData[__readSize+1]==rx_checkcode){

                    for(int m=1;m<=__readSize;m++){
                        __readData[m-1] = rxData[m];
                    }

            
                }
                rx=0;
            }
        }
        
        uint8_t serial_extend::readable_check(){
            
            return __serial__.readable();
            
            }