mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
PolledTimeout Class for wrapping millis() loops (WIP) (#5198)
* PolledTimeout Class for wrapping millis() loops * Add yield policies, improve reset, add host tests * Fix copyright, comments * adjust host tests for better time precision * add fuzzyness to timing tests for CI jitter * add blink example with polledTimeout * improve namespace and type naming, add copyright, comments * fix astyle
This commit is contained in:
@ -0,0 +1,78 @@
|
||||
/*
|
||||
ESP8266 Blink with polledTimeout by Daniel Salazar
|
||||
|
||||
Copyright (c) 2018 Daniel Salazar. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
|
||||
Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
|
||||
*/
|
||||
|
||||
|
||||
#include <PolledTimeout.h>
|
||||
|
||||
void ledOn() {
|
||||
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on (Note that LOW is the voltage level
|
||||
}
|
||||
|
||||
void ledOff() {
|
||||
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off by making the voltage HIGH
|
||||
}
|
||||
|
||||
void ledToggle() {
|
||||
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // Change the state of the LED
|
||||
}
|
||||
|
||||
|
||||
|
||||
esp8266::polledTimeout::periodic halfPeriod(500); //use fully qualified type and avoid importing all ::esp8266 namespace to the global namespace
|
||||
|
||||
// the setup function runs only once at start
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
|
||||
|
||||
using esp8266::polledTimeout::oneShot; //import the type to the local namespace
|
||||
|
||||
//STEP1; turn the led ON
|
||||
ledOn();
|
||||
|
||||
//STEP2: wait for ON timeout
|
||||
oneShot timeoutOn(2000);
|
||||
while (!timeoutOn) {
|
||||
yield();
|
||||
}
|
||||
|
||||
//STEP3: turn the led OFF
|
||||
ledOff();
|
||||
|
||||
//STEP4: wait for OFF timeout to assure the led is kept off for this time before exiting setup
|
||||
oneShot timeoutOff(2000);
|
||||
while (!timeoutOff) {
|
||||
yield();
|
||||
}
|
||||
|
||||
//Done with STEPs, do other stuff
|
||||
halfPeriod.reset(); //halfPeriod is global, so it gets inited on sketch start. Clear it here to make it ready for loop, where it's actually used.
|
||||
}
|
||||
|
||||
|
||||
// the loop function runs over and over again forever
|
||||
void loop() {
|
||||
if (halfPeriod) {
|
||||
ledToggle();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user