]> oss.titaniummirror.com Git - rgblamp.git/commitdiff
These changes save 685 code words
authorR. Steve McKown <rsmckown@gmail.com>
Sat, 10 Dec 2011 08:31:35 +0000 (01:31 -0700)
committerR. Steve McKown <rsmckown@gmail.com>
Sat, 10 Dec 2011 09:07:44 +0000 (02:07 -0700)
btn.c
btn.h
isr.c
isr.h
main.c
rgb.h
task.c
task.h

diff --git a/btn.c b/btn.c
index 84a73aaedfadedc6b70e252153442175f3ebcfcd..2fefcbe0c42c2b6c20619e08f5a4f11c775b330f 100644 (file)
--- a/btn.c
+++ b/btn.c
@@ -25,6 +25,39 @@ void btn_init()
   GIE = 1;             /* enable global interrupts */
 }
 
+void btn_pben()
+{
+  ndi();
+  IOCBP0 = 1; IOCBN0 = 1; IOCBF0 = 0;
+  nei();
+}
+
+/* Disable the pushbutton from user code */
+void btn_pbdis()
+{
+  ndi();
+  IOCBP0 = 0; IOCBN0 = 0; IOCBF0 = 0;
+  nei();
+}
+
+/* Enable the rocker switch from user code */
+void btn_rsen()
+{
+  ndi();
+  IOCBP4 = 1; IOCBN4 = 1; IOCBF4 = 0;
+  IOCBP5 = 1; IOCBN5 = 1; IOCBF5 = 0;
+  nei();
+}
+
+/* Disable the rocker switch from user code */
+void btn_rsdis()
+{
+  ndi();
+  IOCBP4 = 0; IOCBN4 = 0; IOCBF4 = 0;
+  IOCBP5 = 0; IOCBN5 = 0; IOCBF5 = 0;
+  nei();
+}
+
 void btn_isr()
 {
   if (IOCIF) {
diff --git a/btn.h b/btn.h
index e0ce0aeb1498f8150d618ff331517ec91a9f5196..093298bb86c66b0134bf63396e62bf2fe0b31652 100644 (file)
--- a/btn.h
+++ b/btn.h
@@ -30,37 +30,19 @@ enum {
 #define btn_pb() (RB0)
 
 /* Read the current state of the rocker switch */
-#define btn_rs() ((RB4 + ((unsigned char)RB5 << 1)) % 3)
+#define btn_rs() (RB4 + ((unsigned char)RB5 << 1))
 
 /* Enable the pushbutton from user code */
-#define btn_pben() do { \
-    ndi(); \
-    IOCBP0 = 1; IOCBN0 = 1; IOCBF0 = 0; \
-    nei(); \
-  } while (0)
+void btn_pben();
 
 /* Disable the pushbutton from user code */
-#define btn_pbdis() do { \
-    ndi(); \
-    IOCBP0 = 0; IOCBN0 = 0; IOCBF0 = 0; \
-    nei(); \
-  } while (0)
+void btn_pbdis();
 
 /* Enable the rocker switch from user code */
-#define btn_rsen() do { \
-    ndi(); \
-    IOCBP4 = 1; IOCBN4 = 1; IOCBF4 = 0; \
-    IOCBP5 = 1; IOCBN5 = 1; IOCBF5 = 0; \
-    nei(); \
-  } while (0)
+void btn_rsen();
 
 /* Disable the rocker switch from user code */
-#define btn_rsdis() do { \
-    ndi(); \
-    IOCBP4 = 0; IOCBN4 = 0; IOCBF4 = 0; \
-    IOCBP5 = 0; IOCBN5 = 0; IOCBF5 = 0; \
-    nei(); \
-  } while (0)
+void btn_rsdis();
 
 /* Initialize the button module */
 void btn_init();
diff --git a/isr.c b/isr.c
index a1b9a491d3602259a8dc9396fcff7b793be95c10..8db018f3672723d06a24b8e2799d8cb0cdba7fc1 100644 (file)
--- a/isr.c
+++ b/isr.c
 bit _isr_gie; /* Used to store the state of GIE for nested ndi()/nei() */
 unsigned char _isr_di; /* Count of nested ndi() */
 
