+
+ button_start[2] = jiffies - 7; // avoid overflow
+}
+
+void timer_check_buttons()
+{
+ handle_button(0, button_state.btn1, prev_state.btn1);
+
+ // when button 1 is on, all other states are unreadable
+ if (button_state.btn1) {
+ prev_state.btn1 = button_state.btn1;
+ } else {
+ handle_button(1, button_state.btn2, prev_state.btn2);
+ handle_brake(button_state.brake, prev_state.brake);
+ prev_state.all = button_state.all;
+ }
+
+ if (user_params_state && jiffies - user_params_starttime > 1000) {
+ user_params_state = 0;
+ set_status_led(0, buttons_setup_status0_pattern_select());
+ set_status_led(1, buttons_setup_status1_pattern_select());
+ }
+}
+
+unsigned char buttons_wait_for_release()
+{
+ uint16_t wake_count = 0;
+ unsigned char pin;
+
+ do {
+ if (wake_count++ > WAKEUP_LIMIT)
+ gpio_set(0, 1); // inform the user
+
+ _delay_ms(100);
+
+ pin = PINA & _BV(PA4);
+ } while (!(pin & _BV(PA4)));
+
+ gpio_set(0, 0);
+
+ return wake_count > WAKEUP_LIMIT;
+}
+
+void button_adc(uint16_t adcval)
+{
+#if 0
+ static unsigned char count=250;
+#endif
+ PORTA &= ~_BV(PA3); // disable +5V to the hall probe
+ // adc.c will re-enable it again
+
+ button_state.btn1 = 0;
+ button_state.btn2 = 0;
+ button_state.brake = 0;
+
+ if (adcval == 0x3FF) {
+ button_state.brake = 1;
+ } else if (adcval > 0x240) {
+ button_state.brake = 1;
+ button_state.btn2 = 1;
+ } else if (adcval > 0x180) {
+ // nothing
+ } else if (adcval > 0xc0) {
+ button_state.btn2 = 1;
+ } else {
+ button_state.btn1 = 1;
+ }
+
+#if 0
+ if (--count)
+ return;
+
+ count = 250;
+ log_word(adcval);
+ log_flush();
+#endif
+}
+
+ISR(PCINT_vect)
+{
+ // empty - let it wake us from sleep, but do nothing else