Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatQueue.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2020-2023 Broadwell Consulting Inc.
4
5"Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7
8 Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27#include <stdint.h>
28#include "SerialWombat.h"
29
30
31 enum class SerialWombatQueueType {
32 QUEUE_TYPE_RAM_BYTE = 0,
33 QUEUE_TYPE_RAM_BYTE_SHIFT = 1,
34};
35
39
40
41 class SerialWombatQueue : public Stream
42{
43 public:
47 */
48 SerialWombatQueue(SerialWombatChip& serialWombat):_sw(serialWombat)
49 {
50 _sw = serialWombat;
51 }
52
61 */
62 int16_t begin(uint16_t index, uint16_t length, SerialWombatQueueType qtype = SerialWombatQueueType::QUEUE_TYPE_RAM_BYTE)
63{
64 startIndex = index;
65 length = length;
66 uint8_t tx[] = {(uint8_t) SerialWombatCommands::COMMAND_BINARY_QUEUE_INITIALIZE ,SW_LE16(index),SW_LE16(length),(uint8_t)qtype, 0x55,0x55 };
67 uint8_t rx[8];
68 int16_t result = _sw.sendPacket(tx,rx);
69 if (result < 0)
70 {
71 return result;
72 }
73 else
74 {
75 return ((int16_t)(rx[3] + 256 * rx[4]));
76 }
77}
78
82 */
83 int available()
84{
85 uint8_t tx[] = {(uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
86 uint8_t rx[8];
87 int16_t sendResult = _sw.sendPacket(tx,rx);
88 if (sendResult >= 0)
89 {
90 return (rx[4] + 256 * rx[5]);
91 }
92 return (0);
93}
94
97 */
98 int read()
99{
100 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_READ_BYTES ,SW_LE16(startIndex),1,0x55,0x55,0x55,0x55 };
101 uint8_t rx[8];
102 int16_t sendResult = _sw.sendPacket(tx, rx);
103 if (sendResult >= 0)
104 {
105 if (rx[1] == 1)
106 return (rx[2]);
107 }
108 return (-1);
109}
110
112 */
113 void flush()
114{
115 begin(startIndex, length);
116}
117
120 */
121 int peek()
122{
123 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
124 uint8_t rx[8];
125 int16_t sendResult = _sw.sendPacket(tx, rx);
126 if (sendResult >= 0)
127 {
128 if ((rx[4] + 256 * rx[5]) > 0)
129 {
130 return(rx[3]);
131 }
132 }
133 return (-1);
134}
135
139 */
140
141 size_t write(uint8_t data)
142{
143 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES ,SW_LE16(startIndex),1,data,0x55,0x55,0x55 };
144 uint8_t rx[8];
145 int16_t sendResult = _sw.sendPacket(tx, rx);
146 if (sendResult >= 0)
147 {
148
149 return(rx[3]);
150 }
151 return (0);
152}
153
158 */
159
160 size_t write(uint16_t data)
161 {
162 if (availableForWrite() < 2) return 0;
163 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES ,SW_LE16(startIndex),2,SW_LE16(data),0x55,0x55};
164 uint8_t rx[8];
165 int16_t sendResult = _sw.sendPacket(tx, rx);
166 if (sendResult >= 0)
167 {
168
169 return(rx[3]);
170 }
171 return (0);
172 }
173
184 */
185
186 size_t write(uint16_t buffer[], size_t size)
187 {
188 return (write((const uint8_t*)buffer, size * 2));
189 }
190
201 */
202
203 size_t write(const uint16_t* buffer, size_t size)
204 {
205 return (write((const uint8_t*)buffer,size*2));
206 }
207
219 */
220 size_t write(const uint8_t* buffer, size_t size)
221{
222 uint8_t nextWriteSize;
223 uint16_t bytesWritten = 0;
224 uint32_t startTime = millis();
225
226 //Write up to the first 4 bytes
227 if (size >= 4)
228 {
229 nextWriteSize = 4;
230 }
231 else
232 {
233 nextWriteSize = (uint8_t)size;
234 }
235 {
236 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES ,SW_LE16(startIndex),0,0x55,0x55,0x55,0x55 };
237 uint8_t i;
238 for (i = 0; i < nextWriteSize; ++i)
239 {
240 tx[4 + i] = buffer[i];
241 }
242 tx[3] = nextWriteSize;
243 uint8_t rx[8];
244 int16_t sendResult = _sw.sendPacket(tx, rx);
245 if (sendResult < 0)
246 {
247 return(bytesWritten);
248 }
249 bytesWritten += rx[3];
250 }
251 while ((size - bytesWritten) >= 7)
252 {
253 yield();
254 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_7BYTES ,
255 buffer[bytesWritten],
256 buffer[bytesWritten + 1],
257 buffer[bytesWritten + 2],
258 buffer[bytesWritten + 3],
259 buffer[bytesWritten + 4],
260 buffer[bytesWritten + 5],
261 buffer[bytesWritten + 6] };
262
263 uint8_t rx[8];
264 int16_t sendResult = _sw.sendPacket(tx, rx);
265 if (sendResult < 0)
266 {
267 return(bytesWritten);
268 }
269 bytesWritten += rx[3];
270 delay(0);
271 if ((rx[3] == 0) && millis() >= startTime + _timeout) // Time out if couldn't write bytes and over timeout
272 {
273 return(bytesWritten);
274 }
275 }
276
277 while (size - bytesWritten > 0)
278 {
279 yield();
280 if (size - bytesWritten >= 4)
281 {
282 nextWriteSize = 4;
283 }
284 else
285 {
286 nextWriteSize = (uint8_t)(size - bytesWritten);
287 }
288 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES ,SW_LE16(startIndex),0,0x55,0x55,0x55,0x55 };
289 uint8_t i;
290 for (i = 0; i < nextWriteSize; ++i)
291 {
292 tx[4 + i] = buffer[i + bytesWritten];
293 }
294 tx[3] = nextWriteSize;
295 uint8_t rx[8];
296 int16_t sendResult = _sw.sendPacket(tx, rx);
297 if (sendResult < 0)
298 {
299 return(bytesWritten);
300 }
301 bytesWritten += rx[3];
302 if ( (rx[3] == 0) && millis() >= startTime + _timeout)
303 {
304 return(bytesWritten);
305 }
306 }
307 return (bytesWritten);
308}
309
313 */
314 int availableForWrite()
315{
316 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
317 uint8_t rx[8];
318 int16_t sendResult = _sw.sendPacket(tx, rx);
319 if (sendResult >= 0)
320 {
321 return (rx[6] + 256 * rx[7]);
322 }
323 return (0);
324}
325
335 */
336 size_t readBytes(char* buffer, size_t length)
337{
338 uint16_t bytesAvailable = 0;
339 uint32_t startTime = millis();
340
341 {
342 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
343 uint8_t rx[8];
344 int16_t sendResult = _sw.sendPacket(tx, rx);
345 if (sendResult >= 0)
346 {
347 bytesAvailable = (rx[4] + 256 * rx[5]);
348 }
349
350 if (bytesAvailable < length)
351 {
352 length = bytesAvailable;
353 }
354 }
355 uint16_t bytesRead = 0;
356 while (bytesRead < length)
357 {
358 yield();
359 uint8_t bytesToRead =(uint8_t)( length - bytesRead);
360 if (bytesToRead > 6)
361 {
362 bytesToRead = 6;
363 }
364 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_READ_BYTES ,SW_LE16(startIndex),bytesToRead,0x55,0x55,0x55,0x55 };
365 uint8_t rx[8];
366 int16_t sendResult = _sw.sendPacket(tx, rx);
367 if (sendResult >= 0)
368 {
369 uint8_t i;
370 for (i = 0; i < rx[1] && bytesRead < length; ++i)
371 {
372 buffer[bytesRead] = rx[2 + i];
373 ++bytesRead;
374 }
375
376 }
377 else
378 {
379 return (bytesRead);
380 }
381 if (millis() >= startTime + _timeout)
382 {
383 return(bytesRead);
384 }
385
386 }
387 return bytesRead;
388}
389
390
391 uint16_t startIndex = 0xFFFF;
392 uint16_t length = 0;
393
394 void setTimeout(long timeout_mS)
395{
396 _timeout = timeout_mS;
397}
398
408 */
409 size_t readUInt16(uint16_t* buffer, size_t length)
410 {
411 uint16_t bytesAvailable = 0;
412
413 uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
414 uint8_t rx[8];
415 int16_t sendResult = _sw.sendPacket(tx, rx);
416 if (sendResult >= 0)
417 {
418 bytesAvailable = (rx[4] + 256 * rx[5]);
419 }
420 uint16_t wordsAvailable = bytesAvailable /2;
421 if (wordsAvailable < length)
422 {
423 length = wordsAvailable ;
424 }
425 return readBytes((char*)buffer, length * 2) / 2;
426 }
427
428 //TODO add copy interface
429 private:
430 SerialWombatChip& _sw;
431
432
433 uint32_t _timeout = 500;
434};
SerialWombat.h
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition SerialWombat.h:46
SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION
@ COMMAND_BINARY_QUEUE_INFORMATION
(0x94)
Definition SerialWombat.h:215
SerialWombatCommands::COMMAND_BINARY_QUEUE_INITIALIZE
@ COMMAND_BINARY_QUEUE_INITIALIZE
(0x90)
Definition SerialWombat.h:211
SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES
@ COMMAND_BINARY_QUEUE_ADD_BYTES
(0x91)
Definition SerialWombat.h:212
SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_7BYTES
@ COMMAND_BINARY_QUEUE_ADD_7BYTES
(0x92)
Definition SerialWombat.h:213
SerialWombatCommands::COMMAND_BINARY_QUEUE_READ_BYTES
@ COMMAND_BINARY_QUEUE_READ_BYTES
(0x93)
Definition SerialWombat.h:214
SerialWombatQueueType
SerialWombatQueueType
Definition SerialWombatQueue.h:31
SerialWombatQueueType::QUEUE_TYPE_RAM_BYTE_SHIFT
@ QUEUE_TYPE_RAM_BYTE_SHIFT
A queue that queues byte-sized data in a queue in the User RAM area.
Definition SerialWombatQueue.h:33
SerialWombatQueueType::QUEUE_TYPE_RAM_BYTE
@ QUEUE_TYPE_RAM_BYTE
A queue that queues byte-sized data in a queue in the User RAM area.
Definition SerialWombatQueue.h:32
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:315
SerialWombatQueue::peek
int peek()
Query the Serial Wombat for the next avaialble byte, but don't remove it from the queue /.
Definition SerialWombatQueue.h:121
SerialWombatQueue::readUInt16
size_t readUInt16(uint16_t *buffer, size_t length)
Reads a specified number of unsigned 16 bit words from the Serial Wombat Queue /.
Definition SerialWombatQueue.h:409
SerialWombatQueue::readBytes
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the Serial Wombat Queue /.
Definition SerialWombatQueue.h:336
SerialWombatQueue::write
size_t write(const uint16_t *buffer, size_t size)
Write unsigned words to the Serial Wombat Queue /.
Definition SerialWombatQueue.h:203
SerialWombatQueue::available
int available()
Queries the Serial Wombat for number bytes available to read /.
Definition SerialWombatQueue.h:83
SerialWombatQueue::write
size_t write(uint8_t data)
Write a byte to the Serial Wombat Queue /.
Definition SerialWombatQueue.h:141
SerialWombatQueue::SerialWombatQueue
SerialWombatQueue(SerialWombatChip &serialWombat)
Constructor for SerialWombatWS2812 class /.
Definition SerialWombatQueue.h:48
SerialWombatQueue::setTimeout
void setTimeout(long timeout_mS)
Definition SerialWombatQueue.h:394
SerialWombatQueue::length
uint16_t length
Definition SerialWombatQueue.h:392
SerialWombatQueue::startIndex
uint16_t startIndex
Definition SerialWombatQueue.h:391
SerialWombatQueue::write
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the Serial Wombat Queue /.
Definition SerialWombatQueue.h:220
SerialWombatQueue::availableForWrite
int availableForWrite()
Queries the Serial Wombat for the amount of free queue space /.
Definition SerialWombatQueue.h:314
SerialWombatQueue::read
int read()
Reads a byte from the Serial Wombat /.
Definition SerialWombatQueue.h:98
SerialWombatQueue::write
size_t write(uint16_t buffer[], size_t size)
Write unsigned words to the Serial Wombat Queue /.
Definition SerialWombatQueue.h:186
SerialWombatQueue::flush
void flush()
Discard all received bytes.
Definition SerialWombatQueue.h:113
SerialWombatQueue::begin
int16_t begin(uint16_t index, uint16_t length, SerialWombatQueueType qtype=SerialWombatQueueType::QUEUE_TYPE_RAM_BYTE)
Initialize a Serial Wombat Queue (RAM Bytes) in User Memory Area on Serial Wombat Chip / /.
Definition SerialWombatQueue.h:62
SerialWombatQueue::write
size_t write(uint16_t data)
Write an unsigned word to the Serial Wombat Queue /.
Definition SerialWombatQueue.h:160