Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatIRRx.h
Go to the documentation of this file.
1#pragma once
2
3/*
4Copyright 2025-26 Broadwell Consulting Inc.
5
6"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
7the United States. See SerialWombat.com for usage guidance.
8
9Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26*/
27
28#include "Stream.h"
29#include "SerialWombat.h"
60
62 public Stream, public SerialWombatPin
63{
64 public:
70
71
76 SerialWombatIRRx(SerialWombatChip &serialWombatChip ):SerialWombatPin(serialWombatChip)
77 {
78 }
79
92 int16_t begin(
93 uint8_t pin, SerialWombatIRRx::publicDataOutput dataOutput = SerialWombatIRRx::publicDataOutput::DATACOUNT , uint8_t irMode = 0, bool useRepeat = true,
94 SerialWombatPinState_t activeState=SW_LOW, uint16_t publicDataTimeoutPeriod_mS = 1000, uint16_t publicDataTimeoutValue = 0xFFFF, bool useAddressFilter = false, uint16_t addressFilterValue = 0x1234)
95 {
96 _pinMode = (uint8_t)PIN_MODE_IRRX;
97 _pin = pin;
98 int16_t returnValue = 0;
99
100 {
101 uint8_t tx[] = { 200, _pin, _pinMode, irMode,
102 useRepeat ? (uint8_t)1 : (uint8_t)0,
103 (uint8_t)activeState,
104 (uint8_t)(addressFilterValue & 0xFF), (uint8_t)((addressFilterValue >> 8) & 0xFF), };
105 returnValue = _sw.sendPacket(tx);
106 if (returnValue < 0)
107 {
108 return returnValue;
109 }
110 }
111 {
112 uint8_t tx[] { 201, _pin, _pinMode, (uint8_t)(publicDataTimeoutPeriod_mS & 0xFF), (uint8_t)((publicDataTimeoutPeriod_mS >> 8) & 0xFF),
113 (uint8_t)(publicDataTimeoutValue & 0xFF), (uint8_t)((publicDataTimeoutValue >> 8) & 0xFF), useAddressFilter ? (uint8_t)1 : (uint8_t)0
114 };
115
116 returnValue = _sw.sendPacket(tx);
117 if (returnValue < 0)
118 {
119 return returnValue;
120 }
121 }
122 {
123 uint8_t tx[] { 205, _pin, _pinMode, (uint8_t)dataOutput,
124 0x55,0x55,0x55,0x55};
125
126 returnValue = _sw.sendPacket(tx);
127 //if (returnValue < 0)
128 {
129 return returnValue;
130 }
131 }
132
133 }
134
140 {
141 uint8_t tx[8] = { 201, _pin, _pinMode, 0,0x55,0x55,0x55,0x55 };
142 uint8_t rx[8];
143 _sw.sendPacket(tx, rx);
144 return (rx[4]);
145 }
146
151 int read()
152 {
153 uint8_t tx[8] = { 202, _pin,_pinMode, 1,0x55,0x55,0x55,0x55 };
154 uint8_t rx[8];
155 if (_sw.sendPacket(tx, rx) < 0)
156 {
157 return -1;
158 }
159
160 if (rx[3] != 0)
161 {
162 return (rx[4]);
163 }
164 else
165 {
166 return (-1);
167 }
168 }
169
170 void flush()
171 {
172 //TODO
173 }
174
178 int peek()
179 {
180 uint8_t tx[8] = { 203, _pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
181 uint8_t rx[8];
182 _sw.sendPacket(tx, rx);
183 if (rx[4] > 0)
184 {
185 return (rx[5]);
186 }
187 else
188 {
189 return (-1);
190 }
191 }
192
200 size_t write(uint8_t data)
201 {
202 (void)data; // Avoid compiler warning about unused parameter
203
204 return (1);
205 }
206
215 size_t write(const uint8_t* buffer, size_t size)
216 {
217 (void)buffer; // Avoid compiler warning about unused parameter
218 return(size);
219 }
220
226 {
227 return(0);
228 }
229
240 size_t readBytes(char* buffer, size_t length)
241 {
242 int index = 0;
243 int bytesAvailable = 0;
244 uint32_t timeoutMillis = millis() + timeout;
245 while (length > 0 && timeoutMillis > millis())
246 {
247 int bytecount = 4;
248 if (length < 4)
249 {
250 bytecount = length;
251 }
252 {
253
254 uint8_t tx[8] = { 202, _pin,_pinMode, (uint8_t)bytecount,0x55,0x55,0x55,0x55 };
255 uint8_t rx[8];
256 _sw.sendPacket(tx, rx);
257 bytesAvailable = rx[3];
258
259 if (bytesAvailable == 0)
260 {
261 continue;
262 }
263 else
264 {
265 timeoutMillis = millis() + timeout;
266 }
267 uint8_t bytesReturned = bytecount;
268 if (rx[3] < bytecount)
269 {
270 bytesReturned = rx[3];
271 }
272 for (int i = 0; i < bytesReturned; ++i)
273 {
274 buffer[index] = rx[i + 4];
275 ++index;
276 --bytesAvailable;
277 --length;
278
279 }
280 }
281
282 }
283 return (index);
284 }
285
289 void setTimeout(long timeout_mS)
290 {
291 if (timeout_mS == 0)
292 {
293 timeout = 0x80000000;
294 }
295 else
296 {
297 timeout = timeout_mS;
298 }
299 }
300
301
306 {
307 return irrx;
308 }
309
314 uint16_t readAddress()
315 {
316 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
317 uint8_t rx[8];
318 _sw.sendPacket(tx, rx);
319 uint16_t returnVal = rx[4];
320 returnVal <<= 8;
321 returnVal|= rx[3];
322 return returnVal;
323 }
324
328 uint16_t readDataCount()
329 {
330 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
331 uint8_t rx[8];
332 _sw.sendPacket(tx, rx);
333 uint16_t returnVal = rx[6];
334 returnVal <<= 8;
335 returnVal|= rx[5];
336 return returnVal;
337 }
338 protected:
339 uint32_t timeout = 1;
340};
341
342
343
@ PIN_MODE_IRRX
(37)
SerialWombatPinState_t
@ SW_LOW
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
void setTimeout(long timeout_mS)
implemented to fulfill Stream requirement.
int peek()
Query the SerialWombatIRRx queue for the next avaialble byte, but don't remove it from the queue.
SerialWombatIRRx & operator=(SerialWombatIRRx &irrx)
used to allow reference copy. Not for user use.
int available()
Returns the number of bytes available in the SerialWombatIRRx queue.
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the SerialWombatIRRx queue queue.
@ COMMAND
The last 8 bit command received (address filtered if enabled)
@ ADDRESS
the last 16 bit address received. Not Address filtered
@ DATACOUNT
The number of data that have been queued (Affected by address filtering and repeat settings)
void flush()
Discard all bytes from the SerialWombatIRRx queue.
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the SerialWombatIRRx queue (Does nothing)
uint16_t readDataCount()
Returns the total number of queued commands (rolls over at 65535)
int16_t begin(uint8_t pin, SerialWombatIRRx::publicDataOutput dataOutput=SerialWombatIRRx::publicDataOutput::DATACOUNT, uint8_t irMode=0, bool useRepeat=true, SerialWombatPinState_t activeState=SW_LOW, uint16_t publicDataTimeoutPeriod_mS=1000, uint16_t publicDataTimeoutValue=0xFFFF, bool useAddressFilter=false, uint16_t addressFilterValue=0x1234)
Initalize the SerialWombatIRRx.
SerialWombatIRRx(SerialWombatChip &serialWombatChip)
Constructor for the SerialWombatIRRx class.
int read()
Reads a byte from the SerialWombatIRRx queue.
uint16_t readAddress()
Returns the address of the last received IR command.
int availableForWrite()
Number of bytes avaialble to write to SerialWombatIRRx queue. Returns 0.
size_t write(uint8_t data)
Write a byte to the SerialWombatIRRx queue (Does Nothing)
SerialWombatChip & _sw
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.