]> www.fi.muni.cz Git - bike-lights.git/commitdiff
preliminary button handling code
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 29 Aug 2012 22:26:21 +0000 (00:26 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 31 Aug 2012 21:08:11 +0000 (23:08 +0200)
Makefile
buttons.c [new file with mode: 0644]
lights.h
main.c
tmr.c

index d51e72d90008d523db3a6266f0ac4d1b7842b31c..6f2a946f1d9bd085d63720b24a694e39438495be 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,7 @@
 
 PROGRAM=lights
-SRC=main.c logging.c adc.c pwm.c tmr.c pwmled.c gpio.c ambient.c pattern.c
+SRC=main.c logging.c adc.c pwm.c tmr.c pwmled.c gpio.c ambient.c pattern.c \
+       buttons.c
 OBJ=$(SRC:.c=.o)
 
 
diff --git a/buttons.c b/buttons.c
new file mode 100644 (file)
index 0000000..83664c7
--- /dev/null
+++ b/buttons.c
@@ -0,0 +1,70 @@
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+#include "lights.h"
+
+static uint16_t button_start[N_BUTTONS];
+static unsigned char button_pressed[N_BUTTONS];
+
+void init_buttons()
+{
+       // MCUCR |= _BV(ISC00); // any edge generates IRQ
+       // GIMSK |= _BV(INT0); // enable INT0
+       DDRB &= ~(_BV(PB6) | _BV(PB0));
+       PORTB |= _BV(PB6) | _BV(PB0);
+
+       // log_byte(PORTB);
+}
+
+extern unsigned char led1_counter;
+
+void timer_check_buttons()
+{
+       unsigned char pinb = PINB;
+       unsigned char i;
+       unsigned char port_states[N_BUTTONS] = {
+               pinb & _BV(PB6),
+               pinb & _BV(PB0),
+       };
+       for (i = 0; i < N_BUTTONS; i++) {
+               if (!port_states[i]) { // is pressed
+                       if (button_pressed[i]) { // been already pressed
+                               // TODO long button press
+                               /*
+                               uint16_t duration = jiffies - button_start[i];
+                               if (duration > 10 && duration < 40) {
+                                       gpio_set(GPIO_LED2, 1);
+                               }
+                               */
+                       } else { // begin of button press
+                               button_pressed[i] = 1;
+                               button_start[i] = jiffies;
+                               // log_byte(0xC0);
+                               // log_word(jiffies);
+                       }
+               } else { // is not pressed
+                       if (button_pressed[i]) { // just depressed
+                               uint16_t duration = jiffies - button_start[i];
+                               log_byte(0xC1);
+                               log_word(duration);
+                               log_flush();
+                               // led_set_number_pattern(N_PWMLEDS, 1 + (duration >> 3));
+                               button_pressed[i] = 0;
+                       }
+               }
+
+               break; // FIXME - delete this when btn1 is ready
+       }
+}
+
+#if 0
+ISR(INT0_vect)
+{
+       unsigned char tmpval = PINB & _BV(PB6);
+
+       gpio_set(GPIO_LED2, tmpval);
+       log_byte(0xbb);
+       log_flush();
+}
+#endif
+
index ef176a40ca7dabb7b27be14df6dce47ae13eba88..ec2397f0e80629eb32c476c8fbd87ec102aea4c7 100644 (file)
--- a/lights.h
+++ b/lights.h
@@ -5,6 +5,8 @@
 #define N_PWMLEDS 3
 #define N_PWMLED_MODES 4
 
+#define N_BUTTONS 2
+
 /* logging.c */
 #ifdef USE_LOGGING
 void log_set_state(unsigned char val);
@@ -57,5 +59,9 @@ void ambient_adc(uint16_t adc_val);
 void pattern_init();
 void patterns_next_tick();
 
+/* buttons.c */
+void init_buttons();
+void timer_check_buttons();
+
 #endif /* !LIGHTS_H__ */
 
diff --git a/main.c b/main.c
index 356f8d4f8ecab70181d87fb8e0b21b6f2f8c0198..60b0030f31bb943474edac34a2fea9f426845010 100644 (file)
--- a/main.c
+++ b/main.c
@@ -14,6 +14,7 @@ int main(void)
        init_pwm();
        init_adc();
        init_tmr();
+       init_buttons();
 
        pwmled_init();
        gpio_init();
diff --git a/tmr.c b/tmr.c
index 2d521890797a01fa651759a21d402d0eb4806190..5a4403797f1db1fb29af62faaae90ce386ebe84f 100644 (file)
--- a/tmr.c
+++ b/tmr.c
@@ -23,6 +23,7 @@ ISR(TIMER0_COMPA_vect)
        ++jiffies;
 
        if (--pattern_div == 0) {
+               timer_check_buttons();
                patterns_next_tick();
                pattern_div = PATTERN_DIV;
        }