From cbc83b9aabc999548e368e6f093967af5c8f317e Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sun, 27 Jun 2021 17:52:00 +1200 Subject: [PATCH] Add fast led demo --- fastled/FastLED_RGBW.h | 58 ++++++++++++++++++++++++++++++++++ fastled/fastled.ino | 70 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 fastled/FastLED_RGBW.h create mode 100644 fastled/fastled.ino diff --git a/fastled/FastLED_RGBW.h b/fastled/FastLED_RGBW.h new file mode 100644 index 0000000..aa5ef38 --- /dev/null +++ b/fastled/FastLED_RGBW.h @@ -0,0 +1,58 @@ +/* + * Hack to enable SK6812 RGBW strips to work with FastLED. + * + * Original code by Jim Bumgardner (http://krazydad.com). + * Modified by David Madison (http://partsnotincluded.com). + * +*/ + +#ifndef FastLED_RGBW_h +#define FastLED_RGBW_h + +struct CRGBW { + union { + struct { + union { + uint8_t g; + uint8_t green; + }; + union { + uint8_t r; + uint8_t red; + }; + union { + uint8_t b; + uint8_t blue; + }; + union { + uint8_t w; + uint8_t white; + }; + }; + uint8_t raw[4]; + }; + + CRGBW(){} + + CRGBW(uint8_t rd, uint8_t grn, uint8_t blu, uint8_t wht){ + r = rd; + g = grn; + b = blu; + w = wht; + } + + inline void operator = (const CRGB c) __attribute__((always_inline)){ + this->r = c.r; + this->g = c.g; + this->b = c.b; + this->white = 0; + } +}; + +inline uint16_t getRGBWsize(uint16_t nleds){ + uint16_t nbytes = nleds * 4; + if(nbytes % 3 > 0) return nbytes / 3 + 1; + else return nbytes / 3; +} + +#endif diff --git a/fastled/fastled.ino b/fastled/fastled.ino new file mode 100644 index 0000000..bcbb848 --- /dev/null +++ b/fastled/fastled.ino @@ -0,0 +1,70 @@ +/* + * Example sketch using FastLED for RGBW strips (SK6812). Includes + * color wipes and rainbow pattern. + * + * Written by David Madison + * http://partsnotincluded.com +*/ + +#include "FastLED.h" +#include "FastLED_RGBW.h" + +#define NUM_LEDS 60 +#define DATA_PIN 6 + +CRGBW leds[NUM_LEDS]; +CRGB *ledsRGB = (CRGB *) &leds[0]; + +const uint8_t brightness = 128; + +void setup() { + FastLED.addLeds(ledsRGB, getRGBWsize(NUM_LEDS)); + FastLED.setBrightness(brightness); + FastLED.show(); +} + +void loop(){ + colorFill(CRGB::Red); + colorFill(CRGB::Green); + colorFill(CRGB::Blue); + fillWhite(); + rainbowLoop(); +} + +void colorFill(CRGB c){ + for(int i = 0; i < NUM_LEDS; i++){ + leds[i] = c; + FastLED.show(); + delay(50); + } + delay(500); +} + +void fillWhite(){ + for(int i = 0; i < NUM_LEDS; i++){ + leds[i] = CRGBW(0, 0, 0, 255); + FastLED.show(); + delay(50); + } + delay(500); +} + +void rainbow(){ + static uint8_t hue; + + for(int i = 0; i < NUM_LEDS; i++){ + leds[i] = CHSV((i * 256 / NUM_LEDS) + hue, 255, 255); + } + FastLED.show(); + hue++; +} + +void rainbowLoop(){ + long millisIn = millis(); + long loopTime = 5000; // 5 seconds + + while(millis() < millisIn + loopTime){ + rainbow(); + delay(5); + } +}