Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
[..] I'd love to see how you do eventually handle the efficient delays
In a cooperative multi-tasking system all active tasks share the CPU time with each other and I cannot stop a Task from the outside. A Task can yield execution but does so willingly (hard-coded). So the cooperative nature is that all Tasks has to play nice together and there is no higher authority that can police the Tasks into behaving....So please tell me if I'm just not getting what you are trying to tell me or are we having some terms mixed up...?
template<const int rate, const byte pin>class BlinkLedTask{public: BlinkLedTask() { pinMode(pin, OUTPUT); digitalWrite(pin, false); _state = false; } Task_Begin(Execute) { while(true) { Task_YieldUntil(Delay::Wait((int)this, rate)); // toggle led _state = !_state; digitalWrite(pin, _state); } } Task_Endprivate: bool _state; int _task;};
#define MAXTASKS 4class Delay{public: static void Init(Time* time) { _time = time; for(int i = 0; i < MAXTASKS; i++) { _ids[i] = 0; _delays[i] = 0; } } static unsigned long Update() { _delta = _time->Update(); return _delta; } static bool Wait(int id, int milliseconds) { for(int i = 0; i < MAXTASKS; i++) { if (_ids[i] == id) { if (_delta >= _delays[i]) { _ids[i] = 0; return true; } _delays[i] -= _delta; return false; } } for(int i = 0; i < MAXTASKS; i++) { if (_ids[i] == 0) { _ids[i] = id; _delays[i] = milliseconds; break; } } return false; }private: static unsigned long _delta; static Time* _time; static int _ids[MAXTASKS]; static int _delays[MAXTASKS]; Delay(){}};
Time time;BlinkLedTask<1000, 13> ledTask1;BlinkLedTask<2000, 12> ledTask2;BlinkLedTask<500, 11> ledTask3;void setup(){ Delay::Init(&time);}void loop(){ Delay::Update(); ledTask1.Execute(); ledTask2.Execute(); ledTask3.Execute();}
struct blinking { unsigned char volatile *port; unsigned char bit; unsigned short delay;};void blinking_task(void *ptr) { blinking &b = *(blinking *)ptr; after(b.delay, blinking_task, ptr); *port ^= b.bit;}blinking a = { &PORTB, 0x08, 400 };blinking b = { &PORTB, 0x10, 500 };blinking c = { &PORTB, 0x20, 600 };void setup() { blinking_task(&a); blinking_task(&b); blinking_task(&c);}