7#include "pulse/tools/Vector.h"
27 uint32_t shortPulseUs = 0, uint32_t longPulseUs = 0)
29 _shortPulseUs(shortPulseUs),
30 _longPulseUs(longPulseUs),
31 _toleranceUs(toleranceUs) {}
35 uint32_t longPulseUs = 0, uint32_t toleranceUs = 0) {
37 _shortPulseUs = shortPulseUs;
38 _longPulseUs = longPulseUs;
39 _toleranceUs = toleranceUs;
42 bool begin(uint32_t bitFrequencyHz)
override {
44 if (_shortPulseUs == 0) _shortPulseUs = 1000000UL / (bitFrequencyHz * 2);
45 if (_longPulseUs == 0) _longPulseUs = 1000000UL / (bitFrequencyHz / 2);
46 if (_toleranceUs == 0) _toleranceUs = _bitPeriodUs * 0.4;
63 pulse.pulseUs = bit ? _longPulseUs : _shortPulseUs;
65 space.pulseUs = _shortPulseUs;
76 CodecEnum
getCodecType()
const override {
return CodecEnum::PulseDistance; }
78 bool getIdleLevel() {
return true; }
81 uint32_t _shortPulseUs = 0;
82 uint32_t _longPulseUs = 0;
83 uint32_t _toleranceUs = 0;
86 if (edges.
size() < 16) {
92 for (
auto& edge : edges) {
94 if (edge.level ==
false) {
95 if (bitMatch(edge.pulseUs,
true)) {
96 byte |= (1 << (7 - bit));
97 }
else if (bitMatch(edge.pulseUs,
false)) {
100 Logger::error(
"Invalid pulse duration for bit %d: %d us", bit,
110 bool bitMatch(uint32_t duration,
bool bit)
const {
111 uint32_t expected = bit ? _longPulseUs : _shortPulseUs;
112 bool rc = (duration >= expected - _toleranceUs &&
113 duration <= expected + _toleranceUs);
115 "Bit match for bit %d: duration=%d, expected=%d, tolerance=%d, "
117 bit ? 1 : 0, duration, expected, _toleranceUs, rc ?
"YES" :
"NO");
Abstract base class for IR protocol encoding and decoding.
virtual bool begin(uint32_t bitFrequencyHz)
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
void setPreamble(Preamble &preamble)
Set the Preamble Detector object.
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.
Pulse-distance encoding/decoding utility class for IR communication.
CodecEnum getCodecType() const override
instance.
bool decodeByte(Vector< OutputEdge > &edges, uint8_t &result) override
Decode edges into a byte.
size_t encodeBit(bool bit, Vector< OutputEdge > &output) override
Fill output vector with PulseDistance OutputSpec(s) for a bit.
bool begin(uint32_t bitFrequencyHz) override
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
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
Provide the end of frame delay in microseconds for this protocol, used by RX driver to.
Small, header-only vector replacement for non-STL environments.
void push_back(const T &value)
Add element to end of vector.
size_t size() const
Number of elements in vector.
Specifies a single IR signal segment for protocol-agnostic transmission.