Skip to content

Commit

Permalink
Update comments and move to doxygen style
Browse files Browse the repository at this point in the history
  • Loading branch information
mmitchellmoss committed Jul 13, 2024
1 parent 3bfb982 commit f861769
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
54 changes: 45 additions & 9 deletions include/Switch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,63 @@
#include "Arduino.h"

namespace M3 {

class Switch {
public:
// Constructors.

/**
* @brief Simple constructor that sets the switch object's high or low type and microcontroller pin.
* @note
* @param highOrLow: HIGH or LOW switch type.
* @param pin: Microcontroller pin that the switch is connected to.
* @retval None
*/
Switch(int highOrLow, int pin);

/**
* @brief Constructor that sets the switch object's high or low type, microcontroller pin, and debounce delay.
* @note
* @param highOrLow: HIGH or LOW switch type.
* @param pin: Microcontroller pin that the switch is connected to.
* @param debounceDelay: Time in millis to allow the switch to debounce.
* @retval None
*/
Switch(int highOrLow, int pin, int debounceDelay);




// Methods.

/**
* @brief Check to see if the switch is pressed.
* @note
* @retval true or false if the switch is pressed.
*/
bool isSwitchPressed();


private:
// Private members.
int m_switchPin { 0 };
int m_debounceDelay { 50 };
unsigned long m_debounceTimer { 0ul };
int m_currentState { LOW };
int m_lastState { LOW };
int m_pressedState { HIGH };

// Private methods
int m_switchPin { 0 }; ///< Microcontroller pin that the switch is connected to.
int m_debounceDelay { 50 }; ///< Time to let the switch settle in millis.
unsigned long m_debounceTimer { 0ul }; ///< Value to track how long we have been waiting for the switch to debounce.
int m_currentState { LOW }; ///< State that the switch is currently in.
int m_lastState { LOW }; ///< State that the switch was in the last time we checked it.
int m_pressedState { HIGH }; ///< What state should the switch be in to be considered pressed.




// Private methods.

/**
* @brief Configures the object as being a high or low activated switch.
* @note
* @param highOrLow: HIGH or LOW
* @retval None
*/
void setHighOrLow(int highOrLow);

};
Expand Down
25 changes: 11 additions & 14 deletions src/Switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@

namespace M3 {

// Constructor. Loads the pin connected to the switch.
Switch::Switch(int highOrLow, int pin) {
setHighOrLow(highOrLow);
m_switchPin = pin;
}

// Constructor. Loads the pin connect to the switch and the desired debounce delay.
Switch::Switch(int highOrLow, int pin, int debounceDelay) {
setHighOrLow(highOrLow);
m_switchPin = pin;
m_debounceDelay = debounceDelay;
}

// Function to configure the object as being a high or low activated switch.
void Switch::setHighOrLow(int highOrLow) {
// If we get a HIGH value then configure the object for high activation.
if (highOrLow == HIGH) {
m_currentState = LOW;
m_lastState = LOW;
m_pressedState = HIGH;

// Otherwise, for anything else configure it for low activation.
} else {
m_currentState = HIGH;
m_lastState = HIGH;
Expand All @@ -33,25 +27,28 @@ namespace M3 {



// Function used to return if the related switch is pressed or not. It contains all debounce logic.
bool Switch::isSwitchPressed() {

int readState = digitalRead(m_switchPin); // Current state as of this function call.
unsigned long currentMillis = millis(); // Current millis() counter as of this function call.
bool ret = false; // Return value
int readState = digitalRead(m_switchPin);
unsigned long currentMillis = millis();
bool ret = false;

// If the state changed then there was switch activity, so start the debounce timer.
// Whenever the state is changing, then the switch contacts have changed or are bouncing.
// As long as the state changes, keep resetting the debouce timer. We need a full debounce
// delay of unchanging state to occur before we move into the following logic areas.
if (readState != m_lastState) {
m_debounceTimer = currentMillis;
}

// See if the debounce delay has passed.
// The state is steady, so we can actually evaluate the switch.
if ((currentMillis - m_debounceTimer) >= m_debounceDelay) {
// See if the switch state has changed.
// A state change must have happened for us to move farther into the check. In other words if we had
// already reported a switch press then we are not going to do it again. You would have to re-press the
// switch to get a possible true return again. We are looking for transitions to pressed, not steady press and hold.
if (readState != m_currentState) {
m_currentState = readState;

// Return true only if the mode switch is still pressed.
// Return true only if the mode switch is in whatever state we consider to be pressed.
if (m_currentState == m_pressedState) {
ret = true;
}
Expand Down

0 comments on commit f861769

Please sign in to comment.