From 9aed5d7e7b3c7bf09da712e9c272ece401a7acc9 Mon Sep 17 00:00:00 2001 From: QuakeGod <QuakeGod@sina.com> Date: 星期一, 25 十一月 2024 14:51:23 +0800 Subject: [PATCH] add UltraSonic and MultiWireLess --- CCT6_TestApp1/Src/SLP.c | 123 +++++++++++++++++++--------------------- 1 files changed, 59 insertions(+), 64 deletions(-) diff --git a/CCT6_TestApp1/Src/SLP.c b/CCT6_TestApp1/Src/SLP.c index c7740ec..85796d1 100644 --- a/CCT6_TestApp1/Src/SLP.c +++ b/CCT6_TestApp1/Src/SLP.c @@ -1,26 +1,8 @@ - #include "SLP.h" -void SLPSendPacket(void * buf, uchar len1); -uchar bSLPMaster; -uchar nStation; -uchar SLPinputB; -uchar SLPoutputB; -uchar nCurStation; -uchar inputBuf[5]; -uchar outputBuf[5]; +//void SLPSendPacket(char * buf, uchar len1); -uchar SendBuf[8]; - -uchar SLPMasterRecved; //主机收到子机回复 -unsigned int SLPSlaveCountOut; - -unsigned int SLPLostPkt=0; // 丢包数量 - -uchar SLPOKSign; -uchar SLPErrSign; - -uchar SLPBCC(uchar * pBuf, uchar len1) +uchar SLPBCC(unsigned char * pBuf, uchar len1) { uchar i; uchar BCC=0; @@ -31,99 +13,112 @@ return BCC; } -void SLPparsePacket(void * pRecvBuf, uchar len1) +void SLPInit(stSLPDef * pSLP, SLPSendPktDef pFunc1) +{ + pSLP->SLPLostPkt = 0; + pSLP->nCount = 0; + pSLP->SLPSendPktFunc = pFunc1; + +} + +void SLPSetCallBack(stSLPDef * pSLP, SLPSendPktDef pFunc1) +{ + pSLP->SLPSendPktFunc = pFunc1; +} + +void SLPparsePacket(stSLPDef * pSLP, unsigned char * pRecvBuf, uchar len1) { stSLPPacket * pPacket = (stSLPPacket *)pRecvBuf; if (len1 != sizeof(stSLPPacket)) return; // if (pPacket->ED != EDsign) return; if (pPacket->BCC != SLPBCC(pRecvBuf,len1-1)) return; - if (bSLPMaster) //master + if (pSLP->bSLPMaster) //master { if (pPacket->ST ==ST_S) { //check - if (pPacket->Dst == nCurStation) { - SLPMasterRecved=1; - SLPLostPkt=0; - inputBuf[nCurStation] = pPacket->Data; + if (pPacket->Dst == pSLP->nCurStation) { + pSLP->SLPMasterRecved=1; + pSLP->SLPLostPkt=0; + pSLP->inputBuf[pSLP->nCurStation] = pPacket->Data; } } // SLPoutputB = (inputBuf[1] &0x0f) | ((inputBuf[2] &0x0f) << 4); - SLPoutputB = inputBuf[1]; + pSLP->SLPoutputB = pSLP->inputBuf[1]; }else { //slave if (pPacket->ST==ST_M) { //check - stSLPPacket * pRplyPkt = (stSLPPacket *)SendBuf; - if (pPacket->Dst == nStation) { - SLPoutputB = pPacket->Data; - SLPSlaveCountOut=0; + stSLPPacket * pRplyPkt = (stSLPPacket *)pSLP->SendBuf; + if (pPacket->Dst == pSLP->nStation) { + pSLP->SLPoutputB = pPacket->Data; + pSLP->SLPSlaveCountOut=0; pRplyPkt->ST = ST_S; - pRplyPkt->Dst = nStation; - pRplyPkt->Data = SLPinputB; - pRplyPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1); + pRplyPkt->Dst = pSLP->nStation; + pRplyPkt->Data = pSLP->SLPinputB; + pRplyPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1); // pRplyPkt->ED = EDsign; - SLPSendPacket(SendBuf,sizeof(stSLPPacket)); + pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket)); } } } } -void SLPMasterSendPacket(void) +void SLPMasterSendPacket(stSLPDef * pSLP) { - stSLPPacket * pReqPkt = (stSLPPacket *)SendBuf; - outputBuf[1]=SLPinputB ;//&0xf; + stSLPPacket * pReqPkt = (stSLPPacket *)pSLP->SendBuf; + pSLP->outputBuf[1]=pSLP->SLPinputB ;//&0xf; // outputBuf[2] = (SLPinputB & 0xf0) >> 4; pReqPkt->ST = ST_M; - pReqPkt->Dst = nCurStation; - pReqPkt->Data = outputBuf[nCurStation]; ; - pReqPkt->BCC = SLPBCC(SendBuf, sizeof(stSLPPacket)-1); + pReqPkt->Dst = pSLP->nCurStation; + pReqPkt->Data = pSLP->outputBuf[pSLP->nCurStation]; ; + pReqPkt->BCC = SLPBCC(pSLP->SendBuf, sizeof(stSLPPacket)-1); // pReqPkt->ED = EDsign; - SLPSendPacket(SendBuf,sizeof(stSLPPacket)); + pSLP->SLPSendPktFunc(pSLP->SendBuf,sizeof(stSLPPacket)); } -void SLPProcess(void) + +void SLPProcess(stSLPDef * pSLP) { - static int nCount =0; - if (bSLPMaster) //master + if (pSLP->bSLPMaster) //master { - if ( (nCount & 0x3f) == 0 ) + if ( (pSLP->nCount & 0xf) == 0 ) { //time up - if (SLPMasterRecved) { + if (pSLP->SLPMasterRecved) { // SLPMasterRecved=0; - SLPOKSign = 1; - if (SLPErrSign) SLPErrSign--; + pSLP->SLPOKSign = 1; + if (pSLP->SLPErrSign) pSLP->SLPErrSign--; }else { - SLPLostPkt++; - if (SLPLostPkt > 10) { - SLPErrSign=20; - SLPOKSign = 0; + pSLP->SLPLostPkt++; + if (pSLP->SLPLostPkt > 10) { + pSLP->SLPErrSign=20; + pSLP->SLPOKSign = 0; } } - if (nStation >0) { - nCurStation++; - if (nCurStation > nStation) { - nCurStation =1; + if (pSLP->nStation >0) { + pSLP->nCurStation++; + if (pSLP->nCurStation > pSLP->nStation) { + pSLP->nCurStation =1; } - SLPMasterRecved=0; - SLPMasterSendPacket(); + pSLP->SLPMasterRecved=0; + SLPMasterSendPacket(pSLP); } } }else { - SLPSlaveCountOut ++; - if (SLPSlaveCountOut >200) // 20mS + pSLP->SLPSlaveCountOut ++; + if (pSLP->SLPSlaveCountOut >200) // 20mS { - SLPErrSign=100; + pSLP->SLPErrSign=100; }else { - if (SLPErrSign) SLPErrSign--; + if (pSLP->SLPErrSign) pSLP->SLPErrSign--; } } - nCount++; + pSLP->nCount++; } -- Gitblit v1.9.1