]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - main.c
These changes save 685 code words
[rgblamp.git] / main.c
diff --git a/main.c b/main.c
index ac75428bce7ca04780b8eae92afbd45dbd24d986..1df9445283f715cef19fe969c49b468d0928d74b 100644 (file)
--- a/main.c
+++ b/main.c
 
 #define AUTO_OFF_COUNT          549316UL  /*  5 hrs in 32.768 ms units */
 #define AUTO_ON_COUNT           2087402UL /* 19 hrs in 32.768 ms units */
-#define rand_u8()               (rand() & 0xff)
-#define rand_u16()              ((rand() << 8) + rand_u8())
-#define rand_incolor_steps(s)   (min_incolor_steps[s & 3] + \
-                                    (rand() % range_incolor_steps[s & 3]))
-#define rand_fade_steps(s)      (min_fade_steps[s & 3] + \
-                                    (rand() % range_fade_steps[s & 3]))
-#define leds_set(r,g,b,w)       rgb_set((r).value >> 7, \
-                                    (g).value >> 7, \
-                                    (b).value >> 7, \
-                                    0)
+#define leds_set(r,g,b,w)       rgb_set((r).value >> 7, (g).value >> 7, \
+                                    (b).value >> 7, 0)
 #define dbgpin_init()           do { \
                                     /* Set RA2 as output low */ \
                                     RA2 = 0; \
@@ -62,11 +54,14 @@ typedef struct {
   signed char remainder;
 } led_t;
 
-/* The index of all step arrays is the speed variable */
+/* The index of all step arrays is the speed variable.  min values must be
+ * at least one.  range values are bit-ANDed with rand() to generate a range, so
+ * ensure they are one less than a power of 2 and at least 1.
+ */
 const static unsigned min_incolor_steps[4] =   {   320,  32, 32,  1 };
-const static unsigned range_incolor_steps[4] = { 32768, 128, 32,  8 };
-const static int min_fade_steps[4] =           {    64,  32, 32,  1 };
-const static int range_fade_steps[4] =         {   416, 128, 32,  8 };
+const static unsigned range_incolor_steps[4] = { 32767, 127, 31,  7 };
+const static int min_fade_steps[4] =           {    64,  32, 32,  8 };
+const static int range_fade_steps[4] =         {   511, 127, 31,  7 };
 
 led_t red;
 led_t grn;
@@ -93,7 +88,8 @@ void start_fade()
   } while (newr == 0 && newg == 0 && newb == 0 && neww == 0);
 
   /* Random # of steps to reach the new color */
-  fade_steps = rand_fade_steps(speed);
+  fade_steps = min_fade_steps[speed & 3] +
+      (rand() & range_fade_steps[speed & 3]);
 
   /* Compute increment per fade step, and remainder, for each led */
   red.increment = (newr - red.value) / fade_steps;
@@ -178,7 +174,8 @@ void fade_task()
     blu.value += blu.remainder;
     wht.value += wht.remainder;
     tmr_stop(TMR_FADE);
-    tmr_start(TMR_INCOLOR, rand_incolor_steps(speed));
+    tmr_start(TMR_INCOLOR, min_incolor_steps[speed & 3] +
+          (rand() & range_incolor_steps[speed & 3]));
   }
   leds_set(red, grn, blu, wht);
 }