-
-
Notifications
You must be signed in to change notification settings - Fork 345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make sample show more examples of std::function and std::bind #1378
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,125 @@ | ||
#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); | ||
}; | ||
|
||
class Task | ||
{ | ||
|
||
public: | ||
Task() {}; | ||
bool setTimer(int reqInterval) { | ||
if (reqInterval <= 0) return false; | ||
ledInterval = 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 blinkOldDelegate() { | ||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding-Style: Put the opening curly brace on the next line. |
||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding-Style: Put the opening curly brace on the next line. |
||
auto b = std::bind(functionWithMoreComlicatedSignature, 2, "parameters"); | ||
taskTimer.initializeMs(taskInterval, b).start(); | ||
} | ||
|
||
// Sming now allows the use of std::function | ||
// This example shows how to use a lamda expression as a callback | ||
void callLamda() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding-Style: Put the opening curly brace on the next line. |
||
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(); | ||
} | ||
|
||
#define LEDPIN_1 2 // GPIO2 | ||
#define LEDPIN_2 4 // GPIO4 | ||
|
||
LedBlinker myLed1 = LedBlinker(LEDPIN_1); | ||
LedBlinker myLed2 = LedBlinker(LEDPIN_2); | ||
|
||
// This example shows how to use a member function as a callback | ||
void callMemberFunction() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding-Style: Put the opening curly brace on the next line. |
||
|
||
// 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; | ||
|
||
}; | ||
|
||
Task task1; | ||
Task task2; | ||
Task task3; | ||
Task task4; | ||
Task task5; | ||
|
||
void init() | ||
{ | ||
myLed1.setTimer(1000); | ||
myLed1.blink(true); | ||
myLed2.setTimer(500); | ||
myLed2.blink(true); | ||
WifiStation.enable(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the Wifi* call on this line and the one below. They are not directly related to the example and to what you are trying to demonstrate. |
||
WifiAccessPoint.enable(false); | ||
Serial.begin(115200); | ||
|
||
task1.setTimer(1500); | ||
task1.blinkOldDelegate(); | ||
|
||
task2.setTimer(1600); | ||
task2.callPlainOldOrdinaryFunction(); | ||
|
||
task3.setTimer(1900); | ||
task3.showHowToUseBind(); | ||
|
||
task4.setTimer(1700); | ||
task4.callMemberFunction(); | ||
|
||
task5.setTimer(1800); | ||
task5.callLamda(); | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coding-Style: Put the opening curly brace on the next line.