From: simimeie Date: Sat, 10 Jul 2010 07:34:51 +0000 (+0000) Subject: - add new mini function ledpwm_set that sets all LEDs at once (including X-Git-Url: http://git.rrze.uni-erlangen.de/gitweb/?a=commitdiff_plain;h=de0e300b1431bf1de890846f98c7511cb820ee7c;p=moodlight.git - add new mini function ledpwm_set that sets all LEDs at once (including correct brightness) (-sijuhamm) - change bit definitions to avoid problems for users of broken avr-libc versions: just _BV(n) instead of _BV(PXn). --- diff --git a/ledpwm.c b/ledpwm.c index e7cdefd..b9ef73c 100644 --- a/ledpwm.c +++ b/ledpwm.c @@ -1,4 +1,4 @@ -/* $Id: ledpwm.c,v 1.2 2010/06/30 19:39:28 simimeie Exp $ +/* $Id: ledpwm.c,v 1.3 2010/07/10 07:34:51 simimeie Exp $ * Functions for led brightness control via PWM (pulse width modulation). */ @@ -22,7 +22,7 @@ uint8_t ledpwm_bri = 128; void ledpwm_init(void) { /* Set OC2B, OC0A and OC0B to output */ - DDRD |= _BV(PD3) | _BV(PD5) | _BV(PD6); + DDRD |= _BV(3) | _BV(5) | _BV(6); /* Set compare output mode for OC2B in timer counter control register: */ #if (PHASECORRECTPWM == 1) /* select phase correct PWM mode (1). WGM2 bit is 0 so we don't need to touch TCCR2B. */ @@ -73,11 +73,11 @@ void ledpwm_setled(uint8_t led, uint8_t val) #else /* not PHASECORRECTPWM */ if (val == 0) { switch (led) { - case LEDPWM_REDLED: DDRD &= (uint8_t)~_BV(PD6); + case LEDPWM_REDLED: DDRD &= (uint8_t)~_BV(6); break; - case LEDPWM_GREENLED: DDRD &= (uint8_t)~_BV(PD5); + case LEDPWM_GREENLED: DDRD &= (uint8_t)~_BV(5); break; - case LEDPWM_BLUELED: DDRD &= (uint8_t)~_BV(PD3); + case LEDPWM_BLUELED: DDRD &= (uint8_t)~_BV(3); break; }; } else { @@ -86,16 +86,22 @@ void ledpwm_setled(uint8_t led, uint8_t val) val = 255; } switch (led) { - case LEDPWM_REDLED: DDRD |= _BV(PD6); + case LEDPWM_REDLED: DDRD |= _BV(6); OCR0A = val; break; - case LEDPWM_GREENLED: DDRD |= _BV(PD5); + case LEDPWM_GREENLED: DDRD |= _BV(5); OCR0B = val; break; - case LEDPWM_BLUELED: DDRD |= _BV(PD3); + case LEDPWM_BLUELED: DDRD |= _BV(3); OCR2B = val; break; }; } #endif /* PHASECORRECTPWM */ } + +void ledpwm_set(uint8_t red, uint8_t green, uint8_t blue, uint8_t br) { + ledpwm_setled(LEDPWM_REDLED, (((uint16_t)red * br) / 255)); + ledpwm_setled(LEDPWM_GREENLED,(((uint16_t)green * br) / 255)); + ledpwm_setled(LEDPWM_BLUELED, (((uint16_t)blue * br) / 255)); +} diff --git a/ledpwm.h b/ledpwm.h index 7863ba1..6d81319 100644 --- a/ledpwm.h +++ b/ledpwm.h @@ -1,4 +1,4 @@ -/* $Id: ledpwm.h,v 1.2 2010/06/30 19:39:28 simimeie Exp $ +/* $Id: ledpwm.h,v 1.3 2010/07/10 07:34:51 simimeie Exp $ * Functions for led brightness control via PWM (pulse width modulation). */ @@ -20,4 +20,7 @@ void ledpwm_init(void); /* Set brightness for a certain LED */ void ledpwm_setled(uint8_t led, uint8_t val); +/* Set brightness for all LEDs */ +void ledpwm_set(uint8_t red, uint8_t green, uint8_t blue, uint8_t br); + #endif /* _LEDPWM_H_ */