FEP interrupt, response, ring buffer
Dependents: 087_myFEP_TX 087_myFEP_RX
FEP.cpp
- Committer:
- piroro4560
- Date:
- 2021-10-11
- Revision:
- 3:12dcc46fb9dc
- Parent:
- 2:aa9a344a42a8
- Child:
- 4:8d754f144b96
File content as of revision 3:12dcc46fb9dc:
#include "FEP.h" myFEP::myFEP(PinName tx, PinName rx, uint8_t addr_, int baud) : RawSerial(tx, rx, baud) { addr = addr_; bufindex = 0; retindex = 0; } void myFEP::StartReceive() { attach(callback(this, &myFEP::ReceiveBytes)); } void myFEP::ReceiveBytes() { buffer[bufindex] = getc(); // get data if ((!strncmp((char*)(buffer + ((256 + bufindex - 1)%256) ), "\r\n", 2)) ) { // <CR><LF> CheckData(); // check message } bufindex++; } void myFEP::CheckData() { uint8_t temp=0, length=0; // temp:where's array length:length of array for (uint16_t i=0; i<256; i++) { temp = (256 + bufindex - i) % 256; if ( !strncmp((char*)(buffer + temp) , "RBN", 3) ) { // check header length = ctoi(buffer[temp+6])*100 + ctoi(buffer[temp+7])*10 + ctoi(buffer[temp+8]); // calculate length of message for (int j = 9; j < length+9; j++) { retdata[j-9] = buffer[temp+j]; // } } } } void myFEP::GetData(uint8_t *data) { strcpy((char*)data, (char*)retdata); } int8_t myFEP::SendData(uint8_t *data) { return SendData(data, sizeof(data)); } int8_t myFEP::SendData(uint8_t *data, uint8_t length) { if(length > 128) return 1; uint8_t sendindex; // index of 'data' printf("@TBN%03d%03d", addr, length); // send comand for (sendindex=0; sendindex<length; sendindex++) { putc(data[sendindex]); // send message } printf("\r\n"); // <cr><lf> return GetResponse(); // check response } int8_t myFEP::GetResponse() { char res[256]; // array of storing response for (uint8_t resindex=0; resindex<256; resindex++) { res[resindex] = getc(); if (!strncmp(res+resindex-1, "\r\n", 2)) { if (!strncmp(res+resindex-3, "P0", 2)) return 0; else if (!strncmp(res+resindex-3, "N0", 2)) return 2; else if (!strncmp(res+resindex-3, "N1", 2)) return 3; else if (!strncmp(res+resindex-3, "N3", 2)) return 4; } } return -1; } int8_t myFEP::ctoi(char c) { if('0' <= c && c <= '9') return (c-'0'); else return -1; }