process a pulse made up of a width of values at high voltage followed by a width at low voltage */
| 45 | followed by a width at low voltage |
| 46 | */ |
| 47 | bool SoftSerial::process_pulse(uint32_t width_high, uint32_t width_low, uint8_t &byte) |
| 48 | { |
| 49 | // convert to bit widths, allowing for a half bit error |
| 50 | uint16_t bits_high = ((width_high+half_bit)*baudrate) / 1000000; |
| 51 | uint16_t bits_low = ((width_low+half_bit)*baudrate) / 1000000; |
| 52 | |
| 53 | byte_timestamp_us = timestamp_us; |
| 54 | timestamp_us += (width_high + width_low); |
| 55 | |
| 56 | if (bits_high == 0 || bits_low == 0) { |
| 57 | // invalid data |
| 58 | goto reset; |
| 59 | } |
| 60 | |
| 61 | if (bits_high >= byte_width) { |
| 62 | // if we have a start bit and a stop bit then we can have at |
| 63 | // most 9 bits in high state for data. The rest must be idle |
| 64 | // bits |
| 65 | bits_high = byte_width-1; |
| 66 | } |
| 67 | |
| 68 | if (state.bit_ofs == 0) { |
| 69 | // we are in idle state, waiting for first low bit. swallow |
| 70 | // the high bits |
| 71 | bits_high = 0; |
| 72 | } |
| 73 | |
| 74 | state.byte |= ((1U<<bits_high)-1) << state.bit_ofs; |
| 75 | |
| 76 | state.bit_ofs += bits_high + bits_low; |
| 77 | |
| 78 | if (state.bit_ofs >= byte_width) { |
| 79 | // check start bit |
| 80 | if ((state.byte & 1) != 0) { |
| 81 | goto reset; |
| 82 | } |
| 83 | // check stop bits |
| 84 | if ((state.byte & stop_mask) != stop_mask) { |
| 85 | goto reset; |
| 86 | } |
| 87 | if (config == SERIAL_CONFIG_8E2I) { |
| 88 | // check parity |
| 89 | if (parity((state.byte>>1)&0xFF) != (state.byte&0x200)>>9) { |
| 90 | goto reset; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | byte = ((state.byte>>1) & 0xFF); |
| 95 | state.byte >>= byte_width; |
| 96 | state.bit_ofs -= byte_width; |
| 97 | if (state.bit_ofs > byte_width) { |
| 98 | state.byte = 0; |
| 99 | state.bit_ofs = bits_low; |
| 100 | } |
| 101 | // swallow idle bits |
| 102 | while (state.bit_ofs > 0 && (state.byte & 1)) { |
| 103 | state.bit_ofs--; |
| 104 | state.byte >>= 1; |