Page 211 - The Unofficial Guide to Lego Mindstorms Robots
P. 211
200
void tm_start(void)
Use this functi on to start up the task manager.
Once the task manager is running, new tasks can be started (with execi()), and running tasks can be stopped with this
function:
void kill(pid_t pid)
Use this function to stop the task represented by pid.
You can suspend tasks for a given amount of time with the follo wing two functions:
ne
unsig d int sleep(unsigned int sec)
This function suspends execution for the given number of seconds.
unsigned int msleep(unsigne d int msec)
This fun ction suspends exec ution for the given number of milliseconds (ms).
Here is a simple exampl e that uses two tasks. The first task shows "Hello" and "nurse" on the display. The second task just
waits for the Run button to be pressed. When it is pressed, the first task is stopped, the second tas k ends, and control returns
to legOS.
#i nclude "conio. h"
#i nclude "direct -button.h"
#i nclude "unis td.h"
#in clude "sys/ tm.h"
pid-t pid;
int display_task(int argc, char ∗∗argv) {
while( 1) {
cpu ts("Hello");
lcd_refresh();
sleep(1);
cputs("nurse");
lcd_refresh();
sleep(1);
}
return 0;
}
int stop_task(int argc, char ∗∗argv) {
msleep(200);
while (!PRESSED (button_state(), BUTTON_RUN));
kill(pi d);
return 0;
}
int main() {
pid = execi(&display_task, 0, NULL, 0, DEFAULT_STACK_SIZE);
execi(&stop_task, 0, NULL, 0, DEFAULT_STACK_SIZE);