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"
56
58 public Stream, public SerialWombatPin
59{
60 public:
66
67
72 SerialWombatIRRx(SerialWombatChip &serialWombatChip ):SerialWombatPin(serialWombatChip)
73 {
74 }
75
88 int16_t begin(
89 uint8_t pin, SerialWombatIRRx::publicDataOutput dataOutput = SerialWombatIRRx::publicDataOutput::DATACOUNT , uint8_t irMode = 0, bool useRepeat = true,
90 SerialWombatPinState_t activeState=SW_LOW, uint16_t publicDataTimeoutPeriod_mS = 1000, uint16_t publicDataTimeoutValue = 0xFFFF, bool useAddressFilter = false, uint16_t addressFilterValue = 0x1234)
91 {
92 _pinMode = (uint8_t)PIN_MODE_IRRX;
93 _pin = pin;
94 int16_t returnValue = 0;
95
96 {
97 uint8_t tx[] = { 200, _pin, _pinMode, irMode,
98 useRepeat ? (uint8_t)1 : (uint8_t)0,
99 (uint8_t)activeState,
100 (uint8_t)(addressFilterValue & 0xFF), (uint8_t)((addressFilterValue >> 8) & 0xFF), };
101 returnValue = _sw.sendPacket(tx);
102 if (returnValue < 0)
103 {
104 return returnValue;
105 }
106 }
107 {
108 uint8_t tx[] { 201, _pin, _pinMode, (uint8_t)(publicDataTimeoutPeriod_mS & 0xFF), (uint8_t)((publicDataTimeoutPeriod_mS >> 8) & 0xFF),
109 (uint8_t)(publicDataTimeoutValue & 0xFF), (uint8_t)((publicDataTimeoutValue >> 8) & 0xFF), useAddressFilter ? (uint8_t)1 : (uint8_t)0
110 };
111
112 returnValue = _sw.sendPacket(tx);
113 if (returnValue < 0)
114 {
115 return returnValue;
116 }
117 }
118 {
119 uint8_t tx[] { 205, _pin, _pinMode, (uint8_t)dataOutput,
120 0x55,0x55,0x55,0x55};
121
122 returnValue = _sw.sendPacket(tx);
123 //if (returnValue < 0)
124 {
125 return returnValue;
126 }
127 }
128
129 }
130
136 {
137 uint8_t tx[8] = { 201, _pin, _pinMode, 0,0x55,0x55,0x55,0x55 };
138 uint8_t rx[8];
139 _sw.sendPacket(tx, rx);
140 return (rx[4]);
141 }
142
147 int read()
148 {
149 uint8_t tx[8] = { 202, _pin,_pinMode, 1,0x55,0x55,0x55,0x55 };
150 uint8_t rx[8];
151 if (_sw.sendPacket(tx, rx) < 0)
152 {
153 return -1;
154 }
155
156 if (rx[3] != 0)
157 {
158 return (rx[4]);
159 }
160 else
161 {
162 return (-1);
163 }
164 }
165
166 void flush()
167 {
168 //TODO
169 }
170
174 int peek()
175 {
176 uint8_t tx[8] = { 203, _pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
177 uint8_t rx[8];
178 _sw.sendPacket(tx, rx);
179 if (rx[4] > 0)
180 {
181 return (rx[5]);
182 }
183 else
184 {
185 return (-1);
186 }
187 }
188
196 size_t write(uint8_t data)
197 {
198 (void)data; // Avoid compiler warning about unused parameter
199
200 return (1);
201 }
202
211 size_t write(const uint8_t* buffer, size_t size)
212 {
213 (void)buffer; // Avoid compiler warning about unused parameter
214 return(size);
215 }
216
222 {
223 return(0);
224 }
225
236 size_t readBytes(char* buffer, size_t length)
237 {
238 int index = 0;
239 int bytesAvailable = 0;
240 uint32_t timeoutMillis = millis() + timeout;
241 while (length > 0 && timeoutMillis > millis())
242 {
243 int bytecount = 4;
244 if (length < 4)
245 {
246 bytecount = length;
247 }
248 {
249
250 uint8_t tx[8] = { 202, _pin,_pinMode, (uint8_t)bytecount,0x55,0x55,0x55,0x55 };
251 uint8_t rx[8];
252 _sw.sendPacket(tx, rx);
253 bytesAvailable = rx[3];
254
255 if (bytesAvailable == 0)
256 {
257 continue;
258 }
259 else
260 {
261 timeoutMillis = millis() + timeout;
262 }
263 uint8_t bytesReturned = bytecount;
264 if (rx[3] < bytecount)
265 {
266 bytesReturned = rx[3];
267 }
268 for (int i = 0; i < bytesReturned; ++i)
269 {
270 buffer[index] = rx[i + 4];
271 ++index;
272 --bytesAvailable;
273 --length;
274
275 }
276 }
277
278 }
279 return (index);
280 }
281
285 void setTimeout(long timeout_mS)
286 {
287 if (timeout_mS == 0)
288 {
289 timeout = 0x80000000;
290 }
291 else
292 {
293 timeout = timeout_mS;
294 }
295 }
296
297
302 {
303 return irrx;
304 }
305
310 uint16_t readAddress()
311 {
312 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
313 uint8_t rx[8];
314 _sw.sendPacket(tx, rx);
315 uint16_t returnVal = rx[4];
316 returnVal <<= 8;
317 returnVal|= rx[3];
318 return returnVal;
319 }
320
324 uint16_t readDataCount()
325 {
326 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
327 uint8_t rx[8];
328 _sw.sendPacket(tx, rx);
329 uint16_t returnVal = rx[6];
330 returnVal <<= 8;
331 returnVal|= rx[5];
332 return returnVal;
333 }
334 protected:
335 uint32_t timeout = 1;
336};
337
338
339
@ 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.