Skip to content

Commit

Permalink
Added WS2812FXT class for effect transitions, and other minor bug fix…
Browse files Browse the repository at this point in the history
…es. See the change log. (#291)
  • Loading branch information
moose4lord authored Aug 30, 2021
1 parent 6f5783d commit d3047cc
Show file tree
Hide file tree
Showing 12 changed files with 651 additions and 88 deletions.
18 changes: 14 additions & 4 deletions examples/esp8266_webinterface/esp8266_webinterface.ino
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@
2018-01-06 added custom effects list option and auto-cycle feature
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#ifdef ARDUINO_ARCH_ESP32
#include <WiFi.h>
#include <WebServer.h>
#define WEB_SERVER WebServer
#define ESP_RESET ESP.restart()
#else
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#define WEB_SERVER ESP8266WebServer
#define ESP_RESET ESP.reset()
#endif

#include <WS2812FX.h>

extern const char index_html[];
Expand Down Expand Up @@ -78,7 +88,7 @@ uint8_t myModes[] = {}; // *** optionally create a custom list of effect/mode nu
bool auto_cycle = false;

WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
ESP8266WebServer server(HTTP_PORT);
WEB_SERVER server(HTTP_PORT);

void setup(){
Serial.begin(115200);
Expand Down Expand Up @@ -171,7 +181,7 @@ void wifi_setup() {
Serial.print("Tried ");
Serial.print(WIFI_TIMEOUT);
Serial.print("ms. Resetting ESP now.");
ESP.reset();
ESP_RESET;
}
}

Expand Down
46 changes: 23 additions & 23 deletions examples/serial_control/serial_control.ino
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

char cmd[MAX_NUM_CHARS]; // char[] to store incoming serial commands
bool cmd_complete = false; // whether the command string is complete
char scmd[MAX_NUM_CHARS]; // char[] to store incoming serial commands
bool scmd_complete = false; // whether the command string is complete

void setup() {
Serial.begin(115200);
Expand All @@ -70,7 +70,7 @@ void loop() {

recvChar(); // read serial comm

if(cmd_complete) {
if(scmd_complete) {
process_command();
}
}
Expand All @@ -79,62 +79,62 @@ void loop() {
* Checks received command and calls corresponding functions.
*/
void process_command() {
if (strcmp(cmd,"b+") == 0) {
if (strcmp(scmd,"b+") == 0) {
ws2812fx.increaseBrightness(25);
Serial.print(F("Increased brightness by 25 to: "));
Serial.println(ws2812fx.getBrightness());
}

if (strcmp(cmd,"b-") == 0) {
if (strcmp(scmd,"b-") == 0) {
ws2812fx.decreaseBrightness(25);
Serial.print(F("Decreased brightness by 25 to: "));
Serial.println(ws2812fx.getBrightness());
}

if (strncmp(cmd,"b ",2) == 0) {
uint8_t b = (uint8_t)atoi(cmd + 2);
if (strncmp(scmd,"b ",2) == 0) {
uint8_t b = (uint8_t)atoi(scmd + 2);
ws2812fx.setBrightness(b);
Serial.print(F("Set brightness to: "));
Serial.println(ws2812fx.getBrightness());
}

if (strcmp(cmd,"s+") == 0) {
if (strcmp(scmd,"s+") == 0) {
ws2812fx.setSpeed(ws2812fx.getSpeed() * 1.2);
Serial.print(F("Increased speed by 20% to: "));
Serial.println(ws2812fx.getSpeed());
}

if (strcmp(cmd,"s-") == 0) {
if (strcmp(scmd,"s-") == 0) {
ws2812fx.setSpeed(ws2812fx.getSpeed() * 0.8);
Serial.print(F("Decreased speed by 20% to: "));
Serial.println(ws2812fx.getSpeed());
}

if (strncmp(cmd,"s ",2) == 0) {
uint16_t s = (uint16_t)atoi(cmd + 2);
if (strncmp(scmd,"s ",2) == 0) {
uint16_t s = (uint16_t)atoi(scmd + 2);
ws2812fx.setSpeed(s);
Serial.print(F("Set speed to: "));
Serial.println(ws2812fx.getSpeed());
}

if (strncmp(cmd,"m ",2) == 0) {
uint8_t m = (uint8_t)atoi(cmd + 2);
if (strncmp(scmd,"m ",2) == 0) {
uint8_t m = (uint8_t)atoi(scmd + 2);
ws2812fx.setMode(m);
Serial.print(F("Set mode to: "));
Serial.print(ws2812fx.getMode());
Serial.print(" - ");
Serial.println(ws2812fx.getModeName(ws2812fx.getMode()));
}

if (strncmp(cmd,"c ",2) == 0) {
uint32_t c = (uint32_t)strtoul(cmd + 2, NULL, 16);
if (strncmp(scmd,"c ",2) == 0) {
uint32_t c = (uint32_t)strtoul(scmd + 2, NULL, 16);
ws2812fx.setColor(c);
Serial.print(F("Set color to: 0x"));
Serial.println(ws2812fx.getColor(), HEX);
}

cmd[0] = '\0'; // reset the commandstring
cmd_complete = false; // reset command complete
scmd[0] = '\0'; // reset the commandstring
scmd_complete = false; // reset command complete
}

/*
Expand Down Expand Up @@ -179,19 +179,19 @@ void printModes() {


/*
* Reads new input from serial to cmd string. Command is completed on \n
* Reads new input from serial to scmd string. Command is completed on \n
*/
void recvChar(void) {
static byte index = 0;
while (Serial.available() > 0 && cmd_complete == false) {
while (Serial.available() > 0 && scmd_complete == false) {
char rc = Serial.read();
if (rc != '\n') {
if(index < MAX_NUM_CHARS) cmd[index++] = rc;
if(index < MAX_NUM_CHARS) scmd[index++] = rc;
} else {
cmd[index] = '\0'; // terminate the string
scmd[index] = '\0'; // terminate the string
index = 0;
cmd_complete = true;
Serial.print("received '"); Serial.print(cmd); Serial.println("'");
scmd_complete = true;
Serial.print("received '"); Serial.print(scmd); Serial.println("'");
}
}
}
87 changes: 87 additions & 0 deletions examples/ws2812fx_teensy/ws2812fx_teensy.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
This sketch is a modification of the ws2812fx_dma sketch, which uses
Paul Stoffregen's WS2812Serial library (instead of Michael Miller's
NeoPixelBus library) to enable the Teensy microcontroller's DMA to
drive the LEDs.
Note the WS2812Serial lib isn't installable from the Teensyduino IDE's
Manage Libraries menu, but instead must be installed using the
Sketch -> Include Library -> Add .ZIP Library after downloading
WS2812Serial from Paul's GitHub page:
https://github.com/PaulStoffregen/WS2812Serial
Keith Lord - 2021
LICENSE
The MIT License (MIT)
Copyright (c) 2021 Keith Lord
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
CHANGELOG
2021-08-20 initial version
*/

#include <WS2812FX.h>
#include <WS2812Serial.h>

// Usable pins:
// Teensy LC: 1, 4, 5, 24
// Teensy 3.2: 1, 5, 8, 10, 31 (overclock to 120 MHz for pin 8)
// Teensy 3.5: 1, 5, 8, 10, 26, 32, 33, 48
// Teensy 3.6: 1, 5, 8, 10, 26, 32, 33
// Teensy 4.0: 1, 8, 14, 17, 20, 24, 29, 39
// Teensy 4.1: 1, 8, 14, 17, 20, 24, 29, 35, 47, 53
#define LED_PIN 1 // digital pin used to drive the LED strip
#define LED_COUNT 144 // number of LEDs on the strip

// allocate a display buffer for WS2812Serial
DMAMEM byte displayMemory[LED_COUNT * 12]; // 12 bytes per RGB LED or 16 bytes per RGBW LED

WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

WS2812Serial *ws2812serialRef;

void setup() {
Serial.begin(115200);

ws2812fx.init();
ws2812fx.setBrightness(32);

const uint32_t colors[] = {GREEN, BLACK, BLACK};
ws2812fx.setSegment(0, 0, LED_COUNT-1, FX_MODE_LARSON_SCANNER, colors, 5000, NO_OPTIONS);

ws2812fx.setCustomShow(myCustomShow); // set the custom show function
ws2812fx.start();

// Instantiate and init the WS2812Serial instance last, so the LED_PIN pin is configured properly.
// For the WS2812Serial instance always use WS2812_BGR, since it's sense of color byte order seems backwards.
ws2812serialRef = new WS2812Serial(LED_COUNT, displayMemory, ws2812fx.getPixels(), LED_PIN, WS2812_BGR);
ws2812serialRef->begin();
}

void loop() {
ws2812fx.service();
}

void myCustomShow(void) {
ws2812serialRef->show(); // run the WS2812Serial show() function
}
84 changes: 84 additions & 0 deletions examples/ws2812fx_transitions/ws2812fx_transitions.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
A demo of effect transitions.
A new class WS2812FXT (note the 'T' at the end) provides the ability to
create effect transitions. Note, WS2812FXT essentially creates THREE
WS2812FX instances to do it's work, so uses considerably more memory
than the normal WS2812FX class.
WS2812FXT creates three WS2812FX objects, two virtual LED strips (v1 and v2)
and one physical strip (p1). You setup the two virtual strips using the normal
WS2812FX API, where each virtual strip can have its own brightness, effect,
speed, segments, etc. The physical strip is used to drive the LLEDs. Behind
the scenes WS2812FXT takes care of blending the two virtual strips together
to create the transition from v1 to v2 on the physical strip.
To transition from the v1 effect to the v2 effect, call the startTransition()
function passing in the duration of the transition (in ms), and optionally
the transition direction (i.e direction == true produces a transition from
v1 to v2, while direction == false produces a transition from v2 to v1).
LICENSE
The MIT License (MIT)
Copyright (c) 2021 Keith Lord
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
CHANGELOG
2021-08-20 initial version
*/

#include <WS2812FX.h>

#define LED_PIN 4
#define LED_COUNT 144

WS2812FXT ws2812fxt = WS2812FXT(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {

ws2812fxt.init();

// setup the effects on the two virtual strips
// note v1 and v2 are pointers, so you must use pointer syntax
// (i.e. use ws2812fxt.v1->setMode(...), not ws2812fxt.v1.setMode(...))
ws2812fxt.v1->setBrightness(32);
ws2812fxt.v1->setSegment(0, 0, LED_COUNT-1, FX_MODE_RAINBOW_CYCLE, BLACK, 5000);

ws2812fxt.v2->setBrightness(128);
ws2812fxt.v2->setSegment(0, 0, LED_COUNT-1, FX_MODE_LARSON_SCANNER, BLUE, 3000);

ws2812fxt.start();
}

void loop() {
ws2812fxt.service();

// every ten seconds transition from v1 to v2 then back to v1
static bool direction = true;
static unsigned long timer = 10000;
unsigned long now = millis();

if(now > timer) {
ws2812fxt.startTransition(5000, direction); // transition duration = 5 seconds
direction = !direction; // change the transition direction
timer = now + 10000; // transition every ten seconds
}
}
Loading

0 comments on commit d3047cc

Please sign in to comment.