Arduino PulseWire Transceiver Library
Loading...
Searching...
No Matches
PulseWidthCodec.h
1#pragma once
2
3#include <stddef.h>
4#include <stdint.h>
5
6#include "Codec.h"
7#include "pulse/tools/Vector.h"
8
9namespace pulsewire {
10
23class PulseWidthCodec : public Codec {
24 public:
25 PulseWidthCodec() = default;
26
27 PulseWidthCodec(Preamble& detector) : Codec(detector) {}
28
29 // Used by IRTransceiver to initialize codec with protocol-specific parameters
30 virtual void init(Preamble& detector, uint32_t shortPulseUs,
31 uint32_t longPulseUs, uint32_t toleranceUs) {
32 setPreamble(detector);
33 _shortPulseUs = shortPulseUs;
34 _longPulseUs = longPulseUs;
35 _toleranceUs = toleranceUs;
36 }
37
38 bool begin(uint32_t bitFrequencyHz) override {
39 Codec::begin(bitFrequencyHz);
40 if (_shortPulseUs == 0) _shortPulseUs = 1000000UL / (bitFrequencyHz * 2);
41 if (_longPulseUs == 0) _longPulseUs = 1000000UL / (bitFrequencyHz / 2);
42 if (_toleranceUs == 0) _toleranceUs = _bitPeriodUs * 0.3;
43
44 _inFrame = false;
45
46 return true;
47 }
48
57 size_t encodeBit(bool bit, Vector<OutputEdge>& output) override {
58 OutputEdge pulse, space;
59 pulse.level = true; // HIGH
60 pulse.pulseUs = bit ? _longPulseUs : _shortPulseUs;
61 space.level = false; // LOW
62 space.pulseUs = _shortPulseUs; // Fixed space duration for both bits
63 output.push_back(pulse);
64 output.push_back(space);
65 return 2;
66 }
67
68 bool decodeByte(Vector<OutputEdge>& edges, uint8_t& result) override {
69 if (edges.size() < 16) {
70 Logger::error("Not enough edges to decode byte: %d", edges.size());
71 return false;
72 }
73 uint8_t byte = 0;
74 int bit = 0;
75 for (auto& edge : edges) {
76 // only consider high edges
77 if (edge.level) {
78 if (bitMatch(edge.pulseUs, true)) {
79 byte |= (1 << (7 - bit));
80 } else if (bitMatch(edge.pulseUs, false)) {
81 // bit is 0
82 } else {
83 Logger::error("Invalid pulse duration for bit %d: %d us", bit,
84 edge.pulseUs);
85 }
86 bit++;
87 }
88 }
89 result = byte;
90 return true;
91 }
92
93 CodecEnum getCodecType() const override { return CodecEnum::PulseWidth; }
94
95 int getEndOfFrameDelayUs() override { return 2 * _longPulseUs; }
96
97 size_t getEdgeCount() const override { return 16; }
98
99 protected:
100 uint32_t _shortPulseUs = 0;
101 uint32_t _longPulseUs = 0;
102 uint32_t _toleranceUs = 0;
103
104 bool bitMatch(uint32_t duration, bool bit) const {
105 uint32_t expected = bit ? _longPulseUs : _shortPulseUs;
106 return (duration >= expected - _toleranceUs &&
107 duration <= expected + _toleranceUs);
108 }
109};
110
111} // namespace pulsewire
Abstract base class for IR protocol encoding and decoding.
Definition Codec.h:41
virtual bool begin(uint32_t bitFrequencyHz)
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
Definition Codec.h:56
void setPreamble(Preamble &preamble)
Set the Preamble Detector object.
Definition Codec.h:84
static void error(const char *format,...)
Log an error message with formatting.
Definition Logger.h:37
Abstract base class for preamble detection and generation.
Definition Preamble.h:40
Pulse-width encoding/decoding utility class for IR communication.
bool decodeByte(Vector< OutputEdge > &edges, uint8_t &result) override
Decode edges into a byte.
CodecEnum getCodecType() const override
instance.
size_t getEdgeCount() const override
Get the number of protocol symbols (bits, pulses, etc.) per encoded byte.
bool begin(uint32_t bitFrequencyHz) override
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
int getEndOfFrameDelayUs() override
Provide the end of frame delay in microseconds for this protocol, used by RX driver to.
virtual void init(Preamble &detector, uint32_t shortPulseUs, uint32_t longPulseUs, uint32_t toleranceUs)
size_t encodeBit(bool bit, Vector< OutputEdge > &output) override
Fill output vector with PulseWidth OutputSpec(s) for a bit.
Small, header-only vector replacement for non-STL environments.
Definition Vector.h:29
void push_back(const T &value)
Add element to end of vector.
Definition Vector.h:92
size_t size() const
Number of elements in vector.
Definition Vector.h:116
Specifies a single IR signal segment for protocol-agnostic transmission.
Definition Preamble.h:23