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 sendData(frameData, _frameSize);
96 sendEnd();
97 }
98 return 1;
99 }
100
102 size_t write(const uint8_t* data, size_t len) {
103 switch (_framingMode) {
104 case FramingMode::FixedSize:
105 for (size_t i = 0; i < len; i++) {
106 _byteBuffer.write(data[i]);
107 if (_byteBuffer.isFull()) {
108 // Handle full buffer (e.g., send frame)
109 uint8_t frameData[_frameSize];
110 for (int j = 0; j < _frameSize; j++) {
111 frameData[j] = _byteBuffer.read();
112 }
113 sendData(data, len);
114 sendEnd();
115 }
116 }
117 break;
118 case FramingMode::WriteBytes:
119 sendPreamble();
120 sendData(data, len);
121 sendEnd();
122 break;
123 case FramingMode::Flush:
124 if (_protocol->isFrameClosed()) {
125 sendPreamble();
126 }
127 sendData(data, len);
128 break;
129 }
130 return len;
131 }
132
133 void flush() override {
134 if (_byteBuffer.available() > 0) {
135 uint8_t frameData[_frameSize]{};
136 _byteBuffer.readArray(frameData, _frameSize);
137 sendData(frameData, _frameSize);
138 }
139 sendEnd();
140 }
141
142 protected:
143 Codec* _codec = nullptr;
144 TxProtocol* _protocol = nullptr;
145 RingBuffer<uint8_t> _byteBuffer;
146 uint16_t _frameSize = DEFAULT_FRAME_SIZE;
147 FramingMode _framingMode = FramingMode::WriteBytes;
148 uint16_t _bitFrequencyHz = DEFAULT_BIT_FREQ_HZ;
149 bool _useChecksum = false;
150 uint8_t check_sum = 0;
151 uint8_t _pin = -1;
152
153 void sendPreamble() {
154 assert(_protocol != nullptr);
155 _protocol->sendPreamble();
156 }
157
158 void sendData(const uint8_t* data, uint8_t len) {
159 assert(_protocol != nullptr);
160 _protocol->sendData(data, len);
161 }
162
163 void sendEnd() {
164 assert(_protocol != nullptr);
165 bool isDelayAfterFrame = (_framingMode != FramingMode::FixedSize);
166 _protocol->sendEnd(_useChecksum, isDelayAfterFrame);
167 }
168}; // end of TxDriverArduino
169
170} // end of 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
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:18
Abstract base class for defining transmission protocols.
Definition TxProtocol.h:125
Small, header-only vector replacement for non-STL environments.
Definition Vector.h:29