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)
192 if (cur && !prev) { // --- just pressed ---
193 button_start[2] = jiffies;
194 } else if (!cur && prev) { // --- just released ---
195 button_start[2] = jiffies;
196 } else { // --- no change ---
197 uint16_t duration = jiffies - button_start[2];
201 if (button_state.brake_working
202 && !button_state.brake_reported) {
203 button_state.brake_reported = 1;
207 button_state.brake_working = 1;
208 if (button_state.brake_reported) {
209 button_state.brake_reported = 0;
213 button_start[2] = jiffies - 7; // avoid overflow
218 void timer_check_buttons()
220 handle_button(0, button_state.btn1, prev_state.btn1);
222 // when button 1 is on, all other states are unreadable
223 if (button_state.btn1) {
224 prev_state.btn1 = button_state.btn1;
226 handle_button(1, button_state.btn2, prev_state.btn2);
227 handle_brake(button_state.brake, prev_state.brake);
228 prev_state.all = button_state.all;
231 if (user_params_state && jiffies - user_params_starttime > 1000) {
232 user_params_state = 0;
233 set_status_led(0, buttons_setup_status0_pattern_select());
234 set_status_led(1, buttons_setup_status1_pattern_select());
238 unsigned char buttons_wait_for_release()
240 uint16_t wake_count = 0;
244 if (wake_count++ > WAKEUP_LIMIT)
245 gpio_set(0, 1); // inform the user
249 pin = PINA & _BV(PA4);
250 } while (!(pin & _BV(PA4)));
254 return wake_count > WAKEUP_LIMIT;
257 void button_adc(uint16_t adcval)
260 static unsigned char count=250;
262 PORTA &= ~_BV(PA3); // disable +5V to the hall probe
263 // adc.c will re-enable it again
265 button_state.btn1 = 0;
266 button_state.btn2 = 0;
267 button_state.brake = 0;
269 if (adcval == 0x3FF) {
270 button_state.brake = 1;
271 } else if (adcval > 0x240) {
272 button_state.brake = 1;
273 button_state.btn2 = 1;
274 } else if (adcval > 0x180) {
276 } else if (adcval > 0xc0) {
277 button_state.btn2 = 1;
279 button_state.btn1 = 1;
294 // empty - let it wake us from sleep, but do nothing else