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 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"
46
47 class SerialWombatIRRx :
48 public Stream, public SerialWombatPin
49{
50 public:
51 enum publicDataOutput {
52 COMMAND = 0,
53 ADDRESS = 1,
54 DATACOUNT = 2,
55 };
56
57
62 SerialWombatIRRx(SerialWombatChip &sw ):SerialWombatPin(sw)
63 {
64 }
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,
83 SerialWombatIRRx::publicDataOutput dataOutput = SerialWombatIRRx::publicDataOutput::COMMAND )
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
125 int available()
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
211 int availableForWrite()
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
291 SerialWombatIRRx operator=(SerialWombatIRRx& irrx)
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
SerialWombat.h
PIN_MODE_IRRX
@ PIN_MODE_IRRX
(37)
Definition SerialWombat.h:286
SerialWombatPinState_t
SerialWombatPinState_t
Definition SerialWombat.h:54
SW_LOW
@ SW_LOW
Definition SerialWombat.h:55
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:300
SerialWombatIRRx::operator=
SerialWombatIRRx operator=(SerialWombatIRRx &irrx)
used to allow reference copy. Not for user use.
Definition SerialWombatIRRx.h:291
SerialWombatIRRx::setTimeout
void setTimeout(long timeout_mS)
implemented to fulfill Stream requirement.
Definition SerialWombatIRRx.h:275
SerialWombatIRRx::peek
int peek()
Query the SerialWombatIRRx queue for the next avaialble byte, but don't remove it from the queue.
Definition SerialWombatIRRx.h:164
SerialWombatIRRx::available
int available()
Definition SerialWombatIRRx.h:125
SerialWombatIRRx::readBytes
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the SerialWombatIRRx queue queue.
Definition SerialWombatIRRx.h:226
SerialWombatIRRx::publicDataOutput
publicDataOutput
Definition SerialWombatIRRx.h:51
SerialWombatIRRx::COMMAND
@ COMMAND
The last 8 bit command received (address filtered if enabled)
Definition SerialWombatIRRx.h:52
SerialWombatIRRx::ADDRESS
@ ADDRESS
the last 16 bit address received. Not Address filtered
Definition SerialWombatIRRx.h:53
SerialWombatIRRx::DATACOUNT
@ DATACOUNT
The number of data that have been queued (Affected by address filtering and repeat settings)
Definition SerialWombatIRRx.h:54
SerialWombatIRRx::flush
void flush()
Discard all bytes from the SerialWombatIRRx queue.
Definition SerialWombatIRRx.h:156
SerialWombatIRRx::write
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the SerialWombatIRRx queue (Does nothing)
Definition SerialWombatIRRx.h:201
SerialWombatIRRx::readDataCount
uint16_t readDataCount()
Definition SerialWombatIRRx.h:308
SerialWombatIRRx::timeout
uint32_t timeout
Definition SerialWombatIRRx.h:319
SerialWombatIRRx::begin
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.
Definition SerialWombatIRRx.h:80
SerialWombatIRRx::read
int read()
Reads a byte from the SerialWombatIRRx queue.
Definition SerialWombatIRRx.h:137
SerialWombatIRRx::SerialWombatIRRx
SerialWombatIRRx(SerialWombatChip &sw)
Constructor for the SerialWombatIRRx class.
Definition SerialWombatIRRx.h:62
SerialWombatIRRx::readAddress
uint16_t readAddress()
Definition SerialWombatIRRx.h:297
SerialWombatIRRx::availableForWrite
int availableForWrite()
Number of bytes avaialble to write to SerialWombatIRRx queue. Returns 0.
Definition SerialWombatIRRx.h:211
SerialWombatIRRx::write
size_t write(uint8_t data)
Write a byte to the SerialWombatIRRx queue (Does Nothing)
Definition SerialWombatIRRx.h:186
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:206
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:205
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition SerialWombatPin.h:121
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:207