init

ComLink.cpp

Committer:
gert_lauritsen
Date:
2015-11-05
Revision:
0:c9f3777fe0b4
Child:
2:c1599cad82c3

File content as of revision 0:c9f3777fe0b4:

//------------------------------------------------------------------------------------
//                       :     
// Title                 :   Comlink 
// Filename              :   Queue.h
// Author                :   Gert Lauritsen
// Origin Date           :   26/05/2015
// Version               :   1.000
// Compiler              :   Keil
// Target                :   
// Notes                 :   None
//
//------------------------------------------------------------------------------------

#include "ComLink.h"
#define Frameheader 3

ComLink::ComLink(PinName tx, PinName rx,callback_type _callback) : _com(tx,rx)
{
    //_com=new Serial(tx,rx);
    init_q();
    _com.baud(19200);
    TxAktiv=0;               //Signal om at der ikke sendes data for øjeblikket
    _com.attach(this,&ComLink::txCallback, Serial::TxIrq); //Interrupt rutiner
    _com.attach(this,&ComLink::rxCallback, Serial::RxIrq);
    callback = _callback;
}

bool ComLink::CRC() {
 unsigned char CRCin=0;
 int framesize=inbuff[1];
 for (int i=0; i<(framesize+Frameheader); i++) CRCin^=inbuff[i];
 //return CRCin==inbuff[framesize+Frameheader];   
 return 1;
}    

//------------------------------------------------------------------------------------
// STX Size type Data       CRC
//  0   1    2   3..Dsize  Dsize+3
//------------------------------------------------------------------------------------

void ComLink::rxCallback()   //modtager data, når frame er modtaget sende besked
{
    static unsigned char InPointer;
    static bool Incoming;
    unsigned char framesize;
    int _Dtype=0;

    inbuff[InPointer]=_com.getc();
    if ((!Incoming) && (inbuff[InPointer]==stx)) {
        Incoming=1;
    }
    if (Incoming) {
        if (InPointer>Frameheader) {
            framesize=inbuff[1];
            if (InPointer==framesize-1) { //Vi har modtaget det hele
                if (CRC()) {
                    _Dtype=inbuff[2]; DSize=framesize;
                    strncpy(&Data[0],&inbuff[3],framesize);                    
                    callback(_Dtype); //sender besked længere op i systemet
                    InPointer=0;
                    Incoming=0;
                } //if crc
               else {
                    InPointer=0;
                    Incoming=0;
                }    
                  
            } //if framesize..
        } //if (Inpo..
        if (Incoming) InPointer++; //
    } //if incoming
}


void ComLink::txCallback()   //Skal tage den næste byt i rækken og sende den
{
  unsigned char NextCh=0;  
  if (Get(&NextCh)==0) TxAktiv=0;
  else
  _com.putc((int) NextCh); //sender næste char fra q
}

void ComLink::Senddata(unsigned char Dtype,unsigned char  Dsize, char *str)
{
   unsigned char NextCh=0;     
   Q_crc=0;
   //printf("N=%d %s\r\n",Dsize, &str[2]);
   Put(stx);   //Start Frame
   Put(Dsize); //FrameSize
   Put(Dtype);  //Frame type
   for (int i=0; i<Dsize; i++) {  //Datasektion
     Put(*str);
     str++;
   }
   Put(Q_crc);  //CRC
   if (!TxAktiv) {      //Hvis vi ikke sender noget for tiden startes tømningen fra Q
     Get(&NextCh);
     _com.putc((int) NextCh); //sender næste char fra q
     TxAktiv=1;
   } 
}
//---------------------------------------------------------------------------------------------------
//Tx Frames
//Public functions:
void ComLink::TxFloats(unsigned char Ftype, float *Fvalue, int Size){
  char OutStr[40];   
     union {
        char bytes[4];
        float val;
    } Float2Byte; 
  for (int i=0; i<Size; i++) {  
    Float2Byte.val=*Fvalue;
    memcpy(&OutStr[i*4],&Float2Byte.bytes[0],4);
  }  
  Senddata(Ftype,Size*4,OutStr);    
}    

void ComLink::TxCalRecord(float *value, int Size){
  TxFloats(2,value,Size);  
}  


void ComLink::TxStatus(float *value, int Size){
  TxFloats(3,value,Size);  
}  

void ComLink::TxErrStateArr(float *value, int Size){
  TxFloats(4,value,Size);  
}    

void ComLink::TxSetStateArr(float *value, int Size){
  TxFloats(5,value,Size);  
}    


void ComLink::WriteLCD(unsigned char x,unsigned char y, char *str)
{
    char outstr[20];
    outstr[0]=x;
    outstr[1]=y;
    strcpy(&outstr[2],str);
    Senddata(6,2+strlen(str),outstr);
}

void ComLink::cls()
{
    Senddata(7,0,"");
}