-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef __CPP_TIMER_CALLBACK | ||
#define __CPP_TIMER_CALLBACK | ||
#include <stdio.h> | ||
#include "CppTimer.h" | ||
#include <unistd.h> | ||
#include <functional> | ||
|
||
class CppTimerCallback : public CppTimer { | ||
|
||
public: | ||
|
||
using CallbackFunction = std::function<void(void)>; | ||
|
||
void registerEventCallback(CallbackFunction cf) { | ||
callbackFunction = cf; | ||
} | ||
|
||
void timerEvent() { | ||
if (!(callbackFunction == nullptr)) { | ||
callbackFunction(); | ||
} | ||
} | ||
|
||
private: | ||
CallbackFunction callbackFunction; | ||
}; | ||
|
||
|
||
#endif |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <stdio.h> | ||
#include "CppTimerStdFuncCallback.h" | ||
#include <unistd.h> | ||
#include <thread> | ||
|
||
class ReceiverClass1 { | ||
public: | ||
void run() { | ||
fprintf(stdout,"Bah! "); | ||
fflush(stdout); | ||
} | ||
}; | ||
|
||
|
||
class ReceiverClass2 { | ||
public: | ||
void run() { | ||
fprintf(stdout,"Buh! \n"); | ||
fflush(stdout); | ||
} | ||
}; | ||
|
||
|
||
int main( int, const char**) { | ||
CppTimerCallback demoTimer1; | ||
ReceiverClass1 receiverClass1; | ||
demoTimer1.registerEventCallback([&receiverClass1](){receiverClass1.run();}); | ||
demoTimer1.startns(77000000); | ||
|
||
CppTimerCallback demoTimer2; | ||
ReceiverClass2 receiverClass2; | ||
demoTimer2.registerEventCallback([&receiverClass2](){receiverClass2.run();}); | ||
demoTimer2.startns(250000000); | ||
|
||
// do nothing and keep sleeping for 2 secs | ||
std::this_thread::sleep_for(std::chrono::seconds(2)); | ||
|
||
demoTimer1.stop(); | ||
demoTimer2.stop(); | ||
|
||
printf("\n"); | ||
|
||
} |