From 7d192626add832898dc6772a7a3643a28bd5f76e Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sun, 22 Oct 2023 19:55:22 +0100 Subject: [PATCH] Allow update of time listener interval --- library.json | 2 +- library.properties | 2 +- src/EvtTimeListener.cpp | 17 +++++++++++------ src/EvtTimeListener.h | 5 +++-- .../test_evt_time_listener.cpp | 12 ++++++++++++ 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/library.json b/library.json index 1f75f7e..9c73660 100644 --- a/library.json +++ b/library.json @@ -18,7 +18,7 @@ "maintainer": true } ], - "version": "2.0.0", + "version": "2.1.0", "frameworks": "*", "platforms": "*" } \ No newline at end of file diff --git a/library.properties b/library.properties index 592124e..c402cc0 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Eventually2 -version=2.0.0 +version=2.1.0 author=Jonathan Bartlett maintainer=Matt Turner sentence=Event-based programming library for Arduino diff --git a/src/EvtTimeListener.cpp b/src/EvtTimeListener.cpp index 8b0f546..11b6a99 100644 --- a/src/EvtTimeListener.cpp +++ b/src/EvtTimeListener.cpp @@ -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; } @@ -34,7 +34,7 @@ bool EvtTimeListener::isEventTriggered() if (curTime >= _startMillis) { /* Normal */ - if (curTime - _startMillis > _millis) + if (curTime - _startMillis > _interval) { shouldFire = true; } @@ -42,7 +42,7 @@ bool EvtTimeListener::isEventTriggered() else { /* Wrap-Around! */ - if (((ULONG_MAX - _startMillis) + curTime) > _millis) + if (((ULONG_MAX - _startMillis) + curTime) > _interval) { shouldFire = true; } @@ -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 @@ -64,6 +64,11 @@ bool EvtTimeListener::performTriggerAction(IEvtContext *c) else { _hasExecuted = true; - return returnval; + return returnVal; } } + +void EvtTimeListener::setInterval(unsigned long interval) +{ + _interval = interval; +} \ No newline at end of file diff --git a/src/EvtTimeListener.h b/src/EvtTimeListener.h index b0e5da1..3dbcefa 100644 --- a/src/EvtTimeListener.h +++ b/src/EvtTimeListener.h @@ -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; diff --git a/test/test_evt_time_listener/test_evt_time_listener.cpp b/test/test_evt_time_listener/test_evt_time_listener.cpp index 24e1f61..792b962 100644 --- a/test/test_evt_time_listener/test_evt_time_listener.cpp +++ b/test/test_evt_time_listener/test_evt_time_listener.cpp @@ -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); @@ -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);