Page 209 - The Unofficial Guide to Lego Mindstorms Robots
P. 209

198






                            The rotation sensor code does not work in the March 30, 1999 build of legOS 0.1.7

          Using the Buttons (d irect-button.h)

          le gOS provides one function and some other handy defi nitions to describe the state of the front panel buttons:

          in t button_state(void)
          This function returns a value that in dicates the state of the RCX's four buttons. Use the following  macros to interpret the
          returned value.

          PRESSED(state, button)

          R ELEASED(state, button)

              These macros return a boolean value indicating if the specified button was pressed or not. To use these macros, pass the
              result  of  button_state()  as the  state  parameter and the  name of  a button for   button.  Buttons names are
              BUTTON_ONOFF, BUTTON_RUN, BUTTON_VIEW, and BUTTON_PROGRAM.

          For example, the following code tests the state  of the View button:

              if (PRESSED(button_st    ate(), BUTTON_VIEW)) {
                  // View button is pressed.
              }

          To test the state of more than one button, it makes sense to store the  result of button_state(), like this:

              int state;
              state = button_state();
              if (PRESSED (state, BUTTON_VIEW)) {
                  // View button is pressed.
              }
              if (PRESSED(state, BUTTON_PROGRAM) ) {
                  // Program but   ton is pressed.
              }

          The  fr ared Port (direct-ir.h)
              n
             I

          To send data out the IR port, ju st use the dir_write() func tion:

          size_t dir_write(void∗ const buf, size_t len)

          This func tion writes len  bytes of data from the supplied buffer out the IR port. It returns the number of bytes written or -1 if
          there is an error.
   204   205   206   207   208   209   210   211   212   213   214