From e3fab7cf4072aa7792b1bb371377bc2fd208ede0 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Mon, 15 Dec 2014 12:43:09 +0100 Subject: pwm: Add method to switch all LEDs off This is used quite often and we can implement it more efficiently. --- pwm.c | 23 ++++++++++++++--------- pwm.h | 1 + ui.c | 32 +++++++++----------------------- 3 files changed, 24 insertions(+), 32 deletions(-) diff --git a/pwm.c b/pwm.c index 49045b4..250e489 100644 --- a/pwm.c +++ b/pwm.c @@ -14,9 +14,9 @@ static uint8_t count = 0; static uint8_t speakerCount = 0; static uint8_t pwmvalue[PWM_MAX_BRIGHTNESS][2]; -/* led bitfield, indicating which ones are pwm-controlled */ -static const uint8_t offbits[2] = {(uint8_t) ~((1 << PB6) | (1 << PB7)), - (uint8_t) ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5) | (1 << PD6))}; +/* inversed(!) bitfield, indicating which LEDs are pwm-controlled */ +static const uint8_t notledbits[2] = {(uint8_t) ~((1 << PB6) | (1 << PB7)), + (uint8_t) ~((1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5))}; ISR(TIMER0_COMPA_vect) { if (speakerCount > 0) { @@ -83,15 +83,11 @@ void pwmStart () { TCCR0B = (1 << CS02) | (0 << CS01) | (1 << CS00); } -static void allLedsOff () { - PORTB &= offbits[0]; - PORTD &= offbits[1]; -} - void pwmStop () { /* zero clock source */ TCCR0B = 0; - allLedsOff (); + PORTB &= notledbits[0]; + PORTD &= notledbits[1]; } /* Set LED brightness @@ -113,6 +109,15 @@ void pwmSet (const uint8_t i, const uint8_t value) { } } +/* Switch all LEDs off + */ +void pwmSetOff () { + for (uint8_t i = 0; i < PWM_MAX_BRIGHTNESS; i++) { + pwmvalue[i][0] &= notledbits[0]; + pwmvalue[i][1] &= notledbits[1]; + } +} + void speakerStart (const speakerMode mode __unused__) { /* 12.8ms */ speakerCount = 100; diff --git a/pwm.h b/pwm.h index c67f2a6..c5075ef 100644 --- a/pwm.h +++ b/pwm.h @@ -6,6 +6,7 @@ void pwmInit (); void pwmStart (); void pwmStop (); void pwmSet (const uint8_t, const uint8_t); +void pwmSetOff (); typedef enum { SPEAKER_BEEP, diff --git a/ui.c b/ui.c index 392f383..a387c17 100644 --- a/ui.c +++ b/ui.c @@ -88,10 +88,8 @@ static int16_t limits (const int16_t in, const int16_t min, const int16_t max) { static void enterIdle () { mode = UIMODE_IDLE; + pwmSetOff (); pwmSet (horizonLed (0), 1); - for (uint8_t i = 1; i < PWM_LED_COUNT; i++) { - pwmSet (horizonLed (i), PWM_OFF); - } } static void enterAlarmFlash () { @@ -115,19 +113,15 @@ static void setFine (const int8_t value) { /* from bottom to top for positive values, top to bottom for negative * values */ if (fineValue >= 0) { + pwmSetOff (); for (uint8_t i = 0; i < fineValue; i++) { pwmSet (horizonLed (i), PWM_ON); } - for (uint8_t i = fineValue; i < PWM_LED_COUNT; i++) { - pwmSet (horizonLed (i), PWM_OFF); - } } else { + pwmSetOff (); for (uint8_t i = 0; i < abs (fineValue); i++) { pwmSet (horizonLed (PWM_LED_COUNT-1-i), PWM_ON); } - for (uint8_t i = abs (fineValue); i < PWM_LED_COUNT; i++) { - pwmSet (horizonLed (PWM_LED_COUNT-1-i), PWM_OFF); - } } } @@ -154,12 +148,10 @@ static void doSelectCoarse () { puts ("\ncoarseValue\n"); fwrite (&coarseValue, sizeof (coarseValue), 1, stdout); + pwmSetOff (); for (uint8_t i = 0; i < coarseValue; i++) { pwmSet (horizonLed (i), PWM_ON); } - for (uint8_t i = coarseValue; i < PWM_LED_COUNT; i++) { - pwmSet (horizonLed (i), PWM_OFF); - } } } @@ -190,9 +182,9 @@ static void doSelectFine () { static void doIdle () { if (horizonChanged) { /* start timer */ + pwmSetOff (); for (uint8_t i = 0; i < PWM_LED_COUNT; i++) { brightness[i] = 0; - pwmSet (horizonLed (i), PWM_OFF); } currLed = PWM_LED_COUNT-1; brightness[currLed] = PWM_MAX_BRIGHTNESS; @@ -219,12 +211,10 @@ static void doIdle () { gyroStart (); mode = UIMODE_SELECT_COARSE; puts ("idle->select"); + speakerStart (SPEAKER_BEEP); /* start with a value of zero */ - for (uint8_t i = 0; i < PWM_LED_COUNT; i++) { - pwmSet (i, PWM_OFF); - } + pwmSetOff (); coarseValue = 0; - speakerStart (SPEAKER_BEEP); return; } } @@ -283,9 +273,7 @@ static void doAlarmFlash () { puts ("alarmflash->alarmwait"); mode = UIMODE_ALARM_WAIT; timerStop (); - for (uint8_t i = 0; i < PWM_LED_COUNT; i++) { - pwmSet (i, PWM_OFF); - } + pwmSetOff (); timerStart (ALARM_TIME_WAIT); } } @@ -344,9 +332,7 @@ void uiLoop () { uint8_t i = 0; uint8_t brightness = 0; while (1) { - for (uint8_t j = 0; j < PWM_LED_COUNT; j++) { - pwmSet (horizonLed (j), PWM_OFF); - } + pwmSetOff (); pwmSet (horizonLed (i), brightness); ++i; if (i >= PWM_LED_COUNT) { -- cgit v1.2.3