]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/buttons.c
buttons, wakeup rework
[bike-lights.git] / firmware / buttons.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <avr/sleep.h>
4 #include <util/delay.h>
5
6 #include "lights.h"
7
8 #define WAKEUP_LIMIT    5       // times 100 ms
9 static uint16_t button_start[N_BUTTONS];
10 static unsigned char prev_pin;
11
12 static unsigned char user_params[MAX_USER_PARAMS] = { 0, 0, 0 };
13 static unsigned char user_params_max[MAX_USER_PARAMS] = { 3, 2, 2 };
14
15 static unsigned char user_params_state = 0;
16         /*
17          * Here 0 means "no setup currently in progress",
18          * 1 .. MAX_USER_PARAMS means "now short presses increase or decrease
19          *           the value of user_params[user_params_state-1]"
20          */
21
22 static uint16_t user_params_starttime;
23
24 static void inline set_status_led(unsigned char n, pattern_t *pattern)
25 {
26         led_set_pattern(n + N_PWMLEDS, pattern);
27 }
28
29 pattern_t *status_pattern_select(unsigned char n)
30 {
31         if (user_params_state) // Setup in progress
32                 return number_pattern(n == 0
33                         ? user_params_state
34                         : 1 + user_params[user_params_state-1]
35                 );
36         else if (!(prev_pin & _BV(PA3)) || !(prev_pin & _BV(PA4)))
37                 // at least one button is pressed
38                 return off_pattern;
39         // otherwise, an ordinary state
40         if (n == 0) {
41                 // error led
42                 return number_pattern(1 + ambient_zone);
43         } else {
44                 return jiffies > 500 ? number_pattern(2) : zero_pattern; // for now
45         }
46 }
47
48 static void toggle_bright_mode()
49 {
50         // TODO
51 }
52
53 static void set_panic_mode()
54 {
55         // TODO
56 }
57
58 unsigned char get_user_param(unsigned char param)
59 {
60         if (param < MAX_USER_PARAMS)
61                 return user_params[param];
62         return 0; // FIXME: internal error?
63 }
64
65 static inline void short_press(unsigned char button)
66 {
67         unsigned char param;
68
69         if (user_params_state == 0) {
70                 if (button == 0)
71                         toggle_bright_mode();
72                 else
73                         set_panic_mode();
74                 return;
75         }
76
77         param = user_params_state-1;
78
79         if (button == 0) {
80                 if (user_params[param])
81                         user_params[param]--;
82                 else
83                         user_params[param] = user_params_max[param]-1;
84         } else {
85                 user_params[param]++;
86
87                 if (user_params[param] >= user_params_max[param])
88                         user_params[param] = 0;
89         }
90         // FIXME: notify somebody about user_params change?
91
92         set_status_led(1, status_pattern_select(1));
93
94         user_params_starttime = jiffies;
95 }
96
97 static inline void long_press(unsigned char button)
98 {
99         if (button == 0) {
100                 power_down();
101                 return;
102         }
103
104         // button 1 - cycle through states
105         user_params_state++;
106
107         if (user_params_state > MAX_USER_PARAMS)
108                 user_params_state = 1;
109
110         set_status_led(0, status_pattern_select(0));
111         set_status_led(1, status_pattern_select(1));
112         user_params_starttime = jiffies;
113 }
114
115 void init_buttons()
116 {
117         DDRA &= ~(_BV(PA3) | _BV(PA4)); // set as input
118         PORTA |=  _BV(PA3) | _BV(PA4);  // enable internal pull-ups
119
120         GIMSK &= ~(_BV(PCIE0) | _BV(PCIE1)); // disable pin-change IRQs
121         PCMSK0 = 0; // disable pin-change IRQs on all pins of port A
122         PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
123
124         button_start[0] = 0;
125         button_start[1] = 0;
126         prev_pin = _BV(PA3) | _BV(PA4);
127         user_params_state = 0;
128 }
129
130 void susp_buttons()
131 {
132         DDRA &= ~(_BV(PA3) | _BV(PA4)); // set as input
133         PORTA |=  _BV(PA3) | _BV(PA4);  // enable internal pull-ups
134
135         GIMSK &= ~_BV(PCIE1); // disable pin-change IRQ on port B
136         GIMSK |= _BV(PCIE0);
137
138         PCMSK0 = _BV(PCINT3) | _BV(PCINT4);
139                 // disable pin-change IRQs on all pins except PA3,PA4
140         PCMSK1 = 0; // disable pin-change IRQs on all pins of port B
141 }
142
143 static void handle_button(unsigned char button, unsigned char cur,
144         unsigned char prev)
145 {
146         // BEWARE: pins are at _zero_ when pressed!
147         if (!cur && prev) {                   // --- just pressed ---
148                 button_start[button] = jiffies;
149                 set_status_led(button, off_pattern);
150
151         } else if (!cur && !prev) {           // --- is still pressed ---
152                 uint16_t duration = jiffies - button_start[button];
153
154                 if (duration > 80) {
155                         set_status_led(button, on_pattern);
156                                 // acknowledge long press
157                 }
158         } else if (cur && !prev) {            // --- just released ---
159                 uint16_t duration = jiffies - button_start[button];
160
161                 if (duration > 6 && duration < 30) {
162                         short_press(button);
163                 } else if (duration > 80) {
164                         set_status_led(button, off_pattern);
165                         long_press(button);
166                 }
167                 // ignore other button-press durations
168         }
169 }
170
171 void timer_check_buttons()
172 {
173         unsigned char pin = PINA & (_BV(PA3) | _BV(PA4));
174
175         handle_button(0, pin & _BV(PA3), prev_pin & _BV(PA3));
176         handle_button(1, pin & _BV(PA4), prev_pin & _BV(PA4));
177
178         prev_pin = pin;
179
180         if (user_params_state && jiffies - user_params_starttime > 1000) {
181                 user_params_state = 0;
182                 set_status_led(0, status_pattern_select(0));
183                 set_status_led(1, status_pattern_select(1));
184         }
185 }
186
187 unsigned char buttons_wait_for_release()
188 {
189         uint16_t wake_count = 0;
190         unsigned char pin;
191
192         do {
193                 if (wake_count++ > WAKEUP_LIMIT)
194                         gpio_set(0, 1); // inform the user
195
196                 _delay_ms(100);
197
198                 pin = PINA & (_BV(PA3) | _BV(PA4));
199         } while (!(pin & _BV(PA3)) || !(pin & _BV(PA4)));
200
201         gpio_set(0, 0);
202
203         return wake_count > WAKEUP_LIMIT;
204
205 }
206
207 ISR(PCINT_vect)
208 {
209         // empty - let it wake us from sleep, but do nothing else
210 }
211