7#include "pulse/tools/Vector.h"
29 _shortPulseUs(shortPulseUs),
30 _longPulseUs(longPulseUs),
31 _toleranceUs(toleranceUs) {}
37 _shortPulseUs = shortPulseUs;
38 _longPulseUs = longPulseUs;
39 _toleranceUs = toleranceUs;
44 if (_shortPulseUs == 0) _shortPulseUs = 1000000UL / (
bitFrequencyHz * 2);
45 if (_longPulseUs == 0) _longPulseUs = 1000000UL / (
bitFrequencyHz / 2);
46 if (_toleranceUs == 0) _toleranceUs = 0.5 * _bitPeriodUs;
48 _inFrame = _preamble->preambleLength() ==
58 CodecEnum
getCodecType()
const override {
return CodecEnum::PulseDistance; }
60 bool getIdleLevel() {
return true; }
67 for (
int i = 0;
i < 8; ++
i) {
85 assert(_preamble !=
nullptr);
87 if (_preamble->preambleLength() == 0) {
89 _decodeEdgeStream.
clear();
96 _decodeEdgeStream.
clear();
105 bool rc = decodeByte(_decodeEdgeStream,
result);
107 _decodeEdgeStream.
clear();
127 pulse.pulseUs =
bit ? _longPulseUs : _shortPulseUs;
129 space.pulseUs = _shortPulseUs;
143 for (
auto& edge : edges) {
145 if (edge.level ==
false) {
146 if (bitMatch(edge.pulseUs,
true)) {
147 byte |= (1 << (7 - bit));
148 }
else if (bitMatch(edge.pulseUs,
false)) {
151 Logger::debug(
"Invalid pulse duration for bit %d: %d us", bit,
161 bool bitMatch(uint32_t duration,
bool bit)
const {
162 uint32_t expected = bit ? _longPulseUs : _shortPulseUs;
163 bool rc = (duration >= expected - _toleranceUs &&
164 duration <= expected + _toleranceUs);
166 "Bit match for bit %d: duration=%d, expected=%d, tolerance=%d, "
168 bit ? 1 : 0, duration, expected, _toleranceUs, rc ?
"YES" :
"NO");
Abstract base class for IR protocol encoding and decoding.
void setPreamble(Preamble &preamble)
Set the Preamble Detector object.
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 bool begin(uint32_t bitFrequencyHz)
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
virtual void reset()
Reset the internal state of the codec.
static void debug(const char *format,...)
Log a debug message with formatting.
static void error(const char *format,...)
Log an error message with formatting.
Abstract base class for preamble detection and generation.
virtual bool detect(const OutputEdge &edge)
Detects if the incoming edge matches the expected preamble pattern.
Pulse-distance encoding/decoding utility class for IR communication.
bool decodeEdge(uint32_t durationUs, bool level, uint8_t &result) override
Edge-based decoding for protocol-agnostic RX drivers.
CodecEnum getCodecType() const override
bool begin(uint32_t bitFrequencyHz) override
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
size_t encodeBit(bool bit, Vector< OutputEdge > &output)
Fill output vector with PulseDistance OutputSpec(s) for a bit.
void init(Preamble &detector, uint32_t shortPulseUs=0, uint32_t longPulseUs=0, uint32_t toleranceUs=0)
size_t getEdgeCount() const override
Get the number of protocol symbols (bits, pulses, etc.) per encoded byte.
int getEndOfFrameDelayUs() override
size_t encode(uint8_t byte, Vector< OutputEdge > &output) override
Fill output vector with protocol-specific OutputSpec(s) for a byte.
Small, header-only vector replacement for non-STL environments.
size_t capacity() const
Current allocated capacity.
void clear()
Remove all elements.
void push_back(const T &value)
Add element to end of vector.
void reserve(size_t cap)
Reserve space for at least cap elements.
size_t size() const
Number of elements in vector.
Specifies a single IR signal segment for protocol-agnostic transmission.