Manchester

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Manchester.h Source File

Manchester.h

00001 /*
00002  ******************************************************************************
00003  * @file    Manchester.h
00004  * @author  Zoltan Hudak
00005  * @version
00006  * @date    2017-May-16
00007  * @brief   Manchester code for mbed
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com>
00012  *
00013  * All rights reserved.
00014 
00015  This program is free software: you can redistribute it and/or modify
00016  it under the terms of the GNU General Public License as published by
00017  the Free Software Foundation, either version 3 of the License, or
00018  (at your option) any later version.
00019 
00020  This program is distributed in the hope that it will be useful,
00021  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  GNU General Public License for more details.
00024 
00025  You should have received a copy of the GNU General Public License
00026  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00027  */
00028 /*
00029    This library implements Manchester code according to both IEEE 802.3 
00030    and G.E. Thomas' conventions. 
00031    • A '0' is expressed by a high-to-low transition, a '1' by low-to-high transition 
00032      in the IEEE 802.3 convention. The reverse is true in the G.E. Thomas' convention.
00033    • The transitions which signify '0' or '1' occur at the midpoint of a period. 
00034    • Transitions at the start of a period are overhead and don't signify data. 
00035    • Least significant bit is sent first 
00036    • There is one synchronization pulse at the begin of transmission 
00037    
00038    The IEEE 802.3 convention is used by default.
00039    Select a convention to be used by commenting or uncommenting 
00040    the line "#define G_E_THOMAS 1" below. 
00041 */
00042 #ifndef MANCHESTER_H
00043 #define MANCHESTER_H
00044 
00045 #include "mbed.h"
00046 
00047 class   Manchester
00048 {
00049     enum State
00050     {
00051         IDLE,
00052         SYNCH_START,
00053         SYNCH_NEXT,
00054         SYNCH_END,
00055         SETUP,
00056         TRANSITION,
00057         COMPLETE,
00058         LISTEN,
00059         DECODE_START,
00060         DECODE,
00061         STOP,
00062         ERROR
00063     };
00064 public:
00065     Manchester
00066     (
00067         PinName     txPin,          /* transmitter pin name */
00068         PinName     rxPin,          /* receiver pin name */
00069         uint32_t    speed = 1200,   /* speed in bits per second */
00070         uint8_t     tol = 10,       /* pulse width tolerance (+/-) in % */
00071         float       rxTimeout = 5   /* receive timeout */
00072     );
00073     ~       Manchester(void)    { }
00074     void    transmit(uint8_t *msg, uint8_t len);
00075     bool    receive(uint8_t *msg, uint8_t *len);
00076     void    setPreamble(uint8_t preamble)   { _preamble = preamble; }
00077     void    setRxTimeout(float seconds)     { _rxTimeout = seconds; }
00078 
00079 private:
00080     DigitalOut  _tx;                // transmitter line
00081     InterruptIn _rx;                // receiver line
00082     Ticker      _txTicker;          // transmitter ticker
00083     Timeout     _timeout;           // timeout (watchdog)
00084     Timer       _timer;
00085     uint32_t    _midBitTime;        // mid-bit time [us]
00086     uint32_t    _minPulseWidth;     // minimum pulse width [us]
00087     uint32_t    _maxPulseWidth;     // maximum pulse width [us]
00088     uint32_t    _tol;
00089     float       _rxTimeout;         // reception timeout [s]
00090     State       _state;             // state
00091     char*       _data;              // data array
00092     uint8_t     _len;               // data length in bytes
00093     uint8_t     _maxLen;            // maximum length
00094     size_t      _preamble;          // number of start patterns
00095     void        transmission(void);
00096     void        reception(void);
00097     void        onTxTimeout(void);
00098     void        onRxTimeout(void);
00099 };
00100 #endif // MANCHESTER_H