Skip to content

Commit

Permalink
convert to using FastLED
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
  • Loading branch information
SvenDowideit committed Dec 17, 2019
1 parent a858c66 commit e7a6f48
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 122 deletions.
117 changes: 35 additions & 82 deletions lib/neopixel/helpers.cpp
Expand Up @@ -3,35 +3,30 @@


// this function sets all the pixels in a group to the same colour
void leds_set(Adafruit_NeoPixel &leds, uint8_t R, uint8_t G, uint8_t B) {
for (uint16_t i = 0; i < leds.numPixels(); i++) {
leds.setPixelColor(i, R, G, B);
leds.show();
//delay(50);
}
void leds_set(CRGB *leds, uint len, uint8_t R, uint8_t G, uint8_t B) {
fill_solid(leds, len, CRGB(R, G, B));
FastLED.show();
}

uint8_t currentColours[3];
typedef struct {
const char* name;
uint8_t colours[3];
CRGB colour;
} colour_def;
// from https://learn.adafruit.com/sparkle-skirt/code-battery
// Here is where you can put in your favorite colors that will appear!
// just add new {nnn, nnn, nnn}, lines.
colour_def myFavoriteColors[] = {
{"gold", {255, 222, 30}}, // Pixie GOLD
{"blue", {50, 255, 255}}, // Alchemy BLUE
{"orange", {255, 100, 0}}, // Animal Orange
{"purple", {242, 90, 255}}, // Garden PINK
{"green", {0, 255, 40}}, // Tinker GREEN
{"off", {0, 0, 0}},
{"white", {180, 180, 180}},
{"red", {255, 0, 0}},
{"green", {0, 255, 0}},
{"blue", {0, 0, 255}},
{"yellow", {255, 255, 0}},
{"pink", {168, 0, 140}}
{"gold", CRGB::Gold},
{"blue", CRGB::Blue},
{"orange", CRGB::Orange},
{"purple", CRGB::Purple},
{"green", CRGB::Green},
{"off", CRGB::Black},
{"white", CRGB::White},
{"red", CRGB::Red},
{"yellow", CRGB::Yellow},
{"pink", CRGB::Pink}
};
#define FAVCOLORS 11u

Expand All @@ -40,33 +35,31 @@ int moredim = 4;
int colour = 3;


void updateColourRGB(Adafruit_NeoPixel &leds, int red, int green, int blue) {
void updateColourRGB(CRGB *leds, uint len, int red, int green, int blue) {
red = red / moredim;
green = green / moredim;
blue = blue / moredim;


leds_set(leds, red, green, blue);
leds_set(leds, len, red, green, blue);
currentColours[0] = red;
currentColours[1] = green;
currentColours[2] = blue;
}


void updateColour(Adafruit_NeoPixel &leds, const char * colourName) {
void updateColour(CRGB *leds, uint len, const char * colourName) {
for (uint i = 0; i < FAVCOLORS; i++) {
if (strcmp(colourName, myFavoriteColors[i].name) == 0) {
colour = i;
break;
}
}
Serial.printf("Asked for %s, got %s\n", colourName, myFavoriteColors[colour].name);
// if (colour > FAVCOLORS) {
// colour = 0;
// }
updateColourRGB(leds, myFavoriteColors[colour].colours[0],
myFavoriteColors[colour].colours[1],
myFavoriteColors[colour].colours[2]);

fill_solid(leds, len, myFavoriteColors[colour].colour);
FastLED.show();

}

// from https://learn.adafruit.com/neopixel-pixie-dust-bag/arduino-code
Expand All @@ -75,78 +68,38 @@ void updateColour(Adafruit_NeoPixel &leds, const char * colourName) {

bool oldState = HIGH; //sets the initial variable for counting touch sensor button pushes

void pixie_dust(Adafruit_NeoPixel &leds, int bright, unsigned long twinkleDelay) {
void pixie_dust(CRGB *leds, uint len, int bright, unsigned long twinkleDelay) {
//color (0-255) values to be set by cycling touch switch, initially GOLD
uint8_t red = currentColours[0];
uint8_t green = currentColours[1];
uint8_t blue = currentColours[2];

//sparkling
int p = random(leds.numPixels()); //select a random pixel
leds.setPixelColor(p, red*bright, green*bright, blue*bright);
leds.show();
int p = random(len-1); //select a random pixel
leds[p] = CRGB(red*bright, green*bright, blue*bright);
FastLED.show();

delay(twinkleDelay * random(DELAY_MULT) ); //delay value randomized to up to DELAY_MULT times longer
leds.setPixelColor(p, red, green, blue);
leds.show();
leds[p] = CRGB(red, green, blue);
FastLED.show();
delay(twinkleDelay * random(DELAY_MULT) ); //delay value randomized to up to DELAY_MULT times longer
}


// Fadeout... starts at bright white and fades to almost zero
void fadeout(Adafruit_NeoPixel & leds) {
void fadeout(CRGB *leds, uint len) {
// swap these two loops to spin around the LEDs
for (uint16_t fade = 255; fade > 0; fade = fade - 17) {
for (uint16_t i = 0; i < leds.numPixels(); i++) {
for (uint16_t i = 0; i < len; i++) {
// now we will 'fade' it in steps
leds.setPixelColor(i, leds.Color(fade, fade, fade));
leds[i] = CRGB(fade, fade, fade);
}
leds.show();
FastLED.show();
delay(5); // milliseconds
}
// now make sure they're all set to 0
for (uint16_t i = 0; i < leds.numPixels(); i++) {
leds.setPixelColor(i, leds.Color(0, 0, 0));
}
leds.show();
}


// first number is 'wait' delay, shorter num == shorter twinkle
// second number is how many neopixels to simultaneously light up
// THIS FUNCTION IS NOT USED AND PROBABLY DOESN'T WORK
void flashRandom(Adafruit_NeoPixel &leds, int wait, uint8_t howmany) {

for (uint16_t i = 0; i < howmany; i++) {
// pick a random favorite color!
int c = random(FAVCOLORS);
int red = myFavoriteColors[c].colours[0];
int green = myFavoriteColors[c].colours[1];
int blue = myFavoriteColors[c].colours[2];

// get a random pixel from the list
int j = random(leds.numPixels());
//Serial.print("Lighting up "); Serial.println(j);

// now we will 'fade' it in 5 steps
for (int x = 0; x < 5; x++) {
int r = red * (x + 1); r /= 5;
int g = green * (x + 1); g /= 5;
int b = blue * (x + 1); b /= 5;

leds.setPixelColor(j, leds.Color(r, g, b));
leds.show();
delay(wait);
}
// & fade out in 5 steps
for (int x = 5; x >= 0; x--) {
int r = red * x; r /= 5;
int g = green * x; g /= 5;
int b = blue * x; b /= 5;

leds.setPixelColor(j, leds.Color(r, g, b));
leds.show();
delay(wait);
}
for (uint16_t i = 0; i < len; i++) {
leds[i] = CRGB(0, 0, 0);
}
// LEDs will be off when done (they are faded to 0)
FastLED.show();
}
16 changes: 5 additions & 11 deletions lib/neopixel/helpers.h
Expand Up @@ -3,18 +3,12 @@


// this function sets all the pixels in a group to the same colour
void leds_set(Adafruit_NeoPixel &leds, uint8_t R, uint8_t G, uint8_t B);
void leds_set(CRGB *leds, uint len, uint8_t R, uint8_t G, uint8_t B);

void updateColourRGB(Adafruit_NeoPixel &leds, int red, int green, int blue);
void updateColourRGB(CRGB *leds, uint len, int red, int green, int blue);

void updateColour(Adafruit_NeoPixel &leds, const char * colourName);
void updateColour(CRGB *leds, uint len, const char * colourName);

void pixie_dust(Adafruit_NeoPixel &leds, int bright, unsigned long twinkleDelay);
void pixie_dust(CRGB *leds, uint len, int bright, unsigned long twinkleDelay);
// Fadeout... starts at bright white and fades to almost zero
void fadeout(Adafruit_NeoPixel & leds);


// first number is 'wait' delay, shorter num == shorter twinkle
// second number is how many neopixels to simultaneously light up
// THIS FUNCTION IS NOT USED AND PROBABLY DOESN'T WORK
void flashRandom(Adafruit_NeoPixel &leds, int wait, uint8_t howmany);
void fadeout(CRGB *leds, uint len);
30 changes: 9 additions & 21 deletions lib/neopixel/neopixel.cpp
Expand Up @@ -9,26 +9,16 @@ const char *NeopixelString::deviceType = "neopixel";
// TODO: set brightness and colour set from mqtt payload
//#define BRIGHTNESS 140 // 140 is reasonably bright

NeopixelString::NeopixelString(uint16_t pin, uint16_t num, neoPixelType type, Mqtt *m) {
ledPin = pin;
ledNum = num;
ledType = type;
NeopixelString::NeopixelString(CRGB *leds, uint len, Mqtt *m) {
mqtt = m;
left_leds = leds;
ledLen = len;
}

void NeopixelString::setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin, LOW);
fill_solid(left_leds, ledLen, CRGB::Black);
FastLED.show();

left_leds = new Adafruit_NeoPixel(ledNum, ledPin, ledType);

left_leds->begin(); // This initializes the NeoPixel library.

updateColourRGB(*left_leds, 0, 0, 0);

//left_leds.setBrightness(BRIGHTNESS);
//left_leds.show(); // turn on all pixels
subscribe();
}

Expand All @@ -48,8 +38,6 @@ const char *NeopixelString::IsMessageForMe(const char * topic) {

void NeopixelString::loop() {
if (!initialised) {
digitalWrite(ledPin, HIGH);

delay(100);

setup();
Expand All @@ -60,7 +48,7 @@ void NeopixelString::loop() {
// sparkle each led once (but in random order, so some of them might sparkle twice and some not at all)
// TODO actually just choose a random LED and sparkle it, since this loop now repeats every time
//for (int i = 0; i < ledNum; i++) {
pixie_dust(*left_leds, 5, operationDelay);
pixie_dust(left_leds, ledLen, 5, operationDelay);
//}
}
if (strcmp(operation, "disco") == 0) {
Expand All @@ -69,7 +57,7 @@ void NeopixelString::loop() {
//Serial.println(v.as<String>());
String colour = colours[i].as<String>();
Serial.printf("disco: %s\r\n", colour.c_str());
updateColour(*left_leds, colour.c_str());
updateColour(left_leds, ledLen, colour.c_str());
delay(operationDelay);
}
}
Expand Down Expand Up @@ -98,12 +86,12 @@ void NeopixelString::mqtt_callback_fn(const char* topic, const char* payload, un
const int green = obj["g"] | 0;
const int blue = obj["b"] | 0;
//const float alpha = obj["a"] | 0.5;
updateColourRGB(*left_leds, red, green, blue);
updateColourRGB(left_leds, ledLen, red, green, blue);
}

if (strcmp(operation, "set") == 0) {
const char* colour = obj["colour"] | "off";
updateColour(*left_leds, colour);
updateColour(left_leds, ledLen, colour);
}
if (strcmp(operation, "twinkle") == 0) {
unsigned long duration = obj["duration"] | 1000; // default to one second
Expand Down
12 changes: 6 additions & 6 deletions lib/neopixel/neopixel.h
Expand Up @@ -4,7 +4,9 @@

#include <device.h>
#include <mqtt.h>
#include <Adafruit_NeoPixel.h>
#define FASTLED_ESP8266_RAW_PIN_ORDER
#define FASTLED_ALLOW_INTERRUPTS 0
#include <FastLED.h>

class NeopixelString: public Device {
public:
Expand All @@ -17,7 +19,7 @@ class NeopixelString: public Device {
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
// NEO_RGBW Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
NeopixelString(uint16_t ledPin, uint16_t ledNum, neoPixelType type, Mqtt *mqtt);
NeopixelString(CRGB *leds, uint len, Mqtt *mqtt);

virtual void setup();
virtual void subscribe();
Expand All @@ -26,13 +28,11 @@ class NeopixelString: public Device {
virtual void mqtt_callback_fn(const char* topic, const char* payload, unsigned int length);

private:
Adafruit_NeoPixel *left_leds;
CRGB *left_leds;
uint ledLen;

static const char *deviceType;
Mqtt *mqtt;
uint16_t ledPin;
uint16_t ledNum;
neoPixelType ledType;

boolean initialised = false;
boolean inLoop = false;
Expand Down
15 changes: 13 additions & 2 deletions src/neopixel_string.ino
Expand Up @@ -35,6 +35,13 @@ Mqtt mqtt = Mqtt(SECRET_SSID, SECRET_PASSWORD, "10.11.11.10", 1883, "multipass")
Device *devices[5];
int deviceCount = 0;


// #define FASTLED_ESP8266_RAW_PIN_ORDER
// #define FASTLED_ALLOW_INTERRUPTS 0
// #include <FastLED.h>
#define NUM_LEDS 50
CRGB leds[NUM_LEDS];

void mqtt_callback_fn(const char* topic, const byte* raw_payload, unsigned int length) {
Serial.printf("Callback: %s\r\n", topic);

Expand Down Expand Up @@ -83,8 +90,12 @@ void setup() {
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
#define LEDPIN D5
#define LED_NUM 50
devices[deviceCount++] = new NeopixelString(D5, 50, NEO_RGB + NEO_KHZ800, &mqtt); // RGB
FastLED.addLeds<WS2812, D5, RGB>(leds, NUM_LEDS);
//CRGBArray<LED_NUM> left_leds;
//FastLED.addLeds<NEOPIXEL, LEDPIN>(left_leds, LED_NUM);
//left_leds = CRGB::HotPink;
devices[deviceCount++] = new NeopixelString(leds, NUM_LEDS, &mqtt); // RGB
//devices[deviceCount++] = new NeopixelString(D5, 50, NEO_RGB + NEO_KHZ800, &mqtt); // RGB


// pins 19&20 / D1&D2 / GPIO4 and GPOI5
Expand Down

0 comments on commit e7a6f48

Please sign in to comment.