16 unsigned int stack[(160 + (90 * 4)) / 4];
18 Interval::Slot Interval::_slots[MAX_SLOTS];
19 volatile unsigned int Interval::_intervalCog = 0;
20 volatile unsigned int Interval::_lockID =
locknew();
24 void Interval::attach(IntervalCallback callback, uint16_t interval,
void * blob)
27 while(
lockset(Interval::_lockID));
30 if (Interval::_intervalCog == 0)
32 Interval::_intervalCog =
cogstart(&intervalLoop, NULL, stack,
sizeof(stack)) + 1;
36 for(uint8_t i = 0; i < MAX_SLOTS; i++)
38 if (!Interval::_slots[i].isAttached())
39 Interval::_slots[i].attach(callback, interval, blob);
48 void Interval::detach(IntervalCallback callback)
51 while(
lockset(Interval::_lockID));
53 int attachedCount = 0;
56 for(uint8_t i = 0; i < MAX_SLOTS; i++)
58 if (Interval::_slots[i]._callback == callback)
59 Interval::_slots[i].detach();
61 if (Interval::_slots[i].isAttached())
67 if (attachedCount == 0 && Interval::_intervalCog != 0)
69 cogstop(Interval::_intervalCog-1);
70 Interval::_intervalCog = 0;
79 void Interval::intervalLoop(
void *par)
85 for(uint8_t i = 0; i < MAX_SLOTS; i++)
88 while(
lockset(Interval::_lockID));
91 if (Interval::_slots[i].isAttached() && Interval::_slots[i].isDue())
93 Interval::_slots[i].invokeCallback();
102 boolean Interval::Slot::isDue()
109 if (now > (_lastTime + _interval))
114 if ((ULONG_MAX - _lastTime + now) > _interval)