20190816

Dependencies:   mbed

TxRxService.cpp

Committer:
VASKO
Date:
2019-08-04
Revision:
15:30e45bd5902e
Parent:
14:b113f676ef42
Child:
16:459ad1bd82fd

File content as of revision 15:30e45bd5902e:

#include "PrjDefs.h"

Serial pc(pcTx,pcRx);

uint8_t ArrTx[ArrTxSize] = ArrTxInitializer;
uint8_t ArrRx[ArrRxSize] = ArrRxInitializer;
//uint8_t RxBuff[ArrRxSize];
MsgType RxBuff;
//uint8_t TxBuff[ArrTxSize];
MsgType TxBuff;
uint8_t *pArrTx = &ArrTx[0];
uint8_t *pArrRx = &ArrRx[0];
bool RxBuffFull = 0;
bool ArrTxBusy = 0;
bool TxBuffFull = 0;

uint8_t GetCheckSum(uint8_t *p){
    uint8_t cs = 0;
    for(int i = 0; i < (MsgCmdSize + MsgParsSize); i++){
        cs = cs + *(p++);
        cs = cs + 1;
        }
    return cs;
    }    

void IntrTx(); 

void CopyArr_InitTx(uint8_t *parr){
    memcpy(&ArrTx[HeaderLength], parr, sizeof(ArrTx));
    pArrTx = &ArrTx[0];
    pc.putc(*pArrTx);
    if(!ArrTxBusy) {
        ArrTxBusy = 1;
        pc.attach(IntrTx, Serial::TxIrq);
        }
    TxBuffFull = 0;
    }

void IntrTx() {
    if ( ++pArrTx <= &ArrTx[ArrTxSize-1] ) pc.putc(*pArrTx);
    else {
        if(TxBuffFull) CopyArr_InitTx(&TxBuff.cmd);
            else {
                pc.attach(NULL, Serial::TxIrq);
                ArrTxBusy = 0;
                }
        }
    }//IntrTx

StartTxStates StartTx(uint8_t *parr){
        if(TxBuffFull){ return TxBusy; }
        *(parr + (MsgCmdSize + MsgParsSize)) = GetCheckSum(parr);
        if(ArrTxBusy) {
            memcpy(&TxBuff.cmd, parr, sizeof(TxBuff));
            TxBuffFull=1;
            if(!ArrTxBusy) CopyArr_InitTx(&TxBuff.cmd);
            } else CopyArr_InitTx(parr);
        return StartOK;
    }//StartTx

void IntrRx() {
    *pArrRx = pc.getc();
    if ( pArrRx++ >= &ArrRx[ArrRxSize-1] ) {
        pArrRx = &ArrRx[0]; 
        RxBuffFull = 1;
        memcpy(&RxBuff.cmd, &ArrRx[HeaderLength], sizeof(RxBuff)); 
        }
    }//IntrRx

void TxRxServiceInit(){
    pc.attach(IntrRx, Serial::RxIrq);
    pc.attach(NULL, Serial::TxIrq);//я УПЁРТЫЙ дебил
    }