Serial Wombat Arduino Library
Loading...
Searching...
No Matches
SerialWombatIRRx.h
Go to the documentation of this file.
1#pragma once
2
3/*
4Copyright 2025 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"
46
48 public Stream, public SerialWombatPin
49{
50 public:
56
57
62 SerialWombatIRRx(SerialWombatChip &serialWombatChip ):SerialWombatPin(serialWombatChip)
63 {
64 }
65
78 int16_t begin(
79 uint8_t pin, uint8_t irMode = 0, bool useRepeat = true, SerialWombatPinState_t activeState = SW_LOW,
80 uint16_t publicDataTimeoutPeriod_mS = 1000, uint16_t publicDataTimeoutValue = 0xFFFF, bool useAddressFilter = false, uint16_t addressFilterValue = 0x1234,
82 {
83 _pinMode = (uint8_t)PIN_MODE_IRRX;
84 _pin = pin;
85 int16_t returnValue = 0;
86
87 {
88 uint8_t tx[] = { 200, _pin, _pinMode, irMode,
89 useRepeat ? (uint8_t)1 : (uint8_t)0,
90 (uint8_t)activeState,
91 (uint8_t)(addressFilterValue & 0xFF), (uint8_t)((addressFilterValue >> 8) & 0xFF), };
92 returnValue = _sw.sendPacket(tx);
93 if (returnValue < 0)
94 {
95 return returnValue;
96 }
97 }
98 {
99 uint8_t tx[] { 201, _pin, _pinMode, (uint8_t)(publicDataTimeoutPeriod_mS & 0xFF), (uint8_t)((publicDataTimeoutPeriod_mS >> 8) & 0xFF),
100 (uint8_t)(publicDataTimeoutValue & 0xFF), (uint8_t)((publicDataTimeoutValue >> 8) & 0xFF), useAddressFilter ? (uint8_t)1 : (uint8_t)0
101 };
102
103 returnValue = _sw.sendPacket(tx);
104 if (returnValue < 0)
105 {
106 return returnValue;
107 }
108 }
109 {
110 uint8_t tx[] { 205, _pin, _pinMode, (uint8_t)dataOutput,
111 0x55,0x55,0x55,0x55};
112
113 returnValue = _sw.sendPacket(tx);
114 //if (returnValue < 0)
115 {
116 return returnValue;
117 }
118 }
119
120 }
121
127 {
128 uint8_t tx[8] = { 201, _pin, _pinMode, 0,0x55,0x55,0x55,0x55 };
129 uint8_t rx[8];
130 _sw.sendPacket(tx, rx);
131 return (rx[4]);
132 }
133
138 int read()
139 {
140 uint8_t tx[8] = { 202, _pin,_pinMode, 1,0x55,0x55,0x55,0x55 };
141 uint8_t rx[8];
142 if (_sw.sendPacket(tx, rx) < 0)
143 {
144 return -1;
145 }
146
147 if (rx[3] != 0)
148 {
149 return (rx[4]);
150 }
151 else
152 {
153 return (-1);
154 }
155 }
156
157 void flush()
158 {
159 //TODO
160 }
161
165 int peek()
166 {
167 uint8_t tx[8] = { 203, _pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
168 uint8_t rx[8];
169 _sw.sendPacket(tx, rx);
170 if (rx[4] > 0)
171 {
172 return (rx[5]);
173 }
174 else
175 {
176 return (-1);
177 }
178 }
179
187 size_t write(uint8_t data)
188 {
189 (void)data; // Avoid compiler warning about unused parameter
190
191 return (1);
192 }
193
202 size_t write(const uint8_t* buffer, size_t size)
203 {
204 (void)buffer; // Avoid compiler warning about unused parameter
205 return(size);
206 }
207
213 {
214 return(0);
215 }
216
227 size_t readBytes(char* buffer, size_t length)
228 {
229 int index = 0;
230 int bytesAvailable = 0;
231 uint32_t timeoutMillis = millis() + timeout;
232 while (length > 0 && timeoutMillis > millis())
233 {
234 int bytecount = 4;
235 if (length < 4)
236 {
237 bytecount = length;
238 }
239 {
240
241 uint8_t tx[8] = { 202, _pin,_pinMode, (uint8_t)bytecount,0x55,0x55,0x55,0x55 };
242 uint8_t rx[8];
243 _sw.sendPacket(tx, rx);
244 bytesAvailable = rx[3];
245
246 if (bytesAvailable == 0)
247 {
248 continue;
249 }
250 else
251 {
252 timeoutMillis = millis() + timeout;
253 }
254 uint8_t bytesReturned = bytecount;
255 if (rx[3] < bytecount)
256 {
257 bytesReturned = rx[3];
258 }
259 for (int i = 0; i < bytesReturned; ++i)
260 {
261 buffer[index] = rx[i + 4];
262 ++index;
263 --bytesAvailable;
264 --length;
265
266 }
267 }
268
269 }
270 return (index);
271 }
272
276 void setTimeout(long timeout_mS)
277 {
278 if (timeout_mS == 0)
279 {
280 timeout = 0x80000000;
281 }
282 else
283 {
284 timeout = timeout_mS;
285 }
286 }
287
288
293 {
294 return irrx;
295 }
296
301 uint16_t readAddress()
302 {
303 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
304 uint8_t rx[8];
305 _sw.sendPacket(tx, rx);
306 uint16_t returnVal = rx[4];
307 returnVal <<= 8;
308 returnVal|= rx[3];
309 return returnVal;
310 }
311
315 uint16_t readDataCount()
316 {
317 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
318 uint8_t rx[8];
319 _sw.sendPacket(tx, rx);
320 uint16_t returnVal = rx[6];
321 returnVal <<= 8;
322 returnVal|= rx[5];
323 return returnVal;
324 }
325 protected:
326 uint32_t timeout = 1;
327};
328
329
330
@ 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, 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, SerialWombatIRRx::publicDataOutput dataOutput=SerialWombatIRRx::publicDataOutput::COMMAND)
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.