提交 a5fc53c9 编写于 作者: Z Zhang Yuexin 提交者: Krzysztof Wicher

Added BH1750FVI Binding (#363)

* added bh1750fvi binding

* remove LightTransmittance.cs

* Fixed some problems
上级 58fff856
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Buffers.Binary;
using System.Device.I2c;
using System.Threading;
namespace Iot.Device.Bh1750fvi
{
/// <summary>
/// Ambient Light Sensor BH1750FVI
/// </summary>
public class Bh1750fvi : IDisposable
{
private const byte DefaultLightTransmittance = 0b_0100_0101;
private I2cDevice _sensor;
private double _lightTransmittance;
/// <summary>
/// BH1750FVI Light Transmittance, from 27.20% to 222.50%
/// </summary>
public double LightTransmittance
{
get => _lightTransmittance;
set
{
SetLightTransmittance(value);
_lightTransmittance = value;
}
}
/// <summary>
/// BH1750FVI Measuring Mode
/// </summary>
public MeasuringMode MeasuringMode { get; set; }
/// <summary>
/// BH1750FVI Illuminance (Lux)
/// </summary>
public double Illuminance => Math.Round(GetIlluminance(), 1);
/// <summary>
/// Creates a new instance of the BH1750FVI
/// </summary>
/// <param name="sensor">I2C Device, like UnixI2cDevice or Windows10I2cDevice</param>
/// <param name="measuringMode">The measuring mode of BH1750FVI</param>
/// <param name="lightTransmittance">BH1750FVI Light Transmittance, from 27.20% to 222.50%</param>
public Bh1750fvi(I2cDevice sensor, MeasuringMode measuringMode = MeasuringMode.ContinuouslyHighResolutionMode, double lightTransmittance = 1)
{
_sensor = sensor;
_sensor.WriteByte((byte)Command.PowerOn);
_sensor.WriteByte((byte)Command.Reset);
LightTransmittance = lightTransmittance;
MeasuringMode = measuringMode;
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_sensor?.Dispose();
_sensor = null;
}
/// <summary>
/// Set BH1750FVI Light Transmittance
/// </summary>
/// <param name="transmittance">Light Transmittance, from 27.20% to 222.50%</param>
private void SetLightTransmittance(double transmittance)
{
if (transmittance > 2.225 || transmittance < 0.272)
{
throw new ArgumentOutOfRangeException(nameof(transmittance), $"{nameof(transmittance)} needs to be in the range of 27.20% to 222.50%.");
}
byte val = (byte)(DefaultLightTransmittance / transmittance);
_sensor.WriteByte((byte)((byte)Command.MeasurementTimeHigh | (val >> 5)));
_sensor.WriteByte((byte)((byte)Command.MeasurementTimeLow | (val & 0b_0001_1111)));
}
/// <summary>
/// Get BH1750FVI Illuminance
/// </summary>
/// <returns>Illuminance (Lux)</returns>
private double GetIlluminance()
{
if (MeasuringMode == MeasuringMode.OneTimeHighResolutionMode || MeasuringMode == MeasuringMode.OneTimeHighResolutionMode2 || MeasuringMode == MeasuringMode.OneTimeLowResolutionMode)
_sensor.WriteByte((byte)Command.PowerOn);
Span<byte> readBuff = stackalloc byte[2];
_sensor.WriteByte((byte)MeasuringMode);
_sensor.Read(readBuff);
ushort raw = BinaryPrimitives.ReadUInt16BigEndian(readBuff);
double result = raw / (1.2 * _lightTransmittance);
if (MeasuringMode == MeasuringMode.ContinuouslyHighResolutionMode2 || MeasuringMode == MeasuringMode.OneTimeHighResolutionMode2)
result *= 2;
return result;
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Device.Gpio" Version="$(SystemDeviceGpioPackageVersion)" />
<Compile Include="Bh1750fvi.cs" />
<Compile Include="Command.cs" />
<Compile Include="I2cAddress.cs" />
<Compile Include="MeasuringMode.cs" />
</ItemGroup>
<ItemGroup>
<None Remove="README.md" />
<None Remove="sensor.jpg" />
</ItemGroup>
</Project>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Iot.Device.Bh1750fvi
{
internal enum Command : byte
{
PowerDown = 0b_0000_0000,
PowerOn = 0b_0000_0001,
Reset = 0b_0000_0111,
MeasurementTimeHigh = 0b_0100_0000,
MeasurementTimeLow = 0b_0110_0000,
}
}
\ No newline at end of file
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Iot.Device.Bh1750fvi
{
/// <summary>
/// BH1750FVI I2C Address
/// </summary>
public enum I2cAddress : byte
{
/// <summary>
/// ADD Pin connect to high power level
/// </summary>
AddPinHigh = 0x5C,
/// <summary>
/// ADD Pin connect to low power level
/// </summary>
AddPinLow = 0x23
}
}
\ No newline at end of file
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
namespace Iot.Device.Bh1750fvi
{
/// <summary>
/// The measuring mode of BH1750FVI
/// </summary>
public enum MeasuringMode : byte
{
// Details in the datasheet P5
/// <summary>
/// Start measurement at 1lx resolution
/// Measurement Time is typically 120ms.
/// </summary>
ContinuouslyHighResolutionMode = 0b_0001_0000,
/// <summary>
/// Start measurement at 0.5lx resolution
/// Measurement Time is typically 120ms.
/// </summary>
ContinuouslyHighResolutionMode2 = 0b_0001_0001,
/// <summary>
/// Start measurement at 4lx resolution
/// Measurement Time is typically 16ms.
/// </summary>
ContinuouslyLowResolutionMode = 0b_0001_0011,
/// <summary>
/// Start measurement at 1lx resolution once
/// Measurement Time is typically 120ms.
/// It is automatically set to Power Down mode after measurement.
/// </summary>
OneTimeHighResolutionMode = 0b_0010_0000,
/// <summary>
/// Start measurement at 0.5lx resolution once
/// Measurement Time is typically 120ms.
/// It is automatically set to Power Down mode after measurement.
/// </summary>
OneTimeHighResolutionMode2 = 0b_0010_0001,
/// <summary>
/// Start measurement at 4lx resolution once
/// Measurement Time is typically 16ms.
/// It is automatically set to Power Down mode after measurement.
/// </summary>
OneTimeLowResolutionMode = 0b_0010_0011
}
}
\ No newline at end of file
# BH1750FVI - Ambient Light Sensor
BH1750FVI is an digital Ambient Light Sensor IC for I2C bus interface. This IC is the most suitable to obtain the ambient light data for adjusting LCD and Keypad backlight power of Mobile phone. It is possible to detect wide range at High resolution.
## Sensor Image
![](sensor.jpg)
## Usage
```C#
I2cConnectionSettings settings = new I2cConnectionSettings(busId: 1, (int)I2cAddress.AddPinLow);
// get I2cDevice (in Linux)
UnixI2cDevice device = new UnixI2cDevice(settings);
// get I2cDevice (in Win10)
//Windows10I2cDevice device = new Windows10I2cDevice(settings);
using (Bh1750fvi sensor = new Bh1750fvi(device))
{
// read illuminance(Lux)
double illuminance = sensor.Illuminance;
}
```
## References
https://cdn.datasheetspdf.com/pdf-down/B/H/1/BH1750FVI_Rohm.pdf
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Bh1750fvi.csproj" />
</ItemGroup>
<ItemGroup>
<None Remove="BH1750FVI_Circuit.fzz" />
<None Remove="BH1750FVI_Circuit_bb.png" />
<None Remove="README.md" />
<None Remove="RunningResult.jpg" />
</ItemGroup>
</Project>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Device.I2c;
using System.Device.I2c.Drivers;
using System.Threading;
namespace Iot.Device.Bh1750fvi.Samples
{
class Program
{
static void Main(string[] args)
{
I2cConnectionSettings settings = new I2cConnectionSettings(busId: 1, (int)I2cAddress.AddPinLow);
UnixI2cDevice device = new UnixI2cDevice(settings);
using (Bh1750fvi sensor = new Bh1750fvi(device))
{
while (true)
{
Console.WriteLine($"Illuminance: {sensor.Illuminance}Lux");
Thread.Sleep(1000);
}
}
}
}
}
# BH1750FVI - Samples
## Hardware Required
* BH1750FVI
* Male/Female Jumper Wires
## Circuit
![](BH1750FVI_Circuit_bb.png)
* SCL - SCL
* SDA - SDA
* VCC - 5V
* GND - GND
* ADDR - GND
## Code
```C#
I2cConnectionSettings settings = new I2cConnectionSettings(busId: 1, (int)I2cAddress.AddPinLow);
UnixI2cDevice device = new UnixI2cDevice(settings);
using (Bh1750fvi sensor = new Bh1750fvi(device))
{
while (true)
{
Console.WriteLine($"Illuminance: {sensor.Illuminance}Lux");
Thread.Sleep(1000);
}
}
```
## Result
![](RunningResult.jpg)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册