Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

formatting + inputtype added #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 129 additions & 90 deletions Buttons.cpp
Original file line number Diff line number Diff line change
@@ -1,132 +1,153 @@
#include <Buttons.h>

PressEvent::PressEvent(PressType t,
int a ,
unsigned long time,
bool triggd):
type(t),
amount(a),
time(time),
triggd(triggd)
{};

void settype(PressType t){
PressEvent::PressEvent(PressType t,
int a,
unsigned long time,
bool triggd) : type(t),
amount(a),
time(time),
triggd(triggd){};

void settype(PressType t)
{
type = t;
};

int PressEvent::ispress(PressType t) const{
if (type == t && amount == a){
int PressEvent::ispress(PressType t) const
{
if (type == t && amount == a)
{
return amount;
}
return 0;
};

void PressEvent::settype(PressType t){
if ( t != type){
void PressEvent::settype(PressType t)
{
if (t != type)
{
triggd = false;
type = t;
time = millis();
};
};

void PressEvent::setamount(int a){
void PressEvent::setamount(int a)
{
amount = a;
};

void PressEvent::settime(unsigned long t){
void PressEvent::settime(unsigned long t)
{
time = t;
};

void PressEvent::settriggd(bool t){
void PressEvent::settriggd(bool t)
{
triggd = t;
};

bool PressEvent::istriggered() const{
bool PressEvent::istriggered() const
{
return triggd;
};

bool PressEvent::isindeterm() const{
bool PressEvent::isindeterm() const
{
return type == INDETERM;
};

void PressEvent::retrig(unsigned long delay){
void PressEvent::retrig(unsigned long delay)
{
triggd = false;
time = millis() + delay;
};

Button::Button(int pin, unsigned long jitterDelay, int activeState, unsigned long pressWindow, unsigned long holdWindow, int maxPresses):
pin(pin),
steadyState(HIGH),
lastRawState(HIGH),
lastRawTime(0),
steadyTime(0),
jitterDelay(jitterDelay),
stateRead(false),
activeState(LOW),
pressWindow(pressWindow),
holdWindow(holdWindow),
holdTime(0),
maxPresses(maxPresses),
stableEvents(0),
pevent(PressEvent())
Button::Button(int pin, unsigned long jitterDelay, int activeState, unsigned long pressWindow, unsigned long holdWindow, int maxPresses, int inputType) : pin(pin),
steadyState(HIGH),
lastRawState(HIGH),
lastRawTime(0),
steadyTime(0),
jitterDelay(jitterDelay),
stateRead(false),
activeState(LOW),
pressWindow(pressWindow),
holdWindow(holdWindow),
holdTime(0),
maxPresses(maxPresses),
stableEvents(0),
pevent(PressEvent())
{
pinMode(pin, INPUT); // Set the pin as input
pinMode(pin, inputType); // Set the pin as input
};

int Button::rawRead() const{
int Button::rawRead() const
{
return digitalRead(pin);
};

void Button::steadyRead(){
void Button::steadyRead()
{
int readstate = rawRead();
if(readstate != lastRawState){ // if state changes
lastRawState = readstate; // save the new state
if (readstate != lastRawState)
{ // if state changes
lastRawState = readstate; // save the new state
lastRawTime = millis(); // note the time
}
else if (millis() - lastRawTime > jitterDelay){
if (readstate != steadyState){ // If the state has been steady for long enough
else if (millis() - lastRawTime > jitterDelay)
{
if (readstate != steadyState)
{ // If the state has been steady for long enough
steadyState = readstate;
stateRead = false;
steadyTime = millis();
}
}
};

int Button::getState() const{
int Button::getState() const
{
return steadyState;
};

void Button::setDelay(const unsigned long newdelay){
void Button::setDelay(const unsigned long newdelay)
{
jitterDelay = newdelay;
};

bool Button::isInactive() const{
bool Button::isInactive() const
{
return getState() == !activeState && stableEvents == 0;
}

bool Button::isActive() const{
bool Button::isActive() const
{
return getState() == activeState;
};

bool Button::isWaiting() const{
bool Button::isWaiting() const
{
return stableEvents != 0;
};

bool Button::previousWaitingState() const{
bool Button::previousWaitingState() const
{
return stableEvents % 2 != 0;
};

bool Button::wasHeld() const{
bool Button::wasHeld() const
{
return holdTime != 0;
};

int Button::press() const{
int Button::press() const
{
return pevent.ispress(PRESS);
};

int Button::holdTriggered() const{
auto holdnum = pevent.ispress(HOLD)
if (holdnum != 0 && !pevent.triggd && millis() >= pevent.time)
int Button::holdTriggered() const
{
auto holdnum = pevent.ispress(HOLD) if (holdnum != 0 && !pevent.triggd && millis() >= pevent.time)
{
pevent.settriggd(true);
return holdnum;
Expand All @@ -135,65 +156,77 @@ int Button::holdTriggered() const{
return 0
};

int Button:held() const{
int Button : held() const
{
return pevent.ispress(HOLD);
};


void Button::retrig(unsigned long delay){
void Button::retrig(unsigned long delay)
{
pevent.retrig(delay);
};

void Button::monitorPress(){
void Button::monitorPress()
{
steadyRead();

if (isInactive()) { // If button is inactive
if (isInactive())
{ // If button is inactive
goto indeterm_event;
}

if (!isWaiting() && isActive()){ // First press after inactivity
if (!isWaiting() && isActive())
{ // First press after inactivity
stableEvents = 1;
goto indeterm_event;
}

if (isWaiting()){
if (isActive()){ // Is waiting and now active
if(previousWaitingState()){ // Case: Is active was active
if (isWaiting())
{
if (isActive())
{ // Is waiting and now active
if (previousWaitingState())
{ // Case: Is active was active
unsigned long this_time = millis();
if (this_time - steadyTime > holdWindow
&& !(pevent.istriggered())){ // If long enough, it's held
// and it was not already
// triggered
int pamount = stableEvents/2 + 1 ; //
if (this_time - steadyTime > holdWindow && !(pevent.istriggered()))
{ // If long enough, it's held
// and it was not already
// triggered
int pamount = stableEvents / 2 + 1; //
pevent.settype(HOLD);
pevent.setamount(pamount);
pevent.settime(this_time);

holdTime = this_time; // is this still neccesary?
}
else{
else
{
goto indeterm_event;
}
}
else{ // Case: Is active was inactive
stableEvents++;
goto indeterm_event;
else
{ // Case: Is active was inactive
stableEvents++;
goto indeterm_event;
}
}
else{ // Is waiting, and currently inactive
if (previousWaitingState()){ // Case: Is inactive was active

else
{ // Is waiting, and currently inactive
if (previousWaitingState())
{ // Case: Is inactive was active

if (wasHeld()){ // If it was held, we're done
if (wasHeld())
{ // If it was held, we're done
stableEvents = 0;
holdTime = 0;
goto indeterm_event;
}

holdTime = 0;

if (stableEvents == maxPresses*2-1){ // If max presses reached
// Immediately terminate
if (stableEvents == maxPresses * 2 - 1)
{ // If max presses reached
// Immediately terminate
pevent.settype(PRESS);
pevent.setamount(maxPresses);
stableEvents = 0;
Expand All @@ -202,26 +235,32 @@ void Button::monitorPress(){
stableEvents++;
goto indeterm_event;
}
else{ // Case: Is inactive was inactive
else
{ // Case: Is inactive was inactive
unsigned long this_time = millis();
if (this_time - steadyTime > pressWindow){ // Press window passed
int pamount = stableEvents/2;
if (this_time - steadyTime > pressWindow)
{ // Press window passed
int pamount = stableEvents / 2;
pevent.settype(PRESS);
pevent.setamount(pamount);
stableEvents = 0;
// return pevent;
}
else{
else
{
goto indeterm_event;
}
}
}
}
indeterm_event:{
if (!pevent.isindeterm()){
pevent.settype(INDETERM);
pevent.settime(millis());
pevent.settriggd(false);
}
indeterm_event:
{
if (!pevent.isindeterm())
{
pevent.settype(INDETERM);
pevent.settime(millis());
pevent.settriggd(false);
}
};s
}
};
s
Loading