1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
//! This is a platform agnostic Rust driver for the VEML6030 and VEML7700 high-accuracy //! ambient light sensors using the [`embedded-hal`] traits. //! //! [`embedded-hal`]: https://github.com/rust-embedded/embedded-hal //! //! This driver allows you to: //! - Enable/disable the device. See: [`enable()`]. //! - Read the measured lux value. See: [`read_lux()`]. //! - Read the white channel measurement. See: [`read_white()`]. //! - Read the measured ALS value in raw format. See: [`read_raw()`]. //! - Calculate the compensated lux for a raw ALS value. See: [`convert_raw_als_to_lux()`]. //! - Set the gain. See: [`set_gain()`]. //! - Set the integration time. See: [`set_integration_time()`]. //! - Set the fault count. See: [`set_fault_count()`]. //! - Enable/disable and configure power saving mode. See: [`enable_power_saving()`]. //! - Enable/disable interrupts. See: [`enable_interrupts()`]. //! - Read the interrupt status. See: [`read_interrupt_status()`]. //! - Set the high/low thresholds in lux or raw. See: [`set_high_threshold_lux()`]. //! - Calculate the compensated raw threshold value ahead of time. See: [`calculate_raw_threshold_value()`]. //! //! [`enable()`]: struct.Veml6030.html#method.enable //! [`read_lux()`]: struct.Veml6030.html#method.read_lux //! [`read_white()`]: struct.Veml6030.html#method.read_white //! [`read_raw()`]: struct.Veml6030.html#method.read_raw //! [`convert_raw_als_to_lux()`]: fn.convert_raw_als_to_lux.html //! [`set_gain()`]: struct.Veml6030.html#method.set_gain //! [`set_integration_time()`]: struct.Veml6030.html#method.set_integration_time //! [`set_fault_count()`]: struct.Veml6030.html#method.set_fault_count //! [`enable_power_saving()`]: struct.Veml6030.html#method.enable_power_saving //! [`enable_interrupts()`]: struct.Veml6030.html#method.enable_interrupts //! [`read_interrupt_status()`]: struct.Veml6030.html#method.read_interrupt_status //! [`set_high_threshold_lux()`]: struct.Veml6030.html#method.set_high_threshold_lux //! [`calculate_raw_threshold_value()`]: fn.calculate_raw_threshold_value.html //! //! [Introductory blog post](https://blog.eldruin.com/veml6030-ambient-light-sensor-driver-in-rust/) //! //! ## The devices //! //! Vishay's VEML6030 / VEML7700 are high accuracy ambient light digital 16-bit //! resolution sensors in a miniature transparent package. They include //! a high sensitive photodiode, a low noise amplifier, a 16-bit A/D converter //! and support an easy to use I2C bus communication interface and additional //! interrupt feature. //! The ambient light result is as digital value available. //! //! Datasheets: [VEML6030](https://www.vishay.com/docs/84366/veml6030.pdf) - [VEML7700](https://www.vishay.com/docs/84286/veml7700.pdf) //! //! Application Note: //! - [Designing the VEML6030 into an application](https://www.vishay.com/docs/84367/designingveml6030.pdf) //! - [Designing the VEML7700 into an application](https://www.vishay.com/docs/84323/designingveml7700.pdf) //! //! ## Usage examples (see also examples folder) //! //! To use this driver, import this crate and an `embedded_hal` implementation, //! then instantiate the appropriate device. //! //! VEML6030 and VEML7700 expose the same interface over I2C. To communicate with a VEML7700 //! simply use this driver as if communicating with a VEML6030. //! //! Please find additional examples using hardware in this repository: [driver-examples] //! //! [driver-examples]: https://github.com/eldruin/driver-examples //! //! ### Read the lux //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{SlaveAddr, Veml6030}; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.enable().unwrap(); //! loop { //! let lux = sensor.read_lux().unwrap(); //! println!("lux: {:2}", lux); //! } //! # } //! ``` //! //! ### Provide an alternative address //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{SlaveAddr, Veml6030}; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let address = SlaveAddr::Alternative(true); //! let mut sensor = Veml6030::new(dev, address); //! # } //! ``` //! //! ### Set the gain and integration time //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{Gain, IntegrationTime, SlaveAddr, Veml6030}; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.set_gain(Gain::OneQuarter).unwrap(); //! sensor.set_integration_time(IntegrationTime::Ms200).unwrap(); //! sensor.enable().unwrap(); //! # } //! ``` //! //! ### Enable power-saving mode //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{PowerSavingMode, SlaveAddr, Veml6030}; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.enable_power_saving(PowerSavingMode::One).unwrap(); //! sensor.enable().unwrap(); //! # } //! ``` //! //! ### Set thresholds, fault count and enable interrupts //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{ //! FaultCount, Gain, IntegrationTime, SlaveAddr, Veml6030 //! }; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.set_gain(Gain::OneQuarter).unwrap(); //! // this will compensate the value automatically before setting it //! sensor.set_high_threshold_lux(10000.0).unwrap(); //! sensor.set_low_threshold_lux(100.0).unwrap(); //! sensor.set_fault_count(FaultCount::Four).unwrap(); //! sensor.enable_interrupts().unwrap(); //! sensor.enable().unwrap(); //! # } //! ``` //! //! ### Precalculate and set compensated threshold values //! //! Using current device configuration //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{Gain, IntegrationTime, SlaveAddr, Veml6030}; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.set_gain(Gain::OneEighth).unwrap(); //! sensor.set_integration_time(IntegrationTime::Ms200).unwrap(); //! let high_th_raw = sensor.calculate_raw_threshold_value(10000.0); //! // ... //! sensor.set_high_threshold_raw(high_th_raw).unwrap(); //! // this requires no compensation because the value is < 1000 //! sensor.set_low_threshold_lux(100.0).unwrap(); //! # } //! ``` //! //! ### Precalculate and set compensated threshold values //! //! Using free function //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{ //! calculate_raw_threshold_value, //! Gain, IntegrationTime, SlaveAddr, Veml6030 //! }; //! //! # fn main() { //! let gain = Gain::OneEighth; //! let it = IntegrationTime::Ms200; //! let high_th_raw = calculate_raw_threshold_value(it, gain, 10000.0); //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.set_gain(gain).unwrap(); //! sensor.set_integration_time(it).unwrap(); //! // ... //! sensor.set_high_threshold_raw(high_th_raw).unwrap(); //! // this requires no compensation because the value is < 1000 //! sensor.set_low_threshold_lux(100.0).unwrap(); //! sensor.enable_interrupts().unwrap(); //! sensor.enable().unwrap(); //! # } //! ``` //! //! ### Read interrupt status //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{SlaveAddr, Veml6030}; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! // ... //! loop { //! let status = sensor.read_interrupt_status().unwrap(); //! if status.was_too_high { //! // ... //! } //! if status.was_too_low { //! // ... //! } //! } //! # } //! ``` //! //! ### Read the raw ALS measurement and convert to lux separately //! //! Using current device configuration //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{SlaveAddr, Veml6030}; //! //! # fn main() { //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.enable().unwrap(); //! loop { //! let raw = sensor.read_raw().unwrap(); //! // ... //! let lux = sensor.convert_raw_als_to_lux(raw); //! println!("lux: {:2}", lux); //! } //! # } //! ``` //! //! ### Read the raw ALS measurement and convert to lux separately //! //! Using free function //! //! ```no_run //! extern crate linux_embedded_hal as hal; //! extern crate veml6030; //! use veml6030::{ //! convert_raw_als_to_lux, //! Gain, IntegrationTime, SlaveAddr, Veml6030 //! }; //! //! # fn main() { //! let gain = Gain::OneEighth; //! let it = IntegrationTime::Ms200; //! let dev = hal::I2cdev::new("/dev/i2c-1").unwrap(); //! let mut sensor = Veml6030::new(dev, SlaveAddr::default()); //! sensor.set_gain(gain).unwrap(); //! sensor.set_integration_time(it).unwrap(); //! sensor.enable().unwrap(); //! loop { //! let raw = sensor.read_raw().unwrap(); //! // ... //! let lux = convert_raw_als_to_lux(it, gain, raw); //! println!("lux: {:2}", lux); //! } //! # } //! ``` #![deny(unsafe_code, missing_docs)] #![no_std] extern crate embedded_hal as hal; extern crate libm; mod correction; mod device_impl; pub use correction::calculate_raw_threshold_value; pub use device_impl::convert_raw_als_to_lux; mod types; pub use types::{ Error, FaultCount, Gain, IntegrationTime, InterruptStatus, PowerSavingMode, SlaveAddr, }; /// VEML6030 device driver #[derive(Debug)] pub struct Veml6030<I2C> { /// The concrete I²C device implementation. i2c: I2C, address: u8, config: Config, gain: Gain, it: IntegrationTime, } #[derive(Debug, Default, Clone, Copy, PartialEq)] struct Config { bits: u16, }