Manchester
Manchester.h@8:c1b5893191fe, 2018-10-14 (annotated)
- Committer:
- hudakz
- Date:
- Sun Oct 14 09:38:33 2018 +0000
- Revision:
- 8:c1b5893191fe
- Parent:
- 6:7454ad91f714
- Child:
- 9:7a23184aa9ef
Number of sync pulses in preamble can be set.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:d5c75b0e5708 | 1 | /* |
hudakz | 0:d5c75b0e5708 | 2 | ****************************************************************************** |
hudakz | 0:d5c75b0e5708 | 3 | * @file Manchester.h |
hudakz | 0:d5c75b0e5708 | 4 | * @author Zoltan Hudak |
hudakz | 2:de778df5892c | 5 | * @version |
hudakz | 6:7454ad91f714 | 6 | * @date 2017-May-16 |
hudakz | 1:11292d238e50 | 7 | * @brief Manchester code for mbed |
hudakz | 0:d5c75b0e5708 | 8 | ****************************************************************************** |
hudakz | 0:d5c75b0e5708 | 9 | * @attention |
hudakz | 0:d5c75b0e5708 | 10 | * |
hudakz | 0:d5c75b0e5708 | 11 | * <h2><center>© COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com> |
hudakz | 0:d5c75b0e5708 | 12 | * |
hudakz | 0:d5c75b0e5708 | 13 | * All rights reserved. |
hudakz | 0:d5c75b0e5708 | 14 | |
hudakz | 0:d5c75b0e5708 | 15 | This program is free software: you can redistribute it and/or modify |
hudakz | 0:d5c75b0e5708 | 16 | it under the terms of the GNU General Public License as published by |
hudakz | 0:d5c75b0e5708 | 17 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 0:d5c75b0e5708 | 18 | (at your option) any later version. |
hudakz | 0:d5c75b0e5708 | 19 | |
hudakz | 0:d5c75b0e5708 | 20 | This program is distributed in the hope that it will be useful, |
hudakz | 0:d5c75b0e5708 | 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 0:d5c75b0e5708 | 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 0:d5c75b0e5708 | 23 | GNU General Public License for more details. |
hudakz | 0:d5c75b0e5708 | 24 | |
hudakz | 0:d5c75b0e5708 | 25 | You should have received a copy of the GNU General Public License |
hudakz | 0:d5c75b0e5708 | 26 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 0:d5c75b0e5708 | 27 | */ |
hudakz | 5:3b2c7e9fda3f | 28 | /* |
hudakz | 5:3b2c7e9fda3f | 29 | This library implements Manchester code according to both IEEE 802.3 |
hudakz | 6:7454ad91f714 | 30 | and G.E. Thomas' conventions. |
hudakz | 5:3b2c7e9fda3f | 31 | • A '0' is expressed by a high-to-low transition, a '1' by low-to-high transition |
hudakz | 5:3b2c7e9fda3f | 32 | in the IEEE 802.3 convention. The reverse is true in the G.E. Thomas' convention. |
hudakz | 6:7454ad91f714 | 33 | • The transitions which signify '0' or '1' occur at the midpoint of a period. |
hudakz | 5:3b2c7e9fda3f | 34 | • Transitions at the start of a period are overhead and don't signify data. |
hudakz | 5:3b2c7e9fda3f | 35 | • Least significant bit is sent first |
hudakz | 5:3b2c7e9fda3f | 36 | • There is one synchronization pulse at the begin of transmission |
hudakz | 5:3b2c7e9fda3f | 37 | |
hudakz | 5:3b2c7e9fda3f | 38 | The IEEE 802.3 convention is used by default. |
hudakz | 5:3b2c7e9fda3f | 39 | Select a convention to be used by commenting or uncommenting |
hudakz | 5:3b2c7e9fda3f | 40 | the line "#define G_E_THOMAS 1" below. |
hudakz | 5:3b2c7e9fda3f | 41 | */ |
hudakz | 0:d5c75b0e5708 | 42 | #ifndef MANCHESTER_H |
hudakz | 0:d5c75b0e5708 | 43 | #define MANCHESTER_H |
hudakz | 0:d5c75b0e5708 | 44 | |
hudakz | 0:d5c75b0e5708 | 45 | #include "mbed.h" |
hudakz | 0:d5c75b0e5708 | 46 | #include "ManchesterMsg.h" |
hudakz | 0:d5c75b0e5708 | 47 | |
hudakz | 8:c1b5893191fe | 48 | //Uncomment the following line to use G.E.Thomas' convention |
hudakz | 5:3b2c7e9fda3f | 49 | |
hudakz | 5:3b2c7e9fda3f | 50 | //#define G_E_THOMAS 1 |
hudakz | 5:3b2c7e9fda3f | 51 | |
hudakz | 0:d5c75b0e5708 | 52 | class Manchester |
hudakz | 0:d5c75b0e5708 | 53 | { |
hudakz | 0:d5c75b0e5708 | 54 | enum State |
hudakz | 0:d5c75b0e5708 | 55 | { |
hudakz | 0:d5c75b0e5708 | 56 | IDLE, |
hudakz | 0:d5c75b0e5708 | 57 | SYNCH_START, |
hudakz | 0:d5c75b0e5708 | 58 | SYNCH_NEXT, |
hudakz | 0:d5c75b0e5708 | 59 | SYNCH_END, |
hudakz | 0:d5c75b0e5708 | 60 | SETUP, |
hudakz | 0:d5c75b0e5708 | 61 | TRANSITION, |
hudakz | 0:d5c75b0e5708 | 62 | COMPLETE, |
hudakz | 0:d5c75b0e5708 | 63 | LISTEN, |
hudakz | 8:c1b5893191fe | 64 | DECODE_START, |
hudakz | 0:d5c75b0e5708 | 65 | DECODE, |
hudakz | 8:c1b5893191fe | 66 | STOP, |
hudakz | 0:d5c75b0e5708 | 67 | ERROR |
hudakz | 0:d5c75b0e5708 | 68 | }; |
hudakz | 0:d5c75b0e5708 | 69 | public: |
hudakz | 0:d5c75b0e5708 | 70 | Manchester |
hudakz | 0:d5c75b0e5708 | 71 | ( |
hudakz | 1:11292d238e50 | 72 | PinName txPin, /* transmitter pin name */ |
hudakz | 1:11292d238e50 | 73 | PinName rxPin, /* receiver pin name */ |
hudakz | 5:3b2c7e9fda3f | 74 | uint32_t speed = 1200, /* speed in bits per second */ |
hudakz | 8:c1b5893191fe | 75 | uint8_t tol = 25, /* pulse width tolerance (+/-) in % */ |
hudakz | 8:c1b5893191fe | 76 | float rxTimeout = 5 /* receive timeout */ |
hudakz | 0:d5c75b0e5708 | 77 | ); |
hudakz | 8:c1b5893191fe | 78 | ~ Manchester(void) { } |
hudakz | 0:d5c75b0e5708 | 79 | void transmit(ManchesterMsg& msg); |
hudakz | 0:d5c75b0e5708 | 80 | bool receive(ManchesterMsg& msg); |
hudakz | 8:c1b5893191fe | 81 | void setPreamble(uint8_t preamble) { _preamble = preamble; } |
hudakz | 8:c1b5893191fe | 82 | void setRxTimeout(float seconds) { _rxTimeout = seconds; } |
hudakz | 8:c1b5893191fe | 83 | char* lastError() { return _error; } |
hudakz | 0:d5c75b0e5708 | 84 | private: |
hudakz | 8:c1b5893191fe | 85 | DigitalOut _tx; // transmitter line |
hudakz | 8:c1b5893191fe | 86 | InterruptIn _rx; // receiver line |
hudakz | 8:c1b5893191fe | 87 | Ticker _txTicker; // transmitter ticker |
hudakz | 8:c1b5893191fe | 88 | Timeout _timeout; // timeout (watchdog) |
hudakz | 8:c1b5893191fe | 89 | Timer _timer; |
hudakz | 8:c1b5893191fe | 90 | uint32_t _midBitTime; // mid-bit time [us] |
hudakz | 8:c1b5893191fe | 91 | uint32_t _minPulseWidth; // minimum pulse width [us] |
hudakz | 8:c1b5893191fe | 92 | uint32_t _maxPulseWidth; // maximum pulse width [us] |
hudakz | 8:c1b5893191fe | 93 | uint32_t _tol; |
hudakz | 8:c1b5893191fe | 94 | float _rxTimeout; // reception timeout [s] |
hudakz | 8:c1b5893191fe | 95 | State _state; // state |
hudakz | 8:c1b5893191fe | 96 | char* _data; // data array |
hudakz | 8:c1b5893191fe | 97 | size_t _len; // data length in bytes |
hudakz | 8:c1b5893191fe | 98 | size_t _maxLen; // maximum length |
hudakz | 8:c1b5893191fe | 99 | size_t _preamble; // number of start patterns |
hudakz | 8:c1b5893191fe | 100 | char* _error; |
hudakz | 0:d5c75b0e5708 | 101 | void transmission(void); |
hudakz | 0:d5c75b0e5708 | 102 | void reception(void); |
hudakz | 8:c1b5893191fe | 103 | void onTxTimeout(void); |
hudakz | 8:c1b5893191fe | 104 | void onRxTimeout(void); |
hudakz | 0:d5c75b0e5708 | 105 | }; |
hudakz | 0:d5c75b0e5708 | 106 | #endif // MANCHESTER_H |