X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=main.c;h=627c94a3158d84a8933fee0436c7e227eaab950e;hb=a0556afb8953ae458fb0fe7bcc68bde851f0e630;hp=f9d45cfa0b6bd68a714ee85a8855f48df2254bd2;hpb=4991af14bca5cf6283f569058843d1a082940f9a;p=rgblamp.git diff --git a/main.c b/main.c index f9d45cf..627c94a 100644 --- a/main.c +++ b/main.c @@ -45,16 +45,16 @@ #include "task.h" #include "adc_random.h" -#define AUTO_OFF_COUNT 150 /* 5 mins on*/ -#define AUTO_ON_COUNT 300 /* 10 mins off */ +//#define AUTO_OFF_COUNT 150 /* 5 mins on*/ +//#define AUTO_ON_COUNT 300 /* 10 mins off */ //#define AUTO_OFF_COUNT 450 /* 15 mins on*/ //#define AUTO_ON_COUNT 1350 /* 45 mins off */ //#define AUTO_OFF_COUNT 600 /* 20 mins on*/ //#define AUTO_ON_COUNT 1200 /* 40 mins off */ //#define AUTO_OFF_COUNT 450 /* 2 hours on*/ //#define AUTO_ON_COUNT 1350 /* 22 hours off */ -//#define AUTO_OFF_COUNT 9000U /* 5 hrs in 2 sec units */ -//#define AUTO_ON_COUNT 34200U /* 19 hrs in 2 sec units */ +#define AUTO_OFF_COUNT 9000U /* 5 hrs in 2 sec units */ +#define AUTO_ON_COUNT 34200U /* 19 hrs in 2 sec units */ #define dbgpin_init() do { \ /* Set RA2 as output low */ \ RA2 = 0; \ @@ -64,11 +64,24 @@ #define dbgpin_low() (RA2 = 0) #define dbgpin_toggle() (RA2 = (LATA2 == 0) ? 1 : 0) +#define cfg_write(mode, color) do { \ + eeprom_write(CFG_MODE_ADDR, (mode)); \ + eeprom_write(CFG_COLOR_ADDR, (color)); \ + } while (0) + +#define cfg_read(mode, color) do { \ + unsigned char tmp; \ + tmp = eeprom_read(CFG_MODE_ADDR); \ + mode = (tmp < MODE_COUNT) ? tmp : MODE_SOLID; \ + tmp = eeprom_read(CFG_COLOR_ADDR); \ + color = (tmp < COLOR_COUNT) ? tmp : 0; \ + } while (0) + enum { /* Operating modes */ MODE_SOLID = 0, /* Cycle through colors[][] before next mode */ //MODE_CANDLE, - MODE_FADE, /* Auto cycle through colors */ + MODE_CYCLE, /* Auto cycle through colors */ MODE_PARTY, /* Random yet fast incolor and fade */ MODE_COUNT, @@ -89,10 +102,13 @@ enum { BRIGHT_MIN = 1, /* ... by min and max. */ BRIGHT_INIT = 42, /* later, get from eeprom */ STD_FADE = 16, /* Fade time in 32.768 ms units */ - STD_INCOLOR = 29491, /* Time in color when MODE_FADE, in 32.768 ms units */ + STD_INCOLOR = 29491, /* Time in color when MODE_CYCLE, in 32.768 ms units */ PARTY_MIN = 8, /* Min party fade and incolor units */ PARTY_RANGE = 8, /* Party fade/incolor range mask for rand() */ /* ... see start_fade() */ + CFG_MODE_ADDR = 0, /* EEPROM address of mode variable */ + CFG_COLOR_ADDR, /* EEPROM address of color variable */ + CFG_DELAY = 92, /* 3 seconds in 32.768 msec units */ }; typedef struct { @@ -128,44 +144,37 @@ signed char bright = BRIGHT_INIT; bit bright_up; signed char pbHeldCount; -/* Return the constrained brightness level for use in computing RGB values */ -unsigned char bright_get() -{ - if (bright > BRIGHT_MAX) - return BRIGHT_MAX; - else if (bright < BRIGHT_MIN) - return BRIGHT_MIN; - else - return bright; -} - -/* RGB values will not illuminate the LED */ -unsigned char led_get(unsigned value) +/* Combine LED color value and brightness level to generate an RGB value. + * bright will be BRIGHT_MIN...BRIGHT_MAX + * + * @param value 0...1023 (6 bit color, left shifted 4 bits, for 10 bits) + * @param b brightness value BRIGHT_MIN...BRIGHT_MAX + * @return An RGB drive value, 0...255 + */ +unsigned char led_get(int value, signed char b) { - value = (value * bright_get()) >> 8; -#ifdef CEIL256 - if (value && value < 0x26) - return 0x26; -#else - if (value && value < 4) - return 4; -#endif - return value; + return (unsigned long)value * b / 4 / BRIGHT_MAX; } -/* Set the LEDs using both color and brightness values. */ +/* Set the LEDs using color values and current brightness. */ void leds_set() { - rgb_set(led_get((red).value), - led_get((grn).value), led_get((blu).value), - led_get((wht).value)); + signed char b; + + if (bright > BRIGHT_MAX) + b = BRIGHT_MAX; + else if (bright < BRIGHT_MIN) + b = BRIGHT_MIN; + else + b = bright; + rgb_set(led_get((red).value, b), led_get((grn).value, b), + led_get((blu).value, b), led_get((wht).value, b)); } 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. + /* RGB PWM values are 8 bits. Color values are 12 bits. See led_get() + * for the conversion of color values and brightness to RGB values. */ int newr, newg, newb, neww; @@ -174,7 +183,7 @@ void start_fade() if (mode == MODE_PARTY) { color = rand() % COLOR_COUNT; fade_steps = PARTY_MIN + (rand() % PARTY_RANGE); - } else if (mode == MODE_FADE) { + } else if (mode == MODE_CYCLE) { if (++color == COLOR_COUNT) color = 0; } @@ -212,6 +221,7 @@ void turnOn() void turnOff() { /* Event on to off, either by switch or auto-off timer */ + tmr_stop(TMR_CFG); tmr_stop(TMR_INCOLOR); tmr_stop(TMR_FADE); rgb_off(); @@ -224,6 +234,8 @@ void turnOff() void pb_clicked() { if (on) { + unsigned char omode = mode; + if (mode == MODE_SOLID) { if (++color == COLOR_COUNT) { color = 0; @@ -234,6 +246,8 @@ void pb_clicked() mode = 0; color = 0; } + if (mode != omode || mode == MODE_SOLID) + tmr_start(TMR_CFG, CFG_DELAY); start_fade(); } } @@ -320,7 +334,7 @@ void fade_task() blu.value += blu.remainder; wht.value += wht.remainder; tmr_stop(TMR_FADE); - if (mode == MODE_FADE) + if (mode == MODE_CYCLE) tmr_start(TMR_INCOLOR, STD_INCOLOR); else if (mode == MODE_PARTY) tmr_start(TMR_INCOLOR, PARTY_MIN + (rand() % PARTY_RANGE)); @@ -341,19 +355,11 @@ void auto_offon_task() } } -void config_read() -{ - /* Read configuration fields from eeprom, notably current mode and - * brightness level. - */ - /* FIXME: implement this */ -} - void user_boot() { dbgpin_high(); srand((adc_random() << 8) + adc_random()); - config_read(); + cfg_read(mode, color); rs_task(); } @@ -381,6 +387,9 @@ void user_tasks(unsigned char block) case TASK_TMR32: /* auto on/off event */ auto_offon_task(); break; + case TASK_CFG: /* Save config to EEPROM event */ + cfg_write(mode, color); + break; } } }