Skip to content
This repository has been archived by the owner on Aug 19, 2021. It is now read-only.

Commit

Permalink
Moved away from global constructors in mbed implementation
Browse files Browse the repository at this point in the history
- Adopted placement-new with word-buffers for global classes
- Better structured initialization of global state
  • Loading branch information
geky committed Aug 15, 2016
1 parent fe021da commit bc0a40d
Showing 1 changed file with 26 additions and 27 deletions.
53 changes: 26 additions & 27 deletions equeue_mbed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,37 @@


// Ticker operations
class EqueueTicker {
public:
EqueueTicker() {
_tick = 0;
_timer.start();
_ticker.attach_us(this, &EqueueTicker::update, (1 << 16) * 1000);
};

void update() {
_timer.reset();
_tick += 1 << 16;
}
static bool equeue_tick_inited = false;
static unsigned equeue_timer[(sizeof(Timer)+1) / sizeof(unsigned)];
static unsigned equeue_ticker[(sizeof(Ticker)+1) / sizeof(unsigned)];
static unsigned equeue_minutes = 0;

static void equeue_tick_update() {
reinterpret_cast<Timer*>(equeue_timer)->reset();
equeue_minutes += 1;
}

unsigned tick() {
return _tick + (unsigned)_timer.read_ms();
}
static void equeue_tick_init() {
MBED_ASSERT(sizeof(equeue_timer) >= sizeof(Timer));
MBED_ASSERT(sizeof(equeue_ticker) >= sizeof(Ticker));
new (equeue_timer) Timer;
new (equeue_ticker) Ticker;

private:
unsigned _tick;
#ifdef DEVICE_LOWPOWERTIMER
LowPowerTimer _timer;
LowPowerTicker _ticker;
#else
Timer _timer;
Ticker _ticker;
#endif
};
equeue_minutes = 0;
reinterpret_cast<Timer*>(equeue_timer)->start();
reinterpret_cast<Ticker*>(equeue_ticker)
->attach_us(equeue_tick_update, (1 << 16)*1000);

static EqueueTicker equeue_ticker;
equeue_tick_inited = true;
}

unsigned equeue_tick() {
return equeue_ticker.tick();
if (!equeue_tick_inited) {
equeue_tick_init();
}

unsigned equeue_ms = reinterpret_cast<Timer*>(equeue_timer)->read_ms();
return (equeue_minutes << 16) + equeue_ms;
}


Expand Down

0 comments on commit bc0a40d

Please sign in to comment.