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

197


              if (SENSOR_1 < 0xf000) {
                  // Touch sensor is pressed.
              }

          If you're working with light or rotation sen sors, legOS does offer some help. First, you can set inputs to be active or passive:

          void ds_active(unsigned∗ const sensor)

          void ds_passive(unsigned∗ const sensor)

              Use these functions to set the specified sensor to active or passive mode. The light and rotation sensors are active  sensors;
              the touch and temperature sensors are passive.

          T he argument to these functions is the address of one of the sensor values. For exa mple, to set input 2 to be active, you would
          do this:

              ds_active(&SENSOR_2);

          Processed light sensor values can be retrieved with the following macros:

          LIGHT_1

          LIGHT_2

          LIGHT_3

              These macros process raw  input values t o  produce a light sensor reading in the  range from 0  to approximately
              LIGHT_MAX.

          legOS also supports rotation sensors with the following functions:

          void ds_rotation_ (unsigned∗ const sensor)
                        on

          void ds_rotation_off(unsigned∗ const sensor)

              These functions turn on or off rotation counting for the specified input.

          void ds_rotation_set(unsigned∗ const sensor, int pos)

          This function sets the current rotation count of the given input.

          Once you get the input set up for rotation, you can retrieve the rotation value with one of the following macros:

          ROTATION_1

          ROTATION_2

          ROTATION_3

              These macros return the rotation count for each of the inputs.

          Setting up input 3 for a rotation sensor, then, looks something like this:

              ds_active(&SENSOR_3);
              ds_rotation_on(&SENSOR_3);
              ds_rotation_set(&SENSOR_3, 0);

          To actually read the rotation value, you would just use the ROTATION_3 macro.
   203   204   205   206   207   208   209   210   211   212   213