![](/media/cache/profiles/c5c2f3677cc6e565cbf176c7f8982c80.jpg.50x50_q85.jpg)
implement button press/release with double check using fall/rise & timeout
Revision 6:88ac8fb51ce5, committed 2016-08-28
- Comitter:
- mintisan
- Date:
- Sun Aug 28 12:57:16 2016 +0000
- Parent:
- 5:f89c9cfa6b38
- Commit message:
- implement button press/release with double check using fall/rise & timeout; ;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun Aug 28 11:55:28 2016 +0000 +++ b/main.cpp Sun Aug 28 12:57:16 2016 +0000 @@ -15,7 +15,6 @@ //------------------------------------ Serial pc(PA_2, PA_3); -// http://www.amobbs.com/thread-4308630-1-1.html Timeout flipper; #define entry() pc.printf("func:%s\n", __FUNCTION__) @@ -26,24 +25,50 @@ enum{PRESS= 0, RELEASE = 1, UNKNOWN}; #define PRESS_JITTER_TIME 0.01 // 10ms #define RELEASE_JITTER_TIME 0.02 // 20ms -uint8_t flag_status = UNKNOWN; +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"); + } + } + } +} void double_check(void) { //entry(); - pc.printf("before = %d, now= %d\n",flag_status,mybutton.read()); - switch(flag_status){ + switch(cur_flag_status){ case PRESS: - if(mybutton.read() == PRESS){ - myled = ON; - pc.printf("Pressed\n"); - } + key_down_status(); break; case RELEASE: - if(mybutton.read() == RELEASE){ - myled = OFF; - pc.printf("Released\n"); - } + key_up_status(); break; default: error(); @@ -55,7 +80,7 @@ { //entry(); if(myled.read()!=ON){ - flag_status = PRESS; + cur_flag_status = PRESS; // double ckeck for press status flipper.attach(&double_check, PRESS_JITTER_TIME); } @@ -65,19 +90,23 @@ { //entry(); if(myled.read()!=OFF){ - flag_status = RELEASE; + cur_flag_status = RELEASE; // double check for releasse status flipper.attach(&double_check, RELEASE_JITTER_TIME); } } - +/* + * 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 } } \ No newline at end of file