Manchester

Files at this revision

API Documentation at this revision

Comitter:
junhong
Date:
Thu Apr 11 07:52:23 2019 +0000
Parent:
8:c1b5893191fe
Commit message:
modified to adopt DALi protocol

Changed in this revision

Manchester.cpp Show annotated file Show diff for this revision Revisions of this file
Manchester.h Show annotated file Show diff for this revision Revisions of this file
--- a/Manchester.cpp	Sun Oct 14 09:38:33 2018 +0000
+++ b/Manchester.cpp	Thu Apr 11 07:52:23 2019 +0000
@@ -40,7 +40,10 @@
    the line "#define G_E_THOMAS 1" in the Manchester.h header file.
  */
 #include "Manchester.h"
-#include "ManchesterMsg.h"
+#include "mbed-trace/mbed_trace.h"
+
+#undef TRACE_GROUP
+#define TRACE_GROUP  "Manchester"
 
 /**
  * @brief   Creates a Manchester object
@@ -70,16 +73,12 @@
     _rx.disable_irq();
     _rx.rise(callback(this, &Manchester::reception));
     _rx.fall(callback(this, &Manchester::reception));
-    _preamble = 8;                                          // number of synch_start patterns
-    _error = "no error";
-    //    printf("_midBitTime    = %d\r\n", _midBitTime);
-    //    printf("_minPulseWidth = %d\r\n", _minPulseWidth);
-    //    printf("_maxPulseWidth = %d\r\n", _maxPulseWidth);
-#ifdef G_E_THOMAS
-    _tx = 1;
-#else
+    _preamble = 1;                                          // number of synch_start patterns
+
+    tr_debug("_midBitTime    = %d", _midBitTime);
+    tr_debug("_minPulseWidth = %d", _minPulseWidth);
+    tr_debug("_maxPulseWidth = %d", _maxPulseWidth);
     _tx = 0;
-#endif
 }
 
 /**
@@ -88,12 +87,12 @@
  * @param   msg Message to transmit
  * @retval
  */
-void Manchester::transmit(ManchesterMsg& msg)
+void Manchester::transmit(uint8_t* msg, uint8_t len)
 {
     bool    txFinished;
 
-    _data = msg.data;
-    _len = msg.len;
+    _data = (char*)msg;
+    _len = len;
     _state = SYNCH_START;
     _txTicker.attach_us(callback(this, &Manchester::transmission), _midBitTime);
 
@@ -133,48 +132,32 @@
                 break;
             }
 
-    #ifdef G_E_THOMAS
-            _tx = 0;                    // pull line low to start synch pulse
-    #else
             _tx = 1;                    // bring line high to start synch pulse
-    #endif
+            
             counter = 0;
             _state = SYNCH_NEXT;
             break;
 
         case SYNCH_NEXT:
             counter++;
-            if ((counter % 4) == 0)
-                _tx = !_tx;
-            if (counter < (_preamble * 8))
-                break;
-            else
-            {
-                byteIndex = 0;
-                encodeByte = _data[byteIndex];
-                bitIndex = 0;
-                _state = SETUP;
-                break;
-            }
+            _tx = !_tx;
+            byteIndex = 0;
+            encodeByte = _data[byteIndex];
+            bitIndex = 0;
+            _state = SETUP;
+            break;
 
         case SETUP:
-    #ifdef G_E_THOMAS
-            _tx = encodeByte & 0x01;    // setup for next bit to transmit
-    #else
-            _tx = !(encodeByte & 0x01); // setup for next bit to transmit
-    #endif
+            _tx = ((encodeByte >> 7) & 0x01); // setup for next bit to transmit
             _state = TRANSITION;
             break;
 
         case TRANSITION:
-    #ifdef G_E_THOMAS
-            _tx = !(encodeByte & 0x01); // set line appropriately for transition
-    #else
-            _tx = encodeByte & 0x01;    // set line appropriately for transition
-    #endif
+            _tx = !((encodeByte >> 7) & 0x01);    // set line appropriately for transition
+            
             if (++bitIndex < 8)
             {
-                encodeByte = (encodeByte >> 1);
+                encodeByte = (encodeByte << 1);
                 _state = SETUP;
             }
             else
@@ -195,34 +178,14 @@
 
         case STOP:
             counter++;
-            if (counter == 1)
-            {
-    #ifdef G_E_THOMAS
-                _tx = 1;
-    #else
-                _tx = 0;
-    #endif
-            }
-            else
-            if (counter == 5)
-            {
-    #ifdef G_E_THOMAS
-                _tx = 0;
-    #else
-                _tx = 1;
-    #endif
-            }
-            else
-            if (counter == 8)
+            _tx = 0;
+
+            if (counter == 4)
                 _state = COMPLETE;
             break;
 
         case COMPLETE:
-    #ifdef G_E_THOMAS
-            _tx = 1;
-    #else
             _tx = 0;
-    #endif
             _state = IDLE;
             break;
 
@@ -253,12 +216,12 @@
  * @retval  true  On success
  *          false Otherwise
  */
-bool Manchester::receive(ManchesterMsg& msg)
+bool Manchester::receive(uint8_t *msg, uint8_t *len)
 {
     bool    rxFinished;
 
-    _data = msg.data;
-    _maxLen = msg.maxLen();
+    _data = (char*)msg;
+    _maxLen = *len;
     _state = LISTEN;
     _timeout.attach(callback(this, &Manchester::onRxTimeout), _rxTimeout);
     _rx.enable_irq();
@@ -275,13 +238,13 @@
 
     if (_state == ERROR)
     {
-        msg.len = 0;
+        len = 0;
         _state = IDLE;
         return false;
     }
     else
     {
-        msg.len = _len;
+        *len = _len;
         return true;
     }
 }
