Ted Lin
/
Nucleo_key_led_with_ticker
implement button press/release with double check using two tickers
Revision 0:ebf8ee3811c2, committed 2016-08-28
- Comitter:
- mintisan
- Date:
- Sun Aug 28 12:54:53 2016 +0000
- Commit message:
- implement button press/release with double check using two tickers; ;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r ebf8ee3811c2 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Aug 28 12:54:53 2016 +0000 @@ -0,0 +1,75 @@ +#include "mbed.h" + +//------------------------------------ +// USER_KEY-PB_3 / USER_BUTTON-PC_13 +//------------------------------------ +DigitalIn mybutton(PB_3); +//------------------------------------ +// LED1-PA5 +//------------------------------------ +DigitalOut myled(PA_5); +//------------------------------------ +// Hyperterminal configuration +// 9600 bauds, 8-bit data, no parity +// SERIAL_TX-PA2, SERIAL_RX-PA3 +//------------------------------------ +Serial pc(PA_2, PA_3); + +Ticker jitter_down, jitter_up; + +#define entry() pc.printf("func:%s\n", __FUNCTION__) + +#define error() pc.printf("Error! func:%s, line: %d\n", __FUNCTION__, __LINE__) + +enum{OFF=0,ON=1}; +enum{PRESS= 0, RELEASE = 1, UNKNOWN}; +#define PRESS_JITTER_TIME 0.01 // 10ms +#define RELEASE_JITTER_TIME 0.02 // 20ms +uint8_t cur_flag_status = UNKNOWN; +uint8_t pre_flag_status = UNKNOWN; + +void update_key_status(void) +{ + pre_flag_status = cur_flag_status; + cur_flag_status = mybutton.read(); +} + +void key_down_status(void) +{ + update_key_status(); + if(cur_flag_status == PRESS){ + if(myled.read()!=ON){ + if(pre_flag_status == PRESS){ + myled = ON; + pc.printf("Pressed\n"); + } + } + } +} + +void key_up_status(void) +{ + update_key_status(); + if(cur_flag_status == RELEASE){ + if(myled.read()!=OFF){ + if(pre_flag_status == RELEASE){ + myled = OFF; + pc.printf("Released\n"); + } + } + } +} + +/* + * 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 + } +} \ No newline at end of file
diff -r 000000000000 -r ebf8ee3811c2 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Aug 28 12:54:53 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/2241e3a39974 \ No newline at end of file