This commit is contained in:
Dmitry Borisenko
2020-03-18 09:57:46 +01:00
parent 5fcb68ac65
commit 857177b5b7
21 changed files with 491 additions and 113 deletions

View File

@@ -0,0 +1,45 @@
/*
Basic Ticker usage
Ticker is an object that will call a given function with a certain period.
Each Ticker calls one function. You can have as many Tickers as you like,
memory being the only limitation.
A function may be attached to a ticker and detached from the ticker.
There are two variants of the attach function: attach and attach_ms.
The first one takes period in seconds, the second one in milliseconds.
The built-in LED will be blinking.
*/
#include <Ticker.h>
Ticker flipper;
int count = 0;
void flip() {
int state = digitalRead(LED_BUILTIN); // get the current state of GPIO1 pin
digitalWrite(LED_BUILTIN, !state); // set pin to the opposite state
++count;
// when the counter reaches a certain value, start blinking like crazy
if (count == 20) {
flipper.attach(0.1, flip);
}
// when the counter reaches yet another value, stop blinking
else if (count == 120) {
flipper.detach();
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
// flip the pin every 0.3s
flipper.attach(0.3, flip);
}
void loop() {
}

View File

@@ -0,0 +1,64 @@
#include "Arduino.h"
#include "Ticker.h"
#define LED1 2
#define LED2 4
#define LED3 12
#define LED4 14
#define LED5 15
class ExampleClass {
public:
ExampleClass(int pin, int duration) : _pin(pin), _duration(duration) {
pinMode(_pin, OUTPUT);
_myTicker.attach_ms(_duration, std::bind(&ExampleClass::classBlink, this));
}
~ExampleClass() {};
int _pin, _duration;
Ticker _myTicker;
void classBlink() {
digitalWrite(_pin, !digitalRead(_pin));
}
};
void staticBlink() {
digitalWrite(LED2, !digitalRead(LED2));
}
void scheduledBlink() {
digitalWrite(LED3, !digitalRead(LED2));
}
void parameterBlink(int p) {
digitalWrite(p, !digitalRead(p));
}
Ticker staticTicker;
Ticker scheduledTicker;
Ticker parameterTicker;
Ticker lambdaTicker;
ExampleClass example(LED1, 100);
void setup() {
pinMode(LED2, OUTPUT);
staticTicker.attach_ms(100, staticBlink);
pinMode(LED3, OUTPUT);
scheduledTicker.attach_ms_scheduled(100, scheduledBlink);
pinMode(LED4, OUTPUT);
parameterTicker.attach_ms(100, std::bind(parameterBlink, LED4));
pinMode(LED5, OUTPUT);
lambdaTicker.attach_ms(100, []() {
digitalWrite(LED5, !digitalRead(LED5));
});
}
void loop() {
}

View File

@@ -0,0 +1,35 @@
/*
Passing paramters to Ticker callbacks
Apart from void(void) functions, the Ticker library supports
functions taking one argument. This argument's size has to be less or
equal to 4 bytes (so char, short, int, float, void*, char* types will do).
This sample runs two tickers that both call one callback function,
but with different arguments.
The built-in LED will be pulsing.
*/
#include <Ticker.h>
Ticker tickerSetHigh;
Ticker tickerSetLow;
void setPin(int state) {
digitalWrite(LED_BUILTIN, state);
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(1, LOW);
// every 25 ms, call setPin(0)
tickerSetLow.attach_ms(25, setPin, 0);
// every 26 ms, call setPin(1)
tickerSetHigh.attach_ms(26, setPin, 1);
}
void loop() {
}