summaryrefslogtreecommitdiffstats
path: root/blinky.c
blob: 43cc88a2ddf958bee9c522930d2a57dd487a9c05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* 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;
    }
}