-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Add Functional and Scheduled callback option to Ticker #4062
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
|
||
#include <stdint.h> | ||
#include <stddef.h> | ||
#include <functional> | ||
#include <Schedule.h> | ||
|
||
extern "C" { | ||
typedef struct _ETSTIMER_ ETSTimer; | ||
|
@@ -34,17 +36,31 @@ class Ticker | |
public: | ||
Ticker(); | ||
~Ticker(); | ||
|
||
typedef void (*callback_t)(void); | ||
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. It might be better to keep this definition for compatibility. I ran grep on my sketchbook and found a few uses of |
||
typedef void (*callback_with_arg_t)(void*); | ||
typedef std::function<void(void)> callback_function_t; | ||
|
||
void attach_scheduled(float seconds, callback_function_t callback) | ||
{ | ||
attach(seconds,std::bind(schedule_function, callback)); | ||
} | ||
|
||
void attach(float seconds, callback_t callback) | ||
void attach(float seconds, callback_function_t callback) | ||
{ | ||
_attach_ms(seconds * 1000, true, reinterpret_cast<callback_with_arg_t>(callback), 0); | ||
_callback_function = callback; | ||
attach(seconds, _static_callback, (void*)this); | ||
} | ||
|
||
void attach_ms(uint32_t milliseconds, callback_t callback) | ||
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback) | ||
{ | ||
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0); | ||
attach_ms(milliseconds, std::bind(schedule_function, callback)); | ||
} | ||
|
||
void attach_ms(uint32_t milliseconds, callback_function_t callback) | ||
{ | ||
_callback_function = callback; | ||
attach_ms(milliseconds, _static_callback, (void*)this); | ||
} | ||
|
||
template<typename TArg> | ||
|
@@ -66,14 +82,26 @@ class Ticker | |
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32); | ||
} | ||
|
||
void once(float seconds, callback_t callback) | ||
void once_scheduled(float seconds, callback_function_t callback) | ||
{ | ||
once(seconds, std::bind(schedule_function, callback)); | ||
} | ||
|
||
void once(float seconds, callback_function_t callback) | ||
{ | ||
_callback_function = callback; | ||
once(seconds, _static_callback, (void*)this); | ||
} | ||
|
||
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback) | ||
{ | ||
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0); | ||
once_ms(milliseconds, std::bind(schedule_function, callback)); | ||
} | ||
|
||
void once_ms(uint32_t milliseconds, callback_t callback) | ||
void once_ms(uint32_t milliseconds, callback_function_t callback) | ||
{ | ||
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0); | ||
_callback_function = callback; | ||
once_ms(milliseconds, _static_callback, (void*)this); | ||
} | ||
|
||
template<typename TArg> | ||
|
@@ -96,11 +124,12 @@ class Ticker | |
|
||
protected: | ||
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, uint32_t arg); | ||
|
||
static void _static_callback (void* arg); | ||
|
||
protected: | ||
ETSTimer* _timer; | ||
}; | ||
callback_function_t _callback_function = nullptr; | ||
|
||
}; | ||
|
||
#endif//TICKER_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This header file is probably only needed in Ticker.cpp