* Created on August 16, 2010, 12:09 PM
*/
-
-#include <htc.h>
-#include <pic16f1827.h>
-
#define RUNAT32MHZ /* else 16 MHz */
-#define BRIGHT_STEP 16 /* starts at 0 */
-#define BRIGHT_STEPS 16 /* includes 0 */
-#define LEDBIT 0x01 /* PORTB.3 */
-#if defined(RUNAT32MHZ)
-__CONFIG(WDTE_OFF & FOSC_INTOSC);
-__CONFIG(LVP_OFF & PLLEN_ON);
+#include <htc.h>
+#include <stdlib.h>
+#include "picinit.h"
+#include "buttons.h"
+#include "rgb.h"
+
+#define STEP_SIZE 64 /* ms */
+#define MIN_INCOLOR_STEPS 16
+#define MAX_INCOLOR_STEPS 57600
+#define MAX_FADE_STEPS 480
+#define reset_steps() do { incolor_steps = 1; fade_steps = 0; } while (0)
+#define rand_u8() (rand() >> 7)
+#define rand_u16() (rand() << 8 + rand() >> 7)
+#if 1
+#define rand_incolor_steps() MIN_INCOLOR_STEPS * 2
+#define rand_fade_steps() 16
#else
-__CONFIG(WDTE_OFF);
-__CONFIG(LVP_OFF);
+#define rand_incolor_steps() (MIN_INCOLOR_STEPS + rand_u16() % \
+ (MAX_INCOLOR_STEPS - MIN_INCOLOR_STEPS))
+#define rand_fade_steps() (1 + rand_u16() % MAX_FADE_STEPS)
#endif
-
-void pic_init()
-{
-#if defined(RUNAT32MHZ)
- OSCCON = 0b11110000;
-#else /* 16 MHz */
- OSCCON = 0b01111010;
-#endif
-
- /* OSCSTAT.HFIOFL is set when oscillator is locked (accurate within 2%) */
- while (!HFIOFL);
-}
-
-void pwm_init()
-{
- /* Initialize PWM on PORTB.0
- * - Fosc = 32MHz
- * - Prescale = 16
- * - PRx value = 0xff
- * = f(pwm) = 1.95 kHz
- */
-
- /* Disable output on PORTB.0 */
- TRISB |= 0x08;
-
- /* Configure ECCP1 */
- CCP1CON = 0b00001100;
- CCPR1L = 0; /* Initial PWM value; only using 8 LSBs */
-
- /* Configure Timer2 */
- CCPTMRS = CCPTMRS & ~0x03; /* bits 1:0 denote CCP1 uses Timer 2 */
- TMR2IF = 0;
- PR2 = 0xff;
- T2CON = 0b00000111;
-
- /* Enable PWM output, PORTB.0 */
- while (!TMR2IF); /* wait until timer overflow */
- TRISB &= ~0x08;
-}
-
-void led_init()
-{
- PORTB &= ~LEDBIT; /* Led is PORTB.3 */
- TRISB &= ~LEDBIT;
-}
-
-void led_set(unsigned short led)
-{
- if (led)
- PORTB |= LEDBIT;
- else
- PORTB &= ~LEDBIT;
-}
-
-void pwm_set(unsigned short step)
-{
- CCPR1L = step * BRIGHT_STEP;
-}
-
-void delay()
-{
- for (unsigned counter = 0; counter < 50000; counter++);
-}
+#define leds_set(r,g,b,w) rgb_set((r).value >> 8, \
+ (g).value >> 8, \
+ (b).value >> 8, \
+ (w).value >> 8)
+
+typedef struct {
+ unsigned value;
+ unsigned increment;
+ unsigned char remainder;
+} led_t;
+#define INIT_LED { 0, 0, 0 }
int main(void)
{
- unsigned short fwd = 1;
- short int step = 0;
- short int led = 0;
+ led_t red = INIT_LED;
+ led_t grn = INIT_LED;
+ led_t blu = INIT_LED;
+ led_t wht = INIT_LED;
+ unsigned char fast = 0;
+ unsigned incolor_steps;
+ unsigned fade_steps;
pic_init();
- led_init();
- pwm_init();
+ buttons_init();
+ rgb_init();
+ reset_steps();
+
+ if (buttons_on())
+ rgb_on();
+
while (1) {
- led = 1 - led;
- led_set(led);
- pwm_set(step);
- if (fwd == 0) {
- step--;
- if (step == -1) {
- fwd = 1;
- step++;
+ unsigned char buttons = buttons_read();
+
+ if ((buttons & (IN_ROCKERA | IN_ROCKERB))) {
+ if (buttons & IN_PUSHBTN) {
+ if (!fast) {
+ fast = 1;
+ reset_steps();
+ } else
+ fast = 0;
}
- } else /* fwd == 1 */ {
- step++;
- if (step == BRIGHT_STEPS) {
- fwd = 0;
- step--;
+
+ if (fade_steps) {
+ fade_steps--;
+ /* Continue the in-progress fade */
+ 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) {
+ unsigned newr, newg, newb, neww;
+
+ /* Next led color. All off is not a valid option. */
+ do {
+ newr = rand() << 8;
+ newg = rand() << 8;
+ newb = rand() << 8;
+ neww = rand() << 8;
+ } while (newr == 0 && newg == 0 && newb == 0 && neww == 0);
+
+ /* Next incolor and fade steps */
+ incolor_steps = rand_incolor_steps();
+ fade_steps = (buttons & IN_ROCKERA) ? 1 : rand_fade_steps();
+ if (fast) {
+ incolor_steps = 1 + incolor_steps / 8;
+ if (buttons & IN_ROCKERB)
+ fade_steps = 1 + fade_steps / 8;
+ }
+
+ /* Compute increment and remainder for each led */
+ red.increment = newr / fade_steps;
+ red.remainder = newr - red.increment * fade_steps;
+ grn.increment = newg / fade_steps;
+ grn.remainder = newg - grn.increment * fade_steps;
+ blu.increment = newb / fade_steps;
+ blu.remainder = newb - blu.increment * fade_steps;
+ wht.increment = neww / fade_steps;
+ wht.remainder = neww - wht.increment * fade_steps;
}
+ __delay_ms(STEP_SIZE); /* step should be start to start... */
+ } else {
+ rgb_off();
+ buttons_sleep();
+ reset_steps();
+ rgb_on();
+ red.value = 0;
+ grn.value = 0;
+ blu.value = 0;
+ wht.value = 0;
}
- delay(); delay();
}
return 0;
}