+++ /dev/null
-/*
- WS2812SFX.cpp - Library for WS2812 LED effects.
- Harm Aldick - 2016
- www.aldick.org
- FEATURES
- * A lot of blinken modes and counting
- * WS2812FX can be used as drop-in replacement for Adafruit Neopixel Library
- NOTES
- * Uses the Adafruit Neopixel library. Get it here:
- https://github.com/adafruit/Adafruit_NeoPixel
- LICENSE
- The MIT License (MIT)
- Copyright (c) 2016 Harm Aldick
- 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, sublicense, 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
- 2016-05-28 Initial beta release
- 2016-06-03 Code cleanup, minor improvements, new modes
- 2016-06-04 2 new sfx, fixed setColor (now also resets _mode_color)
- 2017-02-02 added external trigger functionality (e.g. for sound-to-light)
- 2017-02-02 removed "blackout" on mode, speed or color-change
-*/
-
-#include "core/systick.h"
-#include "extdrv/ws2812.h"
-#include "extdrv/ws2812sfx.h"
-
-
-static uint32_t random(uint32_t max)
-{
- static uint32_t seed = 0;
- /* Numerical Recipes */
- seed = seed * 1664525 + 1013904223;
- return seed % max;
-}
-
-static uint32_t abs(int32_t val)
-{
- return val < 0 ? -val : val;
-}
-
-static uint32_t min(uint32_t a, uint32_t b)
-{
- return a < b ? a : b;
-}
-
-static unsigned long millis(void)
-{
- return systick_get_tick_count();
-}
-
-static uint32_t constrain(int32_t val, int32_t min, int32_t max)
-{
- return val < min ? min : val > max ? max : val;
-}
-
-
-/* #####################################################
-#
-# Color and Blinken Functions
-#
-##################################################### */
-
-
-static void set_color(uint32_t color)
-{
- ws2812_set_pixel(0, color >> 16, (color >> 8) & 0xff, color & 0xff);
- ws2812_send_frame(1);
-}
-
-
-/*
- * Put a value 0 to 255 in to get a color value.
- * The colours are a transition r -> g -> b -> back to r
- * Inspired by the Adafruit examples.
- */
-static uint32_t color_wheel(uint8_t pos)
-{
- pos = 255 - pos;
- if(pos < 85) {
- return ((uint32_t)(255 - pos * 3) << 16) | ((uint32_t)(0) << 8) | (pos * 3);
- } else if(pos < 170) {
- pos -= 85;
- return ((uint32_t)(0) << 16) | ((uint32_t)(pos * 3) << 8) | (255 - pos * 3);
- } else {
- pos -= 170;
- return ((uint32_t)(pos * 3) << 16) | ((uint32_t)(255 - pos * 3) << 8) | (0);
- }
-}
-
-
-/*
- * Returns a new, random wheel index with a minimum distance of 42 from pos.
- */
-static uint8_t get_random_wheel_index(uint8_t pos)
-{
- uint8_t r = 0;
- uint8_t x = 0;
- uint8_t y = 0;
- uint8_t d = 0;
-
- while(d < 42) {
- r = random(256);
- x = abs(pos - r);
- y = 255 - x;
- d = min(x, y);
- }
-
- return r;
-}
-
-
-/*
- * No blinking. Just plain old static light.
- */
-static void mode_static(ws2812sfx_t *sfx)
-{
- set_color(sfx->_color);
-
- sfx->_mode_delay = 50;
-}
-
-
-/*
- * Normal blinking. 50% on/off time.
- */
-static void mode_blink(ws2812sfx_t *sfx)
-{
- if(sfx->_counter_mode_call % 2 == 1) {
- set_color(sfx->_color);
- } else {
- set_color(0);
- }
-
- sfx->_mode_delay = 100 + ((1986 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
-}
-
-
-/*
- * Lights all LEDs in one random color up. Then switches them
- * to the next random color.
- */
-static void mode_random_color(ws2812sfx_t *sfx)
-{
- sfx->_mode_color = get_random_wheel_index(sfx->_mode_color);
-
- set_color(color_wheel(sfx->_mode_color));
-
- sfx->_mode_delay = 100 + ((5000 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
-}
-
-
-/*
- * Lights every LED in a random color. Changes all LED at the same time
- * to new random colors.
- */
-static void mode_multi_dynamic(ws2812sfx_t *sfx)
-{
- set_color(color_wheel(random(256)));
-
- sfx->_mode_delay = 100 + ((5000 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
-}
-
-
-/*
- * Does the "standby-breathing" of well known i-Devices. Fixed Speed.
- * Use mode "fade" if you like to have something similar with a different speed.
- */
-static void mode_breath(ws2812sfx_t *sfx)
-{
- // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 // step
- uint16_t breath_delay_steps[] = { 7, 9, 13, 15, 16, 17, 18, 930, 19, 18, 15, 13, 9, 7, 4, 5, 10 }; // magic numbers for breathing LED
- uint8_t breath_brightness_steps[] = { 150, 125, 100, 75, 50, 25, 16, 15, 16, 25, 50, 75, 100, 125, 150, 220, 255 }; // even more magic numbers!
-
- if(sfx->_counter_mode_call == 0) {
- sfx->_mode_color = breath_brightness_steps[0] + 1;
- }
-
- uint32_t breath_brightness = sfx->_mode_color; // we use sfx->_mode_color to store the brightness
-
- if(sfx->_counter_mode_step < 8) {
- breath_brightness--;
- } else {
- breath_brightness++;
- }
-
- // update index of current delay when target brightness is reached, start over after the last step
- if(breath_brightness == breath_brightness_steps[sfx->_counter_mode_step]) {
- sfx->_counter_mode_step = (sfx->_counter_mode_step + 1) % (sizeof(breath_brightness_steps)/sizeof(uint8_t));
- }
-
- int b = (breath_brightness * (sfx->_brightness + 1)) >> 8; // keep brightness below brightness set by user
- ws2812_set_brightness(b);
-
- set_color(sfx->_color); // set all LEDs to selected color
-
- sfx->_mode_color = breath_brightness; // we use sfx->_mode_color to store the brightness
- sfx->_mode_delay = breath_delay_steps[sfx->_counter_mode_step];
-}
-
-
-/*
- * Fades the LEDs on and (almost) off again.
- */
-static void mode_fade(ws2812sfx_t *sfx)
-{
- int b = sfx->_counter_mode_step - 127;
- b = 255 - (abs(b) * 2);
- b = (b * (min(20, sfx->_brightness) + 1)) >> 8;
- ws2812_set_brightness(b);
- set_color(sfx->_color);
-
- sfx->_counter_mode_step = (sfx->_counter_mode_step + 1) % 255;
- sfx->_mode_delay = 5 + ((15 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
-}
-
-
-/*
- * Cycles all LEDs at once through a rainbow.
- */
-static void mode_rainbow(ws2812sfx_t *sfx)
-{
- uint32_t color = color_wheel(sfx->_counter_mode_step);
- set_color(color);
-
- sfx->_counter_mode_step = (sfx->_counter_mode_step + 1) % 256;
-
- sfx->_mode_delay = 1 + ((50 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
-}
-
-
-/*
- * Classic Strobe effect.
- */
-static void mode_strobe(ws2812sfx_t *sfx)
-{
- if(sfx->_counter_mode_call % 2 == 0) {
- set_color(sfx->_color);
- sfx->_mode_delay = 20;
- } else {
- set_color(0);
- sfx->_mode_delay = 50 + ((1986 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
- }
-}
-
-
-/*
- * Strobe effect with different strobe count and pause, controled by sfx->_speed.
- */
-static void mode_multi_strobe(ws2812sfx_t *sfx)
-{
- if(sfx->_counter_mode_step < (2 * ((sfx->_speed / 10) + 1))) {
- if(sfx->_counter_mode_step % 2 == 0) {
- set_color(sfx->_color);
- sfx->_mode_delay = 20;
- } else {
- set_color(0);
- sfx->_mode_delay = 50;
- }
-
- } else {
- set_color(0);
- sfx->_mode_delay = 100 + ((9 - (sfx->_speed % 10)) * 125);
- }
-
- sfx->_counter_mode_step = (sfx->_counter_mode_step + 1) % ((2 * ((sfx->_speed / 10) + 1)) + 1);
-}
-
-
-/*
- * Classic Strobe effect. Cycling through the rainbow.
- */
-static void mode_strobe_rainbow(ws2812sfx_t *sfx)
-{
- if(sfx->_counter_mode_call % 2 == 0) {
- set_color(color_wheel(sfx->_counter_mode_call % 256));
- sfx->_mode_delay = 20;
- } else {
- set_color(0);
- sfx->_mode_delay = 50 + ((1986 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
- }
-}
-
-
-/*
- * Classic Blink effect. Cycling through the rainbow.
- */
-static void mode_blink_rainbow(ws2812sfx_t *sfx)
-{
- if(sfx->_counter_mode_call % 2 == 1) {
- set_color(color_wheel(sfx->_counter_mode_call % 256));
- } else {
- set_color(0);
- }
-
- sfx->_mode_delay = 100 + ((1986 * (uint32_t)(SFX_SPEED_MAX - sfx->_speed)) / SFX_SPEED_MAX);
-}
-
-
-typedef void (*mode_ptr)(ws2812sfx_t *sfx);
-
-static struct {
- mode_ptr _fn;
- char *_name;
-} const modes[] = {
- { mode_static, "Static" },
- { mode_blink, "Blink" },
- { mode_breath, "Breath" },
- { mode_random_color, "Random Color" },
- { mode_multi_dynamic, "Multi Dynamic" },
- { mode_rainbow, "Rainbow" },
- { mode_fade, "Fade" },
- { mode_strobe, "Strobe" },
- { mode_strobe_rainbow, "Strobe Rainbow" },
- { mode_multi_strobe, "Multi Strobe" },
- { mode_blink_rainbow, "Blink Rainbow" },
-};
-
-#define CALL_MODE(sfx, n) (modes[n]._fn)(sfx);
-
-void WS2812SFX(ws2812sfx_t *sfx, const struct pio *pin)
-{
- ws2812_config(pin);
-
- sfx->_speed = SFX_DEFAULT_SPEED;
- sfx->_brightness = SFX_DEFAULT_BRIGHTNESS;
- sfx->_running = 0;
- sfx->_mode_last_call_time = 0;
- sfx->_mode_delay = 0;
- sfx->_color = SFX_DEFAULT_COLOR;
- sfx->_mode_color = SFX_DEFAULT_COLOR;
- sfx->_counter_mode_call = 0;
- sfx->_counter_mode_step = 0;
-}
-
-void SFX_init(ws2812sfx_t *sfx)
-{
- SFX_setBrightness(sfx, sfx->_brightness);
- ws2812_send_frame(1);
-}
-
-void SFX_service(ws2812sfx_t *sfx)
-{
- if(sfx->_running) {
- unsigned long now = millis();
-
- if(now - sfx->_mode_last_call_time > sfx->_mode_delay) {
- CALL_MODE(sfx, sfx->_mode_index);
- sfx->_counter_mode_call++;
- sfx->_mode_last_call_time = now;
- }
- }
-}
-
-void SFX_start(ws2812sfx_t *sfx)
-{
- sfx->_counter_mode_call = 0;
- sfx->_counter_mode_step = 0;
- sfx->_mode_last_call_time = 0;
- sfx->_running = 1;
-}
-
-void SFX_stop(ws2812sfx_t *sfx)
-{
- sfx->_running = 0;
- set_color(0);
-}
-
-void SFX_setMode(ws2812sfx_t *sfx, uint8_t m)
-{
- sfx->_counter_mode_call = 0;
- sfx->_counter_mode_step = 0;
- sfx->_mode_last_call_time = 0;
- sfx->_mode_index = constrain(m, 0, SFX_MODE_COUNT-1);
- sfx->_mode_color = sfx->_color;
- ws2812_set_brightness(sfx->_brightness);
- //set_color(0);
-}
-
-void SFX_setSpeed(ws2812sfx_t *sfx, uint8_t s)
-{
- sfx->_counter_mode_call = 0;
- sfx->_counter_mode_step = 0;
- sfx->_mode_last_call_time = 0;
- sfx->_speed = constrain(s, SFX_SPEED_MIN, SFX_SPEED_MAX);
- //set_color(0);
-}
-
-void SFX_increaseSpeed(ws2812sfx_t *sfx, uint8_t s)
-{
- s = constrain(sfx->_speed + s, SFX_SPEED_MIN, SFX_SPEED_MAX);
- SFX_setSpeed(sfx, s);
-}
-
-void SFX_decreaseSpeed(ws2812sfx_t *sfx, uint8_t s)
-{
- s = constrain(sfx->_speed - s, SFX_SPEED_MIN, SFX_SPEED_MAX);
- SFX_setSpeed(sfx, s);
-}
-
-void SFX_setRGBColor(ws2812sfx_t *sfx, uint8_t r, uint8_t g, uint8_t b)
-{
- SFX_setColor(sfx, ((uint32_t)r << 16) | ((uint32_t)g << 8) | b);
-}
-
-void SFX_setColor(ws2812sfx_t *sfx, uint32_t c)
-{
- sfx->_color = c;
- sfx->_counter_mode_call = 0;
- sfx->_counter_mode_step = 0;
- sfx->_mode_last_call_time = 0;
- sfx->_mode_color = sfx->_color;
- ws2812_set_brightness(sfx->_brightness);
- //set_color(0);
-}
-
-void SFX_setBrightness(ws2812sfx_t *sfx, uint8_t b)
-{
- sfx->_brightness = constrain(b, SFX_BRIGHTNESS_MIN, SFX_BRIGHTNESS_MAX);
- ws2812_set_brightness(sfx->_brightness);
- ws2812_send_frame(1);
-}
-
-void SFX_increaseBrightness(ws2812sfx_t *sfx, uint8_t s)
-{
- s = constrain(sfx->_brightness + s, SFX_BRIGHTNESS_MIN, SFX_BRIGHTNESS_MAX);
- SFX_setBrightness(sfx, s);
-}
-
-void SFX_decreaseBrightness(ws2812sfx_t *sfx, uint8_t s)
-{
- s = constrain(sfx->_brightness - s, SFX_BRIGHTNESS_MIN, SFX_BRIGHTNESS_MAX);
- SFX_setBrightness(sfx, s);
-}
-
-
-const char* SFX_getModeName(enum ModeSFX m)
-{
- if (m < SFX_MODE_COUNT) {
- return modes[m]._name;
- } else {
- return "";
- }
-}
+++ /dev/null
-/*
- WS2812SFX.h - Library for WS2812 LED effects.
-
- Harm Aldick - 2016
- www.aldick.org
- FEATURES
- * A lot of blinken modes and counting
- * WS2812FX can be used as drop-in replacement for Adafruit Neopixel Library
- NOTES
- * Uses the Adafruit Neopixel library. Get it here:
- https://github.com/adafruit/Adafruit_NeoPixel
- LICENSE
- The MIT License (MIT)
- Copyright (c) 2016 Harm Aldick
- 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, sublicense, 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
- 2016-05-28 Initial beta release
- 2016-06-03 Code cleanup, minor improvements, new modes
- 2016-06-04 2 new sfx, fixed setColor (now also resets _mode_color)
- 2017-02-02 added external trigger functionality (e.g. for sound-to-light)
-*/
-
-#ifndef WS2812SFX_h
-#define WS2812SFX_h
-
-#include "core/pio.h"
-
-#define SFX_DEFAULT_BRIGHTNESS 50
-#define SFX_DEFAULT_MODE 0
-#define SFX_DEFAULT_SPEED 150
-#define SFX_DEFAULT_COLOR 0xFFFFFF
-
-#define SFX_SPEED_MIN 0
-#define SFX_SPEED_MAX 255
-
-#define SFX_BRIGHTNESS_MIN 0
-#define SFX_BRIGHTNESS_MAX 255
-
-enum ModeSFX {
- SFX_MODE_STATIC,
- SFX_MODE_BLINK,
- SFX_MODE_BREATH,
- SFX_MODE_RANDOM_COLOR,
- SFX_MODE_MULTI_DYNAMIC,
- SFX_MODE_FADE,
- SFX_MODE_STROBE,
- SFX_MODE_STROBE_RAINBOW,
- SFX_MODE_MULTI_STROBE,
- SFX_MODE_BLINK_RAINBOW,
- SFX_MODE_COUNT
-};
-
-typedef struct {
- int _running;
-
- uint8_t _mode_index;
- uint8_t _speed;
- uint8_t _brightness;
-
- uint32_t _color;
- uint32_t _counter_mode_call;
- uint32_t _counter_mode_step;
- uint32_t _mode_color;
- uint32_t _mode_delay;
-
- unsigned long _mode_last_call_time;
-} ws2812sfx_t;
-
-void WS2812SFX(ws2812sfx_t *sfx, const struct pio *pin);
-
-void SFX_init(ws2812sfx_t *sfx);
-void SFX_service(ws2812sfx_t *sfx);
-void SFX_start(ws2812sfx_t *sfx);
-void SFX_stop(ws2812sfx_t *sfx);
-
-void SFX_setMode(ws2812sfx_t *sfx, uint8_t m);
-void SFX_setSpeed(ws2812sfx_t *sfx, uint8_t s);
-void SFX_increaseSpeed(ws2812sfx_t *sfx, uint8_t s);
-void SFX_decreaseSpeed(ws2812sfx_t *sfx, uint8_t s);
-void SFX_setRGBColor(ws2812sfx_t *sfx, uint8_t r, uint8_t g, uint8_t b);
-void SFX_setColor(ws2812sfx_t *sfx, uint32_t c);
-void SFX_setBrightness(ws2812sfx_t *sfx, uint8_t b);
-void SFX_increaseBrightness(ws2812sfx_t *sfx, uint8_t s);
-void SFX_decreaseBrightness(ws2812sfx_t *sfx, uint8_t s);
-
-const char* SFX_getModeName(enum ModeSFX m);
-
-static inline int SFX_isRunning(ws2812sfx_t *sfx)
-{
- return sfx->_running;
-}
-
-static inline uint8_t SFX_getMode(ws2812sfx_t *sfx)
-{
- return sfx->_mode_index;
-}
-
-static inline uint8_t SFX_getSpeed(ws2812sfx_t *sfx)
-{
- return sfx->_speed;
-}
-
-static inline uint8_t SFX_getBrightness(ws2812sfx_t *sfx)
-{
- return sfx->_brightness;
-}
-
-static inline uint8_t SFX_getModeCount(ws2812sfx_t *sfx)
-{
- return SFX_MODE_COUNT;
-}
-
-static inline uint32_t SFX_getColor(ws2812sfx_t *sfx)
-{
- return sfx->_color;
-}
-
-#endif