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 &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,
81 SerialWombatIRRx::publicDataOutput dataOutput = SerialWombatIRRx::publicDataOutput::COMMAND )
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
126 int available()
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
212 int availableForWrite()
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
292 SerialWombatIRRx& operator=(SerialWombatIRRx& irrx)
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
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:313
SerialWombatIRRx::setTimeout
void setTimeout(long timeout_mS)
implemented to fulfill Stream requirement.
Definition SerialWombatIRRx.h:276
SerialWombatIRRx::peek
int peek()
Query the SerialWombatIRRx queue for the next avaialble byte, but don't remove it from the queue.
Definition SerialWombatIRRx.h:165
SerialWombatIRRx::operator=
SerialWombatIRRx & operator=(SerialWombatIRRx &irrx)
used to allow reference copy. Not for user use.
Definition SerialWombatIRRx.h:292
SerialWombatIRRx::available
int available()
Returns the number of bytes available in the SerialWombatIRRx queue.
Definition SerialWombatIRRx.h:126
SerialWombatIRRx::readBytes
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the SerialWombatIRRx queue queue.
Definition SerialWombatIRRx.h:227
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:157
SerialWombatIRRx::write
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the SerialWombatIRRx queue (Does nothing)
Definition SerialWombatIRRx.h:202
SerialWombatIRRx::readDataCount
uint16_t readDataCount()
Returns the total number of queued commands (rolls over at 65535)
Definition SerialWombatIRRx.h:315
SerialWombatIRRx::timeout
uint32_t timeout
Definition SerialWombatIRRx.h:326
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:78
SerialWombatIRRx::SerialWombatIRRx
SerialWombatIRRx(SerialWombatChip &serialWombatChip)
Constructor for the SerialWombatIRRx class.
Definition SerialWombatIRRx.h:62
SerialWombatIRRx::read
int read()
Reads a byte from the SerialWombatIRRx queue.
Definition SerialWombatIRRx.h:138
SerialWombatIRRx::readAddress
uint16_t readAddress()
Returns the address of the last received IR command.
Definition SerialWombatIRRx.h:301
SerialWombatIRRx::availableForWrite
int availableForWrite()
Number of bytes avaialble to write to SerialWombatIRRx queue. Returns 0.
Definition SerialWombatIRRx.h:212
SerialWombatIRRx::write
size_t write(uint8_t data)
Write a byte to the SerialWombatIRRx queue (Does Nothing)
Definition SerialWombatIRRx.h:187
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