Library for detecting button clicks, doubleclicks and long press pattern on a single button.

Dependents:   OneButton_Example ABBlind

Revision:
1:fdf67f893a5c
Parent:
0:62d614796022
diff -r 62d614796022 -r fdf67f893a5c OneButton.h
--- a/OneButton.h	Wed Aug 31 10:09:43 2016 +0000
+++ b/OneButton.h	Thu Sep 01 09:21:00 2016 +0000
@@ -1,3 +1,6 @@
+// Modifyed to support mbed environment, rewrite the state machine.
+// by Zibin Zheng <znbin@qq.com>
+
 // -----
 // OneButton.h - Library for detecting button clicks, doubleclicks and long press pattern on a single button.
 // This class is implemented for use with the Arduino environment.
@@ -5,21 +8,66 @@
 // This work is licensed under a BSD style license. See http://www.mathertel.de/License.aspx
 // More information on: http://www.mathertel.de/Arduino
 // -----
-// 02.10.2010 created by Matthias Hertel
-// 21.04.2011 transformed into a library
-// 01.12.2011 include file changed to work with the Arduino 1.0 environment
-// 23.03.2014 Enhanced long press functionalities by adding longPressStart and longPressStop callbacks
-// 21.09.2015 A simple way for debounce detection added.
-// -----
+
 
 #ifndef OneButton_h
 #define OneButton_h
 
 #include "mbed.h"
-#include "millis.h"
+
+
+/**
+ * Example:
+ * @code
+ 
+#include "mbed.h"
+#include "OneButton.h"
+
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+OneButton btn1(PC_13);
+
+void pressed() {
+	pc.printf("pressed\r\n");
+}
+
+void click() {
+	pc.printf("click\r\n");
+}
+
+void double_click() {
+	pc.printf("double_click\r\n");
+}
 
-#define HIGH		1
-#define LOW         0
+void long_start() {
+	pc.printf("long_start\r\n");
+}
+
+void long_hold() {
+	pc.printf("long_hold\r\n");
+}
+
+void long_stop() {
+	pc.printf("long_stop\r\n");
+}
+
+int main() {
+
+	btn1.attachClick(&click);
+	btn1.attachPress(&pressed);
+	btn1.attachDoubleClick(&double_click);
+	btn1.attachLongPressStart(&long_start);
+	btn1.attachDuringLongPress(&long_hold);
+	btn1.attachLongPressStop(&long_stop);
+
+	while(1) {
+
+		btn1.tick(); //loop
+	}
+}
+
+*/
+
 
 // ----- Callback function types -----
 
@@ -31,8 +79,9 @@
 class OneButton
 {
 public:
+
 	// ----- Constructor -----
-	OneButton(PinName pin, int active = 1);
+	OneButton(PinName pin, bool active_level = 0);
 
 	// ----- Set runtime parameters -----
 
@@ -57,15 +106,11 @@
 	bool isLongPressed();
 
 private:
-	int _pin;        // hardware pin number.
 	int _clickTicks; // number of ticks that have to pass by before a click is detected
 	int _pressTicks; // number of ticks that have to pass by before a long button press is detected
-	static 
-	const int _debounceTicks = 10; // number of ticks for debounce times.
 
 	int _buttonReleased;
 	int _buttonPressed;
-
 	bool _isLongPressed;
 
 	// These variables will hold functions acting as event source.
@@ -78,13 +123,18 @@
 
 	// These variables that hold information across the upcoming tick calls.
 	// They are initialized once on program start and are updated every time the tick function is called.
-	int _state;
-	unsigned long _startTime; // will be set in state 1
+	
+	uint16_t ticks;
+	uint8_t _state;
+	uint8_t _debounce_cnt;
+	uint8_t _debounceTicks; // number of ticks for debounce times.
+	bool button_level;
+	bool active;
 	
 	DigitalIn* btn_pin;
+	Timer *timer;
 };
 
 #endif
 
 
-