Light

The light sensor returns a value representing the illuminance of the external environment. Values are measured in lux (lx), where one lux is equal to one lumen per square meter (1 lx = 1 lm/m2).

A BPS event with the code SENSOR_LIGHT_READING is generated when a new illuminance value is available. You can call the function sensor_event_get_illuminance() to retrieve the value of the sensor reading from the event.

The code sample below demonstrates how you can use the light sensor in your app:

#include <stddef.h>
#include <bps/bps.h>
#include <bps/sensor.h>


int main(void)
{
  int event_domain;
  unsigned int event_code;
  bool isDark = false;
  float luxlevel;

  bps_initialize();

  // Request light sensor events.
  if (sensor_is_supported(SENSOR_TYPE_LIGHT))
  {
    sensor_request_events(SENSOR_TYPE_LIGHT);
  }

  while (1)
  {
    bps_event_t *current_event = NULL;
    bps_get_event(&current_event, -1);

    event_domain = bps_event_get_domain(current_event);
    if (event_domain == sensor_get_domain())
    {
      event_code = bps_event_get_code(current_event);
      switch (event_code)
      {
        case SENSOR_LIGHT_READING:
          // Get latest reading from the light sensor.
          luxlevel = 
              sensor_event_get_illuminance(current_event);

              // Determine if light level has changed from light
              // to dark, or dark to light.
              if ((luxlevel < 100) && (!isDark)) {
                isDark = true;
                // Adjust app for low-light operation.
              } else if ((luxlevel >= 100) && (isDark)) {
                isDark = false;
                // Adjust app for regular operation.
              } else {
                // No change to lighting conditions.
              }
              break;

            default:
              break;
      } // switch
    } // if
  } // while

  // Program cleanup
  sensor_stop_events(SENSOR_TYPE_LIGHT);
  bps_shutdown();

  return 0;
}                          
            

Last modified: 2013-12-21



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus