private ushort[] GetFullLuminosity(Gain gain, IntegrationTime time) { // We need to configure the device for the correct gain and integration time for this reading this._i2cDevice.Write(new byte[] { TSL2591_CONTROL_RW, (byte)((byte)gain << 4 | (byte)time) }); try { // Enabled the device so that we can start a reading this._i2cDevice.Write(new byte[] { TSL2591_ENABLE_RW, 0x03 }); try { // We now need to wait for the integration time to pass so that we know we have a valid reading. The specification // states that the MAX integration time per gain step is 108ms. System.Threading.Thread.Sleep(120 * ((byte)time + 1)); var readBuffer = new byte[4]; this._i2cDevice.WriteRead(new byte[] { TSL2591_C0DATAL_R }, readBuffer); var visible = (ushort)((readBuffer[1] << 8) | readBuffer[0]); var ir = (ushort)((readBuffer[3] << 8) | readBuffer[2]); return(new ushort[] { (ushort)(visible - ir), ir }); } finally { this._i2cDevice.Write(new byte[] { TSL2591_ENABLE_RW, 0x00 }); } } finally { this._i2cDevice.Write(new byte[] { TSL2591_CONTROL_RW, 0x00 }); } }
/// <summary> /// Construct a new VEML6070 attached to the specified I2C port /// </summary> /// <param name="i2c">the I2C port to use</param> /// <param name="time">The integration time setting</param> /// <remarks> /// Note that once the sensor is running, it will take 500 milliseconds to get a correct reading. The first reading you obtain will be invalid, and should be discarded. /// </remarks> public Veml6070(I2C i2c, IntegrationTime time = IntegrationTime.Times1) { this.i2c = i2c; this.i2c.Enabled = true; this.time = time; this.i2c.SendReceiveAsync(0x38, new byte[] { (byte)((byte)time << 2 | 0x02) }, 0); }
public async Task <double> GetLux(Gain gain = Gain.Low, IntegrationTime time = IntegrationTime.Shortest) { await ConnectAndInitialize(); await Configure(gain, time); Enable(); await Task.Delay((int)time); var y = ReadRegister(Registers.Command | Registers.C0DataL, x => (ushort)((x[1] << 8) | x[0]), 2) | 0; var luminosity = (ReadRegister(Registers.Command | Registers.C1DataL, x => (ushort)((x[1] << 8) | x[0]), 2) << 16) | y; Disable(); var full = luminosity & 0xFFFF; var ir = luminosity >> 16; if ((full == 0xFFFF) | (ir == 0xFFFF)) { return(0.0); } var cpl = (float)gain * (float)time / 408.0F; return((full - ir) * (1.0f - ir / full) / cpl); }
public async Task Configure(Gain gain, IntegrationTime time) { await ConnectAndInitialize(); _gain = (int)gain; WriteRegister(Registers.Command | Registers.Oscillator, gain.ToByte()); _integrationTime = (int)time; WriteRegister(Registers.Command | Registers.Oscillator, time.ToByte()); }
public Luminosity GetLuminosity(Gain gain = Gain.Low, IntegrationTime time = IntegrationTime.MS100) { var luminosity = this.GetFullLuminosity(gain, time); var lux = CalculateLux(luminosity[0], luminosity[1], gain, time); return(new Luminosity() { Visible = luminosity[0], IR = luminosity[1], Lux = lux, Gain = gain }); }
private async void setIntegrationTime(IntegrationTime integrationTime) { if (!initialized) { await begin(); } this.integrationTime = integrationTime; byte[] WriteBuffer = new byte[] { TCS34725Params.ATIME | TCS34725Params.COMMAND_BIT, (byte)integrationTime }; colorSensor.Write(WriteBuffer); }
/// <summary> /// Creates a new instance of the MAX44009, MAX44009 working mode is continuous. (Consume slightly higher power than in the default mode) /// </summary> /// <param name="i2cDevice">The I2C device used for communication.</param> /// <param name="integrationTime">Measurement Cycle</param> public Max44009(I2cDevice i2cDevice, IntegrationTime integrationTime) { _i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice)); // Details in the Datasheet P8 Span <byte> writeBuff = stackalloc byte[2] { (byte)Register.MAX_CONFIG, (byte)(0b_1100_0000 | (byte)integrationTime) }; _i2cDevice.Write(writeBuff); }
public async Task <double> GetUv(IntegrationTime time = IntegrationTime.Short) { await ConnectAndInitialize(); await Delay(time); WriteRegister(Registers.AlertRespAddress, 0x01); var uv = ReadRegister(Registers.AddressHigh, x => x[0]); uv <<= 8; uv |= ReadRegister(Registers.AddressLow, x => x[0]); return(uv); }
public async Task Configure(Gain gain, IntegrationTime time) { await ConnectAndInitialize(); WriteRegister(Registers.Command | Registers.Control, (byte)(gain.ToByte() | time.ToByte())); }
public static extern void SetIntegrationTime(IntPtr config, IntegrationTime time);
/// <summary> /// Set Integration Time for sensor /// </summary> /// <param name="it">Value of integration</param> public void SetIntegrationTime(IntegrationTime it) { WriteRegister(Register.RegisterTimeIntegration, (byte)it); _it = it; }
public static extern void SetIntegrationTime(IntPtr board, IntegrationTime time);
private double CalculateLux(double visible, double ir, Gain gain, IntegrationTime time) { double t = 0.0; double g = 0.0; switch (time) { case IntegrationTime.MS100: t = 100.0; break; case IntegrationTime.MS200: t = 200.0; break; case IntegrationTime.MS300: t = 300.0; break; case IntegrationTime.MS400: t = 400.0; break; case IntegrationTime.MS500: t = 500.0; break; case IntegrationTime.MS600: t = 600.0; break; default: t = 100.0; break; } switch (gain) { case Gain.Low: g = 1.0; break; case Gain.Medium: g = 25.0; break; case Gain.High: g = 428.0; break; case Gain.Max: g = 9876.0; break; default: g = 1.0; break; } var countsPerLux = (t * g) / DEFAULT_LUX_PER_COUNT; var lux1 = ((visible + ir) - (TSL2591_LUX_COEFB * ir)) / countsPerLux; var lux2 = ((TSL2591_LUX_COEFC * (visible + ir)) - (TSL2591_LUX_COEFD * ir)) / countsPerLux; return(lux1 > lux2 ? lux1 : lux2); }
public Luminosity GetGainAdjustedLuminosity() { Gain gain = Gain.Low; IntegrationTime time = IntegrationTime.MS200; var luminosity = this.GetFullLuminosity(gain, time); if ((luminosity[0] * 9876) < ushort.MaxValue) { gain = Gain.Max; // Sensor saturated or too low of a signal (saturated is unlikely). Switch to MAX and read again. luminosity = this.GetFullLuminosity(gain, time); if (luminosity[0] == 0) { // Unable to get a reading. The light is just too low. return(new Luminosity() { Gain = gain }); } } else if ((luminosity[0] * 428) < ushort.MaxValue) { // We can safely get a signal from the HIGH gain sensor gain = Gain.High; luminosity = this.GetFullLuminosity(gain, time); } else if ((luminosity[0] * 25) < ushort.MaxValue) { // We can safely get a signal from the MEDIUM gain sensor gain = Gain.Medium; luminosity = this.GetFullLuminosity(gain, time); } // Now calculate the LUX based off the visible, ir and gain values. var lux = this.CalculateLux(luminosity[0], luminosity[1], gain, time); double x1 = luminosity[0]; double x2 = luminosity[1]; // Adjust the visible and ir values for the gain. So we have a base RAW value switch (gain) { case Gain.Medium: x1 = luminosity[0] / 25.0; x2 = luminosity[1] / 25.0; break; case Gain.High: x1 = luminosity[0] / 428.0; x2 = luminosity[1] / 428.0; break; case Gain.Max: x1 = luminosity[0] / 9876.0; x2 = luminosity[1] / 9876.0; break; } return(new Luminosity() { Visible = x1, IR = x2, Lux = lux, Gain = gain }); }
public void Configure(Gain gain = Gain._1x, IntegrationTime time = IntegrationTime._100ms, MeasurementRate rate = MeasurementRate._500ms) { int gainMask = gain == Gain._48x || gain == Gain._96x ? (int)gain + 2 : (int)gain; bridge.sendCommand(new byte[] { (byte)AMBIENT_LIGHT, CONFIG, (byte)(gainMask << 2), (byte)((int)time << 3 | (int)rate) }); }
private async Task Delay(IntegrationTime time) { await Task.Delay((int)Math.Pow(1, (int)time) * 63); }