4 flexible ways to implement key-press/release with double check

/media/uploads/mintisan/img_20160828_215959r.jpg

If your button/key is ideal, then your code will very easy as below.

    myled = mybutton.read() ;

Note: we use the led to show the result.

But the reality is not always ideal, the button you buy/made may not so well.

When you press it, it will generate much blur just as blow. So you will output some wrong down/up.

/media/uploads/mintisan/mbed-key-not-stable.png

You may think I may add some capacitor to filter the blur. But hardware is money.

So I will offer you 4 flexible ways to implement key press/release with double check. And I will analysis the trade-off between the 4 ways.

1. mbed OS

You need two threads to check press/release separately.

The main function is just as below as clearly after you implement key press/release:

/* 
 *  one thread for down check;
 *  another thread for up check.
 */
int main(void)
{
    myled = OFF;    
    Thread thread_down(user_thread_down);
    Thread thread_up(user_thread_up);
    while(true){
    }
}

Import programNucleo_key_led_with_mbed_os

implement button press/release with double check using mbed OS

2. coroutine

You need two coroutines to check press/release separately.

The main function is just as below as clearly after you implement key press/release:

/* 
 *  one thread for down check;
 *  another thread for up check.
 */
int main(void)
{
    myled = OFF;    
    Thread thread_down(user_thread_down);
    Thread thread_up(user_thread_up);
    while(true){
    }
}

Import programNucleo_key_led_with_coroutine

implement button press/release with double check using coroutine

3. two-tickers

You need two tickes to check press/release separately.

The main function is just as below as clearly after you implement key press/release:

/* 
 *  ticker down for down check;
 *  ticker down for up check.
 */
int main(void)
{
    myled = OFF;
    jitter_down.attach(&key_down_status, PRESS_JITTER_TIME);
    jitter_up.attach(&key_up_status, RELEASE_JITTER_TIME);
    while(1) {
        // extend more task here
    }
}

Import programNucleo_key_led_with_ticker

implement button press/release with double check using two tickers

4. fall/rise edge & timeout

You need one fall/rise edge interrupt and one timeout interrupt to check press/release separately.

The main function is just as below as clearly after you implement key press/release:

/* 
 *  fall edge & timout interrupt for down check;
 *  rise edge & timout interrupt for up check.
 */
int main(void)
{
    myled = OFF;
    mybutton.fall(&pressed);
    mybutton.rise(&released);
    while(1) {
        // extend more task here
    }
}

Import programNucleo_key_led_with_timeout

implement button press/release with double check using fall/rise & timeout

Compare trade-off

SoC component

  • 1. one ticker interrupt + one mbed OS
  • 2. one ticker interrupt + coroutine
  • 3. two tickers interrupt
  • 4. one fall/rise interrupt & one timeout interrupt

power

1 = 2 > 3 > 4

Note: the power SoC cost is determined by component used.

size

1 > 2 > 3 > 4

Note: both code and RAM

Complexity

1 < 2 < 3 < 4

flexible

1 > 2 > 3 > 4

Note: The more software abstracted, the flexible the system will be.

Reliability

1 = 2 = 3 > 4

After double checked, your code will support button not so well. :D

/media/uploads/mintisan/mbed-key-stablized.png


Please log in to post comments.