8000 Minimize header use, move Ticker function definitions into cpp file by dok-net · Pull Request #6496 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Minimize header use, move Ticker function definitions into cpp file #6496

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

Merged
merged 10 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Inlining via header has better baseline ROM footprint.
  • Loading branch information
dok-net committed Nov 14, 2019
commit cab7657ee61a187c76c22de638c26501d9b55df1
49 changes: 0 additions & 49 deletions libraries/Ticker/src/Ticker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,6 @@ Ticker::~Ticker()
detach();
}

void Ticker::attach(float seconds, callback_function_t callback)
{
_callback_function = std::move(callback);
_attach_ms(1000UL * seconds, true, _static_callback, this);
}

void Ticker::attach_ms(uint32_t milliseconds, callback_function_t callback)
{
_callback_function = std::move(callback);
_attach_ms(milliseconds, true, _static_callback, this);
}

void Ticker::attach_scheduled(float seconds, callback_function_t callback)
{
attach(seconds, [callback]() { schedule_function(callback); });
}

void Ticker::attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
{
attach_ms(milliseconds, [callback]() { schedule_function(callback); });
}

void Ticker::once(float seconds, callback_function_t callback)
{
_callback_function = std::move(callback);
_attach_ms(1000UL * seconds, false, _static_callback, this);
}

void Ticker::once_ms(uint32_t milliseconds, callback_function_t callback)
{
_callback_function = std::move(callback);
_attach_ms(milliseconds, false, _static_callback, this);
}

void Ticker::once_scheduled(float seconds, callback_function_t callback)
{
once(seconds, [callback]() { schedule_function(callback); });
}

void Ticker::once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
{
once_ms(milliseconds, [callback]() { schedule_function(callback); });
}

void Ticker::_attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg)
{
_attach_ms(1000UL * seconds, repeat, callback, arg);
}

void Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg)
{
if (_timer)
Expand Down
10000
66 changes: 47 additions & 19 deletions libraries/Ticker/src/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,25 @@ class Ticker
typedef void (*callback_with_arg_t)(void*);
typedef std::function<void(void)> callback_function_t;

void attach(float seconds, callback_function_t callback);
void attach_ms(uint32_t milliseconds, callback_function_t callback);
void attach_scheduled(float seconds, callback_function_t callback);
void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback);
void attach_ms(uint32_t milliseconds, callback_function_t callback)
{
_callback_function = std::move(callback);
_attach_ms(milliseconds, true, _static_callback, this);
}

template<typename TArg>
void attach(float seconds, void (*callback)(TArg), TArg arg)
void attach(float seconds, callback_function_t callback)
{
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
// C-cast serves two purposes:
// static_cast for smaller integer types,
// reinterpret_cast + const_cast for pointer types
_attach_s(seconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
attach_ms(1000UL * seconds, callback);
}

void attach_scheduled(float seconds, callback_function_t callback)
{
attach(seconds, [callback]() { schedule_function(callback); });
}

void attach_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
{
attach_ms(milliseconds, [callback]() { schedule_function(callback); });
}

template<typename TArg>
Expand All @@ -57,16 +63,32 @@ class Ticker
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
}

void once(float seconds, callback_function_t callback);
void once_ms(uint32_t milliseconds, callback_function_t callback);
void once_scheduled(float seconds, callback_function_t callback);
void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback);

template<typename TArg>
void once(float seconds, void (*callback)(TArg), TArg arg)
void attach(float seconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
_attach_s(seconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
attach_ms(1000UL * seconds, callback, arg);
}

void once_ms(uint32_t milliseconds, callback_function_t callback)
{
_callback_function = std::move(callback);
_attach_ms(milliseconds, false, _static_callback, this);
}

void once(float seconds, callback_function_t callback)
{
once_ms(1000UL * seconds, callback);
}

void once_scheduled(float seconds, callback_function_t callback)
{
once(seconds, [callback]() { schedule_function(callback); });
}

void once_ms_scheduled(uint32_t milliseconds, callback_function_t callback)
{
once_ms(milliseconds, [callback]() { schedule_function(callback); });
}

template<typename TArg>
Expand All @@ -76,12 +98,18 @@ class Ticker
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), reinterpret_cast<void*>(arg));
}

template<typename TArg>
void once(float seconds, void (*callback)(TArg), TArg arg)
{
static_assert(sizeof(TArg) <= sizeof(void*), "attach() callback argument size must be <= sizeof(void*)");
once_ms(1000UL * seconds, callback, arg);
}

void detach();
bool active() const;

protected:
static void _static_callback(void* arg);
void _attach_s(float seconds, bool repeat, callback_with_arg_t callback, void* arg);
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, void* arg);

ETSTimer* _timer;
Expand Down
0