6#include "pulse/Preamble.h"
7#include "pulse/SignalBase.h"
8#include "pulse/tools/Vector.h"
9#include "pulse/tools/Logger.h"
14enum class CodecEnum { PulseDistance, PulseWidth, Manchester, DifferentialManchester, NRZ };
16const char* toStr(CodecEnum codec) {
18 case CodecEnum::PulseDistance:
19 return "PulseDistance";
20 case CodecEnum::PulseWidth:
22 case CodecEnum::Manchester:
24 case CodecEnum::DifferentialManchester:
25 return "DifferentialManchester";
45 Codec(
Preamble& preambleDetector) : _preamble(&preambleDetector) {}
50 uint32_t longPulseUs = 1200, uint32_t toleranceUs = 200) {}
56 virtual bool begin(uint32_t bitFrequencyHz) {
58 assert(_preamble !=
nullptr);
60 _bitFrequencyHz = bitFrequencyHz;
61 _bitPeriodUs = 1000000UL / bitFrequencyHz;
62 _preamble->begin(bitFrequencyHz);
67 virtual void reset() {
68 _decodeEdgeStream.clear();
106 virtual bool decodeEdge(uint32_t durationUs,
bool level, uint8_t& result) {
108 assert(_decodeEdgeStream.capacity() > 0);
112 Logger::debug(
"Idle gap detected: %d us, resetting decoder", durationUs);
118 assert(_preamble !=
nullptr);
120 if (_preamble->preambleLength() == 0) {
122 _decodeEdgeStream.clear();
123 _decodeEdgeStream.push_back(newEdge);
126 }
else if (_preamble->
detect(newEdge)) {
129 _decodeEdgeStream.clear();
133 _decodeEdgeStream.push_back(newEdge);
138 bool rc =
decodeByte(_decodeEdgeStream, result);
140 _decodeEdgeStream.clear();
147 if (_preamble ==
nullptr)
return 0;
167 for (
int i = 0; i < 8; ++i) {
173 void setFrameSize(uint16_t size) {
194 virtual void encodeByte(uint8_t
byte, std::vector<bool> &bits)
const {
195 for (
int i = 7; i >= 0; --i) {
196 bool bit = (
byte >> i) & 0x01;
197 bits[7 - i] = bit ? 1 : 0;
223 Preamble* _preamble = &_defaultPreamble;
224 uint16_t _bitFrequencyHz = 0;
225 uint32_t _bitPeriodUs = 0;
227 volatile bool _inFrame =
false;
Abstract base class for IR protocol encoding and decoding.
virtual size_t encode(uint8_t byte, Vector< OutputEdge > &output)
Fill output vector with protocol-specific OutputSpec(s) for a byte.
virtual bool decodeEdge(uint32_t durationUs, bool level, uint8_t &result)
Edge-based decoding for protocol-agnostic RX drivers.
virtual bool begin(uint32_t bitFrequencyHz)
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
virtual size_t encodeBit(bool bit, Vector< OutputEdge > &output)
Fill output vector with protocol-specific OutputSpec(s) for a bit.
virtual int getEndOfFrameDelayUs()=0
Provide the end of frame delay in microseconds for this protocol, used by RX driver to.
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
instance.
Preamble & getPreamble()
Get the preamble detector associated with this codec.
virtual bool decodeByte(Vector< OutputEdge > &edges, uint8_t &result)=0
Decode edges into a byte.
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),...
CustomPreambleUs: Allows users to define their own preamble by setting expected edges....
static void debug(const char *format,...)
Log a debug message with formatting.
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.
virtual bool detect(const OutputEdge &edge)
Detects if the incoming edge matches the expected preamble pattern.
Small, header-only vector replacement for non-STL environments.
void reserve(size_t cap)
Reserve space for at least cap elements.
Specifies a single IR signal segment for protocol-agnostic transmission.