From 9d533f124d9bd37c0590b9cf58c3f98f013a5bf0 Mon Sep 17 00:00:00 2001 From: DarioGHub <49622669+DarioGHub@users.noreply.github.com> Date: Sun, 13 Nov 2022 16:32:15 -0700 Subject: [PATCH 1/6] Update DelayedExecution.ino import -> include, add step_count calc --- examples/DelayedExecution/DelayedExecution.ino | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/DelayedExecution/DelayedExecution.ino b/examples/DelayedExecution/DelayedExecution.ino index f668aac..2f1cbd6 100644 --- a/examples/DelayedExecution/DelayedExecution.ino +++ b/examples/DelayedExecution/DelayedExecution.ino @@ -1,7 +1,8 @@ -#import +#include -long long dt = {1000}; -Async_Operations delayed(&dt, 1, 1); +long long dt[] = {1000}; +int step_count = sizeof(dt)/sizeof(dt[0]); +Async_Operations delayed(dt, step_count, 1); void setup() { pinMode(LED_BUILTIN, OUTPUT); From ab96da6b22e83e5e85fe68323836583d775f0456 Mon Sep 17 00:00:00 2001 From: DarioGHub <49622669+DarioGHub@users.noreply.github.com> Date: Mon, 14 Nov 2022 19:15:48 -0700 Subject: [PATCH 2/6] Update TimeoutHandler.ino Added call to update, renamed dt to steps, step_count now calculated, a few wiring notes, and some serial output. --- examples/TimeoutHandler/TimeoutHandler.ino | 35 +++++++++++++++++----- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/examples/TimeoutHandler/TimeoutHandler.ino b/examples/TimeoutHandler/TimeoutHandler.ino index 62093cf..0ab58bf 100644 --- a/examples/TimeoutHandler/TimeoutHandler.ino +++ b/examples/TimeoutHandler/TimeoutHandler.ino @@ -1,23 +1,44 @@ -#include +/* + External Restart of Async_Operations + Button press used to start an asynchronous timeout (a non-blocking delay) + Wire a momentary button switch, between a digital/GPIO pin and ground. + Alternatives to an external button (these all used pin 0 (zero): + Many ESP boards came with a button labelled as "flash|boot|load." + Uno/Nano could have a lead attached to RX0, and another to GND, + then when shorted briefly, would act as an emergency button. + Once you have a button wired, set the variable 'buttonPin' + esp32, esp8266, could use 0 (labelled GPIO0) + uno, nano, could also use 0 (labelled RX0) +*/ -long long dt = {10000}; -Async_Operations handler(&dt, 1, 1); +#include -bool waiting = false; -int buttonPin = 10; +int buttonPin = 0; +long long steps[] = {5000}; +int step_count = sizeof(steps)/sizeof(steps[0]); +Async_Operations handler(steps, step_count, 1); void setup() { + Serial.begin(115200); Serial.println(); handler.setLoopCallback(&resetWaiting); pinMode(buttonPin, INPUT_PULLUP); + // handler.start(); // not here; started by button press + Serial.print(millis()); Serial.print(F(" Press button connected to pin ")); + Serial.println(buttonPin); Serial.print(F("\tto start ")); Serial.print(steps[0]); + Serial.println(" msec asynchronous (non-blocking) timeout."); Serial.println(); } void resetWaiting() { - waiting = false; + Serial.print(millis()); + Serial.println(F(" Async timed out; press button again to restart the timeout.")); Serial.println(); } void loop() { if (digitalRead(buttonPin) == LOW) { - waiting = true; + Serial.print(millis()); + Serial.println(F(" Button pressed; starting async timeout.")); handler.restart(); + delay(300); // economical debounce } + handler.update(); } From bcc1ca59376cabd0b33572ccc10456a7fb5bf649 Mon Sep 17 00:00:00 2001 From: DarioGHub <49622669+DarioGHub@users.noreply.github.com> Date: Mon, 14 Nov 2022 19:26:52 -0700 Subject: [PATCH 3/6] Update BlinkingLed.ino Rename dt -> steps, step_count now calculated --- examples/BlinkingLed/BlinkingLed.ino | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/BlinkingLed/BlinkingLed.ino b/examples/BlinkingLed/BlinkingLed.ino index 79ba14a..cd74a6f 100644 --- a/examples/BlinkingLed/BlinkingLed.ino +++ b/examples/BlinkingLed/BlinkingLed.ino @@ -1,7 +1,8 @@ #include -long long dt[] = {900, 100}; -Async_Operations blinker(dt, 2, -1, LED_BUILTIN); +long long steps[] = {900, 100}; +int step_count = sizeof(steps)/sizeof(steps[0]); +Async_Operations blinker(steps, step_count, -1, LED_BUILTIN); void setup() { blinker.start(); From d73ab3a44d62eff2f8896ab1b6fd63bd76b4f931 Mon Sep 17 00:00:00 2001 From: DarioGHub <49622669+DarioGHub@users.noreply.github.com> Date: Mon, 14 Nov 2022 19:36:53 -0700 Subject: [PATCH 4/6] Update DelayedExecution.ino dt renamed steps --- examples/DelayedExecution/DelayedExecution.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/DelayedExecution/DelayedExecution.ino b/examples/DelayedExecution/DelayedExecution.ino index 2f1cbd6..d47dafc 100644 --- a/examples/DelayedExecution/DelayedExecution.ino +++ b/examples/DelayedExecution/DelayedExecution.ino @@ -1,8 +1,8 @@ #include -long long dt[] = {1000}; -int step_count = sizeof(dt)/sizeof(dt[0]); -Async_Operations delayed(dt, step_count, 1); +long long steps[] = {1000}; +int step_count = sizeof(steps)/sizeof(steps[0]); +Async_Operations delayed(steps, step_count, 1); void setup() { pinMode(LED_BUILTIN, OUTPUT); From 90bc770cf46342eea695a50d3b3c54d366d25cfe Mon Sep 17 00:00:00 2001 From: DarioGHub <49622669+DarioGHub@users.noreply.github.com> Date: Mon, 14 Nov 2022 20:11:24 -0700 Subject: [PATCH 5/6] loopCallback() earlier, def prop values moved to h loopCallback() was not reached if entered update() with repeat=1; repeat-- resulted in update returning. Constructors contained repeated assignments; moved them to the header file. Constructors with >2 params update the property values. --- Async_Operations.cpp | 59 +++++--------------------------------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/Async_Operations.cpp b/Async_Operations.cpp index 9a8627d..f404ed9 100644 --- a/Async_Operations.cpp +++ b/Async_Operations.cpp @@ -5,38 +5,16 @@ #include "Async_Operations.h" + Async_Operations::Async_Operations(long long *steps, int stepCount) { this->steps = steps; this->stepCount = stepCount; - this->initialRepeat = 0; - this->pin = -1; - if (this->pin >= 0) { - pinMode(this->pin, OUTPUT); - } - this->initialState = true; - this->endState = false; - this->running = false; - this->startTime = 0; - this->lastUpdate = 0; - this->stateChangeCallback = 0; - this->loopCallback = 0; } Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat) { this->steps = steps; this->stepCount = stepCount; this->initialRepeat = repeat; - this->pin = -1; - if (this->pin >= 0) { - pinMode(this->pin, OUTPUT); - } - this->initialState = true; - this->endState = false; - this->running = false; - this->startTime = 0; - this->lastUpdate = 0; - this->stateChangeCallback = 0; - this->loopCallback = 0; } Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, int pin) { @@ -44,16 +22,6 @@ Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, this->stepCount = stepCount; this->initialRepeat = repeat; this->pin = pin; - if (this->pin >= 0) { - pinMode(this->pin, OUTPUT); - } - this->initialState = true; - this->endState = false; - this->running = false; - this->startTime = 0; - this->lastUpdate = 0; - this->stateChangeCallback = 0; - this->loopCallback = 0; } Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, int pin, bool initialState) { @@ -61,16 +29,7 @@ Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, this->stepCount = stepCount; this->initialRepeat = repeat; this->pin = pin; - if (this->pin >= 0) { - pinMode(this->pin, OUTPUT); - } this->initialState = initialState; - this->endState = false; - this->running = false; - this->startTime = 0; - this->lastUpdate = 0; - this->stateChangeCallback = 0; - this->loopCallback = 0; } Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, int pin, bool initialState, bool endState) { @@ -78,21 +37,14 @@ Async_Operations::Async_Operations(long long *steps, int stepCount, int repeat, this->stepCount = stepCount; this->initialRepeat = repeat; this->pin = pin; - if (this->pin >= 0) { - pinMode(this->pin, OUTPUT); - } this->initialState = initialState; this->endState = endState; - this->running = false; - this->startTime = 0; - this->lastUpdate = 0; - this->stateChangeCallback = 0; - this->loopCallback = 0; } void Async_Operations::start() { this->startTime = millis(); if (this->pin >= 0) { + pinMode(this->pin, OUTPUT); digitalWrite(this->pin, this->state); } this->lastUpdate = this->startTime; @@ -138,6 +90,9 @@ void Async_Operations::update() { } long long currentTime = millis(); while (this->lastUpdate + this->remaining < currentTime) { + if (this->loopCallback) { + this->loopCallback(); + } this->step++; if (this->step >= this->stepCount) { this->step = 0; @@ -152,10 +107,8 @@ void Async_Operations::update() { } return; } - if (this->loopCallback) { - this->loopCallback(); - } } + this->lastUpdate += this->remaining; this->remaining = this->steps[this->step]; this->state = !this->state; From 3b069f05fd48a72784d3eafcc35f61e6939b71c3 Mon Sep 17 00:00:00 2001 From: DarioGHub <49622669+DarioGHub@users.noreply.github.com> Date: Mon, 14 Nov 2022 21:20:18 -0700 Subject: [PATCH 6/6] Moved def prop vals from cpp, callbacks updated Moved majority of default property value assignments here from constructors in cpp. Readability: function pointers assigned nullptr in place of 0. --- Async_Operations.h | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Async_Operations.h b/Async_Operations.h index 2e902d4..0294714 100644 --- a/Async_Operations.h +++ b/Async_Operations.h @@ -29,22 +29,25 @@ class Async_Operations { void deleteStateChangeCallback(); void setLoopCallback(void (*loopCallback)(void)); void deleteLoopCallback(); - private: + private: // order as in constructor long long *steps; int stepCount; int step; - int pin; - bool running; - long long startTime; - long long lastUpdate; - long long remaining; + int pin = -1; + bool running = false; + long long startTime = 0; + long long lastUpdate = 0; + int initialRepeat = 0; + bool initialState = true; + bool endState = false; bool state; - bool initialState; - bool endState; int repeat; - int initialRepeat; - void (*stateChangeCallback)(void); - void (*loopCallback)(void); + long long remaining; + void (*stateChangeCallback)(void) = nullptr; + void (*loopCallback)(void) = nullptr; + + + }; #endif