From 067c9fe0e57a896c128fcc7266705850c46d0581 Mon Sep 17 00:00:00 2001 From: Thammi Date: Thu, 29 Aug 2013 20:56:43 +0200 Subject: [PATCH] Add high level button code and some documentation --- firmware/include/pentabug/app.h | 2 ++ firmware/include/pentabug/hal.h | 9 +++++++-- firmware/lib/hal.c | 27 +++++++++++++++++++++++++-- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/firmware/include/pentabug/app.h b/firmware/include/pentabug/app.h index f1e0564..f708d41 100644 --- a/firmware/include/pentabug/app.h +++ b/firmware/include/pentabug/app.h @@ -1,6 +1,8 @@ #ifndef APP_H #define APP_H +#include + #define MAX_APPS 8 #define REG(run) REGISTER(run, NULL, NULL); diff --git a/firmware/include/pentabug/hal.h b/firmware/include/pentabug/hal.h index ce6f269..5806387 100644 --- a/firmware/include/pentabug/hal.h +++ b/firmware/include/pentabug/hal.h @@ -6,7 +6,6 @@ #define LEFT 0 #define RIGHT 1 - // INITIALIZATION // initializes the hardware (timers, ...) @@ -18,9 +17,12 @@ void reset_hw(void); // BUTTONS -// returns 1 if the button is pressed, 0 otherwise +// returns 1 if the button is currently pressed, 0 otherwise uint8_t button_state(uint8_t btn); +// returns 1 if the button was clicked since the last call to this function or button_reset() +uint8_t button_clicked(uint8_t btn); +void button_reset(uint8_t btn); // LEDS @@ -32,9 +34,12 @@ void led_inv(uint8_t led); void motor_on(void); void motor_off(void); +void motor_inv(void); // WAITING +// waits the given amount of ms +// WARNING: the time is actually measured in pentamilliseconds which are similar but not identical to regular milliseconds void wait_ms(uint16_t ms); #endif /* HAL_H */ diff --git a/firmware/lib/hal.c b/firmware/lib/hal.c index 5068aee..db51d66 100644 --- a/firmware/lib/hal.c +++ b/firmware/lib/hal.c @@ -13,7 +13,8 @@ static volatile uint8_t ir_active = 0; static int int_skip = 0; static volatile int16_t wait_time = 0; -static int button_count[2]; +static uint16_t button_count[2]; +static uint8_t button_pressed[2]; // major interrupt for button handling, every 5ms inline static void major_interrupt(void) { @@ -21,6 +22,10 @@ inline static void major_interrupt(void) { for(i = 0; i < 2; ++i) { if(PINB & (1 << i)) { + if(button_count[i] && button_count[i] > 10 && button_count[i] < 200) { + button_pressed[i] = 1; + } + button_count[i] = 0; } else { ++button_count[i]; @@ -29,7 +34,6 @@ inline static void major_interrupt(void) { // 1s pressed if(button_count[i] == 200) { next_app(i ? 1 : -1); - PORTC ^= 1 << 2; } } } @@ -87,6 +91,21 @@ void reset_hw(void) { // 4: LED PORTD = (1 << 4); DDRD = (1 << 2) | (1 << 4); + + // do not carry button state + + button_pressed[0] = 0; + button_pressed[1] = 0; +} + +uint8_t button_clicked(uint8_t btn) { + uint8_t clicked = button_pressed[btn]; + button_pressed[btn] = 0; + return clicked; +} + +void button_reset(uint8_t btn) { + button_pressed[btn] = 0; } void led_on(uint8_t led) { @@ -121,6 +140,10 @@ void motor_off(void) { PORTB &= ~(1 << 6); } +void motor_inv(void) { + PORTB ^= 1 << 6; +} + void wait_ms(uint16_t ms) { // TODO: this function seems to be ~10% too fast int32_t cycles = ms * (int32_t)64;