Skip to content

Commit

Permalink
Add Trigger class.
Browse files Browse the repository at this point in the history
  • Loading branch information
kisom committed Mar 1, 2019
1 parent 00cb540 commit 8965d1b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SOURCES := \
include/feather/feather_m0.h \
include/feather/feather_m4.h \
include/feather/scheduling.h \
include/feather/trigger.h \
include/feather/util.h \
include/feather/wing/adalogger.h\
include/feather/wing/gps.h \
Expand All @@ -16,6 +17,7 @@ SOURCES := \
src/button.cc \
src/feather_m0.cc \
src/feather_m4.cc \
src/trigger.cc \
src/util.cc \
src/wing_adalogger.cc \
src/wing.cc \
Expand Down
19 changes: 19 additions & 0 deletions include/feather/trigger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __FEATHERLIB_TRIGGER_H
#define __FEATHERLIB_TRIGGER_H


// A Trigger returns true if enough time has passed.
class Trigger {
public:
Trigger(unsigned long delta) : next(0), delta(delta) {}
Trigger(unsigned long delta, bool skipFirst);
bool ready();
bool ready(unsigned long now);

private:
unsigned long next;
unsigned long delta;
};


#endif // __FEATHERLIB_TRIGGER_H
32 changes: 32 additions & 0 deletions src/trigger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <Arduino.h>

#include <feather/trigger.h>


Trigger::Trigger(unsigned long delta, bool skipFirst)
{
this->next = 0;
this->delta = delta;
if (skipFirst) {
this->next = millis() + delta;
}
}


bool
Trigger::ready()
{
return this->ready(millis());
}


bool
Trigger::ready(unsigned long now)
{
if (this->next < now) {
this->next = now + this->delta;
return true;
}

return false;
}

0 comments on commit 8965d1b

Please sign in to comment.