Skip to content

Longer delays for Ticker and some internal updates #8625

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 14 commits into from
Nov 1, 2022
Prev Previous commit
Next Next commit
restore static_assert
  • Loading branch information
mcspr committed Jun 30, 2022
commit 89fc3e84ffe31d8be70240fb5ba07c23e1ed6af3
50 changes: 25 additions & 25 deletions libraries/Ticker/src/Ticker.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,28 +97,26 @@ class Ticker
_attach(Milliseconds(milliseconds), true);
}

// use function pointer directly. SDK used for argument storage, so we don't support anything larger than 4 bytes
template <typename T>
using callback_with_typed_arg_t = void(*)(T);

// instead of copying the callback function, store function pointer and it's argument
// callback will still be called in SYS ctx when ticker fires in N seconds
template <typename TArg, typename Callback = void(*)(TArg)>
void attach(float seconds, Callback callback, TArg arg)
template <typename T>
void attach(float seconds, callback_with_typed_arg_t<T> callback, T arg)
{
_callback = callback_ptr_t{
.func = reinterpret_cast<callback_with_arg_t>(callback),
.arg = reinterpret_cast<void*>(arg),
};
_callback = callback_ptr_t(callback, arg);
_attach(Seconds(seconds), true);
}

// instead of copying the callback function, store function pointer and it's argument
// (that may not be larger than the size of the pointer aka 4bytes)
// (that **cannot** be larger than the size of the pointer - 4 bytes)
// callback will still be called in SYS ctx when ticker fires in N milliseconds
template <typename TArg, typename Callback = void(*)(TArg)>
void attach_ms(uint32_t milliseconds, Callback callback, TArg arg)
template <typename T>
void attach_ms(uint32_t milliseconds, callback_with_typed_arg_t<T> callback, T arg)
{
_callback = callback_ptr_t{
.func = reinterpret_cast<callback_with_arg_t>(callback),
.arg = reinterpret_cast<void*>(arg),
};
_callback = callback_ptr_t(callback, arg);
_attach(Milliseconds(milliseconds), true);
}

Expand Down Expand Up @@ -151,24 +149,18 @@ class Ticker
}

// callback will be called in SYS ctx when ticker fires
template <typename TArg, typename Callback = void(*)(TArg)>
void once(float seconds, Callback callback, TArg arg)
template <typename T>
void once(float seconds, callback_with_typed_arg_t<T> callback, T arg)
{
_callback = callback_ptr_t{
.func = reinterpret_cast<callback_with_arg_t>(callback),
.arg = reinterpret_cast<void*>(arg),
};
_callback = callback_ptr_t(callback, arg);
_attach(Seconds(seconds), false);
}

// callback will be called in SYS ctx when ticker fires
template <typename TArg, typename Callback = void(*)(TArg)>
void once_ms(uint32_t milliseconds, Callback callback, TArg arg)
template <typename T>
void once_ms(uint32_t milliseconds, callback_with_typed_arg_t<T> callback, T arg)
{
_callback = callback_ptr_t{
.func = reinterpret_cast<callback_with_arg_t>(callback),
.arg = reinterpret_cast<void*>(arg),
};
_callback = callback_ptr_t(callback, arg);
_attach(Milliseconds(milliseconds), false);
}

Expand Down Expand Up @@ -212,6 +204,14 @@ class Ticker
private:
struct callback_ptr_t
{
template <typename T>
callback_ptr_t(callback_with_typed_arg_t<T> func, T arg) :
func(reinterpret_cast<callback_with_arg_t>(func)),
arg(reinterpret_cast<void*>(arg))
{
static_assert(sizeof(T) <= sizeof(void*), "");
}

callback_with_arg_t func = nullptr;
void* arg = nullptr;
};
Expand Down