Johannes Stratmann / OneButton

Dependents:   ECH_V004

Fork of OneButton by Zibin Zheng

Revision:
2:26960fb1d783
Parent:
1:fdf67f893a5c
--- a/OneButton.h	Thu Sep 01 09:21:00 2016 +0000
+++ b/OneButton.h	Thu Sep 22 20:36:52 2016 +0000
@@ -15,7 +15,6 @@
 
 #include "mbed.h"
 
-
 /**
  * Example:
  * @code
@@ -69,12 +68,13 @@
 */
 
 
+#if (MBED_LIBRARY_VERSION < 122)
 // ----- Callback function types -----
 
 extern "C" {
 	typedef void (*callbackFunction)(void);
 }
-
+#endif
 
 class OneButton
 {
@@ -92,13 +92,53 @@
 	void setPressTicks(int ticks);
 
 	// attach functions that will be called when button was pressed in the specified way.
+    
+#if (MBED_LIBRARY_VERSION < 122)
 	void attachClick(callbackFunction newFunction);
 	void attachDoubleClick(callbackFunction newFunction);
 	void attachPress(callbackFunction newFunction); // DEPRECATED, replaced by longPressStart, longPressStop and duringLongPress
 	void attachLongPressStart(callbackFunction newFunction);
 	void attachLongPressStop(callbackFunction newFunction);
 	void attachDuringLongPress(callbackFunction newFunction);
+#else
+	// click function
+	void attachClick(Callback<void()> func){
+		clickFn.attach(func);
+	};
 
+	// doubleClick function
+	void attachDoubleClick(Callback<void()> func){
+		doubleClickFn.attach(func);
+	};
+
+	// press function
+	void attachPress(Callback<void()> func){
+		pressFn.attach(func);
+	};
+
+	// long press start function
+	void attachLongPressStart(Callback<void()> func){
+		longPressStartFn.attach(func);
+	};
+
+	// long press stop function
+	void attachLongPressStop(Callback<void()> func){
+		longPressStopFn.attach(func);
+	};
+
+	// during long press function
+	void attachDuringLongPress(Callback<void()> func){
+		duringLongPressFn.attach(func);
+	};
+    
+   	Callback<void()> clickFn;
+   	Callback<void()> doubleClickFn;
+   	Callback<void()> pressFn;
+   	Callback<void()> longPressStartFn;
+   	Callback<void()> longPressStopFn;
+   	Callback<void()> duringLongPressFn;
+
+#endif
 	// ----- State machine functions -----
 
 	// call this function every some milliseconds for handling button events.
@@ -114,17 +154,21 @@
 	bool _isLongPressed;
 
 	// These variables will hold functions acting as event source.
-	callbackFunction _clickFunc;
-	callbackFunction _doubleClickFunc;
-	callbackFunction _pressFunc;
-	callbackFunction _longPressStartFunc;
-	callbackFunction _longPressStopFunc;
-	callbackFunction _duringLongPressFunc;
+	// mbed lib < 122 will use a simple function pointer, 
+	// for libs >= 122 we can use the new Callback template to avoid compiler warnings
 
+#if (MBED_LIBRARY_VERSION < 122)
+	callbackFunction clickFn;
+	callbackFunction doubleClickFn;
+	callbackFunction pressFn;
+	callbackFunction longPressStartFn;
+	callbackFunction longPressStopFn;
+	callbackFunction duringLongPressFn;
+#endif
 	// 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.
 	
-	uint16_t ticks;
+	int ticks;
 	uint8_t _state;
 	uint8_t _debounce_cnt;
 	uint8_t _debounceTicks; // number of ticks for debounce times.