FEP interrupt, response, ring buffer

Dependents:   087_myFEP_TX 087_myFEP_RX

Files at this revision

API Documentation at this revision

Comitter:
piroro4560
Date:
Fri Oct 15 13:23:15 2021 +0000
Parent:
3:12dcc46fb9dc
Commit message:
Completed for the time being

Changed in this revision

FEP.cpp Show annotated file Show diff for this revision Revisions of this file
FEP.h Show annotated file Show diff for this revision Revisions of this file
--- 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
--- a/FEP.h	Mon Oct 11 13:22:16 2021 +0000
+++ b/FEP.h	Fri Oct 15 13:23:15 2021 +0000
@@ -1,86 +1,89 @@
-/** @FEP.h
- *  @brief FEP
+/**
+ *  @file   FEP.h
+ *  @brief  FEPライブラリ
+ *  @author 安澤瑠
+ *  @date   21/10/15
  */
 #ifndef FEP_H
 #define FEP_H
 
+#define TIMEOUT_COUNT 10
+
 #include "mbed.h"
 
 /**
  *  @class myFEP Class for communicating using FEP-01, FEP-02
  *  @brief Class for communicating using FEP-01, FEP-02
- *  @note  Not compatible with mbed-os 6
+ *  @note  mbed-os 6 では使えません。  Not compatible with mbed-os 6
  */
 class myFEP : public RawSerial {
 public :
     /** constructor
-     *  @param tx   SerialTX mbed pin to connect to FEP
-     *  @param rx   SerialRX mbed pin to connect to FEP
-     *  @param addr Destination address
-     *  @param baud baudrate
+     *  @param tx   FEPと接続するSerialTX pin
+     *  @param rx   FEPと接続するSerialRX pin
+     *  @param addr 通信相手のアドレス
+     *  @param baud 通信速度(デフォルト115200)
      */
     myFEP(PinName tx, PinName rx, uint8_t addr_, int baud=115200);
 
-    /** Receive start function
+    /** Start receiving
      */
     void StartReceive();
+    
+    /** Check timeout
+     * @brief 0.1秒毎のループで受信のタイムアウトをチェック
+     */
+    void TimeoutLoop();
 
-    /** Receive interrupt function
+    /** Interrupt input
      */
     void ReceiveBytes();
 
-    /** Message reading function
+    /** extract the message
      */
     void CheckData();
 
-    /** Received message substitution function
-     *  @param data Data address of storage destination
+    /** Write the received message
+     *  @param data 受信メッセージを格納する配列
      */
-    void GetData(uint8_t *data);
+    uint8_t GetData(uint8_t *data);
 
-    /** Send data to the other party FEP
-     *  Send all data in the @brief argument
-     *  @param data Array to send
-     *  @return 0  Successful transmission
-     *  @return 1  Excessive amount of datax
-     *  @return 2  command error
-     *  @return 3  No response from the other party
-     *  @return 4  The other party failed to receive
-     *  @return -1 Not sure
-     */
-    int8_t SendData(uint8_t *data);
-
-    /** Send by specifying the length
-     *  @brief Send   'data' of 'length' length
-     *  @param data   address of array to send
-     *  @param length length to send
+    /** send message
+     *  @brief data配列のデータをlength分送信する
+     *  @param data   送るデータ配列のアドレス
+     *  @param length 送るデータのバイト数
      *
-     *  @return 0  Successful transmission
-     *  @return 1  Excessive amount of data
-     *  @return 2  command error
-     *  @return 3  No response from the other party
-     *  @return 4  The other party failed to receive
-     *  @return -1 Not sure
+     *  @return 0  通信成功
+     *  @return 1  データ量過多
+     *  @return 2  コマンドエラー
+     *  @return 3  通信相手からのレスポンス無し
+     *  @return 4  通信相手が受け取りに失敗
+     *  @return -1 可否不能
      */
     int8_t SendData(uint8_t *data, uint8_t length);
 
     /** Response acquisition function
-     *  @return 0  Successful transmission
-     *  @return 2  command error
-     *  @return 3  No response from the other party
-     *  @return 4  The other party failed to receive
-     *  @return -1 Not sure
+     *  @return 0  通信成功
+     *  @return 2  コマンドエラー
+     *  @return 3  通信相手からのレスポンス無し
+     *  @return 4  通信相手が受け取りに失敗
+     *  @return -1 可否不能
      */
     int8_t GetResponse();
+    
+    bool status; //! (TIMEOUT_COUNT * 0.1)秒間通信が来ていないと0
 
 private :
-    int8_t ctoi(char c);
+    
+    Ticker timeoutTimer;
 
-    uint8_t  addr;         //! Destination address
-    uint8_t  buffer[256];  //! Array for storing received data
-    uint8_t  retdata[256]; //! Data storage array for Substitution
-    uint16_t bufindex;     //! index of buffer
-    uint16_t retindex;     //! index of retdata
+    uint8_t addr;         //! 通信相手のアドレス
+    uint8_t buffer[256];  //! 全受信データを格納するリングバッファ配列
+    uint8_t retdata[128]; //! メッセージのみを格納する配列
+    uint8_t bufindex;     //! buffer の添え字変数
+    uint8_t retindex;     //! retdata の添え字変数
+    uint8_t length;       //! メッセージバイト数
+    uint8_t timeout;      //! 0.1秒間通信が成功していないと1増える
 };
 
 #endif
\ No newline at end of file