diff --git a/examples/AnalogButtonToggleLedOnClick/AnalogButtonToggleLedOnClick.ino b/examples/AnalogButtonToggleLedOnClick/AnalogButtonToggleLedOnClick.ino index 555c4e0..d75916d 100644 --- a/examples/AnalogButtonToggleLedOnClick/AnalogButtonToggleLedOnClick.ino +++ b/examples/AnalogButtonToggleLedOnClick/AnalogButtonToggleLedOnClick.ino @@ -4,7 +4,9 @@ * This sketch demonstrates using ObjectButton library with single analog button, * which will turn built-in LED on or off after being clicked. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/AnalogSensorToggleLedOnDetect/AnalogSensorToggleLedOnDetect.ino b/examples/AnalogSensorToggleLedOnDetect/AnalogSensorToggleLedOnDetect.ino index 5c418bd..45fea07 100644 --- a/examples/AnalogSensorToggleLedOnDetect/AnalogSensorToggleLedOnDetect.ino +++ b/examples/AnalogSensorToggleLedOnDetect/AnalogSensorToggleLedOnDetect.ino @@ -4,7 +4,9 @@ * This sketch demonstrates using ObjectButton library with single analog sensor, * which will turn built-in LED on or off after some motion is being detected. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/BlinkMachine/BlinkMachine.ino b/examples/BlinkMachine/BlinkMachine.ino index aa43e7e..ea211f4 100644 --- a/examples/BlinkMachine/BlinkMachine.ino +++ b/examples/BlinkMachine/BlinkMachine.ino @@ -11,7 +11,9 @@ * * See their sketch for more details on internal workings, if it's not clear for your from this code. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/ClassNameConflictResolution/ClassNameConflictResolution.ino b/examples/ClassNameConflictResolution/ClassNameConflictResolution.ino index 2703560..4a6f448 100644 --- a/examples/ClassNameConflictResolution/ClassNameConflictResolution.ino +++ b/examples/ClassNameConflictResolution/ClassNameConflictResolution.ino @@ -33,7 +33,9 @@ * In this example, we'll show you, how to write your code, when you * have class name conflicts. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/InvertInputLogic/InvertInputLogic.ino b/examples/InvertInputLogic/InvertInputLogic.ino new file mode 100644 index 0000000..e27d512 --- /dev/null +++ b/examples/InvertInputLogic/InvertInputLogic.ino @@ -0,0 +1,170 @@ +/** + * @brief Invert input logic of two DigitalButtons. + * + * This sketch demonstrates ObjectButton library with the option to invert input logic. + * Logical 1 will be considered as logical 0 and vice versa. + * + * By default, the user can invert the logic when creating an DigitalButton instance using + * the second parameter in the constructor: + * - DigitalButton(INPUT_PIN, true) or + * - DigitalButton(INPUT_PIN, false) + * + * In this example, however, the input logic can be also inverted during program execution. + * A long press of button 1 (INPUT_PIN_BUTTON1) inverts the logic of button 2 (INPUT_PIN_BUTTON2). + * + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +using namespace jsc; + +constexpr static byte INPUT_PIN_BUTTON1 = A1; +constexpr static byte INPUT_PIN_BUTTON2 = A2; + +class TwoButtons : private virtual IOnClickListener, + private virtual IOnDoubleClickListener, private virtual IOnPressListener { +public: + TwoButtons() = default; + + void init(); + + void update(); + +private: + void onClick(Button& button) override; + + void onDoubleClick(Button& button) override; + + void onPress(Button& button) override; + + void onRelease(Button& button) override; + + void onLongPressStart(Button& button) override; + + void onLongPressEnd(Button& button) override; + + DigitalButton button1 = DigitalButton(INPUT_PIN_BUTTON1, true /* optional, logic inverted if true */); + DigitalButton button2 = DigitalButton(INPUT_PIN_BUTTON2, true /* optional, logic inverted if true */); +}; + +void TwoButtons::onClick(Button& button) { + switch (button.getId()) { + case INPUT_PIN_BUTTON1: + Serial.println("Button 1 clicked!"); + break; + case INPUT_PIN_BUTTON2: + Serial.println("Button 2 clicked!"); + break; + } +} + +void TwoButtons::onDoubleClick(Button& button) { + switch (button.getId()) { + case INPUT_PIN_BUTTON1: + Serial.println("Button 1 double-clicked!"); + break; + case INPUT_PIN_BUTTON2: + Serial.println("Button 2 double-clicked!"); + break; + } +} + +void TwoButtons::onPress(Button& button) { + switch (button.getId()) { + case INPUT_PIN_BUTTON1: + Serial.println("Button 1 pressed!"); + break; + case INPUT_PIN_BUTTON2: + Serial.println("Button 2 pressed!"); + break; + } +} + +void TwoButtons::onRelease(Button& button) { + switch (button.getId()) { + case INPUT_PIN_BUTTON1: + Serial.println("Button 1 released!"); + break; + case INPUT_PIN_BUTTON2: + Serial.println("Button 2 released!"); + break; + } +} + +void TwoButtons::onLongPressStart(Button& button) { + switch (button.getId()) { + case INPUT_PIN_BUTTON1: + Serial.println("Long press started on button 1!"); + button2.invertInputLogic(); + if (button2.isInputLogicInverted()) { + Serial.println("Input logic is inverted."); + } else { + Serial.println("Input logic is NOT inverted."); + } + break; + case INPUT_PIN_BUTTON2: + Serial.println("Long press started on button 2!"); + break; + } +} + +void TwoButtons::onLongPressEnd(Button& button) { + switch (button.getId()) { + case INPUT_PIN_BUTTON1: + Serial.println("Long press ended on button 1!"); + break; + case INPUT_PIN_BUTTON2: + Serial.println("Long press ended on button 2!"); + break; + } +} + +void TwoButtons::init() { + // Setup the Serial port. See http://arduino.cc/en/Serial/IfSerial + Serial.begin(9600); + while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only + } + Serial.println("Starting TwoButtons..."); + + button1.setDebounceTicks(100); + button1.setOnClickListener(this); + button1.setOnDoubleClickListener(this); + button1.setOnPressListener(this); + button1.setLongPressTicks(3000 /* ms */); + + button2.setDebounceTicks(100); + button2.setOnClickListener(this); + button2.setOnDoubleClickListener(this); + button2.setOnPressListener(this); + button2.setLongPressTicks(3000 /* ms */); + +} + +void TwoButtons::update() { + button1.tick(); + button2.tick(); +} + +TwoButtons twoButtons = TwoButtons(); + +void setup() { + twoButtons.init(); +} + +void loop() { + twoButtons.update(); +} \ No newline at end of file diff --git a/examples/ToggleLedOnClick/ToggleLedOnClick.ino b/examples/ToggleLedOnClick/ToggleLedOnClick.ino index ac9005e..f98045a 100644 --- a/examples/ToggleLedOnClick/ToggleLedOnClick.ino +++ b/examples/ToggleLedOnClick/ToggleLedOnClick.ino @@ -4,7 +4,9 @@ * This sketch demonstrates using ObjectButton library with single digital button, * which will turn built-in LED on or off after being clicked. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/ToggleLedOnDoubleClick/ToggleLedOnDoubleClick.ino b/examples/ToggleLedOnDoubleClick/ToggleLedOnDoubleClick.ino index 6aa91e4..4bb6890 100644 --- a/examples/ToggleLedOnDoubleClick/ToggleLedOnDoubleClick.ino +++ b/examples/ToggleLedOnDoubleClick/ToggleLedOnDoubleClick.ino @@ -4,7 +4,9 @@ * This sketch demonstrates using ObjectButton library with single digital button, * which will turn built-in LED on or off after being double-clicked. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/ToggleLedOnDoubleClickWithInterrupt/ToggleLedOnDoubleClickWithInterrupt.ino b/examples/ToggleLedOnDoubleClickWithInterrupt/ToggleLedOnDoubleClickWithInterrupt.ino index b21713e..1acd0b9 100644 --- a/examples/ToggleLedOnDoubleClickWithInterrupt/ToggleLedOnDoubleClickWithInterrupt.ino +++ b/examples/ToggleLedOnDoubleClickWithInterrupt/ToggleLedOnDoubleClickWithInterrupt.ino @@ -7,7 +7,9 @@ * This example is almost identical to ToggleLedOnDoubleClick.ino, except * button state refresh is bound to interrupt. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/TurnLedOnLongPress/TurnLedOnLongPress.ino b/examples/TurnLedOnLongPress/TurnLedOnLongPress.ino index f9d2b00..2948bc9 100644 --- a/examples/TurnLedOnLongPress/TurnLedOnLongPress.ino +++ b/examples/TurnLedOnLongPress/TurnLedOnLongPress.ino @@ -4,7 +4,9 @@ * This sketch demonstrates using ObjectButton library with single digital button, * which will turn built-in LED on when it's long pressed. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/TwoAnalogButtonsOnTheSamePin/TwoAnalogButtonsOnTheSamePin.ino b/examples/TwoAnalogButtonsOnTheSamePin/TwoAnalogButtonsOnTheSamePin.ino index 6c1dc25..c79b8a9 100644 --- a/examples/TwoAnalogButtonsOnTheSamePin/TwoAnalogButtonsOnTheSamePin.ino +++ b/examples/TwoAnalogButtonsOnTheSamePin/TwoAnalogButtonsOnTheSamePin.ino @@ -6,7 +6,9 @@ * In this example we receive all events produced by buttons and print * which event occurred on which button to the serial monitor. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/TwoAnalogSensorsOnTheSamePin/TwoAnalogSensorsOnTheSamePin.ino b/examples/TwoAnalogSensorsOnTheSamePin/TwoAnalogSensorsOnTheSamePin.ino index 3056099..220a719 100644 --- a/examples/TwoAnalogSensorsOnTheSamePin/TwoAnalogSensorsOnTheSamePin.ino +++ b/examples/TwoAnalogSensorsOnTheSamePin/TwoAnalogSensorsOnTheSamePin.ino @@ -5,7 +5,9 @@ * * In this example we receive onClick() event, which simulates a motion detection. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/examples/TwoDigitalButtons/TwoDigitalButtons.ino b/examples/TwoDigitalButtons/TwoDigitalButtons.ino index 4b64cde..5d124c7 100644 --- a/examples/TwoDigitalButtons/TwoDigitalButtons.ino +++ b/examples/TwoDigitalButtons/TwoDigitalButtons.ino @@ -6,7 +6,9 @@ * In this example we receive all events produced by buttons and print * which event occurred on which button to the serial monitor. * - * Copyright 2019-2020 JSC electronics + * ObjectButton library: https://github.com/JSC-electronics/ObjectButton + * + * Copyright © JSC electronics * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/src/base/Button.cpp b/src/base/Button.cpp index 0fd5bd2..d78f567 100644 --- a/src/base/Button.cpp +++ b/src/base/Button.cpp @@ -136,7 +136,7 @@ void Button::setClickTicks(uint16_t ticks) { * * @param ticks a long press time interval in milliseconds. */ -void Button::setLongPressTicks(uint16_t ticks) { +void Button::setLongPressTicks(uint16_t ticks /* ms */) { m_longPressTicks = ticks; } diff --git a/src/digital/DigitalButton.cpp b/src/digital/DigitalButton.cpp index 08805e9..f54cca5 100644 --- a/src/digital/DigitalButton.cpp +++ b/src/digital/DigitalButton.cpp @@ -55,7 +55,7 @@ int DigitalButton::getId() { * Therefore, a low voltage level (0 V DC) will be represented as log 1. */ void DigitalButton::invertInputLogic() { - m_buttonPressed = LOW ? HIGH : LOW; + m_buttonPressed = (m_buttonPressed == LOW) ? HIGH : LOW; } /** @@ -67,8 +67,7 @@ void DigitalButton::invertInputLogic() { * @return True if logic is inverted. */ bool DigitalButton::isInputLogicInverted() { - if ( m_buttonPressed = LOW ) {return true;} - else {return false;} + return (m_buttonPressed == LOW); } /** * @brief Evaluate wheter a button is pressed. @@ -76,5 +75,5 @@ bool DigitalButton::isInputLogicInverted() { * This is a private method called from the state machine. It evaluates whether a button is pressed. */ bool DigitalButton::isButtonPressed() { - return digitalRead(m_pin) == m_buttonPressed; + return (digitalRead(m_pin) == m_buttonPressed); }