Skip to content

Commit

Permalink
Scheduler callback argument preliminary support (Issue Toshik#4)
Browse files Browse the repository at this point in the history
Example: t.add(0, 1000, [&](int*arg) { Serial.println((int)*arg); }, &arg);
  • Loading branch information
Toshik authored and dogoepp committed Jun 25, 2018
1 parent 181c138 commit 2fc25d8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
9 changes: 5 additions & 4 deletions TickerScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ void TickerScheduler::handleTickerFlag(volatile bool * flag)
*flag = true;
}

void TickerScheduler::handleTicker(tscallback_t f, volatile bool * flag)
void TickerScheduler::handleTicker(tscallback_t f, void * arg, volatile bool * flag)
{
if (*flag)
{
yield();
*flag = false;
yield();
f();
f(arg);
yield();
}
}

bool TickerScheduler::add(uint8_t i, uint32_t period, tscallback_t f, boolean shouldFireNow)
bool TickerScheduler::add(uint8_t i, uint32_t period, tscallback_t f, void* arg, boolean shouldFireNow)
{
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].period = period;
this->items[i].is_used = true;
Expand Down Expand Up @@ -116,7 +117,7 @@ void TickerScheduler::update()
this->items[i].t.Tick();
#endif

handleTicker(this->items[i].cb, &this->items[i].flag);
handleTicker(this->items[i].cb, this->items[i].cb_arg, &this->items[i].flag);
}
yield();
}
Expand Down
7 changes: 4 additions & 3 deletions TickerScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ void tickerFlagHandle(volatile bool * flag);
#ifdef _GLIBCXX_FUNCTIONAL
typedef std::function<void(void)> tscallback_t;
#else
typedef void(*tscallback_t)(void);
typedef void(*tscallback_t)(void*);
#endif

struct TickerSchedulerItem
{
Ticker t;
volatile bool flag = false;
tscallback_t cb;
void * cb_arg;
uint32_t period;
volatile bool is_used = false;
};
Expand All @@ -69,14 +70,14 @@ class TickerScheduler
uint8_t size;
TickerSchedulerItem *items = NULL;

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

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

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

0 comments on commit 2fc25d8

Please sign in to comment.