2 #include <util/delay.h>
3 #include <avr/interrupt.h>
7 // #define CHRISTMAS_TREE 1
9 #define rgb_return(r, g, b) do { send_rgb((r), (g), (b)); return 1; } while(0)
13 /* RNG from ADC noise */
14 static unsigned char rand_pool[8], rand_pool_off, prev_bit, rand_pool_out;
16 static void init_rng()
18 ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1); // enable, clk/64
19 ADMUX = _BV(REFS1) | _BV(MUX0) | _BV(MUX3); // 1.1V, PB5:PB5, gain 20
21 ADCSRA |= _BV(ADIE) | _BV(ADSC);
26 static unsigned char rand() {
29 rv = rand_pool[rand_pool_out];
31 if (rand_pool_out >= sizeof(rand_pool))
39 if ((rand_pool_off & 1) == 0) { // first bit of the pair
43 unsigned char bit = ADCW & 1;
44 if (bit == prev_bit) { // whitening fail: try again
50 rand_pool[rand_pool_off >> 4]
51 ^= 1 << ((rand_pool_off >> 1) & 7);
55 if (rand_pool_off >= 16*sizeof(rand_pool))
60 static unsigned int slow_dim[] = {
64 static void fill_color(unsigned char r, unsigned char g, unsigned char b)
68 for (i = 0; i < STRIP_SIZE; i++)
76 static void do_buttons()
78 static uint8_t prev_buttons = _BV(PB0) | _BV(PB3) | _BV(PB4);
79 static uint16_t prev_len = 0;
81 uint8_t buttons = PINB & (_BV(PB0) | _BV(PB3) | _BV(PB4));
83 if (prev_buttons == buttons) {
89 if (prev_len < 3 || (buttons != (_BV(PB0) | _BV(PB3) | _BV(PB4)))) {
90 prev_buttons = buttons;
95 if ((prev_buttons & _BV(PB0)) == 0) {
98 } else if ((prev_buttons & _BV(PB3)) == 0) {
101 } else if ((prev_buttons & _BV(PB4)) == 0) {
105 prev_buttons = buttons;
109 #define LED_MAX (2*STRIP_SIZE/5)
111 #define R_BIAS(x) ((x) << 3)
112 #define G_BIAS(x) ((x) >> 1)
113 #define B_BIAS(x) ((x) << 1)
117 static unsigned char color, led_off;
118 static uint16_t jiffies;
119 unsigned char i, c0, l0;
121 if ((jiffies++ & 0x03f) == 0) {
122 if (++led_off >= LED_MAX) {
125 if (color >= 3*N_COLORS)
133 for (i = 0; i < STRIP_SIZE; i++) {
135 send_rgb(R_BIAS(N_COLORS-c0), G_BIAS(c0), 0);
136 } else if (c0 < 2*N_COLORS) {
137 send_rgb(0, G_BIAS(2*N_COLORS-c0), B_BIAS(c0-N_COLORS));
139 send_rgb(R_BIAS(c0-2*N_COLORS), 0, B_BIAS(3*N_COLORS-c0));
142 if (++l0 >= LED_MAX) {
145 if (c0 >= 3*N_COLORS)
159 _delay_ms(3000/8); // wait for a bit and then increase the CPU clock
163 PORTB |= _BV(PB0) | _BV(PB3) | _BV(PB4); // pull-ups for buttons
180 while (i < STRIP_SIZE) {
195 fill_color(16, 4, 8);
198 fill_color(96, 64, 64);
201 fill_color(255, 255, 255);
204 { unsigned char light;
206 light = slow_dim[sizeof(slow_dim)/sizeof(slow_dim[0]) - state];
207 fill_color(4*light, light, 2*light);