Library for Manchester encoding using UART's hardware.

Dependents:   ManchesterUART_Transmitter ManchesterUART_Receiver

Manchester encoding using UART's hardware

This library implements Manchester code using UART. Each data byte is encoded into two bytes representing two nibbles of the original data. UART's hardware is then used to generate the Manchester encoded bit stream. Prior to decoding, the receiver converts the received bit stream to bytes using its UART. Start and stop patterns are sent to identify the boundaries (begin and end) of a data frame.

ACKNOWLEDGEMENT: The library is based on an article published by Adrian Mills.


Import programManchesterUART_Transmitter

Transmitter demo for the Manchester encoding library using UART's hardware.


Import programManchesterUART_Receiver

Receiver demo for the Manchester encoding library using UART's hardware.

NOTE: To perform a simple test (without radio link) connect the txPin on transmitter board to the rxPin on the receiver board and make sure that grounds are also connected one another.

Revision:
1:b869674fe56e
Parent:
0:e076052bcffd
--- a/ManchesterUART.h	Wed Nov 22 16:37:13 2017 +0000
+++ b/ManchesterUART.h	Mon Jul 30 09:38:03 2018 +0000
@@ -26,25 +26,24 @@
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 /*
- * This library implements Manchester code using UART serial connection. Each 
+ * This library implements Manchester code using UART serial connection. Each
  * data byte is encoded into two bytes representing two nibbles of the original
- * data byte. These two bytes are then sent over UART serial link connection. 
+ * data byte. These two bytes are then sent over UART serial link connection.
  * The receiver reconstructs the original data byte from the two bytes received.
  * A start and stop pattern are sent to signify the begin and end of a message.
- *  
+ *
  * The library is based on the article published by Adrian Mills:
- * http://www.quickbuilder.co.uk/qb/articles/Manchester_encoding_using_RS232.pdf 
+ * http://www.quickbuilder.co.uk/qb/articles/Manchester_encoding_using_RS232.pdf
  */
-
 #ifndef MANCHESTERUART_H
 #define MANCHESTERUART_H
 
 #include "mbed.h"
 #include "ManchesterMsg.h"
 
-#define START 0xF0          // start pattern
-#define STOP  0x0F          // stop pattern
+#define START   0xF0    // start pattern
 
+#define STOP    0x0F    // stop pattern
 class   ManchesterUART
 {
     enum Error
@@ -54,34 +53,49 @@
         RX_TIMEOUT,
         BUF_OVERRUN
     };
-
 public:
     /**
      * @brief   Creates a ManchesterUART object
      * @note
      * @param   txPin Pin name of transmitter line
-     *          rxPin Pin name of receiver line
-     *          speed Communication bit rate in bits per second. Defaults to 9600bps
-     *          timeout Receive timeout in seconds. Defaults to 5 seconds.
+     * @param   rxPin Pin name of receiver line
+     * @param   preamble Number of start patterns at the begin of each transmission
+     * @param   baudrate Communication bit rate in bits per second. Defaults to 115200bps
+     * @param   timeout Receive timeout in seconds. Defaults to 5 seconds.
      * @retval
      */
-    ManchesterUART(PinName txPin, PinName rxPin, int speed = 9600, float timeout_sec = 5) :
-        _serial(txPin, rxPin, speed), _timeout_sec(timeout_sec), _error(NO_ERROR) {}
+    ManchesterUART
+    (
+        PinName txPin,
+        PinName rxPin,
+        int     baudrate = 9600,    /* bits/sec */
+        uint8_t preamble = 8,       /* number of start patterns */
+        float   rxTimeout = 5       /* seconds */
+    ) :
+    _serial(txPin, rxPin, baudrate),
+    _rxTimeout(rxTimeout),
+    _preamble(preamble),
+    _error(NO_ERROR)
+    { }
 
-    ~ManchesterUART(void) { }
+    ~       ManchesterUART(void)    { }
     void    transmit(ManchesterMsg& msg);
+    void    attach(Callback<void ()> func, SerialBase::IrqType type = SerialBase::RxIrq)    { _serial.attach(func, type); }
     bool    receive(ManchesterMsg& msg);
-    Error   lastError() { return _error; }
-
+    bool    readable()                      { return _serial.readable(); }
+    void    baud(int baudrate)              { _serial.baud(baudrate); }
+    void    setPreamble(uint8_t length)     { _preamble = length; }
+    void    setRxTimeout(int seconds)       { _rxTimeout = seconds; }
+    Error   lastError()                     { return _error; }
 private:
     Serial  _serial;
-    char*   _data;          // data array
-    size_t  _len;           // data length in bytes
-    size_t  _maxLen;        // maximum length
-    Timeout _timeout;       // timeout
-    float   _timeout_sec;   // timeout in seconds
-    Error   _error;         // error flag
-
+    char*   _data;      // data array
+    size_t  _len;       // data length in bytes
+    size_t  _maxLen;    // data maximum length
+    Timeout _timeout;   // timeout
+    float   _rxTimeout; // timeout time in seconds
+    uint8_t _preamble;  // number of start patterns
+    Error   _error;     // error flag
     void    rxTimeout(void);
     void    transmitByte(uint8_t data);
     uint8_t receiveByte(void);