-uint8_t read_button(void){
- uint8_t t,v=0;
- t = DDRB;
- DDRB &= ~(1<<BUTTON_PIN);
- PORTB |= 1<<BUTTON_PIN;
- PORTB &= ~(1<<BUTTON_PIN);
- v |= PINB;
- DDRB |= t&(1<<BUTTON_PIN);
- PORTB &= ~(t&(1<<BUTTON_PIN));
- v >>= BUTTON_PIN;
- v &= 1;
- v ^= 1;
- return v;
+static inline
+int8_t button_get_debounced(uint8_t debounce_count) {
+ uint8_t v;
+ v = PINB & _BV(BUTTON_PIN);
+ while (debounce_count-- && v == (PINB & _BV(BUTTON_PIN))) {
+ ;
+ }
+ if (debounce_count) {
+ return -1;
+ }
+ return v ? 0 : 1;