Skip to content

Commit

Permalink
Allow update of time interval (#18)
Browse files Browse the repository at this point in the history
* Remove compiler warnings (#14)

* Allow update of time listener interval
  • Loading branch information
matthewturner authored Oct 22, 2023
1 parent 60ea8be commit 6050aa6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"maintainer": true
}
],
"version": "2.0.0",
"version": "2.1.0",
"frameworks": "*",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Eventually2
version=2.0.0
version=2.1.0
author=Jonathan Bartlett <[email protected]>
maintainer=Matt Turner <[email protected]>
sentence=Event-based programming library for Arduino
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ platform = native
test_build_src = yes
build_flags =
-D EVENTUALLY_MAX_CONTEXTS=3
lib_deps = fabiobatsilva/ArduinoFake@^0.3.1
lib_deps = https://github.com/matthewturner/ArduinoFake.git#v0.3.2

[env:debug]
platform = native
build_type = debug
debug_test = test_evt_pin_listener
lib_deps = fabiobatsilva/ArduinoFake@^0.3.1
lib_deps = https://github.com/matthewturner/ArduinoFake.git#v0.3.2
17 changes: 11 additions & 6 deletions src/EvtTimeListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ EvtTimeListener::EvtTimeListener()
{
}

EvtTimeListener::EvtTimeListener(unsigned long time, bool multiFire, EvtAction triggerAction)
EvtTimeListener::EvtTimeListener(unsigned long interval, bool multiFire, EvtAction triggerAction)
{
_millis = time;
_interval = interval;
_triggerAction = triggerAction;
_multiFire = multiFire;
}
Expand Down Expand Up @@ -34,15 +34,15 @@ bool EvtTimeListener::isEventTriggered()
if (curTime >= _startMillis)
{
/* Normal */
if (curTime - _startMillis > _millis)
if (curTime - _startMillis > _interval)
{
shouldFire = true;
}
}
else
{
/* Wrap-Around! */
if (((ULONG_MAX - _startMillis) + curTime) > _millis)
if (((ULONG_MAX - _startMillis) + curTime) > _interval)
{
shouldFire = true;
}
Expand All @@ -53,7 +53,7 @@ bool EvtTimeListener::isEventTriggered()

bool EvtTimeListener::performTriggerAction(IEvtContext *c)
{
bool returnval = (*_triggerAction)(this, c);
bool returnVal = (*_triggerAction)(this, c);
if (_multiFire)
{
// On multifire, setup to receive the event again
Expand All @@ -64,6 +64,11 @@ bool EvtTimeListener::performTriggerAction(IEvtContext *c)
else
{
_hasExecuted = true;
return returnval;
return returnVal;
}
}

void EvtTimeListener::setInterval(unsigned long interval)
{
_interval = interval;
}
5 changes: 3 additions & 2 deletions src/EvtTimeListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ class EvtTimeListener : public EvtListener
{
public:
EvtTimeListener();
EvtTimeListener(unsigned long time, bool multiFire, EvtAction trigger);
EvtTimeListener(unsigned long interval, bool multiFire, EvtAction trigger);
void reset();
bool isEventTriggered();
bool performTriggerAction(IEvtContext *);
void setInterval(unsigned long interval);

private:
unsigned long _millis;
unsigned long _interval;
unsigned long _startMillis;
bool _multiFire = false;
bool _hasExecuted = false;
Expand Down
12 changes: 12 additions & 0 deletions test/test_evt_time_listener/test_evt_time_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ void test_triggers_after_time_delay(void)
TEST_ASSERT_TRUE(target.isEventTriggered());
}

void test_triggers_after_updated_time_delay(void)
{
When(Method(ArduinoFake(), digitalRead)).AlwaysReturn(HIGH);

When(Method(ArduinoFake(), millis)).AlwaysReturn(210);

target.setInterval(199);

TEST_ASSERT_TRUE(target.isEventTriggered());
}

void test_triggers_after_wrap_around(void)
{
When(Method(ArduinoFake(), digitalRead)).AlwaysReturn(HIGH);
Expand Down Expand Up @@ -92,6 +103,7 @@ int main(int argc, char **argv)
RUN_TEST(test_does_not_trigger_when_disabled);
RUN_TEST(test_does_not_trigger_before_time_delay);
RUN_TEST(test_triggers_after_time_delay);
RUN_TEST(test_triggers_after_updated_time_delay);
RUN_TEST(test_triggers_after_wrap_around);
RUN_TEST(test_multifire_always_returns_false);
RUN_TEST(test_multifire_resets_delay_check);
Expand Down

0 comments on commit 6050aa6

Please sign in to comment.