Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4859

SDK • Multi function button

$
0
0
Hello Everyone,

I'm working on a few projects and I'm trying get some extra functionality from a single button.

I've written my code so that pressing and releasing the button fires an interrupt. When the button is pressed it starts a timer this timer is used to detect a long hold timeout trigger there are so flags saved so that the button release code doesn't fire in the event of the time out.

Code:

int64_t force_power_off(alarm_id_t id, void *user_data){    set_power(PWR_OFF);    context.forced = true;    return 0;}void button_isr() {    if (context.forced) {        context.F_tmr = -1;        context.forced = false;        return;    }    cancel_alarm(context.F_tmr);    context.button_state = gpio_get(POWER_BUTTON);    if (context.button_state)    {        // button up        switch (context.state)        {            case PWR_OFF:                set_power(PWR_ON);                break;            case PWR_ON:                pifo_write(context.pifo, EVENT_BTN | 0x01);                break;            default:                break;        }    } else {        // button pressed        context.F_tmr = add_alarm_in_ms(15000, force_power_off, NULL, true);    }}
This all works exactly like I want but I need to figure out how to do double press or a long press. I was thinking another timer that starts on key up when you press it gets cancelled. and I save the press time and release time to determine a short vs long press. but is this the right way to do things?

Any advice here would be helpful,
Thanks

Statistics: Posted by DarkElvenAngel — Tue Jun 11, 2024 12:35 am



Viewing all articles
Browse latest Browse all 4859

Trending Articles