first commit
This commit is contained in:
79
libraries/Ticker/examples/FunctionalARM/FunctionalARM.ino
Normal file
79
libraries/Ticker/examples/FunctionalARM/FunctionalARM.ino
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <Ticker.h>
|
||||
|
||||
void func() {
|
||||
Serial.println("func.");
|
||||
}
|
||||
|
||||
Ticker ticker1(func, 1000, 0, MILLIS);
|
||||
|
||||
// Simple class with (non static) method
|
||||
class A {
|
||||
public:
|
||||
A(bool flag) : flag(flag) {}
|
||||
|
||||
void func() {
|
||||
Serial.printf("A::func: %s.\n", flag ? "true" : "false");
|
||||
}
|
||||
bool flag;
|
||||
};
|
||||
|
||||
A a1(true);
|
||||
|
||||
// use lambda to capture a1 and execute a1.func()
|
||||
|
||||
Ticker ticker2([](){a1.func();}, 1000, 0, MILLIS);
|
||||
|
||||
A a2(false);
|
||||
|
||||
Ticker ticker3([](){a2.func();}, 1000, 0, MILLIS);
|
||||
|
||||
// Class that registers its own method as a callback when it's instantiated.
|
||||
class B {
|
||||
public:
|
||||
B(bool flag) : flag(flag), ticker{[this](){this->func();}, 1000, 0, MILLIS} {
|
||||
ticker.start();
|
||||
}
|
||||
|
||||
void func() {
|
||||
Serial.printf("B::func: %s.\n", flag ? "true" : "false");
|
||||
}
|
||||
bool flag;
|
||||
Ticker ticker;
|
||||
};
|
||||
|
||||
B b(true);
|
||||
|
||||
// Class that acts like a function (functor)
|
||||
class C {
|
||||
public:
|
||||
C(int num) : num(num){}
|
||||
|
||||
// you can call an instance directly with parenthesis and this is executed
|
||||
void operator()() const {
|
||||
Serial.printf("C(): %d.\n", num);
|
||||
}
|
||||
int num;
|
||||
};
|
||||
|
||||
C c(4);
|
||||
|
||||
Ticker ticker4(c, 1000, 0, MILLIS);
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
Serial.println();
|
||||
|
||||
ticker1.start();
|
||||
ticker2.start();
|
||||
ticker3.start();
|
||||
ticker4.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
ticker1.update();
|
||||
ticker2.update();
|
||||
ticker3.update();
|
||||
b.ticker.update();
|
||||
ticker4.update();
|
||||
}
|
||||
65
libraries/Ticker/examples/Ticker/Ticker.ino
Executable file
65
libraries/Ticker/examples/Ticker/Ticker.ino
Executable file
@@ -0,0 +1,65 @@
|
||||
#include "Ticker.h"
|
||||
|
||||
void printMessage();
|
||||
void printCounter();
|
||||
void printCountdown();
|
||||
void blink();
|
||||
void printCountUS();
|
||||
|
||||
bool ledState;
|
||||
int counterUS;
|
||||
|
||||
Ticker timer1(printMessage, 0, 1);
|
||||
Ticker timer2(printCounter, 1000, 0, MILLIS);
|
||||
Ticker timer3(printCountdown, 1000, 5);
|
||||
Ticker timer4(blink, 500);
|
||||
Ticker timer5(printCountUS, 100, 0, MICROS_MICROS);
|
||||
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
Serial.begin(9600);
|
||||
delay(2000);
|
||||
timer1.start();
|
||||
timer2.start();
|
||||
timer3.start();
|
||||
timer4.start();
|
||||
timer5.start();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
timer1.update();
|
||||
timer2.update();
|
||||
timer3.update();
|
||||
timer4.update();
|
||||
timer5.update();
|
||||
if (timer4.counter() == 20) timer4.interval(200);
|
||||
if (timer4.counter() == 80) timer4.interval(1000);
|
||||
}
|
||||
|
||||
void printCounter() {
|
||||
Serial.print("Counter ");
|
||||
Serial.println(timer2.counter());
|
||||
}
|
||||
|
||||
void printCountdown() {
|
||||
Serial.print("Countdowm ");
|
||||
Serial.println(5 - timer3.counter());
|
||||
}
|
||||
|
||||
void printMessage() {
|
||||
Serial.println("Hello!");
|
||||
}
|
||||
|
||||
void blink() {
|
||||
digitalWrite(LED_BUILTIN, ledState);
|
||||
ledState = !ledState;
|
||||
}
|
||||
|
||||
void printCountUS() {
|
||||
counterUS++;
|
||||
if (counterUS == 10000) {
|
||||
Serial.println("10000 * 100us");
|
||||
counterUS = 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user