]> www.fi.muni.cz Git - bike-lights.git/blob - buttons.c
83664c70f9a3e085a7985c1eca041669c5f6a948
[bike-lights.git] / buttons.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3
4 #include "lights.h"
5
6 static uint16_t button_start[N_BUTTONS];
7 static unsigned char button_pressed[N_BUTTONS];
8
9 void init_buttons()
10 {
11         // MCUCR |= _BV(ISC00); // any edge generates IRQ
12         // GIMSK |= _BV(INT0); // enable INT0
13         DDRB &= ~(_BV(PB6) | _BV(PB0));
14         PORTB |= _BV(PB6) | _BV(PB0);
15
16         // log_byte(PORTB);
17 }
18
19 extern unsigned char led1_counter;
20
21 void timer_check_buttons()
22 {
23         unsigned char pinb = PINB;
24         unsigned char i;
25         unsigned char port_states[N_BUTTONS] = {
26                 pinb & _BV(PB6),
27                 pinb & _BV(PB0),
28         };
29         for (i = 0; i < N_BUTTONS; i++) {
30                 if (!port_states[i]) { // is pressed
31                         if (button_pressed[i]) { // been already pressed
32                                 // TODO long button press
33                                 /*
34                                 uint16_t duration = jiffies - button_start[i];
35                                 if (duration > 10 && duration < 40) {
36                                         gpio_set(GPIO_LED2, 1);
37                                 }
38                                 */
39                         } else { // begin of button press
40                                 button_pressed[i] = 1;
41                                 button_start[i] = jiffies;
42                                 // log_byte(0xC0);
43                                 // log_word(jiffies);
44                         }
45                 } else { // is not pressed
46                         if (button_pressed[i]) { // just depressed
47                                 uint16_t duration = jiffies - button_start[i];
48                                 log_byte(0xC1);
49                                 log_word(duration);
50                                 log_flush();
51                                 // led_set_number_pattern(N_PWMLEDS, 1 + (duration >> 3));
52                                 button_pressed[i] = 0;
53                         }
54                 }
55
56                 break; // FIXME - delete this when btn1 is ready
57         }
58 }
59
60 #if 0
61 ISR(INT0_vect)
62 {
63         unsigned char tmpval = PINB & _BV(PB6);
64
65         gpio_set(GPIO_LED2, tmpval);
66         log_byte(0xbb);
67         log_flush();
68 }
69 #endif
70