rgb-lights: new slow-changing mode
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 12 Apr 2016 17:39:38 +0000 (19:39 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 12 Apr 2016 17:40:23 +0000 (19:40 +0200)
projects/rgb-led-light/main.c
projects/rgb-led-light/rgbstring.h

index a89481c..bac7ecb 100644 (file)
@@ -4,8 +4,6 @@
 
 #include "rgbstring.h"
 
-static volatile uint16_t  jiffies;
-
 // #define CHRISTMAS_TREE 1
 
 #define rgb_return(r, g, b)    do { send_rgb((r), (g), (b)); return 1; } while(0)
@@ -38,7 +36,6 @@ static unsigned char rand() {
 
 ISR(ADC_vect) {
        ADCSRA |= _BV(ADSC);
-       jiffies++;
        if ((rand_pool_off & 1) == 0) { // first bit of the pair
                prev_bit = ADCW & 1;
                rand_pool_off++;
@@ -109,6 +106,49 @@ static void do_buttons()
        prev_len = 0;
 }
 
+#define LED_MAX (2*STRIP_SIZE/5)
+#define N_COLORS 8
+#define R_BIAS(x)      ((x) << 3)
+#define G_BIAS(x)      ((x) >> 1)
+#define B_BIAS(x)      ((x) << 1)
+
+static void do_hue()
+{
+       static unsigned char color, led_off;
+       static uint16_t jiffies;
+       unsigned char i, c0, l0;
+
+       if ((jiffies++ & 0x03f) == 0) {
+               if (++led_off >= LED_MAX) {
+                       led_off = 0;
+                       color++;
+                       if (color >= 3*N_COLORS)
+                               color = 0;
+               }
+       }
+
+       l0 = led_off;
+       c0 = color;
+
+       for (i = 0; i < STRIP_SIZE; i++) {
+               if (c0 < N_COLORS) {
+                       send_rgb(R_BIAS(N_COLORS-c0), G_BIAS(c0), 0);
+               } else if (c0 < 2*N_COLORS) {
+                       send_rgb(0, G_BIAS(2*N_COLORS-c0), B_BIAS(c0-N_COLORS));
+               } else {
+                       send_rgb(R_BIAS(c0-2*N_COLORS), 0, B_BIAS(3*N_COLORS-c0));
+               }
+
+               if (++l0 >= LED_MAX) {
+                       l0 = 0;
+                       c0++;
+                       if (c0 >= 3*N_COLORS)
+                               c0 = 0;
+               }
+       }
+       end_frame();
+}
+
 int main(void)
 {
 
@@ -122,13 +162,12 @@ int main(void)
 
        PORTB |= _BV(PB0) | _BV(PB3) | _BV(PB4); // pull-ups for buttons
 
-       state = 0;
+       state = 2;
 
        sei();
 
        while (1) {
                unsigned char i;
-               static unsigned char c = 28;
 
                do_buttons();
 
@@ -150,31 +189,13 @@ int main(void)
                        end_frame();
                        break;
                case 2:
-                       if ((jiffies & 0x1ff) == 0) {
-                               c++;
-                               if (c >= 30)
-                                       c = 0;
-                       }
-
-                       for (i = 0; i < STRIP_SIZE; i++) {
-                               unsigned char x = c; // + i / 2;
-                               if (x >= 30)
-                                       x %= 30;
-                               if (x < 10) {
-                                       send_rgb(8*(10-x), x, 0);
-                               } else if (x < 20) {
-                                       send_rgb(0, 20-x, x-10);
-                               } else {
-                                       send_rgb(8*(x-20), 0, 30-x);
-                               }
-                       }
-                       end_frame();
+                       do_hue();
                        break;
                case 3:
-                       fill_color(32, 4, 8);
+                       fill_color(16, 4, 8);
                        break;
                case 4:
-                       fill_color(255, 92, 92);
+                       fill_color(96, 64, 64);
                        break;
                case 5:
                        fill_color(255, 255, 255);
index 1f14867..880b585 100644 (file)
@@ -19,7 +19,7 @@ void inline log_word(uint16_t word) { }
 #endif
 
 /* serial.c */
-#define STRIP_SIZE 10
+#define STRIP_SIZE 72
 void init_serial();
 void zero_frame();
 void end_frame();