FEP interrupt, response, ring buffer

Dependents:   087_myFEP_TX 087_myFEP_RX

Revision:
4:8d754f144b96
Parent:
3:12dcc46fb9dc
--- a/FEP.cpp	Mon Oct 11 13:22:16 2021 +0000
+++ b/FEP.cpp	Fri Oct 15 13:23:15 2021 +0000
@@ -1,59 +1,75 @@
+/**
+ *  @file   FEP.cpp
+ *  @author 安澤瑠
+ *  @date   21/10/15
+ */
 #include "FEP.h"
 
 myFEP::myFEP(PinName tx, PinName rx, uint8_t addr_, int baud) :
     RawSerial(tx, rx, baud)
 {
     addr     = addr_;
+    timeout  = TIMEOUT_COUNT;
     bufindex = 0;
     retindex = 0;
+    length   = 0;
 }
 
 void myFEP::StartReceive()
 {
     attach(callback(this, &myFEP::ReceiveBytes));
-    
+    timeoutTimer.attach(callback(this, &myFEP::TimeoutLoop), 0.1);
+}
+
+void myFEP::TimeoutLoop()
+{
+    if (timeout >= TIMEOUT_COUNT) {
+        status = false;
+    } else {
+        status = true;
+        timeout++;
+    }
 }
 
 void myFEP::ReceiveBytes()
 {
-    buffer[bufindex] = getc(); // get data
+    buffer[bufindex] = getc(); // Receive 1byte
+    timeout = 0;
 
-    if ((!strncmp((char*)(buffer + ((256 + bufindex - 1)%256) ), "\r\n", 2)) ) { // <CR><LF>
-        CheckData(); // check message
+    if ( (!strncmp((char*)(buffer + ((256 + bufindex - 1)%256) ), "\r\n", 2)) ) { // <CR><LF> bufindex = <LF>(='\n')
+        CheckData(); // check and extract message
     }
     bufindex++;
 }
 
 void myFEP::CheckData()
 {
-    uint8_t temp=0, length=0; // temp:where's array   length:length of array
+    uint8_t temp=0;
     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]; // 
+        if ( !strncmp((char*)(buffer + temp) , "RBN", 3) ) { // check header  temp='R'
+            length = buffer[(temp+9)%256];
+            for (int j = 10; j < length+10; j++) {
+                retdata[j-10] = buffer[(temp+j)%256]; // get message
             }
+            return;
         }
     }
 }
 
-void myFEP::GetData(uint8_t *data)
+uint8_t myFEP::GetData(uint8_t *data)
 {
-    strcpy((char*)data, (char*)retdata);
+    for (int i=0; i<length; i++) data[i] = retdata[i];
+    return length;
 }
 
-int8_t myFEP::SendData(uint8_t *data)
+int8_t myFEP::SendData(uint8_t *data, uint8_t length_)
 {
-    return SendData(data, sizeof(data));
-}
-
-int8_t myFEP::SendData(uint8_t *data, uint8_t length)
-{
-    if(length > 128) return 1;
+    if(length_ > 126) return 1;
     uint8_t sendindex; // index of 'data'
-    printf("@TBN%03d%03d", addr, length); // send comand
-    for (sendindex=0; sendindex<length; sendindex++) {
+    printf("@TBN%03d%03d", addr, length_+1); // send comand
+    putc(length_);
+    for (sendindex=0; sendindex<length_; sendindex++) {
         putc(data[sendindex]); // send message
     }
     printf("\r\n"); // <cr><lf>
@@ -73,10 +89,4 @@
         }
     }
     return -1;
-}
-
-int8_t myFEP::ctoi(char c)
-{
-    if('0' <= c && c <= '9') return (c-'0');
-    else return -1;
 }
\ No newline at end of file