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; 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 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(); diff --git a/examples/DelayedExecution/DelayedExecution.ino b/examples/DelayedExecution/DelayedExecution.ino index f668aac..d47dafc 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 steps[] = {1000}; +int step_count = sizeof(steps)/sizeof(steps[0]); +Async_Operations delayed(steps, step_count, 1); void setup() { pinMode(LED_BUILTIN, OUTPUT); 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(); }