2 #include <avr/interrupt.h>
4 #include <util/delay.h>
8 #define WAKEUP_LIMIT 5 // times 100 ms
9 static uint16_t button_start[N_BUTTONS];
10 static unsigned char prev_pin;
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 };
15 static unsigned char user_params_state = 0;
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]"
22 static uint16_t user_params_starttime;
24 static void inline set_status_led(unsigned char n, pattern_t *pattern)
26 led_set_pattern(n + N_PWMLEDS, pattern);
29 pattern_t *status_pattern_select(unsigned char n)
31 if (user_params_state) // Setup in progress
32 return number_pattern(n == 0
34 : 1 + user_params[user_params_state-1]
36 else if (!(prev_pin & _BV(PA3)) || !(prev_pin & _BV(PA4)))
37 // at least one button is pressed
39 // otherwise, an ordinary state
42 return number_pattern(1 + ambient_zone);
44 return jiffies > 500 ? number_pattern(2) : zero_pattern; // for now
48 static void toggle_bright_mode()
53 static void set_panic_mode()
58 unsigned char get_user_param(unsigned char param)
60 if (param < MAX_USER_PARAMS)
61 return user_params[param];
62 return 0; // FIXME: internal error?
65 static inline void short_press(unsigned char button)
69 if (user_params_state == 0) {
77 param = user_params_state-1;
80 if (user_params[param])
83 user_params[param] = user_params_max[param]-1;
87 if (user_params[param] >= user_params_max[param])
88 user_params[param] = 0;
90 // FIXME: notify somebody about user_params change?
92 set_status_led(1, status_pattern_select(1));
94 user_params_starttime = jiffies;
97 static inline void long_press(unsigned char button)
104 // button 1 - cycle through states
107 if (user_params_state > MAX_USER_PARAMS)
108 user_params_state = 1;
110 set_status_led(0, status_pattern_select(0));
111 set_status_led(1, status_pattern_select(1));
112 user_params_starttime = jiffies;
117 DDRA &= ~(_BV(PA3) | _BV(PA4)); // set as input
118 PORTA |= _BV(PA3) | _BV(PA4); // enable internal pull-ups
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
126 prev_pin = _BV(PA3) | _BV(PA4);
127 user_params_state = 0;
132 DDRA &= ~(_BV(PA3) | _BV(PA4)); // set as input
133 PORTA |= _BV(PA3) | _BV(PA4); // enable internal pull-ups
135 GIMSK &= ~_BV(PCIE1); // disable pin-change IRQ on port B
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
143 static void handle_button(unsigned char button, unsigned char cur,
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);
151 } else if (!cur && !prev) { // --- is still pressed ---
152 uint16_t duration = jiffies - button_start[button];
155 set_status_led(button, on_pattern);
156 // acknowledge long press
158 } else if (cur && !prev) { // --- just released ---
159 uint16_t duration = jiffies - button_start[button];
161 if (duration > 6 && duration < 30) {
163 } else if (duration > 80) {
164 set_status_led(button, off_pattern);
167 // ignore other button-press durations
171 void timer_check_buttons()
173 unsigned char pin = PINA & (_BV(PA3) | _BV(PA4));
175 handle_button(0, pin & _BV(PA3), prev_pin & _BV(PA3));
176 handle_button(1, pin & _BV(PA4), prev_pin & _BV(PA4));
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));
187 unsigned char buttons_wait_for_release()
189 uint16_t wake_count = 0;
193 if (wake_count++ > WAKEUP_LIMIT)
194 gpio_set(0, 1); // inform the user
198 pin = PINA & (_BV(PA3) | _BV(PA4));
199 } while (!(pin & _BV(PA3)) || !(pin & _BV(PA4)));
203 return wake_count > WAKEUP_LIMIT;
209 // empty - let it wake us from sleep, but do nothing else