1
0
mirror of https://github.com/sjlongland/atinysynth.git synced 2025-09-13 10:03:15 +10:00

attiny861 main: Use switch instead of if.

Years ago, and on MSP430 not AVR, I found that gcc produced tighter code
using `if` instead of `switch`.  Thus, I found myself always using `if`
and never `switch` if I needed branching, since smaller code is faster
code, and we needed all the speed we could get.

Turns out, AVR does not have this problem, and we get better performance
using `switch`.
This commit is contained in:
Stuart Longland 2017-07-15 15:59:49 +10:00
parent b6e678f045
commit 98df61229d
Signed by: stuartl
GPG Key ID: F954BBBB7948D546

View File

@ -237,41 +237,45 @@ ISR(TIMER0_COMPA_vect) {
static uint8_t gpio_state = GPIO_STATE_READ;
static uint8_t cur_light = 0;
if (gpio_state == GPIO_STATE_READ) {
/* We are reading inputs */
static uint8_t last = 0;
static uint8_t delay = DEBOUNCE_DELAY;
uint8_t now = ~PINA;
switch (gpio_state) {
case GPIO_STATE_READ:
{
/* We are reading inputs */
static uint8_t last = 0;
static uint8_t delay = DEBOUNCE_DELAY;
uint8_t now = ~PINA;
if (now != last) {
/*
* We detected a change reset
* debounce timer
*/
last = now;
delay = DEBOUNCE_DELAY;
} else if (delay) {
/* Wait for bouncing to finish */
delay--;
} else {
/* Bouncing finished */
uint8_t diff;
if (now != last) {
/*
* We detected a change reset
* debounce timer
*/
last = now;
delay = DEBOUNCE_DELAY;
} else if (delay) {
/* Wait for bouncing to finish */
delay--;
} else {
/* Bouncing finished */
uint8_t diff;
button_last = button_state;
button_state = now;
/* Determine differences from last */
diff = button_state ^ button_last;
/* Detect button presses & releases */
button_hit |= button_state & diff;
button_release |= (~button_state) & diff;
button_last = button_state;
button_state = now;
/* Determine differences from last */
diff = button_state ^ button_last;
/* Detect button presses & releases */
button_hit |= button_state & diff;
button_release |= (~button_state) & diff;
}
/* Turn off lights */
OCR1D = 0;
/* Switch to setting outputs */
PORTB &= ~GPIO_EN;
DDRA = 0xff;
gpio_state = GPIO_STATE_SELECT;
break;
}
/* Turn off lights */
OCR1D = 0;
/* Switch to setting outputs */
PORTB &= ~GPIO_EN;
DDRA = 0xff;
gpio_state = GPIO_STATE_SELECT;
} else if (gpio_state == GPIO_STATE_SELECT) {
case GPIO_STATE_SELECT:
/* We are setting outputs */
PORTA = (1 << cur_light);
/* Switch on input MUXes */
@ -280,12 +284,14 @@ ISR(TIMER0_COMPA_vect) {
PORTA = 0xff;
DDRA = 0x00;
gpio_state = GPIO_STATE_PWM;
} else if (gpio_state == GPIO_STATE_PWM) {
break;
case GPIO_STATE_PWM:
OCR1D = light_output[cur_light];
/* Increment the light for later (modulo 8) */
cur_light++;
cur_light &= 0x07;
gpio_state = GPIO_STATE_READ;
break;
}
/* Tick down the one-second timer */