add microsecond into Ticker lib #23

This commit is contained in:
Bigbits
2019-07-11 15:47:50 +08:00
parent b6da4a0466
commit 363b2bade7
3 changed files with 40 additions and 0 deletions

View File

@@ -10,8 +10,10 @@ Ticker KEYWORD1
attach KEYWORD2
attach_ms KEYWORD2
attach_us KEYWORD2
once KEYWORD2
once_ms KEYWORD2
once_us KEYWORD2
detach KEYWORD2
#######################################

View File

@@ -25,6 +25,17 @@ Ticker::_attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callb
sysctl_enable_irq();
}
void
Ticker::_attach_us(uint32_t microseconds, bool repeat, callback_with_arg_t callback, size_t arg)
{
user_callback = callback;
_arg = arg;
timer_set_interval(timer_device_number_t(timer_id), TIMER_CHANNEL_0, microseconds * 1000);
timer_irq_register(timer_device_number_t(timer_id), TIMER_CHANNEL_0, !repeat, 4, timer_callback, this);
timer_set_enable(timer_device_number_t(timer_id), TIMER_CHANNEL_0, 1);
sysctl_enable_irq();
}
void
Ticker::detach()
{

View File

@@ -32,6 +32,11 @@ public:
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
void attach_us(uint32_t microseconds, callback_t callback)
{
_attach_us(microseconds, true, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
template<typename TArg>
void attach(float seconds, void (*callback)(TArg), TArg arg)
{
@@ -48,6 +53,14 @@ public:
_attach_ms(milliseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
template<typename TArg>
void attach_us(uint32_t microseconds, void (*callback)(TArg), TArg arg)
{
configASSERT(sizeof(TArg) <= sizeof(size_t));
size_t arg32 = (size_t)arg;
_attach_us(microseconds, true, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
void once(float seconds, callback_t callback)
{
_attach_ms(seconds * 1000, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
@@ -58,6 +71,11 @@ public:
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
void once_us(uint32_t microseconds, callback_t callback)
{
_attach_us(microseconds, false, reinterpret_cast<callback_with_arg_t>(callback), 0);
}
template<typename TArg>
void once(float seconds, void (*callback)(TArg), TArg arg)
{
@@ -74,6 +92,14 @@ public:
_attach_ms(milliseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
template<typename TArg>
void once_us(uint32_t microseconds, void (*callback)(TArg), TArg arg)
{
configASSERT(sizeof(TArg) <= sizeof(size_t));
size_t arg32 = (size_t)(arg);
_attach_us(microseconds, false, reinterpret_cast<callback_with_arg_t>(callback), arg32);
}
void detach();
bool active();
@@ -82,6 +108,7 @@ public:
protected:
void _attach_ms(uint32_t milliseconds, bool repeat, callback_with_arg_t callback, size_t arg);
void _attach_us(uint32_t microseconds, bool repeat, callback_with_arg_t callback, size_t arg);
private:
timer_id_t timer_id;