Page 213 - The Unofficial Guide to Lego Mindstorms Robots
P. 213
202
sleep(1);
}
return 0;
}
wakeup_t button_press_wakeup(wakeup_t data) {
return PRE SSED(button_state(),data);
}
int stop_task(int argc, char ∗∗argv) {
msleep(200);
wait_event(&button_press_wakeup, BUTTON_RUN);
kill(pid);
return 0;
}
int main() {
pid = execi(&display_task, 0, NULL, 0, DEFAULT_STACK_SIZE);
execi(&stop_task, 0, NULL, 0, DEFAULT_STACK_SIZE);
tm_start();
return 0 ;
}
A ll that happened in this code was that the while loop from the previous example was replaced with a call to
wait_event. The giv en function, button_press_wakeup(), does the dirty work of checking the state of the button.
Memory (stdlib.h)
You can request chunks of memory in the RCX using the familiar malloc() and calloc() functions. LegOS, by itself,
ta kes up about 5K or 6K of the RCX's 32K of RAM. You should have about 26K left for your program and its data.
void ∗malloc(size_t size)
This function allocates a chunk of memory of the given size (in bytes). A pointer to the memory is returned. If the memory
ca nnot be allocated, NULL is returned.
vo id ∗calloc(size_t nmemb, size_t size)
This function allocates enough memory for nmemb chunks of memory, e ach size bytes long. It also sets each byte of the
al located m emory to zero. Like malloc(), it returns NULL if something goes wrong.
void free(void ∗ptr)
When you are done with memory you've allocated, you should free it using this function.