Skip to content

Commit

Permalink
Fixed bugs, example InvertInputLogic added
Browse files Browse the repository at this point in the history
  • Loading branch information
Ales-Svoboda authored and vzahradnik committed May 21, 2023
1 parent f7d8e84 commit 563e539
Show file tree
Hide file tree
Showing 14 changed files with 207 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion examples/BlinkMachine/BlinkMachine.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
170 changes: 170 additions & 0 deletions examples/InvertInputLogic/InvertInputLogic.ino
Original file line number Diff line number Diff line change
@@ -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 <ObjectButton.h>
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();
}
4 changes: 3 additions & 1 deletion examples/ToggleLedOnClick/ToggleLedOnClick.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion examples/ToggleLedOnDoubleClick/ToggleLedOnDoubleClick.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion examples/TurnLedOnLongPress/TurnLedOnLongPress.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion examples/TwoDigitalButtons/TwoDigitalButtons.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/base/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
7 changes: 3 additions & 4 deletions src/digital/DigitalButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -67,14 +67,13 @@ 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.
*
* 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);
}

0 comments on commit 563e539

Please sign in to comment.