/* * Light_WS2812 library example - RGB_blinky * * cycles one LED through red, green, blue * * This example is configured for a ATtiny85 with PLL clock fuse set and * the WS2812 string connected to PB4. */ #include "project.h" #define N_COLORS 288 #define N_LEDS 24 struct RGB color[N_COLORS]; struct RGB led[N_LEDS]; static int map (int j, int n) { j = (255UL * j) / n; if (j > 255) j = 255; return j; } static int ramp (int j, int n) { if ((j >= -n) && (j < 0)) return map (j + n, n); if ((j >= 0) && (j <= n)) return map (n - j, n); return 0; } static void setup_colors (struct RGB *colors, int n) { int i; int n3 = n / 3; for (i = 0; i < n; ++i) { colors[i].r = ramp (((i) % n) - n3, n3); colors[i].g = ramp (((i + n3) % n) - n3, n3); colors[i].b = ramp (((i + n3 + n3) % n) - n3, n3); printf ("%3d: %3d %3d %3d\n", i, colors[i].r, colors[i].g, colors[i].b); } } int main (void) { int i, j, k; setup_clocks (); stdio_init (); setup_colors (color, N_COLORS); i = 0; while (1) { for (j = 0; j < N_LEDS; ++j) { k = (j * (N_COLORS - 1)) / (N_LEDS - 1); k += i; while (k >= N_COLORS) k -= N_COLORS; led[j] = color[k]; } ws2812_setleds (led, N_LEDS); _delay_ms (5); // wait for 500ms. i++; i %= N_COLORS; } }