NHK2020_master_program

Dependencies:   mbed QEI

Committer:
skouki
Date:
Sat Nov 07 00:43:06 2020 +0000
Revision:
2:e2ec64c77b6e
Parent:
1:496ab5127b06
2020_11_07;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
skouki 1:496ab5127b06 1 #include "FEP.h"
skouki 1:496ab5127b06 2
skouki 1:496ab5127b06 3 FEP::FEP(PinName tx, PinName rx, uint8_t addr, int baud) :
skouki 1:496ab5127b06 4 RawSerial(tx, rx, baud)
skouki 1:496ab5127b06 5 {
skouki 1:496ab5127b06 6 _address = addr;
skouki 1:496ab5127b06 7 }
skouki 1:496ab5127b06 8
skouki 1:496ab5127b06 9 void FEP::startReceive(uint16_t dataSize)
skouki 1:496ab5127b06 10 {
skouki 1:496ab5127b06 11 bufferSize = dataSize + 15;
skouki 1:496ab5127b06 12 bufferPoint = 0;
skouki 1:496ab5127b06 13 receivedBytes = 0;
skouki 1:496ab5127b06 14 buffer = new uint8_t[bufferSize];
skouki 1:496ab5127b06 15 data = new uint8_t[dataSize];
skouki 1:496ab5127b06 16 attach(callback(this, &FEP::receiveByte));
skouki 1:496ab5127b06 17 }
skouki 1:496ab5127b06 18
skouki 1:496ab5127b06 19 void FEP::getData(uint8_t data_[])
skouki 1:496ab5127b06 20 {
skouki 1:496ab5127b06 21 for(int i = 0; i < bufferSize - 15; i++) {
skouki 1:496ab5127b06 22 data_[i] = data[i];
skouki 1:496ab5127b06 23 }
skouki 1:496ab5127b06 24 }
skouki 1:496ab5127b06 25
skouki 1:496ab5127b06 26 void FEP::sendData(uint8_t *data_[], uint8_t length)
skouki 1:496ab5127b06 27 {
skouki 1:496ab5127b06 28 uint8_t checksum = 0x00;
skouki 1:496ab5127b06 29
skouki 1:496ab5127b06 30 printf("@TBN%03d%03d", _address, length + 1);
skouki 1:496ab5127b06 31 for (int j = 0; j < length; j++) {
skouki 1:496ab5127b06 32 checksum += *data_[j];
skouki 1:496ab5127b06 33 putc(*data_[j]);
skouki 1:496ab5127b06 34 }
skouki 1:496ab5127b06 35 putc(checksum);
skouki 1:496ab5127b06 36 putc('\r');
skouki 1:496ab5127b06 37 putc('\n');
skouki 1:496ab5127b06 38 }
skouki 1:496ab5127b06 39
skouki 1:496ab5127b06 40 void FEP::receiveByte()
skouki 1:496ab5127b06 41 {
skouki 1:496ab5127b06 42 buffer[bufferPoint % bufferSize] = getc();
skouki 1:496ab5127b06 43
skouki 1:496ab5127b06 44 if(bufferPoint != 0xff) {
skouki 1:496ab5127b06 45 ++bufferPoint;
skouki 1:496ab5127b06 46 } else {
skouki 1:496ab5127b06 47 bufferPoint = (255%bufferSize)+1;
skouki 1:496ab5127b06 48 }
skouki 1:496ab5127b06 49
skouki 1:496ab5127b06 50 ++receivedBytes;
skouki 1:496ab5127b06 51
skouki 1:496ab5127b06 52 if(receivedBytes >= bufferSize) checkData();
skouki 1:496ab5127b06 53 }
skouki 1:496ab5127b06 54
skouki 1:496ab5127b06 55 void FEP::checkData()
skouki 1:496ab5127b06 56 {
skouki 1:496ab5127b06 57 for(int i = 0; i < bufferSize; i++) {
skouki 1:496ab5127b06 58 if(buffer[i % bufferSize] == FEP_HEADER0 && buffer[(i + 1) % bufferSize] == FEP_HEADER1 && buffer[(i + 2) % bufferSize] == FEP_HEADER2) {
skouki 1:496ab5127b06 59 uint8_t checksum = 0x00;
skouki 1:496ab5127b06 60 for(int j = 9; j < bufferSize - 6; j++) {
skouki 1:496ab5127b06 61 checksum += buffer[(i + j)% bufferSize];
skouki 1:496ab5127b06 62 }
skouki 1:496ab5127b06 63 if(checksum == buffer[(i + bufferSize - 6)% bufferSize]) {
skouki 1:496ab5127b06 64 for(int j = 9; j < bufferSize - 6; j++) {
skouki 1:496ab5127b06 65 data[j - 9] = buffer[(i + j) % bufferSize];
skouki 1:496ab5127b06 66 }
skouki 1:496ab5127b06 67 receivedBytes = 0;
skouki 1:496ab5127b06 68 return;
skouki 1:496ab5127b06 69 }
skouki 1:496ab5127b06 70 }
skouki 1:496ab5127b06 71 }
skouki 1:496ab5127b06 72 }