Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatWS2812.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2021-2025 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
27 enum SWWS2812Mode {
28 ws2812ModeBuffered = 0,
29 ws2812ModeAnimation = 1,
30 ws2812ModeChase = 2,
31};
32
87
88 class SerialWombatWS2812 : public SerialWombatPin
89{
90 public:
95 SerialWombatWS2812(SerialWombatChip& serialWombat):SerialWombatPin(serialWombat)
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 if (swapRG)
135 {
136 uint8_t x = tx[6];
137 tx[6] = tx[5];
138 tx[5] = x;
139 }
140 tx[7] = 0x55;
141 return _sw.sendPacket(tx);
142 }
143
145 int16_t write(uint8_t led, int16_t color)
146 {
147 return write(led, (uint32_t)color);
148 }
149
151 int16_t write(uint8_t led, int32_t color)
152 {
153 return write(led, (uint32_t)color);
154 }
155
156 /*
157 \brief Set a number of LEDs to colors based on an array of uint32_t colors
158
159 \param led The index of the first led to set
160 \param length The number of LEDs to set, and the number of entires in colors array
161 \param An array of uint32_t integer colors in the format 0x00RRGGBB format
162 \return 0 or higher for success or a negative number indicating an error code from the Serial Wombat chip.
163 */
164 int16_t write(uint8_t led, uint8_t length, uint32_t colors[])
165 {
166 for (int i = 0; i < length; ++i)
167 {
168 int16_t result =
169 write(led + i, colors[i]);
170
171 if (result < 0)
172 {
173 return (result);
174 }
175
176 }
177 return(0);
178 }
179
180 /*
181 \brief set the color of one LED in an animation frame
182
183 \param frame The Frame index of the color being set
184 \param led The LED index in that frame of the color being set
185 \param color The color of the LED in 0x00RRGGBB format
186 \return 0 or higher for success or a negative number indicating an error code from the Serial Wombat chip.
187 */
188 int16_t writeAnimationLED(uint8_t frame, uint8_t led, uint32_t color)
189 {
190 uint8_t tx[8] = { 203,_pin,12,frame,led,(uint8_t)((color >>16 ) & 0xFF),(uint8_t)((color >> 8) & 0xFF),(uint8_t)( color & 0xFF) };
191 return _sw.sendPacket(tx);
192 }
193
195 int16_t writeAnimationLED(uint8_t frame, uint8_t led, int16_t color)
196 {
197 return writeAnimationLED(frame, led,(uint32_t)color);
198 }
199
201 int16_t writeAnimationLED(uint8_t frame, uint8_t led, int32_t color)
202 {
203 return writeAnimationLED(frame, led, (uint32_t)color);
204 }
205
213 int16_t writeAnimationFrame(uint8_t frame, uint32_t colors[])
214 {
215 for (int i = 0; i < _numLEDS; ++i)
216 {
217 int16_t result;
218 result = writeAnimationLED(frame, i, colors[i]);
219 if (result < 0)
220 {
221 return (result);
222 }
223 }
224 return(0);
225
226 }
227
234 int16_t writeAnimationFrameDelay(uint8_t frame, uint16_t delay_mS)
235 {
236 uint8_t tx[8] = { 205,_pin,12,frame,SW_LE16(delay_mS),0x55,0x55 };
237 return (_sw.sendPacket(tx));
238 }
239
247 int16_t writeAnimationUserBufferIndex(uint16_t index, uint8_t numberOfFrames)
248 {
249 uint8_t tx[8] = { 204,_pin,12,SW_LE16(index),numberOfFrames,0x55,0x55 };
250 return (_sw.sendPacket(tx));
251 }
252
259 int16_t readBufferSize()
260 {
261 uint8_t tx[8] = { 202,_pin,12,_numLEDS,0x55,0x55,0x55,0x55 };
262 uint8_t rx[8];
263 int16_t result = _sw.sendPacket(tx,rx);
264 if (result >= 0)
265 {
266 return (rx[3] + rx[4] * 256);
267 }
268 else
269 {
270 return (result);
271 }
272
273 return int16_t();
274 }
275
276
281 int16_t writeMode(SWWS2812Mode mode)
282 {
283 uint8_t tx[8] = { 206,_pin,12,(uint8_t)mode,0x55,0x55,0x55,0x55 };
284 return _sw.sendPacket(tx);
285 }
286
296 int16_t barGraph(uint8_t sourcePin, uint32_t offRGB, uint32_t onRGB, uint16_t min, uint16_t max)
297 {
298 uint8_t tx[8] = { 206,_pin,12,3,sourcePin,0x55,0x55,0x55 };
299 int16_t result = 0;
300 result = _sw.sendPacket(tx); if (result < 0) { return result; }
301 result = write(0, offRGB); if (result < 0) { return result; }
302 result = write(1, onRGB); if (result < 0) { return result; }
303
304 uint8_t minMax[8] = { 207,_pin,12,SW_LE16(min), SW_LE16(max),0x55 };
305 return _sw.sendPacket(minMax);
306
307
308 }
309
313 bool swapRG = false;
314 private:
315 uint8_t _numLEDS = 0;
316 uint16_t _userBufferIndex=0;
317};
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
SW_LE32
#define SW_LE32(_a)
Convert a uint32_t to four bytes in little endian format for array initialization.
Definition SerialWombat.h:49
SWWS2812Mode
SWWS2812Mode
Definition SerialWombatWS2812.h:27
ws2812ModeAnimation
@ ws2812ModeAnimation
Multiple arrays with delays are uploaded by the host and displayed over time by the Serial Wombat chi...
Definition SerialWombatWS2812.h:29
ws2812ModeBuffered
@ ws2812ModeBuffered
Standard buffered mode. Colors are uploaded by the host.
Definition SerialWombatWS2812.h:28
ws2812ModeChase
@ ws2812ModeChase
A single lit LED cycles through all of the LEDs.
Definition SerialWombatWS2812.h:30
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:313
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
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:201
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:145
SerialWombatWS2812::SerialWombatWS2812
SerialWombatWS2812(SerialWombatChip &serialWombat)
Constructor for SerialWombatWS2812 class.
Definition SerialWombatWS2812.h:95
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:247
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:195
SerialWombatWS2812::swapRG
bool swapRG
Swap the Red and Green byte values. Set this to true for WS2811 chips which reverse the red and green...
Definition SerialWombatWS2812.h:313
SerialWombatWS2812::write
int16_t write(uint8_t led, uint8_t length, uint32_t colors[])
Definition SerialWombatWS2812.h:164
SerialWombatWS2812::writeAnimationFrame
int16_t writeAnimationFrame(uint8_t frame, uint32_t colors[])
Store an array of colors for an entire animation frame.
Definition SerialWombatWS2812.h:213
SerialWombatWS2812::readBufferSize
int16_t readBufferSize()
returns the number of bytes of UserBuffer required to service the configured number of LEDs
Definition SerialWombatWS2812.h:259
SerialWombatWS2812::writeMode
int16_t writeMode(SWWS2812Mode mode)
Sets the mode of the WS2812 LED Driver.
Definition SerialWombatWS2812.h:281
SerialWombatWS2812::write
int16_t write(uint8_t led, uint32_t color)
Set an LED color.
Definition SerialWombatWS2812.h:131
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:296
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::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:151
SerialWombatWS2812::writeAnimationLED
int16_t writeAnimationLED(uint8_t frame, uint8_t led, uint32_t color)
Definition SerialWombatWS2812.h:188
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:234