implement button press/release with double check using mbed OS

Files at this revision

API Documentation at this revision

Comitter:
mintisan
Date:
Sun Aug 28 12:29:03 2016 +0000
Commit message:
setup two thread to double check down/up status

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4ff16441b862 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Aug 28 12:29:03 2016 +0000
@@ -0,0 +1,89 @@
+#include "mbed.h"
+
+//------------------------------------
+// USER_KEY-PB_3 / USER_BUTTON-PC_13
+//------------------------------------
+InterruptIn 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);
+
+#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       10    // 10ms
+#define RELEASE_JITTER_TIME     20    // 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");
+           }
+        }
+    }
+}
+
+//https://developer.mbed.org/handbook/RTOS
+void user_thread_down(const void* args)
+{
+    while(true){
+        key_down_status();
+        Thread::wait(PRESS_JITTER_TIME);
+    }
+}
+
+void user_thread_up(const void* args)
+{
+    while(true){
+        key_up_status();
+        Thread::wait(RELEASE_JITTER_TIME);
+    }
+}
+
+/* 
+ *  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){
+    }
+}
diff -r 000000000000 -r 4ff16441b862 mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Sun Aug 28 12:29:03 2016 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#d2202045ac8f2fc63836a5b7131c9b56ac58f06a