Serial Wombat Arduino Library
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 
34 };
35 
41 class SerialWombatQueue : public Stream
42 {
43 public:
47 
48  SerialWombatQueue(SerialWombatChip& serialWombat):_sw(serialWombat)
49  {
50  _sw = serialWombat;
51  }
52 
60 
62 {
63  startIndex = index;
64  length = length;
65  uint8_t tx[] = {(uint8_t) SerialWombatCommands::COMMAND_BINARY_QUEUE_INITIALIZE ,SW_LE16(index),SW_LE16(length),(uint8_t)qtype, 0x55,0x55 };
66  uint8_t rx[8];
67  int16_t result = _sw.sendPacket(tx,rx);
68  if (result < 0)
69  {
70  return result;
71  }
72  else
73  {
74  return ((int16_t)(rx[3] + 256 * rx[4]));
75  }
76 }
77 
81 
82  int available()
83 {
84  uint8_t tx[] = {(uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
85  uint8_t rx[8];
86  int16_t sendResult = _sw.sendPacket(tx,rx);
87  if (sendResult >= 0)
88  {
89  return (rx[4] + 256 * rx[5]);
90  }
91  return (0);
92 }
96 
97  int read()
98 {
99  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_READ_BYTES ,SW_LE16(startIndex),1,0x55,0x55,0x55,0x55 };
100  uint8_t rx[8];
101  int16_t sendResult = _sw.sendPacket(tx, rx);
102  if (sendResult >= 0)
103  {
104  if (rx[1] == 1)
105  return (rx[2]);
106  }
107  return (-1);
108 }
111 
112  void flush()
113 {
115 }
119 
120  int peek()
121 {
122  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
123  uint8_t rx[8];
124  int16_t sendResult = _sw.sendPacket(tx, rx);
125  if (sendResult >= 0)
126  {
127  if ((rx[4] + 256 * rx[5]) > 0)
128  {
129  return(rx[3]);
130  }
131  }
132  return (-1);
133 }
138 
140  size_t write(uint8_t data)
141 {
142  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES ,SW_LE16(startIndex),1,data,0x55,0x55,0x55 };
143  uint8_t rx[8];
144  int16_t sendResult = _sw.sendPacket(tx, rx);
145  if (sendResult >= 0)
146  {
147 
148  return(rx[3]);
149  }
150  return (0);
151 }
152 
164 
165  size_t write(const uint8_t* buffer, size_t size)
166 {
167  uint8_t nextWriteSize;
168  uint16_t bytesWritten = 0;
169  uint32_t startTime = millis();
170 
171  //Write up to the first 4 bytes
172  if (size >= 4)
173  {
174  nextWriteSize = 4;
175  }
176  else
177  {
178  nextWriteSize = (uint8_t)size;
179  }
180  {
181  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES ,SW_LE16(startIndex),0,0x55,0x55,0x55,0x55 };
182  uint8_t i;
183  for (i = 0; i < nextWriteSize; ++i)
184  {
185  tx[4 + i] = buffer[i];
186  }
187  tx[3] = nextWriteSize;
188  uint8_t rx[8];
189  int16_t sendResult = _sw.sendPacket(tx, rx);
190  if (sendResult < 0)
191  {
192  return(bytesWritten);
193  }
194  bytesWritten += rx[3];
195  }
196  while ((size - bytesWritten) >= 7)
197  {
198  yield();
200  buffer[bytesWritten],
201  buffer[bytesWritten + 1],
202  buffer[bytesWritten + 2],
203  buffer[bytesWritten + 3],
204  buffer[bytesWritten + 4],
205  buffer[bytesWritten + 5],
206  buffer[bytesWritten + 6] };
207 
208  uint8_t rx[8];
209  int16_t sendResult = _sw.sendPacket(tx, rx);
210  if (sendResult < 0)
211  {
212  return(bytesWritten);
213  }
214  bytesWritten += rx[3];
215  delay(0);
216  if (millis() > startTime + _timeout)
217  {
218  return(bytesWritten);
219  }
220  }
221 
222  while (size - bytesWritten > 0)
223  {
224  yield();
225  if (size - bytesWritten >= 4)
226  {
227  nextWriteSize = 4;
228  }
229  else
230  {
231  nextWriteSize = (uint8_t)(size - bytesWritten);
232  }
233  {
234  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES ,SW_LE16(startIndex),0,0x55,0x55,0x55,0x55 };
235  uint8_t i;
236  for (i = 0; i < nextWriteSize; ++i)
237  {
238  tx[4 + i] = buffer[i + bytesWritten];
239  }
240  tx[3] = nextWriteSize;
241  uint8_t rx[8];
242  int16_t sendResult = _sw.sendPacket(tx, rx);
243  if (sendResult < 0)
244  {
245  return(bytesWritten);
246  }
247  bytesWritten += rx[3];
248  }
249  if (millis() > startTime + _timeout)
250  {
251  return(bytesWritten);
252  }
253  }
254  return (bytesWritten);
255 }
256 
260 
262 {
263  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
264  uint8_t rx[8];
265  int16_t sendResult = _sw.sendPacket(tx, rx);
266  if (sendResult >= 0)
267  {
268  return (rx[6] + 256 * rx[7]);
269  }
270  return (0);
271 }
272 
282 
283  size_t readBytes(char* buffer, size_t length)
284 {
285  uint16_t bytesAvailable = 0;
286  uint32_t startTime = millis();
287 
288  {
289  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION ,SW_LE16(startIndex),0x55,0x55,0x55,0x55,0x55 };
290  uint8_t rx[8];
291  int16_t sendResult = _sw.sendPacket(tx, rx);
292  if (sendResult >= 0)
293  {
294  bytesAvailable = (rx[4] + 256 * rx[5]);
295  }
296 
297  if (bytesAvailable < length)
298  {
299  length = bytesAvailable;
300  }
301  }
302  uint16_t bytesRead = 0;
303  while (bytesRead < length)
304  {
305  yield();
306  uint8_t bytesToRead =(uint8_t)( length - bytesRead);
307  if (bytesToRead > 6)
308  {
309  bytesToRead = 6;
310  }
311  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_QUEUE_READ_BYTES ,SW_LE16(startIndex),bytesToRead,0x55,0x55,0x55,0x55 };
312  uint8_t rx[8];
313  int16_t sendResult = _sw.sendPacket(tx, rx);
314  if (sendResult >= 0)
315  {
316  uint8_t i;
317  for (i = 0; i < rx[1] && bytesRead < length; ++i)
318  {
319  buffer[bytesRead] = rx[2 + i];
320  ++bytesRead;
321  }
322 
323  }
324  else
325  {
326  return (bytesRead);
327  }
328  if (millis() > startTime + _timeout)
329  {
330  return(bytesRead);
331  }
332 
333  }
334  return bytesRead;
335 }
336 
337 
338  uint16_t startIndex = 0xFFFF;
339  uint16_t length = 0;
340 
341  void setTimeout(long timeout_mS)
342 {
343  _timeout = timeout_mS;
344 }
345 
346  //TODO add copy interface
347 private:
348  SerialWombatChip& _sw;
349 
350 
351  uint32_t _timeout = 500;
352 };
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:283
SerialWombatQueue::write
size_t write(uint8_t data)
Write a byte to the Serial Wombat Queue /.
Definition: SerialWombatQueue.h:140
SerialWombatQueue::setTimeout
void setTimeout(long timeout_mS)
Definition: SerialWombatQueue.h:341
SerialWombatQueueType::QUEUE_TYPE_RAM_BYTE
@ QUEUE_TYPE_RAM_BYTE
A queue that queues byte-sized data in a queue in the User RAM area.
SerialWombatCommands::COMMAND_BINARY_QUEUE_INFORMATION
@ COMMAND_BINARY_QUEUE_INFORMATION
(0x94)
SerialWombatQueue::startIndex
uint16_t startIndex
Definition: SerialWombatQueue.h:338
SerialWombatQueue::length
uint16_t length
Definition: SerialWombatQueue.h:339
SerialWombatQueue::flush
void flush()
Discard all received bytes.
Definition: SerialWombatQueue.h:112
SerialWombatCommands::COMMAND_BINARY_QUEUE_READ_BYTES
@ COMMAND_BINARY_QUEUE_READ_BYTES
(0x93)
SerialWombat.h
SerialWombatQueue::read
int read()
Reads a byte from the Serial Wombat /.
Definition: SerialWombatQueue.h:97
SerialWombatQueue::SerialWombatQueue
SerialWombatQueue(SerialWombatChip &serialWombat)
Constructor for SerialWombatWS2812 class /.
Definition: SerialWombatQueue.h:48
SerialWombatQueue::write
size_t write(const uint8_t *buffer, size_t size)
Write bytes to the Serial Wombat Queue /.
Definition: SerialWombatQueue.h:165
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:61
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.
SerialWombatChip::sendPacket
int sendPacket(uint8_t tx[], uint8_t rx[])
Send an 8 byte packet to the Serial Wombat chip and wait for 8 bytes back.
Definition: SerialWombat.cpp:114
SerialWombatQueueType
SerialWombatQueueType
Definition: SerialWombatQueue.h:31
SerialWombatQueue::readBytes
size_t readBytes(char *buffer, size_t length)
Reads a specified number of bytes from the Serial Wombat Queue /.
Definition: SerialWombatQueue.h:283
SerialWombatQueue::available
int available()
Queries the Serial Wombat for number bytes available to read /.
Definition: SerialWombatQueue.h:82
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition: SerialWombat.h:41
SerialWombatQueue::peek
int peek()
Query the Serial Wombat for the next avaialble byte, but don't remove it from the queue /.
Definition: SerialWombatQueue.h:120
SerialWombatQueue
A Class representing a Queue in the User Ram area on the Serial Wombat Chip.
Definition: SerialWombatQueue.h:41
SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_7BYTES
@ COMMAND_BINARY_QUEUE_ADD_7BYTES
(0x92)
SerialWombatQueue::availableForWrite
int availableForWrite()
Queries the Serial Wombat for the amount of free queue space /.
Definition: SerialWombatQueue.h:261
SerialWombatCommands::COMMAND_BINARY_QUEUE_INITIALIZE
@ COMMAND_BINARY_QUEUE_INITIALIZE
(0x90)
SerialWombatCommands::COMMAND_BINARY_QUEUE_ADD_BYTES
@ COMMAND_BINARY_QUEUE_ADD_BYTES
(0x91)