Skip to content

Commit

Permalink
Loop config change and more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
prampec committed Apr 24, 2018
1 parent c5551df commit 5f60347
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 5 deletions.
34 changes: 34 additions & 0 deletions examples/SoftTimer5DelayRun2/SoftTimer5DelayRun2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <PciManager.h>
#include <SoftTimer.h>
#include <Debouncer.h>
#include <DelayRun.h>

#define INPUT_PIN 3
#define OUTPUT_PIN LED_BUILTIN

// -- Define method signatures.
void onPinChanged();
// -- Define method signatures.
boolean turnOff(Task* task);

Debouncer debouncer(INPUT_PIN, MODE_CLOSE_ON_PUSH, onPinChanged, NULL);
// -- Runs after 2 seconds
DelayRun offTask(2000, turnOff);

void setup() {
Serial.begin(9800);
PciManager.registerListener(INPUT_PIN, &debouncer);
pinMode(OUTPUT_PIN, OUTPUT);
Serial.println("Ready.");
}

void onPinChanged() {
Serial.println("Event occurred");
digitalWrite(OUTPUT_PIN, HIGH);
offTask.startDelayed();
}

boolean turnOff(Task* task) {
Serial.println("Delay elapsed");
digitalWrite(OUTPUT_PIN, LOW);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <SoftTimer.h>
#include <DelayRun.h>

const char* ssid = "***";
const char* password = "***";

void myLoop(Task* me);
boolean turnOff(Task* me);

Task loopTask(0, myLoop);
DelayRun turnOffTask(1000, turnOff);

ESP8266WebServer server(80);

const int led = 2;

void handleRoot() {
server.send(200, "text/html",
"<html><body><button type=\"button\" onclick=\"var xhttp = new XMLHttpRequest();xhttp.open('GET', '/click', true);xhttp.send();\">Click Me!</button></body></html>"
);
}

void setup(void){
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}

server.on("/", handleRoot);

server.on("/click", [](){
digitalWrite(led, LOW);
Serial.println("Clicked");
turnOffTask.startDelayed();
server.send(200, "text/plain", "Okay");
});

server.begin();
Serial.println("HTTP server started");
SoftTimer.add(&loopTask);
digitalWrite(led, HIGH);
}

void myLoop(Task* me) {
// ESP.wdtFeed();
server.handleClient();
}

boolean turnOff(Task* me)
{
digitalWrite(led, HIGH);
}

4 changes: 2 additions & 2 deletions src/SoftTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void SoftTimerClass::remove(Task* task) {
* Walk through the chain looking for task to call.
*/
void SoftTimerClass::run() {
#ifdef PREVENT_LOOP_ITERATION
#ifndef ENABLE_LOOP_ITERATION
while(true) {
#endif
Task* task = this->_tasks;
Expand All @@ -102,7 +102,7 @@ void SoftTimerClass::run() {
this->testAndCall(task);
task = task->nextTask;
}
#ifdef PREVENT_LOOP_ITERATION
#ifndef ENABLE_LOOP_ITERATION
}
#endif
}
Expand Down
6 changes: 3 additions & 3 deletions src/SoftTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@

// -- With preventing loop() iteration you will benefit some milliseconds.
// -- On the other hand some platforms might depend on the loop(). If you are
// -- facing with unexpected problems, you might want to try disableing this macro.
// -- PREVENT_LOOP_ITERATION is enabled by default.
#define PREVENT_LOOP_ITERATION
// -- facing with unexpected problems, you might want to try enabling this macro.
// -- ENABLE_LOOP_ITERATION is disabled by default.
//#define ENABLE_LOOP_ITERATION

// -- By default the next start of a task scheduled from the begining of the previous
// -- execution. But executions might shift if an other task does not finish in time.
Expand Down

0 comments on commit 5f60347

Please sign in to comment.