From c51d1d90eb746e1d877ba8d9dfbe061212449c87 Mon Sep 17 00:00:00 2001 From: i3water <121024123@qq.com> Date: Wed, 9 May 2018 14:59:11 +0800 Subject: [PATCH] update button codes, support long press --- .../Blinker_Button/Button_BLE/Button_BLE.ino | 9 +++- .../Button_WiFi/Button_WiFi.ino | 9 +++- src/Blinker/BlinkerApi.h | 43 +++++++++++++++++-- src/Blinker/BlinkerConfig.h | 4 +- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/examples/Blinker_Button/Button_BLE/Button_BLE.ino b/examples/Blinker_Button/Button_BLE/Button_BLE.ino index b999e903..c39137ef 100644 --- a/examples/Blinker_Button/Button_BLE/Button_BLE.ino +++ b/examples/Blinker_Button/Button_BLE/Button_BLE.ino @@ -32,5 +32,12 @@ void loop() if (Blinker.button(BUTTON_1)) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); - } + } // simple tap + + // if (Blinker.button(BUTTON_1)) { + // digitalWrite(LED_BUILTIN, LOW); + // } + // else { + // digitalWrite(LED_BUILTIN, HIGH); + // } //long press } \ No newline at end of file diff --git a/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino b/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino index 3bb1ab14..b8244218 100644 --- a/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino +++ b/examples/Blinker_Button/Button_WiFi/Button_WiFi.ino @@ -35,5 +35,12 @@ void loop() if (Blinker.button(BUTTON_1)) { digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); - } + } // simple tap + + // if (Blinker.button(BUTTON_1)) { + // digitalWrite(LED_BUILTIN, LOW); + // } + // else { + // digitalWrite(LED_BUILTIN, HIGH); + // } //long press } diff --git a/src/Blinker/BlinkerApi.h b/src/Blinker/BlinkerApi.h index 31b4a340..7d3ad816 100644 --- a/src/Blinker/BlinkerApi.h +++ b/src/Blinker/BlinkerApi.h @@ -48,13 +48,15 @@ class BlinkerButton void name(String name) { buttonName = name; } String getName() { return buttonName; } - void freshState(bool state) { buttonState = state; } + void freshState(bool state, bool isLong = false) { buttonState = state; isLPress = isLong; } bool getState() { return buttonState; } + bool longPress() { return isLPress; } bool checkName(String name) { return ((buttonName == name) ? true : false); } private : String buttonName; bool buttonState; + bool isLPress; }; class BlinkerSlider @@ -222,7 +224,7 @@ class BlinkerApi _fresh = true; } - if (state == BLINKER_CMD_BUTTON_PRESSED || state == BLINKER_CMD_BUTTON_TAP) { + if (state == BLINKER_CMD_BUTTON_TAP) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { _Button[_bCount] = new BlinkerButton(); @@ -238,6 +240,22 @@ class BlinkerApi _fresh = true; return true; } + else if (state == BLINKER_CMD_BUTTON_PRESSED) { + if( num == BLINKER_OBJECT_NOT_AVAIL ) { + if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { + _Button[_bCount] = new BlinkerButton(); + _Button[_bCount]->name(_bName); + _Button[_bCount]->freshState(true, true); + _bCount++; + } + } + else { + _Button[num]->freshState(true, true); + } + + _fresh = true; + return true; + } else if (state == BLINKER_CMD_BUTTON_RELEASED) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { @@ -265,7 +283,8 @@ class BlinkerApi } bool _state = _Button[num]->getState(); - _Button[num]->freshState(false); + if ( !_Button[num]->longPress() ) + _Button[num]->freshState(false); return _state; } @@ -562,7 +581,7 @@ class BlinkerApi _fresh = true; } - if (state == BLINKER_CMD_BUTTON_PRESSED || state == BLINKER_CMD_BUTTON_TAP) { + if (state == BLINKER_CMD_BUTTON_TAP) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { _Button[_bCount] = new BlinkerButton(); @@ -578,6 +597,22 @@ class BlinkerApi _fresh = true; return true; } + else if (state == BLINKER_CMD_BUTTON_PRESSED) { + if( num == BLINKER_OBJECT_NOT_AVAIL ) { + if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { + _Button[_bCount] = new BlinkerButton(); + _Button[_bCount]->name(_bName); + _Button[_bCount]->freshState(true, true); + _bCount++; + } + } + else { + _Button[num]->freshState(true, true); + } + + _fresh = true; + return true; + } else if (state == BLINKER_CMD_BUTTON_RELEASED) { if( num == BLINKER_OBJECT_NOT_AVAIL ) { if ( _bCount < BLINKER_MAX_WIDGET_SIZE ) { diff --git a/src/Blinker/BlinkerConfig.h b/src/Blinker/BlinkerConfig.h index 14b69d79..9d636975 100644 --- a/src/Blinker/BlinkerConfig.h +++ b/src/Blinker/BlinkerConfig.h @@ -49,9 +49,9 @@ #define BLINKER_CMD_BUTTON_TAP "tap" -#define BLINKER_CMD_BUTTON_PRESSED "pressed" +#define BLINKER_CMD_BUTTON_PRESSED "press" -#define BLINKER_CMD_BUTTON_RELEASED "released" +#define BLINKER_CMD_BUTTON_RELEASED "pressup" #define BLINKER_CMD_NEWLINE "\n"