Arduino PulseWire Transceiver Library
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes
pulsewire::MillerCodec Class Reference

Miller (Delay Modulation) encoding/decoding utility class. More...

#include <MillerCodec.h>

Inheritance diagram for pulsewire::MillerCodec:
Inheritance graph
[legend]
Collaboration diagram for pulsewire::MillerCodec:
Collaboration graph
[legend]

Public Member Functions

 MillerCodec (Preamble &preamble)
 
CodecEnum getCodecType () const override
 
size_t getEdgeCount () const override
 Get the number of protocol symbols (bits, pulses, etc.) per encoded byte.
 
int getEndOfFrameDelayUs () override
 
bool getIdleLevel () const override
 Provides the initial ldle state (low or hith)
 
void reset () override
 Reset the internal state of the codec.
 
size_t encode (uint8_t byte, Vector< OutputEdge > &output) override
 Encode a byte using Miller encoding (byte-aligned).
 
size_t flushEncoder (Vector< OutputEdge > &output) override
 Add termination edge at end of frame.
 
bool decodeEdge (uint32_t durationUs, bool level, uint8_t &result) override
 Decode Miller encoding edge by edge.
 
virtual void init (Preamble &detector, uint32_t shortPulseUs=600, uint32_t longPulseUs=1200, uint32_t toleranceUs=200)
 
virtual bool begin (uint32_t bitFrequencyHz)
 initialization method for codecs that require setup before use (e.g., loading PIO programs, configuring state machines).
 
void setPreamble (Preamble &preamble)
 Set the Preamble Detector object.
 
PreamblegetPreamble ()
 Get the preamble detector associated with this codec.
 
void setFrameSize (uint16_t size)
 Set the Frame Size.
 
uint16_t getFrameSize () const
 Get the configured frame size.
 
const charname () const
 Get the name of the codec type as a string (e.g., "PulseDistance", "Manchester").
 
size_t encodePreamble (Vector< OutputEdge > &output)
 

Protected Member Functions

bool handleIdleGap (uint32_t durationUs, bool level, uint8_t &result)
 Handle idle gap detection and end-of-frame processing.
 
bool handlePreamble (uint32_t durationUs, bool level)
 Handle preamble detection.
 
int durationToHalfPeriods (uint32_t durationUs)
 Convert duration to half-periods (rounded to nearest).
 
void decodeMiller (int hp, bool level)
 Decode Miller half-periods into bits.
 
void decodeWhileExpectingMid (int hp)
 Decode when expecting mid-transition of a '1' bit.
 
void decodeAtBoundary (int hp)
 Decode when at bit boundary (not expecting mid-transition).
 
bool checkByteComplete (uint8_t &result)
 Check if a complete byte has been assembled.
 
void pushBit (bool bit)
 Push a decoded bit into the receive buffer.
 
virtual void encodeByte (uint8_t byte, std::vector< bool > &bits) const
 Encode a byte to protocol bitstream. Default implementation encodes to raw bits (MSB first), but can be overridden by protocols that require different bit formatting (e.g., Manchester). The output bits are stored in.
 

Protected Attributes

bool _encLevel = true
 
uint8_t _rxByte = 0
 
uint8_t _rxBitPos = 0
 
bool _rxExpectingMidTransition = false
 
CustomPreambleUs _defaultPreamble
 
Preamble_preamble = &_defaultPreamble
 
uint16_t _bitFrequencyHz = 0
 
uint32_t _bitPeriodUs = 0
 
Vector< OutputEdge_decodeEdgeStream
 
volatile bool _inFrame = false
 
uint16_t _frameSize = 0
 

Detailed Description

Miller (Delay Modulation) encoding/decoding utility class.

Miller encoding rules (bit period = T):

This implementation uses BYTE-ALIGNED encoding where each byte is encoded independently. State is reset at byte boundaries to ensure clean decoding.

Definition at line 17 of file MillerCodec.h.

Constructor & Destructor Documentation

◆ MillerCodec()

pulsewire::MillerCodec::MillerCodec ( Preamble preamble)
inline

Definition at line 20 of file MillerCodec.h.

Member Function Documentation

◆ begin()

virtual bool pulsewire::Codec::begin ( uint32_t  bitFrequencyHz)
inlinevirtualinherited

initialization method for codecs that require setup before use (e.g., loading PIO programs, configuring state machines).

Reimplemented in pulsewire::RecorderCodec, pulsewire::DifferentialManchesterCodec, pulsewire::ManchesterCodec, pulsewire::PulseDistanceCodec, and pulsewire::PulseWidthCodec.

Definition at line 70 of file Codec.h.

◆ checkByteComplete()

