Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatIRRx.h
Go to the documentation of this file.
1#pragma once
2
3/*
4 Copyright 2025-26 Broadwell Consulting Inc.
5
6"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
7 the United States. See SerialWombat.com for usage guidance.
8
9 Permission 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
16 The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18
19 THE 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
57 class SerialWombatIRRx :
58 public Stream, public SerialWombatPin
59{
60 public:
61 enum publicDataOutput {
62 COMMAND = 0,
63 ADDRESS = 1,
64 DATACOUNT = 2,
65 };
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
135 int available()
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
221 int availableForWrite()
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
301 SerialWombatIRRx& operator=(SerialWombatIRRx& irrx)
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
SerialWombat.h
PIN_MODE_IRRX
@ PIN_MODE_IRRX
(37)
Definition SerialWombat.h:294
SerialWombatPinState_t
SerialWombatPinState_t
Definition SerialWombat.h:57
SW_LOW
@ SW_LOW
Definition SerialWombat.h:58
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:315
SerialWombatIRRx::setTimeout
void setTimeout(long timeout_mS)
implemented to fulfill Stream requirement.
Definition SerialWombatIRRx.h:285
SerialWombatIRRx::peek
int peek()
Query the SerialWombatIRRx queue for the next avaialble byte, but don't remove it from the queue.
Definition SerialWombatIRRx.h:174
SerialWombatIRRx::operator=
SerialWombatIRRx & operator=(SerialWombatIRRx &irrx)
used to allow reference copy. Not for user use.
Definition SerialWombatIRRx.h:301
SerialWombatIRRx::available
int available()
Returns the number of bytes available in the SerialWombatIRRx queue.
Definition SerialWombatIRRx.h:135
SerialWombatIRRx::readBytes
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the SerialWombatIRRx queue queue.
Definition SerialWombatIRRx.h:236
SerialWombatIRRx::publicDataOutput
publicDataOutput
Definition SerialWombatIRRx.h:61
SerialWombatIRRx::COMMAND
@ COMMAND
The last 8 bit command received (address filtered if enabled)
Definition SerialWombatIRRx.h:62
SerialWombatIRRx::ADDRESS
@ ADDRESS
the last 16 bit address received. Not Address filtered
Definition SerialWombatIRRx.h:63
SerialWombatIRRx::DATACOUNT
@ DATACOUNT
The number of data that have been queued (Affected by address filtering and repeat settings)
Definition SerialWombatIRRx.h:64
SerialWombatIRRx::flush
void flush()
Discard all bytes from the SerialWombatIRRx queue.
Definition SerialWombatIRRx.h:166
SerialWombatIRRx::write
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the SerialWombatIRRx queue (Does nothing)
Definition SerialWombatIRRx.h:211
SerialWombatIRRx::readDataCount
uint16_t readDataCount()
Returns the total number of queued commands (rolls over at 65535)
Definition SerialWombatIRRx.h:324
SerialWombatIRRx::timeout
uint32_t timeout
Definition SerialWombatIRRx.h:335
SerialWombatIRRx::begin
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.
Definition SerialWombatIRRx.h:88
SerialWombatIRRx::SerialWombatIRRx
SerialWombatIRRx(SerialWombatChip &serialWombatChip)
Constructor for the SerialWombatIRRx class.
Definition SerialWombatIRRx.h:72
SerialWombatIRRx::read
int read()
Reads a byte from the SerialWombatIRRx queue.
Definition SerialWombatIRRx.h:147
SerialWombatIRRx::readAddress
uint16_t readAddress()
Returns the address of the last received IR command.
Definition SerialWombatIRRx.h:310
SerialWombatIRRx::availableForWrite
int availableForWrite()
Number of bytes avaialble to write to SerialWombatIRRx queue. Returns 0.
Definition SerialWombatIRRx.h:221
SerialWombatIRRx::write
size_t write(uint8_t data)
Write a byte to the SerialWombatIRRx queue (Does Nothing)
Definition SerialWombatIRRx.h:196
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:214
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:213
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition SerialWombatPin.h:120
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:215