All checks were successful
Generate Documentation / build (push) Successful in 21s
94 lines
2.0 KiB
C++
94 lines
2.0 KiB
C++
/**
|
|
* @file commonFunctions.cpp
|
|
* @author Lyubomir Penev (me@lpenev.com, 571147@student.fontys.nl)
|
|
* @brief This file contains all the functions used to read buttons
|
|
* @version 0.1
|
|
* @date 2025-10-31
|
|
*
|
|
* @copyright Copyright (c) 2025
|
|
*
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include <commonFunctions.h>
|
|
#include <constants.h>
|
|
|
|
/**
|
|
* @brief Function used to read state of a normally open(LOW) button
|
|
*
|
|
* @param buttonPin The pin of the button
|
|
* @return true
|
|
* @return false
|
|
*/
|
|
bool Reader::debounceReadNO(int buttonPin)
|
|
{
|
|
bool reading = digitalRead(buttonPin);
|
|
|
|
if (reading)
|
|
{
|
|
lastPin = buttonPin;
|
|
}
|
|
|
|
bool buttonPressed = false;
|
|
|
|
if (reading != lastButtonStateNO && lastPin == buttonPin)
|
|
{
|
|
lastDebounceTime = millis(); // reset debounce timer
|
|
}
|
|
|
|
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
|
|
{
|
|
// If button state is stable and just changed to pressed
|
|
if (reading == HIGH && buttonStateNO == LOW)
|
|
{
|
|
buttonPressed = true;
|
|
}
|
|
buttonStateNO = reading;
|
|
}
|
|
|
|
if (lastPin == buttonPin)
|
|
{
|
|
lastButtonStateNO = reading;
|
|
}
|
|
return buttonPressed;
|
|
}
|
|
|
|
/**
|
|
* @brief Function used to read state of a normally closed(HIGH) button
|
|
*
|
|
* @param buttonPin The pin of the button
|
|
* @return true
|
|
* @return false
|
|
*/
|
|
bool Reader::debounceReadNC(int buttonPin)
|
|
{
|
|
bool reading = digitalRead(buttonPin);
|
|
|
|
if (reading == LOW)
|
|
{
|
|
lastPin = buttonPin;
|
|
}
|
|
|
|
bool buttonPressed = false;
|
|
|
|
if (reading != lastButtonStateNC && lastPin == buttonPin)
|
|
{
|
|
lastDebounceTime = millis(); // reset debounce timer
|
|
}
|
|
|
|
if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY)
|
|
{
|
|
// If button state is stable and just changed to pressed
|
|
if (reading == LOW && buttonStateNC == HIGH)
|
|
{
|
|
buttonPressed = true;
|
|
}
|
|
buttonStateNC = reading;
|
|
}
|
|
|
|
if (lastPin == buttonPin)
|
|
{
|
|
lastButtonStateNC = reading;
|
|
}
|
|
return buttonPressed;
|
|
} |