bool pulsewire::MillerCodec::checkByteComplete ( uint8_t result)
inlineprotected

Check if a complete byte has been assembled.

Returns
true if byte complete, with result set.

Definition at line 301 of file MillerCodec.h.

◆ decodeAtBoundary()

void pulsewire::MillerCodec::decodeAtBoundary ( int  hp)
inlineprotected

Decode when at bit boundary (not expecting mid-transition).

Definition at line 275 of file MillerCodec.h.

◆ decodeEdge()

bool pulsewire::MillerCodec::decodeEdge ( uint32_t  durationUs,
bool  level,
uint8_t result 
)
inlineoverridevirtual

Decode Miller encoding edge by edge.

Handles preamble detection using the base Codec's preamble logic, then interprets edge durations in terms of half-periods (hp):

  • hp=1 (0.5T): Half of a '1' bit (start or complete)
  • hp=2 (1T): One '0' bit with transition at boundary
  • hp=3 (1.5T): '0' followed by start of '1', OR '1' followed by '0'
  • hp=4 (2T): Two '0' bits, OR '1' + '0' + start of '1'

State machine tracks whether we're expecting the mid-transition of a '1'.

Implements pulsewire::Codec.

Definition at line 146 of file MillerCodec.h.

◆ decodeMiller()

void pulsewire::MillerCodec::decodeMiller ( int  hp,
bool  level 
)
inlineprotected

Decode Miller half-periods into bits.

Definition at line 232 of file MillerCodec.h.

◆ decodeWhileExpectingMid()

void pulsewire::MillerCodec::decodeWhileExpectingMid ( int  hp)
inlineprotected

Decode when expecting mid-transition of a '1' bit.

Definition at line 246 of file MillerCodec.h.

◆ durationToHalfPeriods()

int pulsewire::MillerCodec::durationToHalfPeriods ( uint32_t  durationUs)
inlineprotected

Convert duration to half-periods (rounded to nearest).

Definition at line 221 of file MillerCodec.h.

◆ encode()

size_t pulsewire::MillerCodec::encode ( uint8_t  byte,
Vector< OutputEdge > &  output 
)
inlineoverridevirtual

Encode a byte using Miller encoding (byte-aligned).

Miller encoding rules:

  • '1': Transition in the MIDDLE of the bit period (T/2)
  • '0' after '0': Transition at the START of the bit period
  • '0' after '1': NO transition (level stays constant for full T)

Each byte is encoded independently with state reset at boundaries. The encoder accumulates "pending" time at the current level and outputs edges when transitions occur.

Implements pulsewire::Codec.

Definition at line 50 of file MillerCodec.h.

◆ encodeByte()

virtual void pulsewire::Codec::encodeByte ( uint8_t  byte,
std::vector< bool > &  bits 
) const
inlineprotectedvirtualinherited

Encode a byte to protocol bitstream. Default implementation encodes to raw bits (MSB first), but can be overridden by protocols that require different bit formatting (e.g., Manchester). The output bits are stored in.

Parameters
byteThe input byte to encode.
bitsOutput buffer for encoded bits (protocol-specific format).

Definition at line 204 of file Codec.h.

◆ encodePreamble()

size_t pulsewire::Codec::encodePreamble ( Vector< OutputEdge > &  output)
inlineinherited

Definition at line 157 of file Codec.h.

◆ flushEncoder()

size_t pulsewire::MillerCodec::flushEncoder ( Vector< OutputEdge > &  output)
inlineoverridevirtual

Add termination edge at end of frame.

Adds a 2T pulse to ensure the receiver sees a final transition and can complete decoding any pending bits before timeout.

Reimplemented from pulsewire::Codec.

Definition at line 119 of file MillerCodec.h.

◆ getCodecType()

CodecEnum pulsewire::MillerCodec::getCodecType ( ) const
inlineoverridevirtual

Get the codec type (e.g., PulseDistance, Manchester) for this Codec instance.

Implements pulsewire::Codec.

Definition at line 22 of file MillerCodec.h.

◆ getEdgeCount()

size_t pulsewire::MillerCodec::getEdgeCount ( ) const
inlineoverridevirtual

Get the number of protocol symbols (bits, pulses, etc.) per encoded byte.

Returns
Number of protocol symbols per byte.

Implements pulsewire::Codec.

Definition at line 24 of file MillerCodec.h.

◆ getEndOfFrameDelayUs()

int pulsewire::MillerCodec::getEndOfFrameDelayUs ( )
inlineoverridevirtual

Provide the end of frame delay in microseconds for this protocol, used by RX driver to

Implements pulsewire::Codec.

Definition at line 26 of file MillerCodec.h.

◆ getFrameSize()

