Page 210 - The Unofficial Guide to Lego Mindstorms Robots
P. 210
199
The dir_write() function does not work in the March 30, 1999 build of legOS 0.1.7
.
Two functions are provided for reading data from the IR port:
size_t dir_read(void∗ buf, size_t len)
T his method reads len bytes of data into the supplied buffer. It returns the number of bytes read or -1 if there is an error.
void dir_fflush(void)
The IR input is buffered, which means incoming data is placed in a buffer. When it fills up, it is made available to
dir_read(). To force the contents of the input buffer to be available to dir_read(), first call dir_fflush().
O ne of the legOS demos is tm-and-ir.c. This program listens for incoming IR data and sh ows it on the RCX's display. You can
type into a terminal emulator on your PC and see the data show up on the display.
Multitasking in legOS (unistd.h and sys/tm.h )
Multiple tasks are su pported in legOS through a reduced version of the standard Unix libraries. The basic idea is to set up some
number of tasks using the execi() function. Then the task manager itse lf must be started with a call to tm_start().
pid_t execi(int (∗code_start)(int, char∗∗),
int argc, char ∗∗argv,priority_tpriority, size_t stack_size)
This function starts the task described by code_start. Don't worry about the nasty-looking syntax above; all you have to do
i s pass the name of a function. You can pass information to the task usi ng the argc and argv parameters. The new task has
the given priority and stack size. Lower priorities take precedence over higher priorities. In general, you can pass 0 for the
priority and DEFAULT_STACK_SIZE as the stack size. This function returns a process identification number (PID). You can
st op a task, as you'll see later, using this number.
If the stack size you pass t o execi() is too small, all sorts of weird behavior results. If your program
crashes, this is on e of the first things you should check.
Remembe r, a function's return addresses and automatic variables (declared in the scope of the function) are
stored on the stack. If you have many levels of function calls, or many automatic variables, or large arrays as
automatic variables, you may overrun your stack.