Manchester code (phase encoding) library.
Dependents: Manchester_Transmitter Manchester_Receiver
Manchester code (phase encoding) library
It implements Manchester code according to both IEEE 802.3 and G.E. Thomas' conventions.
- A '0' is expressed by a high-to-low transition, a '1' by low-to-high transition in the IEEE 802.3 convention. The reverse is true in the G.E. Thomas' convention.
- The transitions which signify '0' or '1' occur at the midpoint of a period.
- Transitions at the start of a period are overhead and don't signify data.
- Least significant bit is sent first
- There are synchronization pulses (the number can be set) at the begin of transmission
Select a convention to be used by commenting or uncommenting the line below in theManchester.h
header file.
Manchester.h
#define G_E_THOMAS 1
The IEEE 802.3 convention is used by default.
A Manchester encoded message (using G.E. Thomas' convention), with one sync pulse in the preamble, carrying four bytes:
ACKNOWLEDGEMENT: The code in this library was based on this article published by Robert Guastella.
Import programManchester_Transmitter
Manchester transmitter demo.
Import programManchester_Receiver
Manchester receiver demo.
NOTE: To perform a simple test (without radio modules) connect the txPin on transmitter board to the rxPin on the receiver board and make sure that grounds are also connected one another.
Manchester.cpp@7:afd0ee36dcd1, 2017-09-03 (annotated)
- Committer:
- hudakz
- Date:
- Sun Sep 03 09:04:18 2017 +0000
- Revision:
- 7:afd0ee36dcd1
- Parent:
- 6:7454ad91f714
- Child:
- 8:c1b5893191fe
Updated: Adapted to changes in mbed lib and some comments modified.
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.cpp |
hudakz | 0:d5c75b0e5708 | 4 | * @author Zoltan Hudak |
hudakz | 2:de778df5892c | 5 | * @version |
hudakz | 6:7454ad91f714 | 6 | * @date 2017-May-16 |
hudakz | 0:d5c75b0e5708 | 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 | /* |
hudakz | 7:afd0ee36dcd1 | 30 | This library implements Manchester code according to both IEEE 802.3 |
hudakz | 7:afd0ee36dcd1 | 31 | and G.E. Thomas' conventions. |
hudakz | 7:afd0ee36dcd1 | 32 | • A '0' is expressed by a high-to-low transition, a '1' by low-to-high transition |
hudakz | 5:3b2c7e9fda3f | 33 | in the IEEE 802.3 convention. The reverse is true in the G.E. Thomas' convention. |
hudakz | 7:afd0ee36dcd1 | 34 | • The transitions which signify '0' or '1' occur at the midpoint of a period. |
hudakz | 7:afd0ee36dcd1 | 35 | • Transitions at the start of a period are overhead and don't signify data. |
hudakz | 7:afd0ee36dcd1 | 36 | • Least significant bit is sent first |
hudakz | 7:afd0ee36dcd1 | 37 | • There is one synchronization pulse at the begin of transmission |
hudakz | 7:afd0ee36dcd1 | 38 | |
hudakz | 5:3b2c7e9fda3f | 39 | The IEEE 802.3 convention is used by default. |
hudakz | 7:afd0ee36dcd1 | 40 | Select a convention to be used by commenting or uncommenting |
hudakz | 5:3b2c7e9fda3f | 41 | the line "#define G_E_THOMAS 1" in the Manchester.h header file. |
hudakz | 5:3b2c7e9fda3f | 42 | */ |
hudakz | 5:3b2c7e9fda3f | 43 | |
hudakz | 0:d5c75b0e5708 | 44 | #include "Manchester.h" |
hudakz | 0:d5c75b0e5708 | 45 | #include "ManchesterMsg.h" |
hudakz | 0:d5c75b0e5708 | 46 | |
hudakz | 0:d5c75b0e5708 | 47 | /** |
hudakz | 0:d5c75b0e5708 | 48 | * @brief Creates a Manchester object |
hudakz | 0:d5c75b0e5708 | 49 | * @note |
hudakz | 0:d5c75b0e5708 | 50 | * @param txPin Pin name of transmitter line |
hudakz | 0:d5c75b0e5708 | 51 | * rxPin Pin name of receiver line |
hudakz | 0:d5c75b0e5708 | 52 | * speed Communication bit rate in bits per second |
hudakz | 3:03109c995123 | 53 | * tol Pulse width tolerance in % |
hudakz | 0:d5c75b0e5708 | 54 | * @retval |
hudakz | 0:d5c75b0e5708 | 55 | */ |
hudakz | 3:03109c995123 | 56 | Manchester::Manchester |
hudakz | 3:03109c995123 | 57 | ( |
hudakz | 7:afd0ee36dcd1 | 58 | PinName txPin, |
hudakz | 7:afd0ee36dcd1 | 59 | PinName rxPin, |
hudakz | 7:afd0ee36dcd1 | 60 | uint32_t speed, /* = 1200 bps */ |
hudakz | 7:afd0ee36dcd1 | 61 | uint8_t tol /* = (+/-)25% */ |
hudakz | 7:afd0ee36dcd1 | 62 | ) : |
hudakz | 7:afd0ee36dcd1 | 63 | _tx(txPin), |
hudakz | 2:de778df5892c | 64 | _rx(rxPin) { |
hudakz | 0:d5c75b0e5708 | 65 | _state = IDLE; |
hudakz | 0:d5c75b0e5708 | 66 | _midBitTime = 1000000 / speed / 2; // mid-bit time [us] |
hudakz | 0:d5c75b0e5708 | 67 | _minPulseWidth = (_midBitTime * 2 * (100 - tol)) / 100; // [us] |
hudakz | 0:d5c75b0e5708 | 68 | _maxPulseWidth = (_midBitTime * 2 * (100 + tol)) / 100; // [us] |
hudakz | 0:d5c75b0e5708 | 69 | _rx.disable_irq(); |
hudakz | 0:d5c75b0e5708 | 70 | _rx.rise(callback(this, &Manchester::reception)); |
hudakz | 0:d5c75b0e5708 | 71 | _rx.fall(callback(this, &Manchester::reception)); |
hudakz | 5:3b2c7e9fda3f | 72 | #ifdef G_E_THOMAS |
hudakz | 5:3b2c7e9fda3f | 73 | _tx = 1; |
hudakz | 5:3b2c7e9fda3f | 74 | #else |
hudakz | 5:3b2c7e9fda3f | 75 | _tx = 0; |
hudakz | 5:3b2c7e9fda3f | 76 | #endif |
hudakz | 0:d5c75b0e5708 | 77 | } |
hudakz | 0:d5c75b0e5708 | 78 | |
hudakz | 0:d5c75b0e5708 | 79 | /** |
hudakz | 0:d5c75b0e5708 | 80 | * @brief Transmits message |
hudakz | 0:d5c75b0e5708 | 81 | * @note |
hudakz | 0:d5c75b0e5708 | 82 | * @param msg Message to transmit |
hudakz | 0:d5c75b0e5708 | 83 | * @retval |
hudakz | 0:d5c75b0e5708 | 84 | */ |
hudakz | 0:d5c75b0e5708 | 85 | void Manchester::transmit(ManchesterMsg& msg) { |
hudakz | 6:7454ad91f714 | 86 | bool txFinished; |
hudakz | 0:d5c75b0e5708 | 87 | |
hudakz | 0:d5c75b0e5708 | 88 | _data = msg.data; |
hudakz | 0:d5c75b0e5708 | 89 | _len = msg.len; |
hudakz | 0:d5c75b0e5708 | 90 | _state = SYNCH_START; |
hudakz | 0:d5c75b0e5708 | 91 | _txTicker.attach_us(callback(this, &Manchester::transmission), _midBitTime); |
hudakz | 0:d5c75b0e5708 | 92 | |
hudakz | 6:7454ad91f714 | 93 | do { |
hudakz | 0:d5c75b0e5708 | 94 | core_util_critical_section_enter(); |
hudakz | 6:7454ad91f714 | 95 | txFinished = (_state == IDLE); |
hudakz | 0:d5c75b0e5708 | 96 | core_util_critical_section_exit(); |
hudakz | 6:7454ad91f714 | 97 | } while(!txFinished); |
hudakz | 0:d5c75b0e5708 | 98 | |
hudakz | 0:d5c75b0e5708 | 99 | _txTicker.detach(); |
hudakz | 0:d5c75b0e5708 | 100 | } |
hudakz | 0:d5c75b0e5708 | 101 | |
hudakz | 0:d5c75b0e5708 | 102 | /** |
hudakz | 0:d5c75b0e5708 | 103 | * @brief ISR handling transmission |
hudakz | 3:03109c995123 | 104 | * @note Called by _txTicker |
hudakz | 0:d5c75b0e5708 | 105 | * @param |
hudakz | 0:d5c75b0e5708 | 106 | * @retval |
hudakz | 0:d5c75b0e5708 | 107 | */ |
hudakz | 0:d5c75b0e5708 | 108 | void Manchester::transmission(void) { |
hudakz | 0:d5c75b0e5708 | 109 | static uint8_t encodeByte; |
hudakz | 2:de778df5892c | 110 | static size_t byteIndex; |
hudakz | 0:d5c75b0e5708 | 111 | static uint8_t bitIndex; |
hudakz | 0:d5c75b0e5708 | 112 | |
hudakz | 2:de778df5892c | 113 | _timeout.attach_us(callback(this, &Manchester::txTimeout), _maxPulseWidth * 4); |
hudakz | 2:de778df5892c | 114 | |
hudakz | 0:d5c75b0e5708 | 115 | switch(_state) { |
hudakz | 0:d5c75b0e5708 | 116 | case SYNCH_START: |
hudakz | 5:3b2c7e9fda3f | 117 | #ifdef G_E_THOMAS |
hudakz | 5:3b2c7e9fda3f | 118 | _tx = 0; // pull line low to start synch pulse |
hudakz | 5:3b2c7e9fda3f | 119 | #else |
hudakz | 5:3b2c7e9fda3f | 120 | _tx = 1; // bring line high to start synch pulse |
hudakz | 5:3b2c7e9fda3f | 121 | #endif |
hudakz | 0:d5c75b0e5708 | 122 | _state = SYNCH_NEXT; |
hudakz | 0:d5c75b0e5708 | 123 | break; |
hudakz | 0:d5c75b0e5708 | 124 | |
hudakz | 0:d5c75b0e5708 | 125 | case SYNCH_NEXT: |
hudakz | 0:d5c75b0e5708 | 126 | _state = SYNCH_END; // synch pulse needs to be twice the interrupt rate |
hudakz | 0:d5c75b0e5708 | 127 | break; |
hudakz | 0:d5c75b0e5708 | 128 | |
hudakz | 0:d5c75b0e5708 | 129 | case SYNCH_END: |
hudakz | 5:3b2c7e9fda3f | 130 | #ifdef G_E_THOMAS |
hudakz | 2:de778df5892c | 131 | _tx = 1; // bring line high for end of sych pulse |
hudakz | 5:3b2c7e9fda3f | 132 | #else |
hudakz | 5:3b2c7e9fda3f | 133 | _tx = 0; // pull line low for end of sych pulse |
hudakz | 7:afd0ee36dcd1 | 134 | #endif |
hudakz | 0:d5c75b0e5708 | 135 | byteIndex = 0; |
hudakz | 0:d5c75b0e5708 | 136 | encodeByte = _data[byteIndex]; |
hudakz | 0:d5c75b0e5708 | 137 | bitIndex = 0; |
hudakz | 0:d5c75b0e5708 | 138 | _state = SETUP; |
hudakz | 0:d5c75b0e5708 | 139 | break; |
hudakz | 0:d5c75b0e5708 | 140 | |
hudakz | 0:d5c75b0e5708 | 141 | case SETUP: |
hudakz | 5:3b2c7e9fda3f | 142 | #ifdef G_E_THOMAS |
hudakz | 5:3b2c7e9fda3f | 143 | _tx = encodeByte & 0x01; // setup for next bit to transmit |
hudakz | 5:3b2c7e9fda3f | 144 | #else |
hudakz | 5:3b2c7e9fda3f | 145 | _tx = !(encodeByte & 0x01); // setup for next bit to transmit |
hudakz | 7:afd0ee36dcd1 | 146 | #endif |
hudakz | 0:d5c75b0e5708 | 147 | _state = TRANSITION; |
hudakz | 0:d5c75b0e5708 | 148 | break; |
hudakz | 0:d5c75b0e5708 | 149 | |
hudakz | 0:d5c75b0e5708 | 150 | case TRANSITION: |
hudakz | 5:3b2c7e9fda3f | 151 | #ifdef G_E_THOMAS |
hudakz | 5:3b2c7e9fda3f | 152 | _tx = !(encodeByte & 0x01); // set line appropriately for transition |
hudakz | 5:3b2c7e9fda3f | 153 | #else |
hudakz | 5:3b2c7e9fda3f | 154 | _tx = encodeByte & 0x01; // set line appropriately for transition |
hudakz | 5:3b2c7e9fda3f | 155 | #endif |
hudakz | 0:d5c75b0e5708 | 156 | if(++bitIndex < 8) { |
hudakz | 0:d5c75b0e5708 | 157 | encodeByte = (encodeByte >> 1); |
hudakz | 0:d5c75b0e5708 | 158 | _state = SETUP; |
hudakz | 0:d5c75b0e5708 | 159 | } |
hudakz | 0:d5c75b0e5708 | 160 | else { |
hudakz | 0:d5c75b0e5708 | 161 | if(++byteIndex < _len) { |
hudakz | 0:d5c75b0e5708 | 162 | encodeByte = _data[byteIndex]; |
hudakz | 0:d5c75b0e5708 | 163 | _state = SETUP; |
hudakz | 0:d5c75b0e5708 | 164 | bitIndex = 0; |
hudakz | 0:d5c75b0e5708 | 165 | } |
hudakz | 0:d5c75b0e5708 | 166 | else |
hudakz | 0:d5c75b0e5708 | 167 | _state = COMPLETE; |
hudakz | 0:d5c75b0e5708 | 168 | } |
hudakz | 0:d5c75b0e5708 | 169 | break; |
hudakz | 0:d5c75b0e5708 | 170 | |
hudakz | 0:d5c75b0e5708 | 171 | case COMPLETE: |
hudakz | 5:3b2c7e9fda3f | 172 | #ifdef G_E_THOMAS |
hudakz | 2:de778df5892c | 173 | _tx = 1; // transmission is complete, bring line high |
hudakz | 5:3b2c7e9fda3f | 174 | #else |
hudakz | 7:afd0ee36dcd1 | 175 | _tx = 0; // transmission is complete, pull line low |
hudakz | 7:afd0ee36dcd1 | 176 | #endif |
hudakz | 0:d5c75b0e5708 | 177 | _state = IDLE; |
hudakz | 0:d5c75b0e5708 | 178 | break; |
hudakz | 0:d5c75b0e5708 | 179 | |
hudakz | 0:d5c75b0e5708 | 180 | case IDLE: |
hudakz | 0:d5c75b0e5708 | 181 | default: |
hudakz | 2:de778df5892c | 182 | _timeout.detach(); |
hudakz | 0:d5c75b0e5708 | 183 | return; |
hudakz | 0:d5c75b0e5708 | 184 | } |
hudakz | 0:d5c75b0e5708 | 185 | } |
hudakz | 0:d5c75b0e5708 | 186 | |
hudakz | 0:d5c75b0e5708 | 187 | /** |
hudakz | 2:de778df5892c | 188 | * @brief ISR handling 'transmission timeout' |
hudakz | 2:de778df5892c | 189 | * @note Called when transmitter is stuck. |
hudakz | 2:de778df5892c | 190 | * Signals 'end of transmission' by setting state to IDLE |
hudakz | 7:afd0ee36dcd1 | 191 | * @param |
hudakz | 7:afd0ee36dcd1 | 192 | * @retval |
hudakz | 2:de778df5892c | 193 | */ |
hudakz | 2:de778df5892c | 194 | void Manchester::txTimeout(void) { |
hudakz | 2:de778df5892c | 195 | _timeout.detach(); |
hudakz | 2:de778df5892c | 196 | _state = IDLE; |
hudakz | 2:de778df5892c | 197 | } |
hudakz | 2:de778df5892c | 198 | |
hudakz | 2:de778df5892c | 199 | /** |
hudakz | 0:d5c75b0e5708 | 200 | * @brief Receives message |
hudakz | 7:afd0ee36dcd1 | 201 | * @note Waits until a message is received or 'reception timeout' occured |
hudakz | 1:11292d238e50 | 202 | * @param msg Container to store the received message |
hudakz | 0:d5c75b0e5708 | 203 | * @retval true On success |
hudakz | 0:d5c75b0e5708 | 204 | * false Otherwise |
hudakz | 0:d5c75b0e5708 | 205 | */ |
hudakz | 0:d5c75b0e5708 | 206 | bool Manchester::receive(ManchesterMsg& msg) { |
hudakz | 0:d5c75b0e5708 | 207 | bool rxFinished; |
hudakz | 0:d5c75b0e5708 | 208 | |
hudakz | 0:d5c75b0e5708 | 209 | _data = msg.data; |
hudakz | 0:d5c75b0e5708 | 210 | _maxLen = msg.maxLen(); |
hudakz | 0:d5c75b0e5708 | 211 | _state = LISTEN; |
hudakz | 0:d5c75b0e5708 | 212 | |
hudakz | 0:d5c75b0e5708 | 213 | _rx.enable_irq(); |
hudakz | 0:d5c75b0e5708 | 214 | |
hudakz | 6:7454ad91f714 | 215 | do { |
hudakz | 0:d5c75b0e5708 | 216 | core_util_critical_section_enter(); |
hudakz | 0:d5c75b0e5708 | 217 | rxFinished = ((_state == IDLE) || (_state == ERROR)); |
hudakz | 0:d5c75b0e5708 | 218 | core_util_critical_section_exit(); |
hudakz | 0:d5c75b0e5708 | 219 | } while(!rxFinished); |
hudakz | 0:d5c75b0e5708 | 220 | |
hudakz | 0:d5c75b0e5708 | 221 | _rx.disable_irq(); |
hudakz | 0:d5c75b0e5708 | 222 | |
hudakz | 0:d5c75b0e5708 | 223 | if(_state == ERROR) { |
hudakz | 0:d5c75b0e5708 | 224 | msg.len = 0; |
hudakz | 0:d5c75b0e5708 | 225 | _state = IDLE; |
hudakz | 0:d5c75b0e5708 | 226 | return false; |
hudakz | 0:d5c75b0e5708 | 227 | } |
hudakz | 0:d5c75b0e5708 | 228 | else { |
hudakz | 0:d5c75b0e5708 | 229 | msg.len = _len; |
hudakz | 0:d5c75b0e5708 | 230 | return true; |
hudakz | 0:d5c75b0e5708 | 231 | } |
hudakz | 0:d5c75b0e5708 | 232 | } |
hudakz | 0:d5c75b0e5708 | 233 | |
hudakz | 0:d5c75b0e5708 | 234 | /** |
hudakz | 2:de778df5892c | 235 | * @brief ISR handling reception |
hudakz | 2:de778df5892c | 236 | * @note Called on signal change (rise or fall) on receiver line |
hudakz | 0:d5c75b0e5708 | 237 | * @param |
hudakz | 0:d5c75b0e5708 | 238 | * @retval |
hudakz | 0:d5c75b0e5708 | 239 | */ |
hudakz | 0:d5c75b0e5708 | 240 | void Manchester::reception(void) { |
hudakz | 0:d5c75b0e5708 | 241 | uint32_t now = us_ticker_read(); |
hudakz | 0:d5c75b0e5708 | 242 | static uint32_t begin; |
hudakz | 0:d5c75b0e5708 | 243 | uint32_t pulseWidth; |
hudakz | 0:d5c75b0e5708 | 244 | static uint8_t decodeByte; |
hudakz | 0:d5c75b0e5708 | 245 | static uint8_t bitIndex; |
hudakz | 0:d5c75b0e5708 | 246 | |
hudakz | 2:de778df5892c | 247 | _timeout.attach_us(callback(this, &Manchester::rxTimeout), _maxPulseWidth * 4); |
hudakz | 2:de778df5892c | 248 | |
hudakz | 0:d5c75b0e5708 | 249 | switch(_state) { |
hudakz | 0:d5c75b0e5708 | 250 | case LISTEN: |
hudakz | 0:d5c75b0e5708 | 251 | begin = now; |
hudakz | 5:3b2c7e9fda3f | 252 | #ifdef G_E_THOMAS |
hudakz | 2:de778df5892c | 253 | if(_rx == 0) |
hudakz | 5:3b2c7e9fda3f | 254 | #else |
hudakz | 5:3b2c7e9fda3f | 255 | if(_rx == 1) |
hudakz | 7:afd0ee36dcd1 | 256 | #endif |
hudakz | 0:d5c75b0e5708 | 257 | _state = SYNCH_START; |
hudakz | 0:d5c75b0e5708 | 258 | else |
hudakz | 3:03109c995123 | 259 | _state = ERROR; // It isn't a synch pulse => error |
hudakz | 0:d5c75b0e5708 | 260 | break; |
hudakz | 0:d5c75b0e5708 | 261 | |
hudakz | 0:d5c75b0e5708 | 262 | case SYNCH_START: |
hudakz | 0:d5c75b0e5708 | 263 | pulseWidth = now - begin; |
hudakz | 5:3b2c7e9fda3f | 264 | #ifdef G_E_THOMAS |
hudakz | 2:de778df5892c | 265 | if((_minPulseWidth <= pulseWidth) && (pulseWidth <= _maxPulseWidth) && (_rx == 1)) { |
hudakz | 5:3b2c7e9fda3f | 266 | #else |
hudakz | 5:3b2c7e9fda3f | 267 | if((_minPulseWidth <= pulseWidth) && (pulseWidth <= _maxPulseWidth) && (_rx == 0)) { |
hudakz | 5:3b2c7e9fda3f | 268 | #endif |
hudakz | 0:d5c75b0e5708 | 269 | begin = now; |
hudakz | 0:d5c75b0e5708 | 270 | decodeByte = 0; |
hudakz | 2:de778df5892c | 271 | bitIndex = 0; |
hudakz | 0:d5c75b0e5708 | 272 | _len = 0; |
hudakz | 0:d5c75b0e5708 | 273 | _state = DECODE; |
hudakz | 0:d5c75b0e5708 | 274 | } |
hudakz | 2:de778df5892c | 275 | else |
hudakz | 2:de778df5892c | 276 | _state = ERROR; // It isn't a synch pulse => error |
hudakz | 0:d5c75b0e5708 | 277 | break; |
hudakz | 0:d5c75b0e5708 | 278 | |
hudakz | 0:d5c75b0e5708 | 279 | case DECODE: |
hudakz | 0:d5c75b0e5708 | 280 | pulseWidth = now - begin; |
hudakz | 0:d5c75b0e5708 | 281 | if((_minPulseWidth <= pulseWidth) && (pulseWidth <= _maxPulseWidth)) { |
hudakz | 0:d5c75b0e5708 | 282 | begin = now; |
hudakz | 5:3b2c7e9fda3f | 283 | #ifdef G_E_THOMAS |
hudakz | 0:d5c75b0e5708 | 284 | decodeByte |= (((_rx == 0) & 0x01) << bitIndex++); |
hudakz | 5:3b2c7e9fda3f | 285 | #else |
hudakz | 5:3b2c7e9fda3f | 286 | decodeByte |= (((_rx == 1) & 0x01) << bitIndex++); |
hudakz | 5:3b2c7e9fda3f | 287 | #endif |
hudakz | 0:d5c75b0e5708 | 288 | if(bitIndex > 7) { |
hudakz | 0:d5c75b0e5708 | 289 | _data[_len++] = decodeByte; |
hudakz | 0:d5c75b0e5708 | 290 | if(_len > _maxLen - 1) |
hudakz | 0:d5c75b0e5708 | 291 | _state = ERROR; |
hudakz | 0:d5c75b0e5708 | 292 | else { |
hudakz | 0:d5c75b0e5708 | 293 | decodeByte = 0; |
hudakz | 0:d5c75b0e5708 | 294 | bitIndex = 0; |
hudakz | 0:d5c75b0e5708 | 295 | } |
hudakz | 0:d5c75b0e5708 | 296 | } |
hudakz | 0:d5c75b0e5708 | 297 | } |
hudakz | 0:d5c75b0e5708 | 298 | |
hudakz | 2:de778df5892c | 299 | if(pulseWidth > _maxPulseWidth) |
hudakz | 2:de778df5892c | 300 | _state = ERROR; // Pulse width out of limit => error |
hudakz | 0:d5c75b0e5708 | 301 | |
hudakz | 0:d5c75b0e5708 | 302 | break; |
hudakz | 0:d5c75b0e5708 | 303 | |
hudakz | 0:d5c75b0e5708 | 304 | case IDLE: |
hudakz | 0:d5c75b0e5708 | 305 | case ERROR: |
hudakz | 0:d5c75b0e5708 | 306 | default: |
hudakz | 2:de778df5892c | 307 | _timeout.detach(); |
hudakz | 0:d5c75b0e5708 | 308 | break; |
hudakz | 0:d5c75b0e5708 | 309 | } |
hudakz | 0:d5c75b0e5708 | 310 | } |
hudakz | 0:d5c75b0e5708 | 311 | |
hudakz | 0:d5c75b0e5708 | 312 | /** |
hudakz | 7:afd0ee36dcd1 | 313 | * @brief ISR handling 'reception timeout' |
hudakz | 3:03109c995123 | 314 | * @note Called when receiver line is idle longer than limit. |
hudakz | 7:afd0ee36dcd1 | 315 | * Signals 'end of reception' by setting state to IDLE |
hudakz | 2:de778df5892c | 316 | * or 'timeout error' by setting state to ERROR. |
hudakz | 7:afd0ee36dcd1 | 317 | * @param |
hudakz | 7:afd0ee36dcd1 | 318 | * @retval |
hudakz | 0:d5c75b0e5708 | 319 | */ |
hudakz | 0:d5c75b0e5708 | 320 | void Manchester::rxTimeout(void) { |
hudakz | 2:de778df5892c | 321 | _timeout.detach(); |
hudakz | 0:d5c75b0e5708 | 322 | |
hudakz | 5:3b2c7e9fda3f | 323 | #ifdef G_E_THOMAS |
hudakz | 2:de778df5892c | 324 | if((_state == DECODE) && (_rx == 1)) |
hudakz | 5:3b2c7e9fda3f | 325 | #else |
hudakz | 5:3b2c7e9fda3f | 326 | if((_state == DECODE) && (_rx == 0)) |
hudakz | 5:3b2c7e9fda3f | 327 | #endif |
hudakz | 7:afd0ee36dcd1 | 328 | _state = IDLE; // Reception successful |
hudakz | 0:d5c75b0e5708 | 329 | else |
hudakz | 7:afd0ee36dcd1 | 330 | _state = ERROR; // Reception incomplete |
hudakz | 0:d5c75b0e5708 | 331 | } |