Skip to content
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

Fix for detaching a Ticker on AVR #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 28 additions & 4 deletions TickerScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,42 @@ void TickerScheduler::handleTicker(tscallback_t f, void * arg, volatile bool * f
}
}

bool TickerScheduler::add(uint8_t i, uint32_t period, tscallback_t f, void* arg, boolean shouldFireNow)
void TickerScheduler::handleTicker(TickerSchedulerItem * item)
{
if(item->flag)
{
yield();
item->flag = false;
yield();
if(!item->repeat) item->is_used = false;
yield();
item->cb(item->cb_arg);
yield();
}
}

bool TickerScheduler::add(uint8_t i, uint32_t period, tscallback_t f, void* arg, boolean repeat)
{
if (i >= this->size || this->items[i].is_used)
return false;

this->items[i].cb = f;
this->items[i].cb_arg = arg;
this->items[i].flag = shouldFireNow;
this->items[i].flag = false;
this->items[i].period = period;
this->items[i].is_used = true;
this->items[i].repeat = repeat;

enable(i);

return true;
}

bool TickerScheduler::once(uint8_t i, uint32_t period, tscallback_t f, void* arg)
{
return add(i, period, f, arg, false);
}

bool TickerScheduler::remove(uint8_t i)
{
if (i >= this->size || !this->items[i].is_used)
Expand Down Expand Up @@ -82,7 +102,11 @@ bool TickerScheduler::enable(uint8_t i)
return false;

volatile bool * flag = &this->items[i].flag;
this->items[i].t.attach_ms(this->items[i].period, TickerScheduler::handleTickerFlag, flag);
if(this->items[i].repeat){
this->items[i].t.attach_ms(this->items[i].period, TickerScheduler::handleTickerFlag, flag);
}else{
this->items[i].t.once_ms(this->items[i].period, TickerScheduler::handleTickerFlag, flag);
}

return true;
}
Expand All @@ -109,7 +133,7 @@ void TickerScheduler::update()
this->items[i].t.Tick();
#endif

handleTicker(this->items[i].cb, this->items[i].cb_arg, &this->items[i].flag);
handleTicker(&this->items[i]);
}
yield();
}
Expand Down
15 changes: 13 additions & 2 deletions TickerScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Ticker

void detach()
{
this->is_attached = true;
this->is_attached = false;
}

template<typename TArg> void attach_ms(uint32_t milliseconds, void(*callback)(TArg), TArg arg)
Expand All @@ -38,6 +38,14 @@ class Ticker
this->callback_argument = arg;
this->is_attached = true;
}

template<typename TArg> void once_ms(uint32_t milliseconds, void(*callback)(TArg), TArg arg)
{
this->period = milliseconds;
this->callback = callback;
this->callback_argument = arg;
this->is_attached = true;
}
};
#endif

Expand All @@ -62,6 +70,7 @@ struct TickerSchedulerItem
void * cb_arg;
uint32_t period;
volatile bool is_used = false;
volatile bool repeat = true;
};

class TickerScheduler
Expand All @@ -71,13 +80,15 @@ class TickerScheduler
TickerSchedulerItem *items = NULL;

void handleTicker(tscallback_t, void *, volatile bool * flag);
void handleTicker(TickerSchedulerItem * item);
static void handleTickerFlag(volatile bool * flag);

public:
TickerScheduler(uint8_t size);
~TickerScheduler();

bool add(uint8_t i, uint32_t period, tscallback_t, void *, boolean shouldFireNow = false);
bool add(uint8_t i, uint32_t period, tscallback_t, void *, boolean repeat = true);
bool once(uint8_t i, uint32_t period, tscallback_t, void *);
bool remove(uint8_t i);
bool enable(uint8_t i);
bool disable(uint8_t i);
Expand Down