# OOK Transition-Count Decode Plan ## Goal Decode an OOK carrier by counting `T0` pin transitions in fixed time windows, then reconstruct framed packet bits with error checking. ## Sampling Strategy - Use Timer0 as an external event counter on `PB0/T0`. - Use Timer1 CTC interrupt at about 1 ms to snapshot and clear `TCNT0`. - Treat each 1 ms transition count as one energy sample. ## Envelope Detection (Hysteresis) - Convert transition count to an ON/OFF envelope state. - Use separate thresholds: - `on_threshold`: count high enough to declare carrier ON. - `off_threshold`: count low enough to declare carrier OFF. - Keep `on_threshold > off_threshold` to avoid chatter. - Adapt thresholds from observed low/high sample levels over time. ## Symbol Timing and Bit Decision - Group multiple 1 ms samples into one bit period (`samples_per_bit`). - Decide each bit by majority ON samples inside the bit period. - Apply simple timing recovery: - When envelope transitions happen very early/late in a bit window, nudge the phase toward bit boundaries. ## Decoder State Machine 1. `SEARCH_PREAMBLE` - Wait for `N` consecutive ON bits. 2. `SYNC` - Require one OFF delimiter bit. 3. `SYNC_WORD` - Require `0xAA` (`10101010`) before payload decode. 4. `FEC` - Shift in two Hamming(8,4) codewords and decode one payload byte. - Correct single-bit errors and reject detected multi-bit errors. ## Packet Format (Current Implementation) - Preamble: `4` ON bits (about `16 ms` at 4 samples/bit and ~1 ms/sample). - Sync delimiter: `1` OFF bit. - Sync word: `0xAA` (`8` bits, msb-first). - Hamming codeword for upper nibble (`8` bits). - Hamming codeword for lower nibble (`8` bits). - Payload: `1` byte total (`16` coded bits, after delimiter + sync word). ## Runtime Behavior - RX runs when TX is idle. - TX control (`PB3` low edge) triggers one OOK frame transmit. - RX resumes automatically after TX frame completion. ## Outputs - `rx_packet_data`: last valid decoded payload byte. - `rx_packet_ready`: set when a valid packet is decoded. - `rx_good_packets`, `rx_bad_packets`: decode statistics. - `rx_corrected_codewords`: count of Hamming codewords corrected by FEC.