X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=main.c;h=e98dd0474b6a05b6e26f73c936899f3a82fc9728;hb=4f58810d846981a1c180c60aa617fdcaba44d1a7;hp=46fe66aae3e7b118465853ef1ee9e99e5d8f4019;hpb=b1783c6a8eab4b9e72ec2166112aabe87e4e4de5;p=rgblamp.git diff --git a/main.c b/main.c index 46fe66a..e98dd04 100644 --- a/main.c +++ b/main.c @@ -29,9 +29,10 @@ #include #include "picinit.h" #include "unused.h" -#include "buttons.h" +#include "btn.h" #include "rgb.h" #include "tmr.h" +#include "task.h" #include "adc_random.h" #if 0 @@ -41,8 +42,6 @@ #define AUTO_OFF_COUNT 1831 /* 1 minute in 32.768 ms units */ #define AUTO_ON_COUNT 3662 /* 2 minutes in 32.768 ms units */ #endif -#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(s) (min_incolor_steps[s & 3] + \ @@ -67,7 +66,6 @@ typedef struct { int increment; signed char remainder; } led_t; -#define INIT_LED { 0, 0, 0 } /* The index of all step arrays is the speed variable */ #if 1 @@ -82,123 +80,195 @@ const static int min_fade_steps[4] = { 64, 32, 16, 8 }; const static int range_fade_steps[4] = { 1, 1, 1, 1 }; #endif -int main(void) +led_t red; +led_t grn; +led_t blu; +led_t wht; +bit on; +unsigned char speed; +int fade_steps; + +void start_fade() +{ + /* RGB PWM values are 8 bits, but computations are done in 15 + * (leaving room for a sign bit), so leds_set() uses >>7 to convert + * to PWM values. + */ + int newr, newg, newb, neww; + + /* New RGB values; all zero is not a valid option */ + do { + newr = rand(); + newg = rand(); + newb = rand(); + neww = rand(); + } while (newr == 0 && newg == 0 && newb == 0 && neww == 0); + + /* Random # of steps to reach the new color */ + fade_steps = rand_fade_steps(speed); + + /* Compute increment per fade step, and remainder, for each led */ + 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); + + /* Start the fade timer */ + tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */ +} + +void turnOn() +{ + dbgpin_high(); + on = 1; + red.value = 0; + grn.value = 0; + blu.value = 0; + wht.value = 0; + leds_set(red, grn, blu, wht); + rgb_on(); + start_fade(); +} + +void turnOff() +{ + /* Event on to off, either by switch or auto-off timer */ + tmr_stop(TMR_INCOLOR); + tmr_stop(TMR_FADE); + rgb_off(); + dbgpin_low(); + on = 0; +} + +void pb_task() +{ + if (btn_pb() == BTN_PB_UP) { + speed = (speed + 1) & ~4; + tmr_stop(TMR_INCOLOR); + start_fade(); + } + btn_pben(); +} + +void rs_task() +{ + switch (btn_rs()) { + case BTN_RS_OFF: + tmr_stop(TMR_AUTO_OFFON); + turnOff(); + break; + case BTN_RS_RIGHT: + tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT); + /* fall through */ + case BTN_RS_LEFT: + turnOn(); + break; + } + btn_rsen(); +} + +void fade_task() +{ + red.value += red.increment; + grn.value += grn.increment; + blu.value += blu.increment; + wht.value += wht.increment; + if (--fade_steps == 0) { + /* This is the last fade step. Finalize the RGB led states and decide how + * long to stay in this color. + */ + red.value += red.remainder; + grn.value += grn.remainder; + blu.value += blu.remainder; + wht.value += wht.remainder; + tmr_stop(TMR_FADE); + tmr_start(TMR_INCOLOR, rand_incolor_steps(speed)); + } + leds_set(red, grn, blu, wht); +} + +void auto_offon_task() { - led_t red = INIT_LED; - led_t grn = INIT_LED; - led_t blu = INIT_LED; - led_t wht = INIT_LED; - unsigned char speed = 0; - unsigned incolor_steps; - int fade_steps; - unsigned char buttons, last_buttons; - - pic_init(); - unused_init(); - buttons_init(); - rgb_init(); - tmr_init(); - dbgpin_init(); - - srand((adc_random() << 8) + adc_random()); - reset_steps(); - if (buttons_on()) - rgb_on(); - - dbgpin_high(); - last_buttons = 0; - buttons = buttons_read(); - while (1) { - /* Wait for an event. Timer firing or button state change. - * When we have a crystal, we can sleep instead. Also, soon we - * can transition button state changes to the ISR. - */ - dbgpin_low(); - while (!tmr_events() && last_buttons == buttons) { - last_buttons = buttons; - buttons = buttons_read(); - } - dbgpin_high(); - - if (((buttons & IN_ROCKERB) && !tmr_on(TMR_AUTO_OFFON)) || - !(last_buttons & (IN_ROCKERB | IN_ROCKERA))) { - /* Event: off to on */ - tmr_startPeriodic(TMR_AUTO_OFFON, AUTO_OFF_COUNT); - tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */ - - } else if (((buttons & IN_ROCKERB) && tmr_fired(TMR_AUTO_OFFON)) || - (!(buttons & (IN_ROCKERA | IN_ROCKERB)))) { - /* Event on to off, either by switch or auto-off timer */ - rgb_off(); - dbgpin_low(); - tmr_stop(TMR_AUTO_OFFON); - tmr_stop(TMR_FADE); - buttons_sleep(); - dbgpin_high(); - rgb_on(); - red.value = 0; - grn.value = 0; - blu.value = 0; - wht.value = 0; - reset_steps(); - tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */ - - } else if (tmr_fired(TMR_FADE)) { - /* Other actions, driven by the fade timer for now */ - - /* Crappy way to detect rising edges to change state of speed var */ - if (!(speed & 4) && (buttons & IN_PUSHBTN)) - speed |= 4; - else if ((speed & 4) && !(buttons & IN_PUSHBTN)) { - speed = (speed + 1) & ~4; - reset_steps(); - } - - if (fade_steps) { - /* Continue the in-progress fade */ - fade_steps--; - red.value += red.increment; - grn.value += grn.increment; - blu.value += blu.increment; - wht.value += wht.increment; - if (fade_steps == 0) { - red.value += red.remainder; - grn.value += grn.remainder; - blu.value += blu.remainder; - wht.value += wht.remainder; - } - leds_set(red, grn, blu, wht); - - } else if (--incolor_steps == 0) { - /* RGB PWM values are 8 bits, but computations are done in 15 - * (leaving room for a sign bit), so leds_set() uses >>7 to convert - * to PWM values. - */ - int newr, newg, newb, neww; - - /* New RGB values; all zero is not a valid option */ - do { - 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(speed); - fade_steps = rand_fade_steps(speed); - - /* Compute increment and remainder for each led */ - 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); - } - } + if (on) { + turnOff(); + if (btn_rs() == BTN_RS_RIGHT) + tmr_start(TMR_AUTO_OFFON, AUTO_ON_COUNT); + } else /* off */ { + turnOn(); + if (btn_rs() == BTN_RS_RIGHT) + tmr_start(TMR_AUTO_OFFON, AUTO_OFF_COUNT); } - return 0; } + +void user_boot() +{ + dbgpin_high(); + srand((adc_random() << 8) + adc_random()); + pb_task(); + rs_task(); +} + +void user_tasks(unsigned char block) +{ + task_id_t tid; + + while ((tid = task_get(block)) >= 0) { + switch (tid) { + case TASK_BTN_PB: /* pushbutton state change */ + pb_task(); + break; + case TASK_BTN_RS: /* rocker switch state change */ + rs_task(); + break; + case TASK_FADE: /* fade timer has fired */ + fade_task(); + break; + case TASK_INCOLOR: /* in-color timer has fired */ + start_fade(); + break; + case TASK_AUTO_OFFON: /* auto on/off timer has fired */ + auto_offon_task(); + break; + } + } +} + +int main(void) +{ + /* Platform initialization */ + pic_init(); + unused_init(); + btn_init(); + rgb_init(); + tmr_init(); + task_init(); + dbgpin_init(); + +#if 0 + /* Execute tasks until the queue empties */ + user_tasks(0); +#endif + +#if 0 + /* Software initialization */ +#endif + +#if 0 + /* Execute tasks until the queue empties */ + user_tasks(0); +#endif + + /* Run user boot code */ + user_boot(); + + /* Process tasks forever */ + user_tasks(1); + + /* Prevent return from main, which causes a device reset */ + while (1); +} +