From 0202c0c1fd50a0817b7a83f55b0a2615d706c91f Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 31 Jan 2014 14:23:04 +0100 Subject: [PATCH] short and long button presses --- firmware/main.c | 74 ++++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/firmware/main.c b/firmware/main.c index edbbf0a..5c9a566 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -27,7 +27,7 @@ * When running, the "-" button is used for decreasing the output power, * the "+" button is for increasing it. * When on the lowest power state, the "-" button switches the system off. - * TODO: Long "-" button press switches the system off, long "+" button + * Long "-" button press switches the system off, long "+" button * press sets the output power to maximum. * * Status LED: @@ -60,9 +60,12 @@ #define WAKEUP_LIMIT 5 // times WAKEUP_POLL /* output power levels */ -#define N_STEPS 5 -static unsigned char steps[] = { 60, 85, 121, 171, 242 }; -static unsigned char intensity = 0; // selected power level +#define N_POWER_LEVELS 5 +static unsigned char power_levels[N_POWER_LEVELS] = { + +}; + +static unsigned char power_level = 0; // selected power level /* which state (output on or output off) are we measuring now */ static volatile unsigned char adc_type, adc_drop; @@ -89,6 +92,10 @@ static unsigned char batt_levels[BATT_N_LEVELS] = { /* timing by WDT */ static volatile unsigned char jiffies, next_clock_tick; +/* button press duration (in jiffies) */ +#define BUTTON_SHORT_MIN 1 +#define BUTTON_LONG_MIN 10 + /* ========= Analog to Digital Converter (battery voltage) ========== */ static void adc_init() { @@ -317,19 +324,27 @@ static void power_down() } /* ======== Button press detection and handling ===================== */ -static void button_one_pressed() -{ - if (intensity > 0) { - pwm_set(steps[--intensity]); - } else { - power_down(); - } -} - -static void button_two_pressed() +static void button_pressed(unsigned char button, unsigned char long_press) { - if (intensity < N_STEPS-1) { - pwm_set(steps[++intensity]); + // ignore simlultaneous button 1 and 2 press + if (long_press) { + if (button == 1) { + power_down(); + } else if (button == 2) { + power_level = N_POWER_LEVELS-1; + } + } else { // short press + if (button == 1) { + if (power_level > 0) { + --power_level; + } else { + power_down(); + } + } else if (button == 2) { + if (power_level < N_POWER_LEVELS-1) { + ++power_level; + } + } } } @@ -340,8 +355,12 @@ static void timer_check_buttons() unsigned char newstate = buttons_pressed(); if (newstate == button_state) { - if (newstate && button_state_time < 4) + if (newstate && button_state_time < BUTTON_LONG_MIN) ++button_state_time; + + if (newstate && button_state_time >= BUTTON_LONG_MIN) { + status_led_on(); + } return; } @@ -352,16 +371,12 @@ static void timer_check_buttons() } // just released - switch (button_state) { - case 1: button_one_pressed(); - break; - case 2: button_two_pressed(); - break; - default: // ignore when both are preseed - break; - } + if (button_state_time >= BUTTON_SHORT_MIN) + button_pressed(button_state, + button_state_time >= BUTTON_LONG_MIN ? 1 : 0); button_state = newstate; + button_state_time = 0; } /* ============ Status LED blinking =================================== */ @@ -387,8 +402,8 @@ static void status_led_next_pattern() { // for now, display the selected intensity - // n_blinks = intensity + 1; - n_blinks = battery_level() + 1; + n_blinks = power_level + 1; + // n_blinks = battery_level() + 1; blink_on_time = 0; blink_off_time = 2; blink_counter = 10; @@ -438,8 +453,11 @@ int main() // FIXME: Maybe handle new ADC readings as well? if (next_clock_tick) { next_clock_tick = 0; - timer_check_buttons(); timer_blink(); + // this has to be after the timer_blink() call + // to override the status LED during long button press + timer_check_buttons(); + if ((jiffies & 0x0F) == 0) { unsigned char i; -- 2.43.5