X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=main.c;h=627c94a3158d84a8933fee0436c7e227eaab950e;hb=a0556afb8953ae458fb0fe7bcc68bde851f0e630;hp=f062b41ff05a5dd3e67a6a36fb666c51e7252836;hpb=40d9cdd17ca886222d3a5df6ed1a2f8958f92f6c;p=rgblamp.git diff --git a/main.c b/main.c index f062b41..627c94a 100644 --- a/main.c +++ b/main.c @@ -1,113 +1,432 @@ /* * File: main.c * - * Created on August 16, 2010, 12:09 PM + * PWM test program + * + * PIC resources in use, 28-pin DIP, by pin: + * - ( 1) RE3 - Vpp/MCLR# + * - ( 2) RA0 - unused + * - ( 3) RA1 - unused + * - ( 4) RA2 - unused + * - ( 5) RA3 - unused + * - ( 6) RA4 - unused (could become PGM/LVP, but apparently not needed) + * - ( 7) RA5 - unused + * - ( 8) Vss + * - ( 9) RA7 - unused + * - (10) RA6 - unused + * - (11) RC0 - T1OSO + * - (12) RC1 - T1OSI + * - (13) RC2 - CCP1 PWM for wht LED + * - (14) RC3 - unused + * - (15) RC4 - unused + * - (16) RC5 - unused + * - (17) RC6 - unused + * - (18) RC7 - unused + * - (19) Vss + * - (20) Vdd + * - (21) RB0 - CCP4 PWM for blu LED + * - (22) RB1 - Pushbutton + * - (23) RB2 - Rocker switch, right + * - (24) RB3 - CCP2 PWM for grn LED + * - (25) RB4 - Rocker switch, left + * - (26) RB5 - CCP3 PWM for red LED + * - (27) RB6 - ICSPCLK + * - (28) RB7 - ICSPDAT */ - #include -#include +#include +#include "picinit.h" +#include "unused.h" +#include "btn.h" +#include "rgb.h" +#include "tmr.h" +#include "tmr32.h" +#include "task.h" +#include "adc_random.h" -#define RUNAT32MHZ /* else 16 MHz */ -#define LEDBIT 0x01 /* PORTB.3 */ +//#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 { \ + /* Set RA2 as output low */ \ + RA2 = 0; \ + TRISA2 = 0; \ + } while (0) +#define dbgpin_high() (RA2 = 1) +#define dbgpin_low() (RA2 = 0) +#define dbgpin_toggle() (RA2 = (LATA2 == 0) ? 1 : 0) -#if defined(RUNAT32MHZ) -__CONFIG(WDTE_OFF & FOSC_INTOSC); -__CONFIG(LVP_OFF & PLLEN_ON); -#else -__CONFIG(WDTE_OFF); -__CONFIG(LVP_OFF); -#endif +#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_CYCLE, /* Auto cycle through colors */ + MODE_PARTY, /* Random yet fast incolor and fade */ + MODE_COUNT, + + /* Indexes in colors[][] for LEDs */ + LED_RED = 0, + LED_GRN, + LED_BLU, + LED_WHT, + LED_COUNT, + + 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, + 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 */ + 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 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 { + int value; + int increment; + signed char remainder; +} led_t; -void pic_init() +/* Available colors, 6 bits per color (no values above 0x3f) */ +const unsigned char colors[COLOR_COUNT][LED_COUNT] = { + { 0x28, 0x00, 0x00, 0x00 }, /* red */ + { 0x30, 0x20, 0x00, 0x00 }, /* orange */ + { 0x2c, 0x2c, 0x00, 0x00 }, /* yellow */ + { 0x20, 0x2c, 0x00, 0x00 }, /* yellow-green */ + { 0x00, 0x30, 0x00, 0x00 }, /* green */ + { 0x00, 0x30, 0x18, 0x00 }, /* green-cyan */ + { 0x00, 0x30, 0x28, 0x00 }, /* cyan */ + { 0x00, 0x18, 0x30, 0x00 }, /* cyan-blue */ + { 0x00, 0x00, 0x30, 0x00 }, /* blue */ + { 0x1c, 0x00, 0x30, 0x00 }, /* magenta */ + { 0x3f, 0x3f, 0x3f, 0x3f }, /* white */ +}; + +unsigned char mode = MODE_SOLID; +unsigned char color = COLOR_WHITE; +led_t red; +led_t grn; +led_t blu; +led_t wht; +bit on; +int fade_steps; +signed char bright = BRIGHT_INIT; +bit bright_up; +signed char pbHeldCount; + +/* 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) { -#if defined(RUNAT32MHZ) - OSCCON = 0b11110000; -#else /* 16 MHz */ - OSCCON = 0b01111010; -#endif + return (unsigned long)value * b / 4 / BRIGHT_MAX; +} - /* OSCSTAT.HFIOFL is set when oscillator is locked (accurate within 2%) */ - while (!HFIOFL); +/* Set the LEDs using color values and current brightness. */ +void leds_set() +{ + 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 pwm_init() +void start_fade() { - /* Initialize PWM on PORTB.0 - * - Fosc = 32MHz - * - Prescale = 16 - * - PRx value = 0xff - * = f(pwm) = 1.95 kHz - */ + /* 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; - /* Disable output on PORTB.0 */ - TRISB |= 0x08; + /* Select the destination color and fade-to time depending upon mode. */ + fade_steps = STD_FADE; + if (mode == MODE_PARTY) { + color = rand() % COLOR_COUNT; + fade_steps = PARTY_MIN + (rand() % PARTY_RANGE); + } else if (mode == MODE_CYCLE) { + if (++color == COLOR_COUNT) + color = 0; + } - /* Configure ECCP1 */ - CCP1CON = 0b00001100; - CCPR1L = 0; /* Initial PWM value; only using 8 LSBs */ + /* Retrieve the destination color */ + newr = colors[color][LED_RED] << 4; + newg = colors[color][LED_GRN] << 4; + newb = colors[color][LED_BLU] << 4; + neww = colors[color][LED_WHT] << 4; - /* Configure Timer2 */ - CCPTMRS = CCPTMRS & ~0x03; /* bits 1:0 denote CCP1 uses Timer 2 */ - TMR2IF = 0; - PR2 = 0xff; - T2CON = 0b00000111; + /* 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_stop(TMR_INCOLOR); + tmr_startPeriodic(TMR_FADE, 1); /* 32.768 msec */ +} + +void turnOn() +{ + dbgpin_high(); + on = 1; + rgb_on(); + start_fade(); + btn_pben(); +} - /* Enable PWM output, PORTB.0 */ - while (!TMR2IF); /* wait until timer overflow */ - TRISB &= ~0x08; +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(); + red.value = grn.value = blu.value = wht.value = 0; + on = 0; + dbgpin_low(); + SLEEP(); } -void led_init() +void pb_clicked() { - PORTB &= ~LEDBIT; /* Led is PORTB.3 */ - TRISB &= ~LEDBIT; + if (on) { + unsigned char omode = mode; + + if (mode == MODE_SOLID) { + if (++color == COLOR_COUNT) { + color = 0; + if (++mode == MODE_COUNT) + mode = 0; + } + } else if (++mode == MODE_COUNT) { + mode = 0; + color = 0; + } + if (mode != omode || mode == MODE_SOLID) + tmr_start(TMR_CFG, CFG_DELAY); + start_fade(); + } } -void led_set(unsigned char led) +void pb_held() { - if (led) - PORTB |= LEDBIT; + /* The button has been held. Change the color magnitude. */ + if (bright_up) { + if (bright == BRIGHT_TOP) + bright_up = 0; + else + bright++; + } else { + if (bright == BRIGHT_BOTTOM) + bright_up = 1; else - PORTB &= ~LEDBIT; + bright--; + } + leds_set(); } -void pwm_set(unsigned char step) +void pb_held_task() { - CCPR1L = step; + pbHeldCount++; + if (pbHeldCount >= 0) { + pbHeldCount = 0; + pb_held(); + } } -void delay() +void pb_task() { - for (unsigned counter = 0; counter < 10000; counter++); + static unsigned char pbPrev = BTN_PB_UP; + unsigned char pb; + + pb = btn_pb(); + if (pbPrev == BTN_PB_UP) { + if (pb == BTN_PB_UP) + pb_clicked(); + else /* (pb == BTN_PB_DOWN) */ { + tmr_startPeriodic(TMR_BTN_PB_HELD, HELD_TMR_PERIODS); + pbHeldCount = -HELD_PERIODS; + } + } else /* if (pbPrev == BTN_PB_DOWN) */ { + if (pb == BTN_PB_UP) { + tmr_stop(TMR_BTN_PB_HELD); + if (pbHeldCount < 0) + pb_clicked(); + } /* nothing to do if button is down */ + } + pbPrev = pb; + btn_pben(); } -int main(void) +void rs_task() { - unsigned char fwd = 1; - unsigned char step = 0; - - pic_init(); - led_init(); - pwm_init(); - while (1) { - pwm_set(step); - if (fwd == 0) { - step--; - if (step == 0xff) { /* rollover */ - led_set(0); - fwd = 1; - step = 0; - } - } else /* fwd == 1 */ { - step++; - if (step == 0) { /* rollover */ - led_set(1); - fwd = 0; - step = 0xff; - } - } - delay(); + btn_rsen(); + switch (btn_rs()) { + case BTN_RS_OFF: + tmr32_set(0); + turnOff(); + break; + case BTN_RS_RIGHT: + tmr32_set(AUTO_OFF_COUNT); + /* fall through */ + case BTN_RS_LEFT: + turnOn(); + break; + } +} + +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); + if (mode == MODE_CYCLE) + tmr_start(TMR_INCOLOR, STD_INCOLOR); + else if (mode == MODE_PARTY) + tmr_start(TMR_INCOLOR, PARTY_MIN + (rand() % PARTY_RANGE)); + } + leds_set(); +} + +void auto_offon_task() +{ + if (on) { + turnOff(); + if (btn_rs() == BTN_RS_RIGHT) + tmr32_set(AUTO_ON_COUNT); + } else /* off */ { + turnOn(); + if (btn_rs() == BTN_RS_RIGHT) + tmr32_set(AUTO_OFF_COUNT); + } +} + +void user_boot() +{ + dbgpin_high(); + srand((adc_random() << 8) + adc_random()); + cfg_read(mode, color); + 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_BTN_PB_HELD: /* pushbutton is being held down */ + pb_held_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_TMR32: /* auto on/off event */ + auto_offon_task(); + break; + case TASK_CFG: /* Save config to EEPROM event */ + cfg_write(mode, color); + break; } - return 0; + } } + +int main(void) +{ + /* Platform initialization */ + pic_init(); + unused_init(); + btn_init(); + rgb_init(); + tmr_init(); + tmr32_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); +} +