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
13 #define SETUP_TIMEOUT 255 // 15 seconds in jiffies
14 // #define USER_PARAMS_WRAPPING 1
16 static uint16_t button_start[3];
20 unsigned char btn1 : 1;
21 unsigned char btn2 : 1;
22 unsigned char brake : 1;
23 unsigned char brake_working : 1;
24 unsigned char brake_reported : 1;
26 } button_state, prev_state;
29 * How does the user_params setup mode work:
31 * There is an array of user-settable parameters user_params[N_USER_PARAMS],
32 * each of which has its own maximum value (user_params_max[N_USER_PARAMS].
33 * These parameters can be modified in the following way:
35 * Long press of button 1 enters the setup mode.
36 * In the setup mode, status and illumination LEDs both blink in inversion
38 * The # of blinks of the illumination LED shows which user_param is
40 * and the # of blinks of the status LED shows the value of this particular
43 * Long press of button 1 selects the next user_param,
44 * short press of button 0 decrements this particular user_param value,
45 * short press of button 1 increments this particular user_param value.
47 * If USER_PARAMS_WRAPPING is #defined, decrementing the zero leads to
48 * the maximum value - 1, and incrementing the maximum value - 1 leads
51 * After the SETUP_TIMEOUT with no button press, the system leaves the setup
54 static unsigned char user_params[N_USER_PARAMS] = { 0, 0, 0 };
55 static unsigned char user_params_max[N_USER_PARAMS] = { 3, 2, 2 };
57 static unsigned char user_params_state = 0;
59 * Here 0 means "no setup currently in progress",
60 * 1 .. N_USER_PARAMS means "now short presses increase or decrease
61 * the value of user_params[user_params_state-1]"
64 static unsigned char user_params_timeout;
66 static void inline set_status_led(unsigned char n, pattern_t *pattern)
68 led_set_pattern(n + 1, pattern);
71 unsigned char buttons_setup_in_progress()
73 if (user_params_state) // setup in progress ...
78 pattern_t *buttons_setup_status1_pattern_select()
80 if (user_params_state) // Setup in progress
81 return number_pattern(user_params_state, 1);
86 pattern_t *buttons_setup_status0_pattern_select()
88 if (user_params_state) // Setup in progress
89 return number_pattern(
90 1 + user_params[user_params_state-1],
97 unsigned char get_user_param(unsigned char param)
99 if (param < N_USER_PARAMS)
100 return user_params[param];
101 return 0; // FIXME: internal error?
104 static inline void short_press(unsigned char button)
108 if (user_params_state == 0) {
116 param = user_params_state-1;
119 if (user_params[param])
120 --user_params[param];
121 #if SETUP_VALUES_WRAPPING
123 user_params[param] = user_params_max[param]-1;
126 ++user_params[param];
128 if (user_params[param] >= user_params_max[param])
129 #if SETUP_VALUES_WRAPPING
130 user_params[param] = 0;
132 user_params[param] = user_params_max[param]-1;
135 // FIXME: notify somebody about user_params change?
137 set_status_led(1, buttons_setup_status1_pattern_select());
139 user_params_timeout = SETUP_TIMEOUT;
142 static inline void long_press(unsigned char button)
149 // button 1 - cycle through states
152 if (user_params_state > N_USER_PARAMS)
153 user_params_state = 1;
155 set_status_led(0, buttons_setup_status0_pattern_select());
156 set_status_led(1, buttons_setup_status1_pattern_select());
157 user_params_timeout = SETUP_TIMEOUT;
162 DDRA &= ~_BV(PA4); // set as input
163 DDRA |= _BV(PA3); // output
164 PORTA &= ~(_BV(PA3) | _BV(PA4)); // PA4 disable internal pull-up
167 GIMSK &= ~(_BV(PCIE0) | _BV(PCIE1)); // disable pin-change IRQs
168 PCMSK0 = 0; // disable pin-change IRQs on all pins of port A
169 PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
176 button_state.all = 0;
177 user_params_state = 0;
182 DDRA &= ~(_BV(PA3) | _BV(PA4)); // set as input
183 PORTA |= _BV(PA4); // enable internal pull-up
184 PORTA &= ~_BV(PA3); // disable pull-up
186 GIMSK &= ~_BV(PCIE0);
189 PCMSK0 = _BV(PCINT4);
190 // disable pin-change IRQs on all pins except PA4
191 PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
194 static void handle_button(unsigned char button, unsigned char cur,
197 if (cur && !prev) { // --- just pressed ---
198 button_start[button] = jiffies;
199 if (!user_params_state)
200 set_status_led(button, NULL);
202 } else if (cur && prev) { // --- is still pressed ---
203 uint16_t duration = jiffies - button_start[button];
205 if (duration >= LONG_PRESS_MIN) {
206 set_status_led(button, on_pattern);
207 // acknowledge long press
209 } else if (!cur && prev) { // --- just released ---
210 uint16_t duration = jiffies - button_start[button];
212 if (duration >= SHORT_PRESS_MIN && duration < SHORT_PRESS_MAX) {
214 } else if (duration > LONG_PRESS_MIN) {
215 set_status_led(button, NULL);
218 // ignore other button-press durations
222 static void handle_brake(unsigned char cur, unsigned char prev)
226 if (cur && !prev) { // --- just pressed ---
227 button_start[2] = jiffies;
229 } else if (!cur && prev) { // --- just released ---
230 button_start[2] = jiffies;
234 duration = jiffies - button_start[2];
240 if (button_state.brake_working) {
241 static unsigned int brake_time;
242 if (button_state.brake_reported) {
247 button_state.brake_working = 0;
248 button_state.brake_reported = 0;
251 button_state.brake_reported = 1;
253 brake_time = 255; // avoid longer than ~16s
257 button_state.brake_working = 1;
258 if (button_state.brake_reported) {
259 button_state.brake_reported = 0;
264 button_start[2] = jiffies - 7; // avoid overflow
267 void timer_check_buttons()
269 handle_button(0, button_state.btn1, prev_state.btn1);
271 // when button 1 is on, all other states are unreadable
272 if (button_state.btn1) {
273 prev_state.btn1 = button_state.btn1;
275 handle_button(1, button_state.btn2, prev_state.btn2);
276 handle_brake(button_state.brake, prev_state.brake);
277 prev_state.all = button_state.all;
280 if (user_params_timeout)
281 --user_params_timeout;
283 if (user_params_state && !user_params_timeout) {
284 user_params_state = 0;
285 set_status_led(0, buttons_setup_status0_pattern_select());
286 set_status_led(1, buttons_setup_status1_pattern_select());
290 unsigned char buttons_wait_for_release()
292 uint16_t wake_count = 0;
296 if (wake_count++ > WAKEUP_LIMIT)
297 gpio_set(0, 1); // inform the user
301 pin = PINA & _BV(PA4);
302 } while (!(pin & _BV(PA4)));
306 return wake_count > WAKEUP_LIMIT;
309 void button_adc(uint16_t adcval)
312 static unsigned char count=250;
314 PORTA &= ~_BV(PA3); // disable +5V to the hall probe
315 // adc.c will re-enable it again
317 button_state.btn1 = 0;
318 button_state.btn2 = 0;
319 button_state.brake = 0;
321 if (adcval == 0x3FF) {
322 button_state.brake = 1;
323 } else if (adcval > 0x240) {
324 button_state.brake = 1;
325 button_state.btn2 = 1;
326 } else if (adcval > 0x180) {
328 } else if (adcval > 0xc0) {
329 button_state.btn2 = 1;
331 button_state.btn1 = 1;
346 // empty - let it wake us from sleep, but do nothing else