LEGO MINDSTORMS Robots Manual page 182

Unofficial guide
Table of Contents

Advertisement

wakeup_t wait_event(wakeup_t (∗wakeup)(wakeup_t), wakeup_t data)
Use this function to wait for a specific event. The function pointed at by wakeup is called, with data as a parameter, until it returns a non-zero result. At this point, wait_event() will return.
It's not hard to use wait_event(), but it's certainly not obvious. It helps to look at some of the examples that come with legOS. The following is a rewrite of the previous example that uses
wait_event() instead of a while loop:
#include "conio.h"
#include "direct-button.h"
#include "unistd.h"
#include "sys/tm.h"
pid_t pid;
int display_task(int argc, char ∗∗argv) {
while(1) {
cputs("Hello");
lcd_refresh();
sleep(1);
cputs("nurse");
lcd_refresh();
sleep(1);
}
return 0;
}
wakeup_t button_press_wakeup(wakeup_t data) {
return PRESSED(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;
}
All that happened in this code was that the while loop from the previous example was replaced with a call to wait_event. The given function, button_press_wakeup(), does the dirty
work of checking the state of the button.
Memory (stdlib.h)
Page 202

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the MINDSTORMS Robots and is the answer not in the manual?

Questions and answers

Table of Contents