-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for lambdas in the Timer class (#1378)
Using C++11 std::function and std::bind
- Loading branch information
1 parent
2946146
commit e71d02c
Showing
3 changed files
with
164 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,119 @@ | ||
#include <user_config.h> | ||
#include <SmingCore.h> | ||
|
||
class LedBlinker | ||
void plainOldOrdinaryFunction() | ||
{ | ||
debugf("plainOldOrdinaryFunction"); | ||
} | ||
|
||
void functionWithMoreComlicatedSignature(int a, String b) | ||
{ | ||
debugf("functionWithMoreComlicatedSignature %d %s", a, b.c_str()); | ||
} | ||
|
||
public : | ||
LedBlinker(int reqPin) : ledPin(reqPin) { | ||
pinMode(ledPin, OUTPUT); | ||
}; | ||
bool setTimer(int reqInterval) { | ||
if (reqInterval <= 0) return false; | ||
ledInterval = reqInterval; | ||
class Task | ||
{ | ||
public: | ||
Task() {}; | ||
bool setTimer(int reqInterval) | ||
{ | ||
if (reqInterval <= 0) { | ||
return false; | ||
} | ||
taskInterval = reqInterval; | ||
return true; | ||
} | ||
void blink(bool reqRun) { | ||
if (reqRun) { | ||
ledTimer.initializeMs(ledInterval, TimerDelegate(&LedBlinker::ledBlink,this)).start(); | ||
} | ||
else { | ||
ledTimer.stop(); | ||
} | ||
|
||
// This example show the way delegates have been used in Sming in the past. | ||
void callOldDelegate() | ||
{ | ||
taskTimer.initializeMs(taskInterval, TimerDelegate(&Task::doOldDelegate, this)).start(); | ||
} | ||
void ledBlink () { ledState = !ledState ; digitalWrite(ledPin, ledState);} | ||
|
||
private : | ||
int ledPin = 2; | ||
Timer ledTimer; | ||
int ledInterval = 1000; | ||
bool ledState = true; | ||
}; | ||
// This example shows how to use a plain old ordinary function as a callback | ||
void callPlainOldOrdinaryFunction() | ||
{ | ||
taskTimer.initializeMs(taskInterval, TimerDelegateStdFunction(plainOldOrdinaryFunction)).start(); | ||
// or just | ||
// taskTimer.initializeMs(taskInterval, plainOldOrdinaryFunction).start(); | ||
} | ||
|
||
// This example shows how to use std::bind to make us of a function that has more parameters than our signature has | ||
void showHowToUseBind() | ||
{ | ||
auto b = std::bind(functionWithMoreComlicatedSignature, 2, "parameters"); | ||
taskTimer.initializeMs(taskInterval, b).start(); | ||
} | ||
|
||
#define LEDPIN_1 2 // GPIO2 | ||
#define LEDPIN_2 4 // GPIO4 | ||
// Sming now allows the use of std::function | ||
// This example shows how to use a lamda expression as a callback | ||
void callLamda() | ||
{ | ||
int foo = 123; | ||
taskTimer.initializeMs(taskInterval, | ||
[foo] // capture just foo by value (Note it would be bad to pass by reference as foo would be out of scope when the lamda function runs later) | ||
() // No parameters to the callback | ||
-> void // Returns nothing | ||
{ | ||
if (foo == 123) { | ||
debugf("lamda Callback foo is 123"); | ||
} | ||
else | ||
{ | ||
debugf("lamda Callback foo is not 123, crikey!"); | ||
} | ||
} | ||
).start(); | ||
} | ||
|
||
// This example shows how to use a member function as a callback | ||
void callMemberFunction() | ||
{ | ||
// A non-static member function must be called with an object. | ||
// That is, it always implicitly passes "this" pointer as its argument. | ||
// But because our callback specifies that we don't take any arguments (<void(void)>), | ||
// you must use std::bind to bind the first (and the only) argument. | ||
|
||
TimerDelegateStdFunction b = std::bind(&Task::callbackMemberFunction, this); | ||
taskTimer.initializeMs(taskInterval, b).start(); | ||
} | ||
|
||
void doOldDelegate() | ||
{ | ||
debugf("doOldDelegate"); | ||
} | ||
void callbackMemberFunction() | ||
{ | ||
debugf("callMemberFunction"); | ||
} | ||
|
||
private: | ||
Timer taskTimer; | ||
int taskInterval = 1000; | ||
}; | ||
|
||
LedBlinker myLed1 = LedBlinker(LEDPIN_1); | ||
LedBlinker myLed2 = LedBlinker(LEDPIN_2); | ||
Task task1; | ||
Task task2; | ||
Task task3; | ||
Task task4; | ||
Task task5; | ||
|
||
void init() | ||
{ | ||
myLed1.setTimer(1000); | ||
myLed1.blink(true); | ||
myLed2.setTimer(500); | ||
myLed2.blink(true); | ||
Serial.begin(115200); | ||
|
||
task1.setTimer(1500); | ||
task1.callOldDelegate(); | ||
|
||
task2.setTimer(1600); | ||
task2.callPlainOldOrdinaryFunction(); | ||
|
||
task3.setTimer(1900); | ||
task3.showHowToUseBind(); | ||
|
||
task4.setTimer(1700); | ||
task4.callMemberFunction(); | ||
|
||
task5.setTimer(1800); | ||
task5.callLamda(); | ||
} |