Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatWS2812.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2021-2026 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
157 int16_t write(int led, int color)
158 {
159 return write((uint8_t)led, (uint32_t)color);
160 }
161
162 /*
163 \brief Set a number of LEDs to colors based on an array of uint32_t colors
164
165 \param led The index of the first led to set
166 \param length The number of LEDs to set, and the number of entires in colors array
167 \param An array of uint32_t integer colors in the format 0x00RRGGBB format
168 \return 0 or higher for success or a negative number indicating an error code from the Serial Wombat chip.
169 */
170 int16_t write(uint8_t led, uint8_t length, uint32_t colors[])
171 {
172 for (int i = 0; i < length; ++i)
173 {
174 int16_t result =
175 write((uint8_t)(led + i),(uint32_t) colors[i]);
176
177 if (result < 0)
178 {
179 return (result);
180 }
181
182 }
183 return(0);
184 }
185
186 /*
187 \brief set the color of one LED in an animation frame
188
189 \param frame The Frame index of the color being set
190 \param led The LED index in that frame of the color being set
191 \param color The color of the LED in 0x00RRGGBB format
192 \return 0 or higher for success or a negative number indicating an error code from the Serial Wombat chip.
193 */
194 int16_t writeAnimationLED(uint8_t frame, uint8_t led, uint32_t color)
195 {
196 uint8_t tx[8] = { 203,_pin,12,frame,led,(uint8_t)((color >>16 ) & 0xFF),(uint8_t)((color >> 8) & 0xFF),(uint8_t)( color & 0xFF) };
197 return _sw.sendPacket(tx);
198 }
199
201 int16_t writeAnimationLED(uint8_t frame, uint8_t led, int16_t color)
202 {
203 return writeAnimationLED(frame, led,(uint32_t)color);
204 }
205
207 int16_t writeAnimationLED(uint8_t frame, uint8_t led, int32_t color)
208 {
209 return writeAnimationLED(frame, led, (uint32_t)color);
210 }
211
219 int16_t writeAnimationFrame(uint8_t frame, uint32_t colors[])
220 {
221 for (int i = 0; i < _numLEDS; ++i)
222 {
223 int16_t result;
224 result = writeAnimationLED(frame, i, colors[i]);
225 if (result < 0)
226 {
227 return (result);
228 }
229 }
230 return(0);
231
232 }
233
240 int16_t writeAnimationFrameDelay(uint8_t frame, uint16_t delay_mS)
241 {
242 uint8_t tx[8] = { 205,_pin,12,frame,SW_LE16(delay_mS),0x55,0x55 };
243 return (_sw.sendPacket(tx));
244 }
245
253 int16_t writeAnimationUserBufferIndex(uint16_t index, uint8_t numberOfFrames)
254 {
255 uint8_t tx[8] = { 204,_pin,12,SW_LE16(index),numberOfFrames,0x55,0x55 };
256 return (_sw.sendPacket(tx));
257 }
258
265 int16_t readBufferSize()
266 {
267 uint8_t tx[8] = { 202,_pin,12,_numLEDS,0x55,0x55,0x55,0x55 };
268 uint8_t rx[8];
269 int16_t result = _sw.sendPacket(tx,rx);
270 if (result >= 0)
271 {
272 return (rx[3] + rx[4] * 256);
273 }
274 else
275 {
276 return (result);
277 }
278
279 return int16_t();
280 }
281
282
287 int16_t writeMode(SWWS2812Mode mode)
288 {
289 uint8_t tx[8] = { 206,_pin,12,(uint8_t)mode,0x55,0x55,0x55,0x55 };
290 return _sw.sendPacket(tx);
291 }
292
302 int16_t barGraph(uint8_t sourcePin, uint32_t offRGB, uint32_t onRGB, uint16_t min, uint16_t max)
303 {
304 uint8_t tx[8] = { 206,_pin,12,3,sourcePin,0x55,0x55,0x55 };
305 int16_t result = 0;
306 result = _sw.sendPacket(tx); if (result < 0) { return result; }
307 result = write((uint8_t)0,(uint32_t) offRGB); if (result < 0) { return result; }
308 result = write((uint8_t)1, (uint32_t)onRGB); if (result < 0) { return result; }
309
310 uint8_t minMax[8] = { 207,_pin,12,SW_LE16(min), SW_LE16(max),0x55 };
311 return _sw.sendPacket(minMax);
312
313
314 }
315
325 int16_t barGraph(SerialWombatDataSource sourcePin, uint32_t offRGB, uint32_t onRGB, uint16_t min, uint16_t max)
326 {
327 return barGraph((uint8_t) sourcePin, offRGB, onRGB, min, max);
328 }
329
332 bool swapRG = false;
333 private:
334 uint8_t _numLEDS = 0;
335 uint16_t _userBufferIndex=0;
336};
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
SerialWombatDataSource
SerialWombatDataSource
A list of Serial Wombat public data sources.
Definition SerialWombat.h:65
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:315
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:207
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:253
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:201
SerialWombatWS2812::write
int16_t write(int led, int color)
An overload for Write in case write(x,0); is interpreted as an int32_t rather than uint32_t.
Definition SerialWombatWS2812.h:157
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:332
SerialWombatWS2812::write
int16_t write(uint8_t led, uint8_t length, uint32_t colors[])
Definition SerialWombatWS2812.h:170
SerialWombatWS2812::barGraph
int16_t barGraph(SerialWombatDataSource sourcePin, uint32_t offRGB, uint32_t onRGB, uint16_t min, uint16_t max)
Display a bargraph using the configured ws2812 class.
Definition SerialWombatWS2812.h:325
SerialWombatWS2812::writeAnimationFrame
int16_t writeAnimationFrame(uint8_t frame, uint32_t colors[])
Store an array of colors for an entire animation frame.
Definition SerialWombatWS2812.h:219
SerialWombatWS2812::readBufferSize
int16_t readBufferSize()
returns the number of bytes of UserBuffer required to service the configured number of LEDs
Definition SerialWombatWS2812.h:265
SerialWombatWS2812::writeMode
int16_t writeMode(SWWS2812Mode mode)
Sets the mode of the WS2812 LED Driver.
Definition SerialWombatWS2812.h:287
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:302
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:194
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:240