X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=main.c;h=6eaf3d77159fdcfe566e85dbf2f7b2ec06a77aa1;hb=HEAD;hp=37fda9036ac1083a474eca0d02a1cd18a47932c8;hpb=8c7b4fef50432ef0775b00b3fe7737a1b924b1af;p=rgblamp.git diff --git a/main.c b/main.c index 37fda90..6eaf3d7 100644 --- a/main.c +++ b/main.c @@ -1,3 +1,32 @@ +/* + * Copyright © 2011, 2012, Titanium Mirror, Inc.. + * All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * - Neither the name of Titanium Mirror, Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + /* * File: main.c * @@ -46,14 +75,6 @@ #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 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 dbgpin_init() do { \ @@ -68,7 +89,6 @@ enum { /* Operating modes */ MODE_SOLID = 0, /* Cycle through colors[][] before next mode */ - //MODE_CANDLE, MODE_CYCLE, /* Auto cycle through colors */ MODE_PARTY, /* Random yet fast incolor and fade */ MODE_COUNT, @@ -82,20 +102,22 @@ enum { HELD_TMR_PERIODS = 1, /* Held timer fires every 32.768 msec */ HELD_PERIODS = 16, /* Held this long before held events fire */ - COLOR_WHITE = 0, /* Color index for white */ - COLOR_COUNT = 11, + COLOR_COUNT = 12, + COLOR_CANDLE = 0, /* Color index for the candle color */ + COLOR_WHITE = 11, /* Color index for white */ BRIGHT_TOP = 100, /* Bright ramps up to top ... */ BRIGHT_BOTTOM = -5, /* ... then down to bottom */ BRIGHT_MAX = 85, /* Bright values are constrained for computations */ BRIGHT_MIN = 1, /* ... by min and max. */ - BRIGHT_INIT = 42, /* later, get from eeprom */ + BRIGHT_INIT = 42, /* Initial value is replaced by value from eeprom */ + CBRIGHT_VARIANCE = 20, /* Brightness range for candle flicker */ STD_FADE = 16, /* Fade time 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 lfsr_get() */ /* ... see start_fade() */ CFG_ADDR = 0, /* EEPROM address of start of configuration */ - CFG_DELAY = 92, /* 3 seconds in 32.768 msec units */ + CFG_DELAY = 183, /* 6 seconds in 32.768 msec units */ }; typedef struct { @@ -104,8 +126,12 @@ typedef struct { signed char remainder; } led_t; +/* Offsets to brightness used for candle color */ +const signed char offsets[4] = { -1, -1, 1, 1 }; + /* Available colors, 6 bits per color (no values above 0x3f) */ const unsigned char colors[COLOR_COUNT][LED_COUNT] = { + { 0x2c, 0x20, 0x00, 0x18 }, /* candle */ { 0x28, 0x00, 0x00, 0x00 }, /* red */ { 0x30, 0x20, 0x00, 0x00 }, /* orange */ { 0x2c, 0x2c, 0x00, 0x00 }, /* yellow */ @@ -120,7 +146,7 @@ const unsigned char colors[COLOR_COUNT][LED_COUNT] = { }; unsigned char mode = MODE_SOLID; -unsigned char color = COLOR_WHITE; +unsigned char color = COLOR_CANDLE; signed char bright = BRIGHT_INIT; bit bright_up; led_t red; @@ -130,14 +156,23 @@ led_t wht; bit on; int fade_steps; signed char pbHeldCount; +signed char cbright; void cfg_write() { unsigned char addr = CFG_ADDR; + unsigned char m, c; + signed char b; - eeprom_write(addr++, mode); - eeprom_write(addr++, color); - eeprom_write(addr++, bright); + m = eeprom_read(addr++); + c = eeprom_read(addr++); + b = eeprom_read(addr); + if (mode != m || color != c || bright != b) { + addr = CFG_ADDR; + eeprom_write(addr++, mode); + eeprom_write(addr++, color); + eeprom_write(addr, bright); + } } void cfg_read() @@ -153,7 +188,7 @@ void cfg_read() if (tmp < COLOR_COUNT) color = tmp; - tmp = eeprom_read(addr++); + tmp = eeprom_read(addr); if ((signed char)tmp >= BRIGHT_BOTTOM && (signed char)tmp <= BRIGHT_TOP) bright = tmp; } @@ -175,12 +210,16 @@ void leds_set() { signed char b; - if (bright > BRIGHT_MAX) - b = BRIGHT_MAX; - else if (bright < BRIGHT_MIN) - b = BRIGHT_MIN; + /* Use correct brightness value, then constrain it */ + if (mode != MODE_PARTY && color != COLOR_WHITE) + b = cbright; else b = bright; + if (b > BRIGHT_MAX) + b = BRIGHT_MAX; + else if (b < BRIGHT_MIN) + b = BRIGHT_MIN; + rgb_set(led_get((red).value, b), led_get((grn).value, b), led_get((blu).value, b), led_get((wht).value, b)); } @@ -219,6 +258,7 @@ void start_fade() wht.remainder = neww - (wht.value + wht.increment * fade_steps); /* Start the fade timer */ + tmr_stop(TMR_CANDLE); tmr_stop(TMR_INCOLOR); tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */ } @@ -232,10 +272,11 @@ void turnOn() btn_pben(); } +/* Event on to off, either by switch or auto-off timer */ void turnOff() { - /* Event on to off, either by switch or auto-off timer */ tmr_stop(TMR_CFG); + tmr_stop(TMR_CANDLE); tmr_stop(TMR_INCOLOR); tmr_stop(TMR_FADE); rgb_off(); @@ -248,8 +289,6 @@ void turnOff() void pb_clicked() { if (on) { - unsigned char omode = mode; - if (mode == MODE_SOLID) { if (++color == COLOR_COUNT) { color = 0; @@ -260,17 +299,14 @@ void pb_clicked() mode = 0; color = 0; } - if (mode != omode || mode == MODE_SOLID) - tmr_start(TMR_CFG, CFG_DELAY); + tmr_start(TMR_CFG, CFG_DELAY); start_fade(); } } +/* The button is being held. Change the color magnitude. */ void pb_held() { - signed char obright = bright; - - /* The button has been held. Change the color magnitude. */ if (bright_up) { if (bright == BRIGHT_TOP) bright_up = 0; @@ -282,8 +318,7 @@ void pb_held() else bright--; } - if (bright != obright) - tmr_start(TMR_CFG, CFG_DELAY); + tmr_start(TMR_CFG, CFG_DELAY); leds_set(); } @@ -337,6 +372,21 @@ void rs_task() } } +void candle_task() +{ + /* Flicker by using the brightness value */ + cbright += offsets[lfsr_get() & 0x03]; + + /* Constraight cbright to a range about bright */ + if (cbright > bright) + cbright = bright; + else if (cbright < bright - CBRIGHT_VARIANCE) + cbright = bright - CBRIGHT_VARIANCE; + + /* Adjust the LEDs */ + leds_set(); +} + void fade_task() { red.value += red.increment; @@ -351,11 +401,18 @@ void fade_task() grn.value += grn.remainder; blu.value += blu.remainder; wht.value += wht.remainder; + tmr_stop(TMR_CANDLE); tmr_stop(TMR_FADE); - if (mode == MODE_CYCLE) - tmr_start(TMR_INCOLOR, STD_INCOLOR); - else if (mode == MODE_PARTY) + if (mode == MODE_PARTY) tmr_start(TMR_INCOLOR, PARTY_MIN + (lfsr_get() % PARTY_RANGE)); + else { + if (mode == MODE_CYCLE) + tmr_start(TMR_INCOLOR, STD_INCOLOR); + if (color != COLOR_WHITE) { + cbright = bright - CBRIGHT_VARIANCE / 2; + tmr_startPeriodic(TMR_CANDLE, 1); + } + } } leds_set(); } @@ -387,25 +444,28 @@ void user_tasks(unsigned char block) while ((tid = task_get(block)) >= 0) { switch (tid) { - case TASK_BTN_PB: /* pushbutton state change */ + case TASK_BTN_PB: pb_task(); break; - case TASK_BTN_RS: /* rocker switch state change */ + case TASK_BTN_RS: rs_task(); break; - case TASK_BTN_PB_HELD: /* pushbutton is being held down */ + case TASK_BTN_PB_HELD: pb_held_task(); break; - case TASK_FADE: /* fade timer has fired */ + case TASK_FADE: fade_task(); break; - case TASK_INCOLOR: /* in-color timer has fired */ + case TASK_INCOLOR: start_fade(); break; - case TASK_TMR32: /* auto on/off event */ + case TASK_CANDLE: + candle_task(); + break; + case TASK_TMR32: auto_offon_task(); break; - case TASK_CFG: /* Save config to EEPROM event */ + case TASK_CFG: cfg_write(); break; }