+void ndi()
+{
+  if (_isr_di++ == 0) {
+    _isr_gie = GIE;
+    di();
+  }
+}
+
+/* Nested enable interrupts inline function.  Should be OK even in ISR. */
+void nei()
+{
+  if (--_isr_di == 0 && _isr_gie)
+    ei();
+}
+
 void interrupt isr()
 {
   tmr_isr();
diff --git a/isr.h b/isr.h
index 4b847740632b9036432607db89e009b99f50829c..220b38191c9ff82c3e2246771e829e35acf84db3 100644 (file)
--- a/isr.h
+++ b/isr.h
@@ -13,20 +13,10 @@ extern bit _isr_gie;
 extern unsigned char _isr_di;
 
 /* Nested disable interrupts inline function.  Should be OK even in ISR. */
-#define ndi() \
-  do { \
-    if (_isr_di++ == 0) { \
-      _isr_gie = GIE; \
-      di(); \
-    } \
-  } while (0)
+void ndi();
 
 /* Nested enable interrupts inline function.  Should be OK even in ISR. */
-#define nei() \
-  do { \
-    if (--_isr_di == 0 && _isr_gie) \
-      ei(); \
-  } while (0)
+void nei();
 
 void interrupt isr();
 
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);
 }
diff --git a/rgb.h b/rgb.h
index d24b41bf34cd3a389cb62b6865551911009608d5..05a061c823c3922cfa72bab0139164afc0ab27b0 100644 (file)
--- a/rgb.h
+++ b/rgb.h
@@ -14,7 +14,7 @@
 void rgb_init();
 
 /* Turn on the rgb.  Outputs are zero, or last values set by rgb_set(). */
-#define rgb_on() do { TMR2ON = 1; } while (0)
+#define rgb_on() (TMR2ON = 1)
 
 /* Turn off the rgb, first setting outputs to zero. */
 void rgb_off();
diff --git a/task.c b/task.c
index bce2b6dbaf30e59a4b3bb7ab25dfe75a3c02ede3..d704d60bd850d6042c95641b5eaeb897af8d8dab 100644 (file)
--- a/task.c
+++ b/task.c
@@ -36,22 +36,24 @@ task_id_t task_get(unsigned char block)
     nei();
     if (ids) {
       for (unsigned char i = 0; t == -1 && i < TASK_COUNT; i++) {
-        if (bit_get(ids, _task_bitno))
+        if (bit_get(ids, _task_bitno)) {
           t = _task_bitno;
+          ndi();
+          bit_clr(_task_ids, t);
+          nei();
+        }
         if (++_task_bitno == TASK_COUNT)
           _task_bitno = 0;
       }
     }
-#if 0 /* Not until we have a crystal and can wake from sleep via tmr module */
+#if 0
+    /* Something like this when we have a crystal.  But watch for the race of
+     * going to sleep when a task is posted by an ISR.
+     */
     else
       SLEEP();
 #endif
   } while (t == -1 && block == 1);
-  if (t >= 0) {
-    ndi();
-    bit_clr(_task_ids, t);
-    nei();
-  }
   return t;
 }
 
diff --git a/task.h b/task.h
index b53640f6ae946965f8d28a485dbf9d76f1898ef2..d08a36307ce65f97b15d44c4e4801f97bc7ed3e1 100644 (file)
--- a/task.h
+++ b/task.h
@@ -41,10 +41,10 @@ extern unsigned long _task_ids;
 extern task_id_t _task_bitno;
 
 /* Post task t.  No need for ndi() since bit_set is a single instruction. */
-#define task_post(t) do { bit_set(_task_ids, t); } while (0)
+#define task_post(t) (bit_set(_task_ids, t))
 
 /* Initialize the task subsystem */
-#define task_init() do { _task_ids = 0; _task_bitno = 0; } while (0)
+#define task_init() (_task_ids = 0)
 
 /* Returns non-zero if one or more tasks are posted.  Does not block. */
 bit task_check();