6#include "pulse/Preamble.h"
7#include "pulse/SignalBase.h"
8#include "pulse/tools/Logger.h"
9#include "pulse/tools/Vector.h"
18 DifferentialManchester,
24const char* toStr(CodecEnum codec) {
26 case CodecEnum::PulseDistance:
27 return "PulseDistance";
28 case CodecEnum::PulseWidth:
30 case CodecEnum::Manchester:
32 case CodecEnum::DifferentialManchester:
33 return "DifferentialManchester";
38 case CodecEnum::Miller:
59 Codec(
Preamble& preambleDetector) : _preamble(&preambleDetector) {}
64 uint32_t longPulseUs = 1200, uint32_t toleranceUs = 200) {}
70 virtual bool begin(uint32_t bitFrequencyHz) {
72 assert(_preamble !=
nullptr);
74 _bitFrequencyHz = bitFrequencyHz;
75 _bitPeriodUs = 1000000UL / bitFrequencyHz;
76 _preamble->begin(bitFrequencyHz);
85 _decodeEdgeStream.clear();
151 virtual bool decodeEdge(uint32_t durationUs,
bool level, uint8_t& result) = 0;
158 if (_preamble ==
nullptr)
return 0;
189 Preamble* _preamble = &_defaultPreamble;
190 uint16_t _bitFrequencyHz = 0;
191 uint32_t _bitPeriodUs = 0;
193 volatile bool _inFrame =
false;
194 uint16_t _frameSize = 0;
204 virtual void encodeByte(uint8_t
byte, std::vector<bool>& bits)
const {
205 for (
int i = 7; i >= 0; --i) {
206 bool bit = (
byte >> i) & 0x01;
207 bits[7 - i] = bit ? 1 : 0;
Abstract base class for IR protocol encoding and decoding.
virtual size_t encode(uint8_t byte, Vector< OutputEdge > &output)=0
Fill output vector with protocol-specific OutputSpec(s) for a byte.
virtual int getEndOfFrameDelayUs()=0
virtual bool decodeEdge(uint32_t durationUs, bool level, uint8_t &result)=0
Edge-based decoding for protocol-agnostic RX drivers.
virtual void init(Preamble &detector, uint32_t shortPulseUs=600, uint32_t longPulseUs=1200, uint32_t toleranceUs=200)
void setPreamble(Preamble &preamble)
Set the Preamble Detector object.
const char * name() const
Get the name of the codec type as a string (e.g., "PulseDistance", "Manchester").
virtual bool getIdleLevel() const
Provides the initial ldle state (low or hith)
virtual CodecEnum getCodecType() const =0
Preamble & getPreamble()
Get the preamble detector associated with this codec.
uint16_t getFrameSize() const
Get the configured frame size.
virtual size_t getEdgeCount() const =0
Get the number of protocol symbols (bits, pulses, etc.) per encoded byte.
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),...
virtual size_t flushEncoder(Vector< OutputEdge > &output)
Flush any pending encoder state at end of frame.
virtual bool begin(uint32_t bitFrequencyHz)
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
void setFrameSize(uint16_t size)
Set the Frame Size.
virtual void reset()
Reset the internal state of the codec.
CustomPreambleUs: Allows users to define their own preamble by setting expected edges....
Abstract base class for preamble detection and generation.
virtual int getEdges(pulsewire::Vector< pulsewire::OutputEdge > &output) const =0
Returns the expected preamble edges for this protocol.
Codec Wrapper that records all edges passed to decodeEdge() for later analysis or testing.
Small, header-only vector replacement for non-STL environments.