]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/buttons.c
firmware/buttons.c: preliminary user params setup
[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 static uint16_t button_start[N_BUTTONS];
9 static unsigned char button_pressed[N_BUTTONS];
10 static unsigned char just_waked_up;
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 do_sleep();
25
26 static void toggle_bright_mode()
27 {
28         // TODO
29 }
30
31 static void set_panic_mode()
32 {
33         // TODO
34 }
35
36 unsigned char get_user_param(unsigned char param)
37 {
38         if (param < MAX_USER_PARAMS)
39                 return user_params[param];
40         return 0; // FIXME: internal error?
41 }
42
43 static inline void short_press(unsigned char button)
44 {
45         unsigned char param;
46
47         if (user_params_state == 0) {
48                 if (button == 0)
49                         toggle_bright_mode();
50                 else
51                         set_panic_mode();
52                 return;
53         }
54
55         param = user_params_state-1;
56
57         if (button == 0) {
58                 if (user_params[param])
59                         user_params[param]--;
60                 else
61                         user_params[param] = user_params_max[param];
62         } else {
63                 user_params[param]++;
64
65                 if (user_params[param] >= user_params_max[param])
66                         user_params[param] = 0;
67         }
68         // FIXME: notify somebody about user_params change?
69
70         // set_status_led(2, user_params[param]);
71
72         user_params_starttime = jiffies;
73 }
74
75 static inline void long_press(unsigned char button)
76 {
77         if (button == 0) {
78                 do_sleep();
79                 return;
80         }
81
82         // button 1 - cycle through states
83         user_params_state++;
84
85         if (user_params_state >= MAX_USER_PARAMS)
86                 user_params_state = 1;
87
88         // set_status_led(1, state);
89         user_params_starttime = jiffies;
90 }
91
92 void init_buttons()
93 {
94         unsigned char i;
95
96         DDRA &= ~(_BV(PA3) | _BV(PA4));
97         PORTA |=  _BV(PA3) | _BV(PA4);
98
99         for (i=0; i < N_BUTTONS; i++) {
100                 button_start[i] = 0;
101                 button_pressed[i] = 0;
102         }
103
104         just_waked_up = 1;
105 }
106
107 static void do_sleep()
108 {
109         log_byte(0xb0);
110         log_flush();
111
112         hw_suspend();
113         gpio_before_poweroff(); // Set the status LED on again
114
115         while((PINA & _BV(PA3)) == 0)
116                 ; // wait for button release
117
118         _delay_ms(100);
119
120         susp_gpio(); // disable the status LED
121
122         power_down();
123 }
124
125 void timer_check_buttons()
126 {
127         unsigned char pin = PINA;
128         unsigned char i;
129         unsigned char port_states[N_BUTTONS] = {
130                 pin & _BV(PA3),
131                 pin & _BV(PA4),
132         };
133
134         for (i = 0; i < N_BUTTONS; i++) {
135                 if (!port_states[i]) { // is pressed
136                         if (i == 0 && just_waked_up)
137                                 continue;
138                         if (button_pressed[i] == 0) {
139                                 // begin of button press
140                                 button_pressed[i] = 1;
141                                 button_start[i] = jiffies;
142 #if 0
143                                 log_byte(0xC0);
144                                 log_word(jiffies);
145 #endif
146                         } else if (button_pressed[i] == 1) {
147                                 // been already pressed
148                                 uint16_t duration = jiffies - button_start[i];
149                                 if (duration > 80) {
150                                         // long button press
151                                         button_pressed[i] = 2;
152                                         long_press(i);
153                                 }
154                         }
155                 } else { // is not pressed
156                         if (i == 0)
157                                 just_waked_up = 0;
158                         if (button_pressed[i]) { // just depressed
159                                 uint16_t duration = jiffies - button_start[i];
160 #if 1
161                                 log_byte(0xC1);
162                                 log_word(duration);
163                                 log_flush();
164 #endif
165                                 button_pressed[i] = 0;
166                                 if (duration > 6 && duration < 30)
167                                         short_press(i);
168                         }
169                 }
170         }
171 }
172