2 #include <avr/interrupt.h>
4 #include <util/delay.h>
8 static uint16_t button_start[N_BUTTONS];
9 static unsigned char prev_pin;
11 static unsigned char user_params[MAX_USER_PARAMS] = { 0, 0, 0 };
12 static unsigned char user_params_max[MAX_USER_PARAMS] = { 3, 2, 2 };
14 static unsigned char user_params_state = 0;
16 * Here 0 means "no setup currently in progress",
17 * 1 .. MAX_USER_PARAMS means "now short presses increase or decrease
18 * the value of user_params[user_params_state-1]"
21 static uint16_t user_params_starttime;
23 static void toggle_bright_mode()
28 static void set_panic_mode()
33 unsigned char get_user_param(unsigned char param)
35 if (param < MAX_USER_PARAMS)
36 return user_params[param];
37 return 0; // FIXME: internal error?
40 static inline void short_press(unsigned char button)
44 if (user_params_state == 0) {
52 param = user_params_state-1;
55 if (user_params[param])
58 user_params[param] = user_params_max[param];
62 if (user_params[param] >= user_params_max[param])
63 user_params[param] = 0;
65 // FIXME: notify somebody about user_params change?
67 // set_status_led(2, user_params[param]);
69 user_params_starttime = jiffies;
72 static inline void long_press(unsigned char button)
79 // button 1 - cycle through states
82 if (user_params_state >= MAX_USER_PARAMS)
83 user_params_state = 1;
85 // set_status_led(1, state);
86 user_params_starttime = jiffies;
93 DDRA &= ~(_BV(PA3) | _BV(PA4));
94 PORTA |= _BV(PA3) | _BV(PA4);
98 prev_pin = _BV(PA3) | _BV(PA4);
101 void timer_check_buttons()
103 unsigned char pin = PINA & (_BV(PA3) | _BV(PA4));
105 handle_button(0, pin & _BV(PA3), prev_pin & _BV(PA3));
106 handle_button(1, pin & _BV(PA4), prev_pin & _BV(PA4));
110 if (jiffies - user_params_starttime > 500) {
111 user_params_state = 0;
112 // set_status_led(0, idle);
113 // set_status_led(1, idle);
117 static void handle_button(unsigned char button, unsigned char cur,
120 // BEWARE: pins are at _zero_ when pressed!
121 if (!cur && prev) { // --- just pressed ---
122 button_start[i] = jiffies;
123 // set_status_led(button, 0);
125 } else if (!cur && !prev) { // --- is still pressed ---
126 uint16_t duration = jiffies - button_start[i];
129 // set_status_led(button, 1); // acknowledge long press
131 } else if (cur && !prev) { // --- just released ---
132 uint16_t duration = jiffies - button_start[i];
134 if (duration > 6 && duration < 30)
136 } else if (duration > 80) {
137 // set_status_led(button, 0);
140 // ignore other button-press durations