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
65
80 int16_t begin(
81 uint8_t pin, uint8_t irMode = 0, bool useRepeat = true, SerialWombatPinState_t activeState = SW_LOW,
82 uint16_t publicDataTimeoutPeriod_mS = 1000, uint16_t publicDataTimeoutValue = 0xFFFF, bool useAddressFilter = false, uint16_t addressFilterValue = 0x1234,
84 {
85 _pinMode = (uint8_t)PIN_MODE_IRRX;
86 _pin = pin;
87 int16_t returnValue = 0;
88
89 {
90 uint8_t tx[] = { 200, _pin, _pinMode, irMode,
91 useRepeat ? (uint8_t)1 : (uint8_t)0,
92 (uint8_t)activeState,
93 (uint8_t)(addressFilterValue & 0xFF), (uint8_t)((addressFilterValue >> 8) & 0xFF), };
94 returnValue = _sw.sendPacket(tx);
95 if (returnValue < 0)
96 {
97 return returnValue;
98 }
99 }
100 {
101 uint8_t tx[] { 201, _pin, _pinMode, (uint8_t)(publicDataTimeoutPeriod_mS & 0xFF), (uint8_t)((publicDataTimeoutPeriod_mS >> 8) & 0xFF),
102 (uint8_t)(publicDataTimeoutValue & 0xFF), (uint8_t)((publicDataTimeoutValue >> 8) & 0xFF), useAddressFilter ? (uint8_t)1 : (uint8_t)0
103 };
104
105 returnValue = _sw.sendPacket(tx);
106 if (returnValue < 0)
107 {
108 return returnValue;
109 }
110 }
111 {
112 uint8_t tx[] { 205, _pin, _pinMode, (uint8_t)dataOutput,
113 0x55,0x55,0x55,0x55};
114
115 returnValue = _sw.sendPacket(tx);
116 //if (returnValue < 0)
117 {
118 return returnValue;
119 }
120 }
121
122 }
123
124
126 {
127 uint8_t tx[8] = { 201, _pin, _pinMode, 0,0x55,0x55,0x55,0x55 };
128 uint8_t rx[8];
129 _sw.sendPacket(tx, rx);
130 return (rx[4]);
131 }
132
137 int read()
138 {
139 uint8_t tx[8] = { 202, _pin,_pinMode, 1,0x55,0x55,0x55,0x55 };
140 uint8_t rx[8];
141 if (_sw.sendPacket(tx, rx) < 0)
142 {
143 return -1;
144 }
145
146 if (rx[3] != 0)
147 {
148 return (rx[4]);
149 }
150 else
151 {
152 return (-1);
153 }
154 }
155
156 void flush()
157 {
158 //TODO
159 }
160
164 int peek()
165 {
166 uint8_t tx[8] = { 203, _pin,_pinMode,0x55,0x55,0x55,0x55,0x55 };
167 uint8_t rx[8];
168 _sw.sendPacket(tx, rx);
169 if (rx[4] > 0)
170 {
171 return (rx[5]);
172 }
173 else
174 {
175 return (-1);
176 }
177 }
178
186 size_t write(uint8_t data)
187 {
188 (void)data; // Avoid compiler warning about unused parameter
189
190 return (1);
191 }
192
201 size_t write(const uint8_t* buffer, size_t size)
202 {
203 (void)buffer; // Avoid compiler warning about unused parameter
204 return(size);
205 }
206
212 {
213 return(0);
214 }
215
226 size_t readBytes(char* buffer, size_t length)
227 {
228 int index = 0;
229 int bytesAvailable = 0;
230 uint32_t timeoutMillis = millis() + timeout;
231 while (length > 0 && timeoutMillis > millis())
232 {
233 int bytecount = 4;
234 if (length < 4)
235 {
236 bytecount = length;
237 }
238 {
239
240 uint8_t tx[8] = { 202, _pin,_pinMode, (uint8_t)bytecount,0x55,0x55,0x55,0x55 };
241 uint8_t rx[8];
242 _sw.sendPacket(tx, rx);
243 bytesAvailable = rx[3];
244
245 if (bytesAvailable == 0)
246 {
247 continue;
248 }
249 else
250 {
251 timeoutMillis = millis() + timeout;
252 }
253 uint8_t bytesReturned = bytecount;
254 if (rx[3] < bytecount)
255 {
256 bytesReturned = rx[3];
257 }
258 for (int i = 0; i < bytesReturned; ++i)
259 {
260 buffer[index] = rx[i + 4];
261 ++index;
262 --bytesAvailable;
263 --length;
264
265 }
266 }
267
268 }
269 return (index);
270 }
271
275 void setTimeout(long timeout_mS)
276 {
277 if (timeout_mS == 0)
278 {
279 timeout = 0x80000000;
280 }
281 else
282 {
283 timeout = timeout_mS;
284 }
285 }
286
287
292 {
293 return irrx;
294 }
295
296
297 uint16_t readAddress()
298 {
299 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
300 uint8_t rx[8];
301 _sw.sendPacket(tx, rx);
302 uint16_t returnVal = rx[4];
303 returnVal <<= 8;
304 returnVal|= rx[3];
305 return returnVal;
306 }
307
308 uint16_t readDataCount()
309 {
310 uint8_t tx[8] = { 204, _pin,_pinMode, 0x55,0x55,0x55,0x55,0x55 };
311 uint8_t rx[8];
312 _sw.sendPacket(tx, rx);
313 uint16_t returnVal = rx[6];
314 returnVal <<= 8;
315 returnVal|= rx[5];
316 return returnVal;
317 }
318 protected:
319 uint32_t timeout = 1;
320};
321
322
323
@ 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.
SerialWombatIRRx operator=(SerialWombatIRRx &irrx)
used to allow reference copy. Not for user use.
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.
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)
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.
int read()
Reads a byte from the SerialWombatIRRx queue.
SerialWombatIRRx(SerialWombatChip &sw)
Constructor for the SerialWombatIRRx class.
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.