4#include "TransceiverConfig.h"
5#include "pulse/TxDriver.h"
6#include "pulse/TxProtocol.h"
7#include "pulse/codecs/Codec.h"
8#include "pulse/tools/Logger.h"
9#include "pulse/tools/RingBuffer.h"
10#include "pulse/tools/Vector.h"
57 _protocol = &protocol;
62 bool begin(uint32_t bitFrequencyHz)
override {
65 _bitFrequencyHz = bitFrequencyHz;
67 _protocol->begin(bitFrequencyHz, _codec, _pin);
68 return _codec->
begin(bitFrequencyHz);
76 _byteBuffer.resize(size);
77 _protocol->setFrameSize(size);
84 int write(
uint8_t byte)
override {
85 if (_byteBuffer.size() == 0) {
88 _byteBuffer.write(
byte);
89 if (_byteBuffer.isFull()) {
91 uint8_t frameData[_frameSize];
92 for (
int j = 0; j < _frameSize; j++) {
93 frameData[j] = _byteBuffer.
read();
96 sendData(frameData, _frameSize);
105 switch (_framingMode) {
106 case FramingMode::FixedSize:
107 for (
size_t i = 0;
i <
len;
i++) {
111 case FramingMode::WriteBytes:
112 for (
size_t i = 0;
i <
len;
i++) {
121 void flush()
override {
122 if (_byteBuffer.available() == 0) {
126 uint8_t frameData[_frameSize]{};
127 int len = _byteBuffer.
readArray(frameData, _frameSize);
128 sendData(frameData, len);
133 Codec* _codec =
nullptr;
134 TxProtocol* _protocol =
nullptr;
135 RingBuffer<uint8_t> _byteBuffer;
136 uint16_t _frameSize = DEFAULT_FRAME_SIZE;
137 FramingMode _framingMode = FramingMode::WriteBytes;
138 uint16_t _bitFrequencyHz = DEFAULT_BIT_FREQ_HZ;
139 bool _useChecksum =
false;
140 uint8_t check_sum = 0;
142 bool isPreambleSent =
false;
144 void sendPreamble() {
146 assert(_protocol !=
nullptr);
147 if (!isPreambleSent) _protocol->sendPreamble();
148 isPreambleSent =
true;
151 void sendData(
const uint8_t* data, uint8_t len) {
153 assert(_protocol !=
nullptr);
154 _protocol->sendData(data, len);
159 assert(_protocol !=
nullptr);
160 _protocol->sendEnd(_useChecksum);
161 isPreambleSent =
false;
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,...
static void debug(const char *format,...)
Log a debug message with formatting.
bool read(T &out)
Read and remove the next element. Returns true if successful.
int readArray(T *dest, size_t len)
Read up to len elements into dest, returns number of elements read.
Provides common logic for transmitting signals using various framing modes.
void setFramingMode(FramingMode mode) override
void setFrameSize(uint16_t size)
Set the expected frame size for dynamic data transmission.
TxDriverCommon(TxProtocol &protocol, Codec &codec, uint8_t pin, bool useChecksum=false)
size_t write(const uint8_t *data, size_t len)
Build frames in the buffer and send when full.
Abstract base class for IR transmitters.
Abstract base class for defining transmission protocols.
Small, header-only vector replacement for non-STL environments.