From 2bcf64740e6f3f8706faa5b97bbb83e5a0cab216 Mon Sep 17 00:00:00 2001 From: bigalex Date: Mon, 1 Oct 2012 23:36:37 +0200 Subject: [PATCH] higher level button functions implemented --- firmware/lib/bughal.c | 117 ++++++++++++++++++++++++++++++++++++++++++ firmware/lib/bughal.h | 38 ++++++++++++++ firmware/main.c | 42 ++++++++++++--- 3 files changed, 189 insertions(+), 8 deletions(-) diff --git a/firmware/lib/bughal.c b/firmware/lib/bughal.c index 0eadfcd..dcff0cf 100644 --- a/firmware/lib/bughal.c +++ b/firmware/lib/bughal.c @@ -1,12 +1,24 @@ #include #include #include "bughal.h" +#include "util.h" //for timer /* Hardware abstraction layer for Pentabug hardware */ /* * initialize LEDs on C0-C3 */ + + +static uint8_t oldinput; // button readings from last poll cycle +static uint8_t curinput; // button readings from current poll cycle +//each switch has its own state machine +static uint8_t btnstates[BTN_BUTTONS]; //array for current button states +static uint8_t btncounters[BTN_BUTTONS]; //individual counter for button state machine +static timer_t btntimers[BTN_BUTTONS]; //individiual timer for for button state machine + + + void init_leds(void){ //enable LED channels as output DDRC |= (1 << PORTC0) | (1 << PORTC1) | (1 << PORTC2) | (1 << PORTC3); @@ -67,6 +79,15 @@ void buzzr_inv(void){ void init_switch(void){ DDRD &= ~( (1 << PORTD1) | (1< BTN_T_LONGFACT){ //500ms held + btnstates[i] = BTNST_LDN; + } + } else { //button was released + btnstates[i] = BTNST_SUP; + //signal shortclick + } + break; + case BTNST_LDN: //is longpressed and still held down + if (!(curinput & (1< going to shortpress + btncounters[i]++; + btnstates[i] = BTNST_SDN; //starting over from short pressed + } else { + btnstates[i] = BTNST_SUP; //nope, it was bouncing, back to old state + } + break; + case BTNST_LUPDBNC: //Button was pressed again after beeing short pressed(or is bouncing) + if ((curinput & (1< going to shortpress + btncounters[i]++; + btnstates[i] = BTNST_SDN; //starting over from short pressed + } else { + btnstates[i] = BTNST_LUP; //nope, it was bouncing, back to old state + } + break; + default: //curently catches nothing + // do nothing yet + ; + } //end switch + timer_set(&btntimers[i], BTN_T_DEBOUNCE); +} + + + +void button_poll(){ + curinput = ~(PIND & 0b00000011); + for(uint8_t i=0; i #include "main.h" -#include "lib/synth.h" + #include "lib/usart.h" #include "lib/bughal.h" #include "lib/util.h" @@ -27,19 +27,45 @@ main(void) /* here the show begins:*/ sei(); - //set_motor(MOTOR_ON); timer_t t; - uint8_t ledmode =0; - uint16_t ct =0; - led_on(LED_R); + uint8_t ledstate_l =0; + uint8_t ledstate_r =0; + uint8_t motorstate =0; timer_set(&t, 100); for(;;) /* ever */ { //do something //main polling loop; + button_poll(); if (timer_expired(&t)){ - if (switch_l()) {led_on(LED_L);} else {led_off(LED_L);}; - if (switch_r()) {led_on(LED_R);} else {led_off(LED_R);}; - if (switch_l()&&switch_r()) {set_motor(MOTOR_ON);} else {set_motor(MOTOR_OFF);}; + //while left button is pressed switch left led on + if (btn_state(BTNST_SDN,BTN_LEFT)|btn_state(BTNST_LDN,BTN_LEFT)){ + led_on(LED_L); + } else { + led_off(LED_L); + }; + //when right button has been pressed short, toggle right led + if (btn_state(BTNST_SUP,BTN_RIGHT)){ + if (ledstate_r ==0){ + ledstate_r = 1; + led_on(LED_R); + } else { + ledstate_r = 0; + led_off(LED_R); + }; + button_clear(BTN_RIGHT); //this is important, to show the event has been processed + }; + //when right button has been pressed long, toggle motor + if (btn_state(BTNST_LUP,BTN_RIGHT)){ + if (motorstate ==0){ + motorstate = 1; + set_motor(MOTOR_ON); + } else { + motorstate = 0; + set_motor(MOTOR_OFF); + }; + button_clear(BTN_RIGHT); //this is important, to show the event has been processed + }; + timer_set(&t, 5); }; //end if timer expired //USART0_put_uint16(0xA09F);