I would like to have a task wait on a particular event for a considerable about of time (2 seconds in this case) for an external input. When the expected input happens, the wait is re-triggered. When the task times out, special handling happens. Additionally, another 'super-task' may halt this waiting task when some other condition is satified. The problem I have is stopping the waiting phase.
MyWaitingTask()
{
for( ; ; )
{
OS_WaitBinSem(MY_BINSEM_P, MY_TIMEOUT, MyTask1);
if(OSTimedOut())
{
handleTimeout();
}
}
}
Since OSStopTask(MyWatingTask) will mostly likely fail, one solution is to allow the timeout, and the handler looks whether this task should stop.
The other idea I have is to use event flags: one bit is the expected external event, another bit is the STOP_TASK. Thus,
for ( ; ; )
{
OS_WaitEFlag(MY_WAIT_FLAGS_P, MY_MASK, OSANY_BITS, OSNO_TIMEOUT, MyTask1);
eflag = OSReadEFlag(MY_WAIT_FLAGS);
if(eflag & STOP_TASK)
OS_Stop(MyTask2);
}
An outside task MUST be responsible (OSNO_TIMEOUT) for 'knowing' about the running task, then executes
MyOutsideTask()
{
// stop the waiting task
OSSetEFlag(MY_WAIT_FLAGS, STOP_TASK);
...
}
rather than the task itself (not very OOP). But it does effectively stop the task relatively quickly, without waiting the 2 seconds.
Anyone out there with a similar idea, or solution?