@@ -304,19 +267,13 @@
     {
         case LISTEN:
             begin = now;
-            if (
-    #ifdef G_E_THOMAS
-            _rx == 0
-    #else
-            _rx == 1
-    #endif
-            )
+            if (_rx == 1)
             {
                 _state = SYNCH_START;
             }
             else
             {
-                _error = "SYNCH_START: Isn't a SYNCH pulse";
+                tr_err("SYNCH_START: Isn't a SYNCH pulse");
                 _state = ERROR;
             }
             break;
@@ -331,7 +288,7 @@
             }
             else
             {
-                _error = "SYNCH_START: Isn't a SYNCH pulse";
+                tr_err("SYNCH_START: Isn't a SYNCH pulse");
                 _state = ERROR;
             }
             break;
@@ -351,11 +308,7 @@
                 decodeByte = 0;
                 bitIndex = 0;
                 _len = 0;
-    #ifdef G_E_THOMAS
-                decodeByte |= (((_rx == 0) & 0x01) << bitIndex++);
-    #else
                 decodeByte |= (((_rx == 1) & 0x01) << bitIndex++);
-    #endif
                 _state = DECODE;
             }
             break;
@@ -365,11 +318,7 @@
             if ((_minPulseWidth <= pulseWidth) && (pulseWidth <= _maxPulseWidth))
             {
                 begin = now;
-    #ifdef G_E_THOMAS
-                decodeByte |= (((_rx == 0) & 0x01) << bitIndex++);
-    #else
                 decodeByte |= (((_rx == 1) & 0x01) << bitIndex++);
-    #endif
                 if (bitIndex > 7)
                 {
                     _data[_len++] = decodeByte;
@@ -386,13 +335,7 @@
 
             if (pulseWidth > _maxPulseWidth)
             {
-                if (
-    #ifdef G_E_THOMAS
-                _rx == 0
-    #else
-                _rx == 1
-    #endif
-                )
+                if (_rx == 1)
                 {
                     begin = now;
                     _state = COMPLETE;  // End of reception
@@ -404,13 +347,7 @@
             pulseWidth = now - begin;
             if (pulseWidth > _maxPulseWidth)
             {
-                if (
-    #ifdef G_E_THOMAS
-                _rx == 1
-    #else
-                _rx == 0
-    #endif
-                )
+                if (_rx == 0)
                 {
                     _state = IDLE;  // End of reception
                 }
@@ -437,18 +374,14 @@
 {
     _timeout.detach();
 
-#ifdef G_E_THOMAS
-    if ((_state == DECODE) && (_rx == 1))
-#else
-        if ((_state == DECODE) && (_rx == 0))
-#endif
-        {
-            _error = "rx timeout";
-            _state = IDLE;  // Reception successful
-        }
-        else
-        {
-            _error = "reception error";
-            _state = ERROR; // Reception incomplete
-        }
+    if ((_state == DECODE) && (_rx == 0))
+    {
+        tr_err("rx timeout");
+        _state = IDLE;  // Reception successful
+    }
+    else
+    {
+        tr_err("reception error");
+        _state = ERROR; // Reception incomplete
+    }
 }
--- a/Manchester.h	Sun Oct 14 09:38:33 2018 +0000
+++ b/Manchester.h	Thu Apr 11 07:52:23 2019 +0000
@@ -43,11 +43,6 @@
 #define MANCHESTER_H
 
 #include "mbed.h"
-#include "ManchesterMsg.h"
-
-//Uncomment the following line to use G.E.Thomas' convention
-
-//#define G_E_THOMAS 1
 
 class   Manchester
 {
@@ -72,15 +67,15 @@
         PinName     txPin,          /* transmitter pin name */
         PinName     rxPin,          /* receiver pin name */
         uint32_t    speed = 1200,   /* speed in bits per second */
-        uint8_t     tol = 25,       /* pulse width tolerance (+/-) in % */
+        uint8_t     tol = 10,       /* pulse width tolerance (+/-) in % */
         float       rxTimeout = 5   /* receive timeout */
     );
     ~       Manchester(void)    { }
-    void    transmit(ManchesterMsg& msg);
-    bool    receive(ManchesterMsg& msg);
+    void    transmit(uint8_t *msg, uint8_t len);
+    bool    receive(uint8_t *msg, uint8_t *len);
     void    setPreamble(uint8_t preamble)   { _preamble = preamble; }
     void    setRxTimeout(float seconds)     { _rxTimeout = seconds; }
-    char*   lastError()                     { return _error; }
+
 private:
     DigitalOut  _tx;                // transmitter line
     InterruptIn _rx;                // receiver line
@@ -94,10 +89,9 @@
     float       _rxTimeout;         // reception timeout [s]
     State       _state;             // state
     char*       _data;              // data array
-    size_t      _len;               // data length in bytes
-    size_t      _maxLen;            // maximum length
+    uint8_t     _len;               // data length in bytes
+    uint8_t     _maxLen;            // maximum length
     size_t      _preamble;          // number of start patterns
-    char*       _error;
     void        transmission(void);
     void        reception(void);
     void        onTxTimeout(void);