Add high level button code and some documentation

This commit is contained in:
Thammi 2013-08-29 20:56:43 +02:00
parent 3a41210d54
commit 067c9fe0e5
3 changed files with 34 additions and 4 deletions

View File

@ -1,6 +1,8 @@
#ifndef APP_H
#define APP_H
#include <stdlib.h>
#define MAX_APPS 8
#define REG(run) REGISTER(run, NULL, NULL);

View File

@ -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 */

View File

@ -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;