2 #include <avr/interrupt.h>
4 #include <util/delay.h>
5 #include <stdlib.h> // for NULL
9 #define WAKEUP_LIMIT 5 // times 100 ms
10 #define SHORT_PRESS_MIN 2 // in jiffies (16 Hz ticks)
11 #define SHORT_PRESS_MAX 5
12 #define LONG_PRESS_MIN 10
14 static uint16_t button_start[3];
18 unsigned char btn1 : 1;
19 unsigned char btn2 : 1;
20 unsigned char brake : 1;
21 unsigned char brake_working : 1;
22 unsigned char brake_reported : 1;
24 } button_state, prev_state;
26 static unsigned char user_params[MAX_USER_PARAMS] = { 0, 0, 0 };
27 static unsigned char user_params_max[MAX_USER_PARAMS] = { 3, 2, 2 };
29 static unsigned char user_params_state = 0;
31 * Here 0 means "no setup currently in progress",
32 * 1 .. MAX_USER_PARAMS means "now short presses increase or decrease
33 * the value of user_params[user_params_state-1]"
36 static uint16_t user_params_starttime;
38 static void inline set_status_led(unsigned char n, pattern_t *pattern)
40 led_set_pattern(n + N_PWMLEDS, pattern);
43 unsigned char buttons_setup_in_progress()
45 if (user_params_state // setup in progress ...
46 // or at least one button is pressed:
53 pattern_t *buttons_setup_status0_pattern_select()
55 if (user_params_state) // Setup in progress
56 return number_pattern(user_params_state, 1);
61 pattern_t *buttons_setup_status1_pattern_select()
63 if (user_params_state) // Setup in progress
64 return number_pattern(
65 1 + user_params[user_params_state-1],
72 unsigned char get_user_param(unsigned char param)
74 if (param < MAX_USER_PARAMS)
75 return user_params[param];
76 return 0; // FIXME: internal error?
79 static inline void short_press(unsigned char button)
83 if (user_params_state == 0) {
91 param = user_params_state-1;
94 if (user_params[param])
97 user_params[param] = user_params_max[param]-1;
101 if (user_params[param] >= user_params_max[param])
102 user_params[param] = 0;
104 // FIXME: notify somebody about user_params change?
106 set_status_led(1, buttons_setup_status1_pattern_select());
108 user_params_starttime = jiffies;
111 static inline void long_press(unsigned char button)
118 // button 1 - cycle through states
121 if (user_params_state > MAX_USER_PARAMS)
122 user_params_state = 1;
124 set_status_led(0, buttons_setup_status0_pattern_select());
125 set_status_led(1, buttons_setup_status1_pattern_select());
126 user_params_starttime = jiffies;
131 DDRA &= ~_BV(PA4); // set as input
132 DDRA |= _BV(PA3); // output
133 PORTA &= ~(_BV(PA3) | _BV(PA4)); // PA4 disable internal pull-up
136 GIMSK &= ~(_BV(PCIE0) | _BV(PCIE1)); // disable pin-change IRQs
137 PCMSK0 = 0; // disable pin-change IRQs on all pins of port A
138 PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
145 button_state.all = 0;
146 user_params_state = 0;
151 DDRA &= ~(_BV(PA3) | _BV(PA4)); // set as input
152 PORTA |= _BV(PA4); // enable internal pull-up
153 PORTA &= ~_BV(PA3); // disable pull-up
155 GIMSK &= ~_BV(PCIE0);
158 PCMSK0 = _BV(PCINT4);
159 // disable pin-change IRQs on all pins except PA4
160 PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
163 static void handle_button(unsigned char button, unsigned char cur,
166 if (cur && !prev) { // --- just pressed ---
167 button_start[button] = jiffies;
168 set_status_led(button, NULL);
170 } else if (cur && prev) { // --- is still pressed ---
171 uint16_t duration = jiffies - button_start[button];
173 if (duration >= LONG_PRESS_MIN) {
174 set_status_led(button, on1_pattern);
175 // acknowledge long press
177 } else if (!cur && prev) { // --- just released ---
178 uint16_t duration = jiffies - button_start[button];
180 if (duration >= SHORT_PRESS_MIN && duration < SHORT_PRESS_MAX) {
182 } else if (duration > LONG_PRESS_MIN) {
183 set_status_led(button, NULL);
186 // ignore other button-press durations
190 static void handle_brake(unsigned char cur, unsigned char prev)
194 if (cur && !prev) { // --- just pressed ---
195 button_start[2] = jiffies;
197 } else if (!cur && prev) { // --- just released ---
198 button_start[2] = jiffies;
202 duration = jiffies - button_start[2];
208 if (button_state.brake_working) {
209 static unsigned int brake_time;
210 if (button_state.brake_reported) {
215 button_state.brake_working = 0;
216 button_state.brake_reported = 0;
219 button_state.brake_reported = 1;
221 brake_time = 255; // avoid longer than ~16s
225 button_state.brake_working = 1;
226 if (button_state.brake_reported) {
227 button_state.brake_reported = 0;
232 button_start[2] = jiffies - 7; // avoid overflow
235 void timer_check_buttons()
237 handle_button(0, button_state.btn1, prev_state.btn1);
239 // when button 1 is on, all other states are unreadable
240 if (button_state.btn1) {
241 prev_state.btn1 = button_state.btn1;
243 handle_button(1, button_state.btn2, prev_state.btn2);
244 handle_brake(button_state.brake, prev_state.brake);
245 prev_state.all = button_state.all;
248 if (user_params_state && jiffies - user_params_starttime > 1000) {
249 user_params_state = 0;
250 set_status_led(0, buttons_setup_status0_pattern_select());
251 set_status_led(1, buttons_setup_status1_pattern_select());
255 unsigned char buttons_wait_for_release()
257 uint16_t wake_count = 0;
261 if (wake_count++ > WAKEUP_LIMIT)
262 gpio_set(0, 1); // inform the user
266 pin = PINA & _BV(PA4);
267 } while (!(pin & _BV(PA4)));
271 return wake_count > WAKEUP_LIMIT;
274 void button_adc(uint16_t adcval)
277 static unsigned char count=250;
279 PORTA &= ~_BV(PA3); // disable +5V to the hall probe
280 // adc.c will re-enable it again
282 button_state.btn1 = 0;
283 button_state.btn2 = 0;
284 button_state.brake = 0;
286 if (adcval == 0x3FF) {
287 button_state.brake = 1;
288 } else if (adcval > 0x240) {
289 button_state.brake = 1;
290 button_state.btn2 = 1;
291 } else if (adcval > 0x180) {
293 } else if (adcval > 0xc0) {
294 button_state.btn2 = 1;
296 button_state.btn1 = 1;
311 // empty - let it wake us from sleep, but do nothing else