serial_extend library for array data transmission and reception

Dependents:   receives_robot_wheel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers serial_extend.cpp Source File

serial_extend.cpp

00001 /* 
00002  *mbed Multibyte Serial Library
00003  */
00004 
00005 #include "mbed.h"
00006 #include "serial_extend.h"
00007 #include "RawSerial.h"
00008 
00009     
00010         serial_extend::serial_extend(PinName tx,PinName rx) : __serial__(tx,rx){
00011             
00012             /*
00013             switch(state){
00014             
00015             case read:
00016             {
00017             __serial__.attach(this,&serial_extend::RX,Serial::RxIrq);
00018             uint8_t k = __serial__.getc();
00019             break;
00020             }
00021             
00022             case write:
00023             {
00024             __serial__.attach(this,&serial_extend::TX,Serial::TxIrq);
00025             __serial__.putc(1);
00026             break;
00027             }
00028             
00029             case both:
00030             {
00031              __serial__.attach(this,&serial_extend::RX,Serial::RxIrq);
00032             uint8_t m = __serial__.getc();
00033             __serial__.attach(this,&serial_extend::TX,Serial::TxIrq);
00034             __serial__.putc(1);
00035             }
00036             
00037         }
00038         */
00039     }
00040     
00041         void serial_extend::start_write(){
00042             
00043             __stop_write = 0;
00044             __serial__.attach(this,&serial_extend::TX,RawSerial::TxIrq);
00045             __serial__.putc(1);
00046             
00047             }
00048             
00049         void serial_extend::stop_write(){
00050             
00051             __stop_write  = 1;
00052             
00053             }
00054             
00055             
00056         void serial_extend::start_read(){
00057             
00058             __stop_read = 0;
00059             __serial__.attach(this,&serial_extend::RX,RawSerial::RxIrq);
00060             uint8_t k = __serial__.getc();
00061             
00062             }
00063             
00064         void serial_extend::stop_read(){
00065             
00066             __stop_read = 1;
00067             
00068             }
00069 
00070         void serial_extend::read_data(uint8_t* readData,uint8_t readKey){
00071 
00072             __readData = readData;
00073             __readSize = __SIZE(__readData);
00074             __readKey = readKey;
00075         }
00076 
00077 
00078         void serial_extend::write_data(uint8_t* writeData,uint8_t writeKey){
00079 
00080             __writeData=writeData;
00081             __writeSize = __SIZE(__writeData);
00082             __writeKey = writeKey;
00083         }
00084 
00085 
00086         void serial_extend::TX(void){
00087 
00088             if(__stop_write==1){return;} 
00089 
00090             static uint8_t tx=__writeSize+2, i;
00091             static uint8_t txData[MAX_DATA_NUM]={__writeKey};   
00092             static uint8_t tx_checkcode=0;
00093             if(tx >= __writeSize+2){
00094                 txData[KEY] = __writeKey;
00095 
00096                 for(int k=1;k<=__writeSize;k++){
00097                     txData[k] = __writeData[k-1];
00098                 }
00099 
00100           
00101                 for(i=KEY+1, tx_checkcode=0; i<__writeSize+1; i++){
00102                     tx_checkcode ^= txData[i];
00103                 }               //CHECKCODE
00104                 txData[__writeSize+1] = tx_checkcode;
00105                 tx=0;
00106             }
00107             __serial__.putc(txData[tx]);
00108             tx++;
00109         }
00110 
00111         void serial_extend::RX(void){
00112             
00113             if(__stop_read==1){return;}
00114             
00115             static uint8_t rx=0, i;
00116             static uint8_t rxData[MAX_DATA_NUM]={__readKey};
00117             static uint8_t rx_checkcode=0;
00118 
00119             rxData[rx] = __serial__.getc();
00120 
00121             if(rxData[KEY]==__readKey){
00122                 rx++;
00123             }
00124 
00125             if(rx==__readSize+1){
00126                 for(i=KEY+1, rx_checkcode=0; i<__readSize+1; i++){
00127                     rx_checkcode ^= rxData[i];
00128                 }       //CHECKCODE
00129             }
00130 
00131             if(rx >= __readSize+2){
00132                 if(rxData[__readSize+1]==rx_checkcode){
00133 
00134                     for(int m=1;m<=__readSize;m++){
00135                         __readData[m-1] = rxData[m];
00136                     }
00137 
00138             
00139                 }
00140                 rx=0;
00141             }
00142         }
00143         
00144         uint8_t serial_extend::readable_check(){
00145             
00146             return __serial__.readable();
00147             
00148             }