]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - main.c
Use pushbutton rising edge for state change
[rgblamp.git] / main.c
diff --git a/main.c b/main.c
index 71693c58fc2e9d314197b66997f6842aedd4549e..b9a8cbcd62de300fa6beb7b91a21f73eab3d710f 100644 (file)
--- a/main.c
+++ b/main.c
 #include "buttons.h"
 #include "rgb.h"
 
-#define STEP_SIZE               64             /* ms */
-#define MIN_INCOLOR_STEPS       16
-#define MAX_INCOLOR_STEPS       57600
-#define MAX_FADE_STEPS          480
-#define reset_steps()           do { incolor_steps = 1; fade_steps = 0; } while (0)
-#define rand_u8()               (rand() >> 7)
-#define rand_u16()              (rand() << 8 + rand() >> 7)
-#if 1
-#define rand_incolor_steps()    MIN_INCOLOR_STEPS * 2
-#define rand_fade_steps()       16
-#else
-#define rand_incolor_steps()    (MIN_INCOLOR_STEPS + rand_u16() % \
-                                    (MAX_INCOLOR_STEPS - MIN_INCOLOR_STEPS))
-#define rand_fade_steps()       (1 + rand_u16() % MAX_FADE_STEPS)
-#endif
-#define leds_set(r,g,b,w)       rgb_set((r).value >> 8, \
-                                    (g).value >> 8, \
-                                    (b).value >> 8, \
-                                    (w).value >> 8)
+#define STEP_SIZE               32 /* ms */
+#define reset_steps()           do { incolor_steps = 1; fade_steps = 0; } \
+                                  while (0)
+#define rand_u8()               (rand() & 0xff)
+#define rand_u16()              ((rand() << 8) + rand_u8())
+#define rand_incolor_steps(f)   (min_incolor_steps[f & 1] + \
+                                    (rand() % range_incolor_steps[f & 1]))
+#define rand_fade_steps(f)      (min_fade_steps[f & 1] + \
+                                    (rand() % range_fade_steps[f & 1]))
+#define leds_set(r,g,b,w)       rgb_set((r).value >> 7, \
+                                  (g).value >> 7, \
+                                  (b).value >> 7, 0)
+//                                  (w).value >> 7)
 
 typedef struct {
-  unsigned value;
-  unsigned increment;
-  unsigned char remainder;
+  int value;
+  int increment;
+  char remainder;
 } led_t;
 #define INIT_LED { 0, 0, 0 }
 
+/* The index of all step arrays is the fast variable, 0=slow, 1=fast. */
+#if 0
+const static unsigned min_incolor_steps[2] =   {   320,  32 };
+const static unsigned range_incolor_steps[2] = { 32768, 128 };
+const static int min_fade_steps[2] =           {    64,  32 };
+const static int range_fade_steps[2] =         {   416, 128 };
+#else
+const static unsigned min_incolor_steps[2] =   { 64, 32 };
+const static unsigned range_incolor_steps[2] = {  1,  1 };
+const static int min_fade_steps[2] =           { 64, 32 };
+const static int range_fade_steps[2] =         {  1,  1 };
+#endif
+
 int main(void)
 {
     led_t red = INIT_LED;
@@ -47,7 +54,7 @@ int main(void)
     led_t wht = INIT_LED;
     unsigned char fast = 0;
     unsigned incolor_steps;
-    unsigned fade_steps;
+    int fade_steps;
 
     pic_init();
     buttons_init();
@@ -61,12 +68,13 @@ int main(void)
         unsigned char buttons = buttons_read();
 
         if ((buttons & (IN_ROCKERA | IN_ROCKERB))) {
-            if (buttons & IN_PUSHBTN) {
-              if (!fast) {
-                fast = 1;
+            /* Crappy way to detect rising edges to change state of fast var */
+            if (!(fast & 2) && (buttons & IN_PUSHBTN)) {
+                fast |= 2;
+            } else if ((fast & 2) && !(buttons & IN_PUSHBTN)) {
+                fast &= ~2;
+                fast = 1 - fast;
                 reset_steps();
-              } else
-                fast = 0;
             }
 
             if (fade_steps) {
@@ -84,34 +92,33 @@ int main(void)
               }
               leds_set(red, grn, blu, wht);
             } else if (--incolor_steps == 0) {
-              unsigned newr, newg, newb, neww;
+              int newr, newg, newb, neww, tmp;
 
-              /* Next led color.  All off is not a valid option. */
+              /* Next led color.  All off is not a valid option.
+               * RGB values are stored and processed as 15-bit non-negative
+               * integers.  Since the RGB pwm values are 8 bits in width,
+               * leds_set() uses >> 7 to convert each color's value.
+               */
               do {
-                newr = rand() << 8;
-                newg = rand() << 8;
-                newb = rand() << 8;
-                neww = rand() << 8;
+                newr = rand();
+                newg = rand();
+                newb = rand();
+                neww = rand();
               } while (newr == 0 && newg == 0 && newb == 0 && neww == 0);
 
               /* Next incolor and fade steps */
-              incolor_steps = rand_incolor_steps();
-              fade_steps = (buttons & IN_ROCKERA) ? 1 : rand_fade_steps();
-              if (fast) {
-                incolor_steps = 1 + incolor_steps / 8;
-                if (buttons & IN_ROCKERB)
-                  fade_steps = 1 + fade_steps / 8;
-              }
+              incolor_steps = rand_incolor_steps(fast);
+              fade_steps = (buttons & IN_ROCKERA) ? 1 : rand_fade_steps(fast);
 
               /* Compute increment and remainder for each led */
-              red.increment = newr / fade_steps;
-              red.remainder = newr - red.increment * fade_steps;
-              grn.increment = newg / fade_steps;
-              grn.remainder = newg - grn.increment * fade_steps;
-              blu.increment = newb / fade_steps;
-              blu.remainder = newb - blu.increment * fade_steps;
-              wht.increment = neww / fade_steps;
-              wht.remainder = neww - wht.increment * fade_steps;
+              red.increment = (newr - red.value) / fade_steps;
+              red.remainder = newr - (red.value + red.increment * fade_steps);
+              grn.increment = (newg - grn.value) / fade_steps;
+              grn.remainder = newg - (grn.value + grn.increment * fade_steps);
+              blu.increment = (newb - blu.value) / fade_steps;
+              blu.remainder = newb - (blu.value + blu.increment * fade_steps);
+              wht.increment = (neww - wht.value) / fade_steps;
+              wht.remainder = neww - (wht.value + wht.increment * fade_steps);
             }
             __delay_ms(STEP_SIZE); /* step should be start to start... */
         } else {