uint16_t pulsewire::Codec::getFrameSize ( ) const
inlineinherited

Get the configured frame size.

Definition at line 120 of file Codec.h.

◆ getIdleLevel()

bool pulsewire::MillerCodec::getIdleLevel ( ) const
inlineoverridevirtual

Provides the initial ldle state (low or hith)

Reimplemented from pulsewire::Codec.

Definition at line 28 of file MillerCodec.h.

◆ getPreamble()

Preamble & pulsewire::Codec::getPreamble ( )
inlineinherited

Get the preamble detector associated with this codec.

Returns
Reference to the Preamble instance used for preamble detection.

Definition at line 100 of file Codec.h.

◆ handleIdleGap()

bool pulsewire::MillerCodec::handleIdleGap ( uint32_t  durationUs,
bool  level,
uint8_t result 
)
inlineprotected

Handle idle gap detection and end-of-frame processing.

Returns
true if a complete byte was output, false otherwise.

Definition at line 170 of file MillerCodec.h.

◆ handlePreamble()

bool pulsewire::MillerCodec::handlePreamble ( uint32_t  durationUs,
bool  level 
)
inlineprotected

Handle preamble detection.

Returns
true if we're in a frame and should process data, false to skip.

Definition at line 194 of file MillerCodec.h.

◆ init()

virtual void pulsewire::Codec::init ( Preamble detector,
uint32_t  shortPulseUs = 600,
uint32_t  longPulseUs = 1200,
uint32_t  toleranceUs = 200 
)
inlinevirtualinherited

Used by IRTransceiver to initialize codec with protocol-specific parameters

Reimplemented in pulsewire::PulseWidthCodec, pulsewire::PulseDistanceCodec, and pulsewire::RecorderCodec.

Definition at line 63 of file Codec.h.

◆ name()

const char * pulsewire::Codec::name ( ) const
inlineinherited

Get the name of the codec type as a string (e.g., "PulseDistance", "Manchester").

Definition at line 135 of file Codec.h.

◆ pushBit()

void pulsewire::MillerCodec::pushBit ( bool  bit)
inlineprotected

Push a decoded bit into the receive buffer.

Shifts the bit into _rxByte from the right (MSB first order). Ignores bits if byte is already complete (overflow protection).

Definition at line 318 of file MillerCodec.h.

◆ reset()

void pulsewire::MillerCodec::reset ( )
inlineoverridevirtual

Reset the internal state of the codec.

Reimplemented from pulsewire::Codec.

Definition at line 30 of file MillerCodec.h.

◆ setFrameSize()

void pulsewire::Codec::setFrameSize ( uint16_t  size)
inlineinherited

Set the Frame Size.

Parameters
size

Definition at line 114 of file Codec.h.

◆ setPreamble()

void pulsewire::Codec::setPreamble ( Preamble preamble)
inlineinherited

Set the Preamble Detector object.

Parameters
preamble

Definition at line 94 of file Codec.h.

Field Documentation

◆ _bitFrequencyHz

uint16_t pulsewire::Codec::_bitFrequencyHz = 0
protectedinherited

Definition at line 190 of file Codec.h.

◆ _bitPeriodUs

uint32_t pulsewire::Codec::_bitPeriodUs = 0
protectedinherited

Definition at line 191 of file Codec.h.

◆ _decodeEdgeStream

Vector<OutputEdge> pulsewire::Codec::_decodeEdgeStream
protectedinherited

Definition at line 192 of file Codec.h.

◆ _defaultPreamble

CustomPreambleUs pulsewire::Codec::_defaultPreamble
protectedinherited

Definition at line 188 of file Codec.h.

◆ _encLevel

bool pulsewire::MillerCodec::_encLevel = true
protected

Definition at line 330 of file MillerCodec.h.

◆ _frameSize

uint16_t pulsewire::Codec::_frameSize = 0
protectedinherited

Definition at line 194 of file Codec.h.

◆ _inFrame

volatile bool pulsewire::Codec::_inFrame = false
protectedinherited

Definition at line 193 of file Codec.h.

◆ _preamble

Preamble* pulsewire::Codec::_preamble = &_defaultPreamble
protectedinherited

Definition at line 189 of file Codec.h.

◆ _rxBitPos

uint8_t pulsewire::MillerCodec::_rxBitPos = 0
protected

Definition at line 334 of file MillerCodec.h.

◆ _rxByte

uint8_t pulsewire::MillerCodec::_rxByte = 0
protected

Definition at line 333 of file MillerCodec.h.

◆ _rxExpectingMidTransition

bool pulsewire::MillerCodec::_rxExpectingMidTransition = false
protected

Definition at line 335 of file MillerCodec.h.


The documentation for this class was generated from the following file: