Arduino PulseWire Transceiver Library
Loading...
Searching...
No Matches
TxDriverCommon.h
1#pragma once
2#include <Arduino.h>
3
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"
11
12namespace pulsewire {
13
36class TxDriverCommon : public TxDriver {
37 public:
38 TxDriverCommon() = default;
46 TxDriverCommon(TxProtocol& protocol, Codec& codec, uint8_t pin,
47
48 bool useChecksum = false) {
49 init(protocol, codec, pin, useChecksum);
50 }
51
52 virtual void init(TxProtocol& protocol, Codec& codec, uint8_t pin,
53
54 bool useChecksum = false) {
55 TRACE();
56 _codec = &codec;
57 _protocol = &protocol;
58 _useChecksum = useChecksum;
59 _pin = pin;
60 }
61
62 bool begin(uint32_t bitFrequencyHz) override {
63 // make sure that frame size is set before beginning
64 TRACE();
65 _bitFrequencyHz = bitFrequencyHz;
66 setFrameSize(_frameSize);
67 _protocol->begin(bitFrequencyHz, _codec, _pin);
68 return _codec->begin(bitFrequencyHz);
69 }
70
73 TRACE();
74 Logger::debug("Setting frame size to %d", size);
75 _frameSize = size;
76 _byteBuffer.resize(size);
77 _protocol->setFrameSize(size);
78 }
79
82 void setFramingMode(FramingMode mode) override { _framingMode = mode; }
83
84 int write(uint8_t byte) override {
85 if (_byteBuffer.size() == 0) {
86 return 0; // No buffer space allocated
87 }
88 _byteBuffer.write(byte);
89 if (_byteBuffer.isFull()) {
90 // Handle full buffer (e.g., send frame)
91 uint8_t frameData[_frameSize];
92 for (int j = 0; j < _frameSize; j++) {
93 frameData[j] = _byteBuffer.read();
94 }
95 sendPreamble();
96 sendData(frameData, _frameSize);
97 sendEnd();
98 }
99 return 1;
100 }
101
103 size_t write(const uint8_t* data, size_t len) {
104 Logger::debug("Writing %d bytes to TxDriverCommon", len);
105 switch (_framingMode) {
106 case FramingMode::FixedSize:
107 for (size_t i = 0; i < len; i++) {
108 write(data[i]);
109 }
110 break;
111 case FramingMode::WriteBytes:
112 for (size_t i = 0; i < len; i++) {
113 write(data[i]);
114 }
115 flush();
116 break;
117 }
118 return len;
119 }
120
121 void flush() override {
122 if (_byteBuffer.available() == 0) {
123 return; // Nothing to flush
124 }
125 sendPreamble();
126 uint8_t frameData[_frameSize]{};
127 int len = _byteBuffer.readArray(frameData, _frameSize);
128 sendData(frameData, len);
129 sendEnd();
130 }
131
132 protected:
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;
141 uint8_t _pin = -1;
142 bool isPreambleSent = false;
143
144 void sendPreamble() {
145 Logger::debug("Sending preamble");
146 assert(_protocol != nullptr);
147 if (!isPreambleSent) _protocol->sendPreamble();
148 isPreambleSent = true;
149 }
150
151 void sendData(const uint8_t* data, uint8_t len) {
152 Logger::debug("Sending data: %d bytes", len);
153 assert(_protocol != nullptr);
154 _protocol->sendData(data, len);
155 }
156
157 void sendEnd() {
158 Logger::debug("Sending end");
159 assert(_protocol != nullptr);
160 _protocol->sendEnd(_useChecksum);
161 isPreambleSent = false; // reset for next frame
162 }
163}; // end of TxDriverArduino
164
165} // end of namespace pulsewire
Abstract base class for IR protocol encoding and decoding.
Definition Codec.h:53
virtual bool begin(uint32_t bitFrequencyHz)
initialization method for codecs that require setup before use (e.g., loading PIO programs,...
Definition Codec.h:70
static void debug(const char *format,...)
Log a debug message with formatting.
Definition Logger.h:82
bool read(T &out)
Read and remove the next element. Returns true if successful.
Definition RingBuffer.h:75
int readArray(T *dest, size_t len)
Read up to len elements into dest, returns number of elements read.
Definition RingBuffer.h:94
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.
Definition TxDriver.h:17
Abstract base class for defining transmission protocols.
Definition TxProtocol.h:125
Small, header-only vector replacement for non-STL environments.
Definition Vector.h:29