Serial Wombat Arduino Library
SerialWombatWS2812.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 Copyright 2021 Broadwell Consulting Inc.
4 
5 Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdint.h>
24 #include "SerialWombat.h"
25 
26 
31 };
32 
89 {
90 public:
96  {
97  }
98 
115  int16_t begin(uint8_t pin, uint8_t numberOfLEDs, uint16_t userBufferIndex)
116  {
117  _pin = pin;
118  _numLEDS = numberOfLEDs;
119  _userBufferIndex = userBufferIndex;
120 
121  uint8_t tx[8] = { 200,_pin,12,SW_LE16(userBufferIndex),_numLEDS,0x55 };
122  return (_sw.sendPacket(tx));
123  }
124 
131  int16_t write(uint8_t led, uint32_t color)
132  {
133  uint8_t tx[8] = { 201,_pin,12,led,SW_LE32(color) };
134  tx[7] = 0x55;
135  return _sw.sendPacket(tx);
136  }
137 
139  int16_t write(uint8_t led, int16_t color)
140  {
141  return write(led, (uint32_t)color);
142  }
143 
145  int16_t write(uint8_t led, int32_t color)
146  {
147  return write(led, (uint32_t)color);
148  }
149 
150  /*
151  \brief Set a number of LEDs to colors based on an array of uint32_t colors
152 
153  \param led The index of the first led to set
154  \param length The number of LEDs to set, and the number of entires in colors array
155  \param An array of uint32_t integer colors in the format 0x00RRGGBB format
156  \return 0 or higher for success or a negative number indicating an error code from the Serial Wombat chip.
157  */
158  int16_t write(uint8_t led, uint8_t length, uint32_t colors[])
159  {
160  for (int i = 0; i < length; ++i)
161  {
162  int16_t result =
163  write(led + i, colors[i]);
164 
165  if (result < 0)
166  {
167  return (result);
168  }
169 
170  }
171  return(0);
172  }
173 
174  /*
175  \brief set the color of one LED in an animation frame
176 
177  \param frame The Frame index of the color being set
178  \param led The LED index in that frame of the color being set
179  \param color The color of the LED in 0x00RRGGBB format
180  \return 0 or higher for success or a negative number indicating an error code from the Serial Wombat chip.
181  */
182  int16_t writeAnimationLED(uint8_t frame, uint8_t led, uint32_t color)
183  {
184  uint8_t tx[8] = { 203,_pin,12,frame,led,(uint8_t)((color >>16 ) & 0xFF),(uint8_t)((color >> 8) & 0xFF),(uint8_t)( color & 0xFF) };
185  return _sw.sendPacket(tx);
186  }
187 
189  int16_t writeAnimationLED(uint8_t frame, uint8_t led, int16_t color)
190  {
191  return writeAnimationLED(frame, led,(uint32_t)color);
192  }
193 
195  int16_t writeAnimationLED(uint8_t frame, uint8_t led, int32_t color)
196  {
197  return writeAnimationLED(frame, led, (uint32_t)color);
198  }
199 
207  int16_t writeAnimationFrame(uint8_t frame, uint32_t colors[])
208  {
209  for (int i = 0; i < _numLEDS; ++i)
210  {
211  int16_t result;
212  result = writeAnimationLED(frame, i, colors[i]);
213  if (result < 0)
214  {
215  return (result);
216  }
217  }
218  return(0);
219 
220  }
221 
228  int16_t writeAnimationFrameDelay(uint8_t frame, uint16_t delay_mS)
229  {
230  uint8_t tx[8] = { 205,_pin,12,frame,SW_LE16(delay_mS),0x55,0x55 };
231  return (_sw.sendPacket(tx));
232  }
233 
241  int16_t writeAnimationUserBufferIndex(uint16_t index, uint8_t numberOfFrames)
242  {
243  uint8_t tx[8] = { 204,_pin,12,SW_LE16(index),numberOfFrames,0x55,0x55 };
244  return (_sw.sendPacket(tx));
245  }
246 
253  int16_t readBufferSize()
254  {
255  uint8_t tx[8] = { 202,_pin,12,_numLEDS,0x55,0x55,0x55,0x55 };
256  uint8_t rx[8];
257  int16_t result = _sw.sendPacket(tx,rx);
258  if (result >= 0)
259  {
260  return (rx[3] + rx[4] * 256);
261  }
262  else
263  {
264  return (result);
265  }
266 
267  return int16_t();
268  }
269 
270 
275  int16_t writeMode(SWWS2812Mode mode)
276  {
277  uint8_t tx[8] = { 206,_pin,12,(uint8_t)mode,0x55,0x55,0x55,0x55 };
278  return _sw.sendPacket(tx);
279  }
280 
290  int16_t barGraph(uint8_t sourcePin, uint32_t offRGB, uint32_t onRGB, uint16_t min, uint16_t max)
291  {
292  uint8_t tx[8] = { 206,_pin,12,3,sourcePin,0x55,0x55,0x55 };
293  int16_t result = 0;
294  result = _sw.sendPacket(tx); if (result < 0) { return result; }
295  result = write(0, offRGB); if (result < 0) { return result; }
296  result = write(1, onRGB); if (result < 0) { return result; }
297 
298  uint8_t minMax[8] = { 207,_pin,12,SW_LE16(min), SW_LE16(max),0x55 };
299  return _sw.sendPacket(minMax);
300 
301 
302  }
303 private:
304  uint8_t _numLEDS = 0;
305  uint16_t _userBufferIndex=0;
306 };
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:283
ws2812ModeChase
@ ws2812ModeChase
A single lit LED cycles through all of the LEDs.
Definition: SerialWombatWS2812.h:30
SWWS2812Mode
SWWS2812Mode
Definition: SerialWombatWS2812.h:27
SerialWombatWS2812::write
int16_t write(uint8_t led, uint8_t length, uint32_t colors[])
Definition: SerialWombatWS2812.h:158
ws2812ModeBuffered
@ ws2812ModeBuffered
Standard buffered mode. Colors are uploaded by the host.
Definition: SerialWombatWS2812.h:28
SerialWombatWS2812::writeAnimationLED
int16_t writeAnimationLED(uint8_t frame, uint8_t led, uint32_t color)
Definition: SerialWombatWS2812.h:182
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition: SerialWombatPin.h:134
SerialWombatWS2812::write
int16_t write(uint8_t led, uint32_t color)
Set an LED color.
Definition: SerialWombatWS2812.h:131
SerialWombatWS2812::writeAnimationLED
int16_t writeAnimationLED(uint8_t frame, uint8_t led, int32_t color)
An overload color is interpreted as an int32_t rather than uint32_t.
Definition: SerialWombatWS2812.h:195
SerialWombatWS2812::barGraph
int16_t barGraph(uint8_t sourcePin, uint32_t offRGB, uint32_t onRGB, uint16_t min, uint16_t max)
Display a bargraph using the configured ws2812 class.
Definition: SerialWombatWS2812.h:290
SerialWombatWS2812::writeAnimationLED
int16_t writeAnimationLED(uint8_t frame, uint8_t led, int16_t color)
An overload color is interpreted as an int16_t rather than uint32_t.
Definition: SerialWombatWS2812.h:189
SerialWombat.h
SerialWombatWS2812::writeAnimationUserBufferIndex
int16_t writeAnimationUserBufferIndex(uint16_t index, uint8_t numberOfFrames)
set the location in UserBuffer where the animation array will be stored and number of frames
Definition: SerialWombatWS2812.h:241
SerialWombatWS2812::writeAnimationFrame
int16_t writeAnimationFrame(uint8_t frame, uint32_t colors[])
Store an array of colors for an entire animation frame.
Definition: SerialWombatWS2812.h:207
SW_LE32
#define SW_LE32(_a)
Convert a uint32_t to four bytes in little endian format for array initialization.
Definition: SerialWombat.h:44
ws2812ModeAnimation
@ ws2812ModeAnimation
Multiple arrays with delays are uploaded by the host and displayed over time by the Serial Wombat chi...
Definition: SerialWombatWS2812.h:29
SerialWombatWS2812::write
int16_t write(uint8_t led, int16_t color)
An overload for Write in case write(x,0); is interpreted as an int16_t rather than uint32_t.
Definition: SerialWombatWS2812.h:139
SerialWombatPin
Describes a Serial Wombat Pin. Is base class for other pin modes.
Definition: SerialWombatPin.h:38
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
SerialWombatWS2812::SerialWombatWS2812
SerialWombatWS2812(SerialWombatChip &serialWombat)
Constructor for SerialWombatWS2812 class.
Definition: SerialWombatWS2812.h:95
SerialWombatWS2812
A Class representing a WS2812 or compatible RGB LED string connected to a Serial Wombat pin.
Definition: SerialWombatWS2812.h:88
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition: SerialWombatPin.h:121
SerialWombatPin::_pin
uint8_t _pin
Definition: SerialWombatPin.h:133
SerialWombatWS2812::write
int16_t write(uint8_t led, int32_t color)
An overload for Write in case write(x,0); is interpreted as an int32_t rather than uint32_t.
Definition: SerialWombatWS2812.h:145
SerialWombatWS2812::writeMode
int16_t writeMode(SWWS2812Mode mode)
Sets the mode of the WS2812 LED Driver.
Definition: SerialWombatWS2812.h:275
SerialWombatWS2812::readBufferSize
int16_t readBufferSize()
returns the number of bytes of UserBuffer required to service the configured number of LEDs
Definition: SerialWombatWS2812.h:253
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition: SerialWombat.h:41
SerialWombatWS2812::begin
int16_t begin(uint8_t pin, uint8_t numberOfLEDs, uint16_t userBufferIndex)
Initialize a WS2812 LED driver object.
Definition: SerialWombatWS2812.h:115
SerialWombatWS2812::writeAnimationFrameDelay
int16_t writeAnimationFrameDelay(uint8_t frame, uint16_t delay_mS)
Set how long an animation frame should be displayed before moving to the next frame.
Definition: SerialWombatWS2812.h:228