From 569867e93d1689d327a4fbf496e41d90cb0a4e1c Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Thu, 30 Aug 2012 00:26:21 +0200 Subject: [PATCH] preliminary button handling code --- Makefile | 3 ++- buttons.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ lights.h | 6 +++++ main.c | 1 + tmr.c | 1 + 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 buttons.c diff --git a/Makefile b/Makefile index d51e72d..6f2a946 100644 --- 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 index 0000000..83664c7 --- /dev/null +++ b/buttons.c @@ -0,0 +1,70 @@ +#include +#include + +#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 + diff --git a/lights.h b/lights.h index ef176a4..ec2397f 100644 --- 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 356f8d4..60b0030 100644 --- 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 2d52189..5a44037 100644 --- 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; } -- 2.39.3