Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move many default property values to header file #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 6 additions & 53 deletions Async_Operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,94 +5,46 @@

#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) {
this->steps = steps;
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) {
this->steps = steps;
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) {
this->steps = steps;
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions Async_Operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions examples/BlinkingLed/BlinkingLed.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <Async_Operations.h>

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();
Expand Down
7 changes: 4 additions & 3 deletions examples/DelayedExecution/DelayedExecution.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#import <Async_Operations.h>
#include <Async_Operations.h>

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);
Expand Down
35 changes: 28 additions & 7 deletions examples/TimeoutHandler/TimeoutHandler.ino
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
#include <Async_Operations.h>
/*
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 <Async_Operations.h>

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();
}