X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=main.c;h=627c94a3158d84a8933fee0436c7e227eaab950e;hb=a0556afb8953ae458fb0fe7bcc68bde851f0e630;hp=0f98b6aada831887f1b8be768b02c62071892a44;hpb=e2680ea449bfc8a72e1d03fbfbec76c83fa0bd18;p=rgblamp.git diff --git a/main.c b/main.c index 0f98b6a..627c94a 100644 --- a/main.c +++ b/main.c @@ -64,6 +64,19 @@ #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 */ @@ -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; @@ -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(); } } @@ -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; } } }