Ted Lin
/
Nucleo_key_led_with_timeout
implement button press/release with double check using fall/rise & timeout
main.cpp@6:88ac8fb51ce5, 2016-08-28 (annotated)
- Committer:
- mintisan
- Date:
- Sun Aug 28 12:57:16 2016 +0000
- Revision:
- 6:88ac8fb51ce5
- Parent:
- 5:f89c9cfa6b38
implement button press/release with double check using fall/rise & timeout; ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mintisan | 0:6a3454713c54 | 1 | #include "mbed.h" |
mintisan | 0:6a3454713c54 | 2 | |
mintisan | 2:d68f26154551 | 3 | //------------------------------------ |
mintisan | 3:e25965edbbe7 | 4 | // USER_KEY-PB_3 / USER_BUTTON-PC_13 |
mintisan | 2:d68f26154551 | 5 | //------------------------------------ |
mintisan | 2:d68f26154551 | 6 | InterruptIn mybutton(PB_3); |
mintisan | 2:d68f26154551 | 7 | //------------------------------------ |
mintisan | 2:d68f26154551 | 8 | // LED1-PA5 |
mintisan | 2:d68f26154551 | 9 | //------------------------------------ |
mintisan | 0:6a3454713c54 | 10 | DigitalOut myled(PA_5); |
mintisan | 2:d68f26154551 | 11 | //------------------------------------ |
mintisan | 2:d68f26154551 | 12 | // Hyperterminal configuration |
mintisan | 2:d68f26154551 | 13 | // 9600 bauds, 8-bit data, no parity |
mintisan | 2:d68f26154551 | 14 | // SERIAL_TX-PA2, SERIAL_RX-PA3 |
mintisan | 2:d68f26154551 | 15 | //------------------------------------ |
mintisan | 2:d68f26154551 | 16 | Serial pc(PA_2, PA_3); |
mintisan | 2:d68f26154551 | 17 | |
mintisan | 3:e25965edbbe7 | 18 | Timeout flipper; |
mintisan | 3:e25965edbbe7 | 19 | |
mintisan | 4:4086e27c299e | 20 | #define entry() pc.printf("func:%s\n", __FUNCTION__) |
mintisan | 2:d68f26154551 | 21 | |
mintisan | 4:4086e27c299e | 22 | #define error() pc.printf("Error! func:%s, line: %d\n", __FUNCTION__, __LINE__) |
mintisan | 3:e25965edbbe7 | 23 | |
mintisan | 3:e25965edbbe7 | 24 | enum{OFF=0,ON=1}; |
mintisan | 3:e25965edbbe7 | 25 | enum{PRESS= 0, RELEASE = 1, UNKNOWN}; |
mintisan | 4:4086e27c299e | 26 | #define PRESS_JITTER_TIME 0.01 // 10ms |
mintisan | 4:4086e27c299e | 27 | #define RELEASE_JITTER_TIME 0.02 // 20ms |
mintisan | 6:88ac8fb51ce5 | 28 | uint8_t cur_flag_status = UNKNOWN; |
mintisan | 6:88ac8fb51ce5 | 29 | uint8_t pre_flag_status = UNKNOWN; |
mintisan | 6:88ac8fb51ce5 | 30 | |
mintisan | 6:88ac8fb51ce5 | 31 | void update_key_status(void) |
mintisan | 6:88ac8fb51ce5 | 32 | { |
mintisan | 6:88ac8fb51ce5 | 33 | pre_flag_status = cur_flag_status; |
mintisan | 6:88ac8fb51ce5 | 34 | cur_flag_status = mybutton.read(); |
mintisan | 6:88ac8fb51ce5 | 35 | } |
mintisan | 6:88ac8fb51ce5 | 36 | |
mintisan | 6:88ac8fb51ce5 | 37 | void key_down_status(void) |
mintisan | 6:88ac8fb51ce5 | 38 | { |
mintisan | 6:88ac8fb51ce5 | 39 | update_key_status(); |
mintisan | 6:88ac8fb51ce5 | 40 | if(cur_flag_status == PRESS){ |
mintisan | 6:88ac8fb51ce5 | 41 | if(myled.read()!=ON){ |
mintisan | 6:88ac8fb51ce5 | 42 | if(pre_flag_status == PRESS){ |
mintisan | 6:88ac8fb51ce5 | 43 | myled = ON; |
mintisan | 6:88ac8fb51ce5 | 44 | pc.printf("Pressed\n"); |
mintisan | 6:88ac8fb51ce5 | 45 | } |
mintisan | 6:88ac8fb51ce5 | 46 | } |
mintisan | 6:88ac8fb51ce5 | 47 | } |
mintisan | 6:88ac8fb51ce5 | 48 | } |
mintisan | 6:88ac8fb51ce5 | 49 | |
mintisan | 6:88ac8fb51ce5 | 50 | void key_up_status(void) |
mintisan | 6:88ac8fb51ce5 | 51 | { |
mintisan | 6:88ac8fb51ce5 | 52 | update_key_status(); |
mintisan | 6:88ac8fb51ce5 | 53 | if(cur_flag_status == RELEASE){ |
mintisan | 6:88ac8fb51ce5 | 54 | if(myled.read()!=OFF){ |
mintisan | 6:88ac8fb51ce5 | 55 | if(pre_flag_status == RELEASE){ |
mintisan | 6:88ac8fb51ce5 | 56 | myled = OFF; |
mintisan | 6:88ac8fb51ce5 | 57 | pc.printf("Released\n"); |
mintisan | 6:88ac8fb51ce5 | 58 | } |
mintisan | 6:88ac8fb51ce5 | 59 | } |
mintisan | 6:88ac8fb51ce5 | 60 | } |
mintisan | 6:88ac8fb51ce5 | 61 | } |
mintisan | 3:e25965edbbe7 | 62 | |
mintisan | 3:e25965edbbe7 | 63 | void double_check(void) |
mintisan | 3:e25965edbbe7 | 64 | { |
mintisan | 3:e25965edbbe7 | 65 | //entry(); |
mintisan | 6:88ac8fb51ce5 | 66 | switch(cur_flag_status){ |
mintisan | 3:e25965edbbe7 | 67 | case PRESS: |
mintisan | 6:88ac8fb51ce5 | 68 | key_down_status(); |
mintisan | 3:e25965edbbe7 | 69 | break; |
mintisan | 3:e25965edbbe7 | 70 | case RELEASE: |
mintisan | 6:88ac8fb51ce5 | 71 | key_up_status(); |
mintisan | 3:e25965edbbe7 | 72 | break; |
mintisan | 3:e25965edbbe7 | 73 | default: |
mintisan | 3:e25965edbbe7 | 74 | error(); |
mintisan | 3:e25965edbbe7 | 75 | break; |
mintisan | 3:e25965edbbe7 | 76 | } |
mintisan | 3:e25965edbbe7 | 77 | } |
mintisan | 3:e25965edbbe7 | 78 | |
mintisan | 2:d68f26154551 | 79 | void pressed(void) |
mintisan | 2:d68f26154551 | 80 | { |
mintisan | 3:e25965edbbe7 | 81 | //entry(); |
mintisan | 3:e25965edbbe7 | 82 | if(myled.read()!=ON){ |
mintisan | 6:88ac8fb51ce5 | 83 | cur_flag_status = PRESS; |
mintisan | 4:4086e27c299e | 84 | // double ckeck for press status |
mintisan | 4:4086e27c299e | 85 | flipper.attach(&double_check, PRESS_JITTER_TIME); |
mintisan | 3:e25965edbbe7 | 86 | } |
mintisan | 2:d68f26154551 | 87 | } |
mintisan | 2:d68f26154551 | 88 | |
mintisan | 2:d68f26154551 | 89 | void released(void) |
mintisan | 2:d68f26154551 | 90 | { |
mintisan | 3:e25965edbbe7 | 91 | //entry(); |
mintisan | 3:e25965edbbe7 | 92 | if(myled.read()!=OFF){ |
mintisan | 6:88ac8fb51ce5 | 93 | cur_flag_status = RELEASE; |
mintisan | 4:4086e27c299e | 94 | // double check for releasse status |
mintisan | 4:4086e27c299e | 95 | flipper.attach(&double_check, RELEASE_JITTER_TIME); |
mintisan | 3:e25965edbbe7 | 96 | } |
mintisan | 2:d68f26154551 | 97 | } |
mintisan | 0:6a3454713c54 | 98 | |
mintisan | 3:e25965edbbe7 | 99 | |
mintisan | 6:88ac8fb51ce5 | 100 | /* |
mintisan | 6:88ac8fb51ce5 | 101 | * fall edge & timout interrupt for down check; |
mintisan | 6:88ac8fb51ce5 | 102 | * rise edge & timout interrupt for up check. |
mintisan | 6:88ac8fb51ce5 | 103 | */ |
mintisan | 0:6a3454713c54 | 104 | int main(void) |
mintisan | 0:6a3454713c54 | 105 | { |
mintisan | 5:f89c9cfa6b38 | 106 | myled = OFF; |
mintisan | 2:d68f26154551 | 107 | mybutton.fall(&pressed); |
mintisan | 2:d68f26154551 | 108 | mybutton.rise(&released); |
mintisan | 0:6a3454713c54 | 109 | while(1) { |
mintisan | 6:88ac8fb51ce5 | 110 | // extend more task here |
mintisan | 0:6a3454713c54 | 111 | } |
mintisan | 0:6a3454713c54 | 112 | } |