implement button press/release with double check using coroutine
Revision 0:54509830df20, committed 2016-08-28
- Comitter:
- mintisan
- Date:
- Sun Aug 28 12:50:45 2016 +0000
- Commit message:
- implement button press/release with double check using coroutine
Changed in this revision
diff -r 000000000000 -r 54509830df20 coroutine.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/coroutine.h Sun Aug 28 12:50:45 2016 +0000 @@ -0,0 +1,34 @@ +#ifndef COROUTINE_H_ +#define COROUTINE_H_ + +#ifdef __cplushplus +extern "C"{ +#endif + +/** + * C coroutine + * https://github.com/mintisan/mintisan.github.io/wiki/C#coroutine + */ + +#define cr_start() \ + static int __s = 0; \ + switch(__s){ \ + case 0: + +// '\' doesn't count for one line in C +#define cr_yield(condition) do{ \ + __s = __LINE__ ; \ + case __LINE__: \ + if(condition) \ + return; \ + }while(0) + +#define cr_end() \ + } __s = 0; + + +#ifdef __cplushplus +} +#endif + +#endif
diff -r 000000000000 -r 54509830df20 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Aug 28 12:50:45 2016 +0000 @@ -0,0 +1,111 @@ +#include "mbed.h" +#include "coroutine.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; + +#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; + +volatile uint16_t cr_time_down; +volatile uint16_t cr_time_up; + +void isr_ticker() { + cr_time_down++; + cr_time_up++; +} + + +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 user_thread_down(void) +{ + cr_start(); + + // inital once each loop + cr_time_down = 0; + // waiting for condition is satisfied, or will be yield + cr_yield(cr_time_down != 1); + key_down_status(); + + cr_end(); +} + +void user_thread_up(void) +{ + cr_start(); + + // inital once each loop + cr_time_up = 0; + // waiting for condition is satisfied, or will be yield + cr_yield(cr_time_up != 2); + key_up_status(); + + cr_end(); +} + +/* + * one coroutine for down check; + * another coroutine for up check. + */ +int main(void) +{ + myled = OFF; + jitter.attach(&isr_ticker, PRESS_JITTER_TIME); + while(1) { + user_thread_down(); + user_thread_up(); + // extend more task here + } +} \ No newline at end of file
diff -r 000000000000 -r 54509830df20 mbed-dev.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-dev.lib Sun Aug 28 12:50:45 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-dev/#423e1876dc07