From fcf61ef3e7a1012c67f14096d0bbf47279297398 Mon Sep 17 00:00:00 2001 From: "R. Steve McKown" Date: Mon, 5 Dec 2011 19:18:15 -0700 Subject: [PATCH] Timing as const arrays For some reason, holding the pushbutton goofs up things... --- main.c | 67 ++++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/main.c b/main.c index 4086fec..f298799 100644 --- a/main.c +++ b/main.c @@ -12,37 +12,40 @@ #include "buttons.h" #include "rgb.h" -#define STEP_SIZE 32 /* ms */ -#if 0 -#define MIN_INCOLOR_STEPS 320 -#define MAX_INCOLOR_STEPS 57600 -#define MIN_FADE_STEPS 32 -#define MAX_FADE_STEPS 480 -#else -#define MIN_INCOLOR_STEPS 32 -#define MAX_INCOLOR_STEPS 160 -#define MIN_FADE_STEPS 32 -#define MAX_FADE_STEPS 160 -#endif -#define reset_steps() do { incolor_steps = 1; fade_steps = 0; } while (0) +#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() & 0xff)) -#define rand_incolor_steps() (MIN_INCOLOR_STEPS + rand_u16() % \ - (MAX_INCOLOR_STEPS - MIN_INCOLOR_STEPS)) -#define rand_fade_steps() (MIN_FADE_STEPS + rand() % \ - (MAX_FADE_STEPS - MIN_FADE_STEPS)) +#define rand_u16() ((rand() << 8) + rand_u8()) +#define rand_incolor_steps(f) (min_incolor_steps[f] + \ + (rand() % range_incolor_steps[f])) +#define rand_fade_steps(f) (min_fade_steps[f] + \ + (rand() % range_fade_steps[f])) #define leds_set(r,g,b,w) rgb_set((r).value >> 7, \ - (g).value >> 7, \ - (b).value >> 7, 0) -// (w).value >> 7) + (g).value >> 7, \ + (b).value >> 7, 0) +// (w).value >> 7) typedef struct { int value; int increment; - int remainder; + 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, 64 }; +const static unsigned range_incolor_steps[2] = { 1, 1 }; +const static int min_fade_steps[2] = { 32, 32 }; +const static int range_fade_steps[2] = { 1, 1 }; +#endif + int main(void) { led_t red = INIT_LED; @@ -90,13 +93,12 @@ int main(void) } else if (--incolor_steps == 0) { 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 { - /* RGB values are processed 15-bit values. This allows for - * signedness when fading to a lesser value. The actual PWM - * values are set from the most significant 8 bits, so - * leds_set() does >> 7. - */ newr = rand(); newg = rand(); newb = rand(); @@ -104,13 +106,8 @@ int main(void) } 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 - red.value) / fade_steps; -- 2.39.2