.. highlight:: swift Ambient Light Sensor ==================== Light sensors measure illuminance, which can be used to measure more than the brightness of a light source. MetaWear RPro and Cpro, and MetaDetector board come with a `Lite-On LTR-329ALS `_ ambient light sensor that can measure light from 0.01 lux to 64k lux. Functions interacting with the light sensor are defined in the `ambientlight_ltr329.h `_ header file. Configuration ------------- The LTR329 sensor has 3 configurable parameters: ================ ========================================= Parameter Description ================ ========================================= Gain Controls data range and resolution Integration Time Measurement time for each cycle Measurement Rate How frequently to update illuminance data ================ ========================================= Possible values for each of these parameters are defined in their respective enums. After configuring the API with the desired settings, call `mbl_mw_als_ltr329_write_config `_. to write the settings to the sensor. :: // Set sensor gain to 96x mbl_mw_als_ltr329_set_gain(board, MBL_MW_ALS_LTR329_GAIN_96X) // Set the integration time to 400ms mbl_mw_als_ltr329_set_integration_time(board, MBL_MW_ALS_LTR329_TIME_400MS) // Set the measurement rate to 1000ms mbl_mw_als_ltr329_set_measurement_rate(board, MBL_MW_ALS_LTR329_RATE_1000MS) // Write the configuration to the sensor mbl_mw_als_ltr329_write_config(board) Illuminance Measurement ----------------------- To start measuring illuminance, call `mbl_mw_als_ltr329_start `_. Illuminance data is represented as an unsigned integer and is in units of milli lux. :: let signal = mbl_mw_als_ltr329_get_illuminance_data_signal(device.board)! mbl_mw_datasignal_subscribe(signal, bridge(obj: self)) { (context, obj) in let illuminance: UInt32 = obj!.pointee.valueAs() print(String(format: "%.3f lux", Double(illuminance) / 1000.0)) } mbl_mw_als_ltr329_start(device.board)