Skip to content

Commit 88442b6

Browse files
authored
Adding Bl1750fvi (#28)
1 parent 9a8013f commit 88442b6

22 files changed

+540
-2
lines changed

devices/Bh1750fvi/Bh1750fvi.cs

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Buffers.Binary;
6+
using System.Device.I2c;
7+
using System.Device.Model;
8+
using UnitsNet;
9+
10+
namespace Iot.Device.Bh1750fvi
11+
{
12+
/// <summary>
13+
/// Ambient Light Sensor BH1750FVI
14+
/// </summary>
15+
[Interface("Ambient Light Sensor BH1750FVI")]
16+
public class Bh1750fvi : IDisposable
17+
{
18+
private const byte DefaultLightTransmittance = 0b_0100_0101;
19+
20+
private I2cDevice _i2cDevice;
21+
22+
private double _lightTransmittance;
23+
24+
/// <summary>
25+
/// BH1750FVI Light Transmittance, from 27.20% to 222.50%
26+
/// </summary>
27+
[Property]
28+
public double LightTransmittance
29+
{
30+
get => _lightTransmittance;
31+
set
32+
{
33+
SetLightTransmittance(value);
34+
_lightTransmittance = value;
35+
}
36+
}
37+
38+
/// <summary>
39+
/// BH1750FVI Measuring Mode
40+
/// </summary>
41+
[Property]
42+
public MeasuringMode MeasuringMode { get; set; }
43+
44+
/// <summary>
45+
/// BH1750FVI Illuminance (Lux)
46+
/// </summary>
47+
[Telemetry]
48+
public Illuminance Illuminance => GetIlluminance();
49+
50+
/// <summary>
51+
/// Creates a new instance of the BH1750FVI
52+
/// </summary>
53+
/// <param name="i2cDevice">The I2C device used for communication.</param>
54+
/// <param name="measuringMode">The measuring mode of BH1750FVI</param>
55+
/// <param name="lightTransmittance">BH1750FVI Light Transmittance, from 27.20% to 222.50%</param>
56+
public Bh1750fvi(I2cDevice i2cDevice, MeasuringMode measuringMode = MeasuringMode.ContinuouslyHighResolutionMode, double lightTransmittance = 1)
57+
{
58+
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
59+
60+
_i2cDevice.WriteByte((byte)Command.PowerOn);
61+
_i2cDevice.WriteByte((byte)Command.Reset);
62+
63+
LightTransmittance = lightTransmittance;
64+
MeasuringMode = measuringMode;
65+
}
66+
67+
/// <summary>
68+
/// Set BH1750FVI Light Transmittance
69+
/// </summary>
70+
/// <param name="transmittance">Light Transmittance, from 27.20% to 222.50%</param>
71+
private void SetLightTransmittance(double transmittance)
72+
{
73+
if (transmittance > 2.225 || transmittance < 0.272)
74+
{
75+
throw new ArgumentOutOfRangeException(nameof(transmittance), $"{nameof(transmittance)} needs to be in the range of 27.20% to 222.50%.");
76+
}
77+
78+
byte val = (byte)(DefaultLightTransmittance / transmittance);
79+
80+
_i2cDevice.WriteByte((byte)((byte)Command.MeasurementTimeHigh | (val >> 5)));
81+
_i2cDevice.WriteByte((byte)((byte)Command.MeasurementTimeLow | (val & 0b_0001_1111)));
82+
}
83+
84+
/// <summary>
85+
/// Get BH1750FVI Illuminance
86+
/// </summary>
87+
/// <returns>Illuminance (Default unit: Lux)</returns>
88+
private Illuminance GetIlluminance()
89+
{
90+
if (MeasuringMode == MeasuringMode.OneTimeHighResolutionMode || MeasuringMode == MeasuringMode.OneTimeHighResolutionMode2 || MeasuringMode == MeasuringMode.OneTimeLowResolutionMode)
91+
{
92+
_i2cDevice.WriteByte((byte)Command.PowerOn);
93+
}
94+
95+
SpanByte readBuff = new byte[2];
96+
97+
_i2cDevice.WriteByte((byte)MeasuringMode);
98+
_i2cDevice.Read(readBuff);
99+
100+
ushort raw = BinaryPrimitives.ReadUInt16BigEndian(readBuff);
101+
102+
double result = raw / (1.2 * _lightTransmittance);
103+
104+
if (MeasuringMode == MeasuringMode.ContinuouslyHighResolutionMode2 || MeasuringMode == MeasuringMode.OneTimeHighResolutionMode2)
105+
{
106+
result *= 2;
107+
}
108+
109+
return Illuminance.FromLux(result);
110+
}
111+
112+
/// <summary>
113+
/// Cleanup
114+
/// </summary>
115+
public void Dispose()
116+
{
117+
_i2cDevice?.Dispose();
118+
_i2cDevice = null!;
119+
}
120+
}
121+
}

devices/Bh1750fvi/Bh1750fvi.nfproj

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}</ProjectGuid>
12+
<OutputType>Library</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>Iot.Device.Bh1750fvi</RootNamespace>
16+
<AssemblyName>Iot.Device.Bh1750fvi</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
<DocumentationFile>bin\$(Configuration)\Iot.Device.Bh1750fvi.xml</DocumentationFile>
19+
<LangVersion>9.0</LangVersion>
20+
</PropertyGroup>
21+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
22+
<ItemGroup>
23+
<Reference Include="mscorlib">
24+
<HintPath>packages\nanoFramework.CoreLibrary.1.10.4-preview.11\lib\mscorlib.dll</HintPath>
25+
<Private>True</Private>
26+
</Reference>
27+
<Reference Include="System.Device.I2c">
28+
<HintPath>packages\nanoFramework.System.Device.I2c.1.0.1-preview.33\lib\System.Device.I2c.dll</HintPath>
29+
<Private>True</Private>
30+
</Reference>
31+
<Reference Include="UnitsNet.Illuminance, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
32+
<HintPath>packages\UnitsNet.nanoFramework.Illuminance.4.92.1\lib\UnitsNet.Illuminance.dll</HintPath>
33+
<Private>True</Private>
34+
<SpecificVersion>True</SpecificVersion>
35+
</Reference>
36+
</ItemGroup>
37+
<ItemGroup>
38+
<None Include="packages.config" />
39+
<Compile Include="Bh1750fvi.cs" />
40+
<Compile Include="Command.cs" />
41+
<Compile Include="I2cAddress.cs" />
42+
<Compile Include="MeasuringMode.cs" />
43+
</ItemGroup>
44+
<ItemGroup>
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
<None Include="*.md" />
47+
</ItemGroup>
48+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
49+
<Import Project="..\..\src\System.Device.Model\System.Device.Model.projitems" Label="Shared" />
50+
<Import Project="..\..\src\BinaryPrimitives\BinaryPrimitives.projitems" Label="Shared" />
51+
<ProjectExtensions>
52+
<ProjectCapabilities>
53+
<ProjectConfigurationsDeclaredAsItems />
54+
</ProjectCapabilities>
55+
</ProjectExtensions>
56+
<Import Project="packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" />
57+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
58+
<PropertyGroup>
59+
<ErrorText> This project references NuGet package(s) that are missing on this computer.Enable NuGet Package Restore to download them.For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
60+
</PropertyGroup>
61+
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets'))" />
62+
</Target>
63+
</Project>

devices/Bh1750fvi/Bh1750fvi.nuspec

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
3+
<metadata>
4+
<id>nanoFramework.Iot.Device.Bh1750fvi</id>
5+
<version>$version$</version>
6+
<title>nanoFramework.Iot.Device.Bh1750fvi</title>
7+
<authors>nanoFramework project contributors</authors>
8+
<owners>nanoFramework project contributors,dotnetfoundation</owners>
9+
<requireLicenseAcceptance>false</requireLicenseAcceptance>
10+
<license type="file">LICENSE.md</license>
11+
<releaseNotes>
12+
</releaseNotes>
13+
<developmentDependency>false</developmentDependency>
14+
<projectUrl>https://github.com/nanoframework/nanoFramework.IoT.Device</projectUrl>
15+
<iconUrl>https://secure.gravatar.com/avatar/97d0e092247f0716db6d4b47b7d1d1ad</iconUrl>
16+
<icon>images\nf-logo.png</icon>
17+
<repository type="git" url="https://github.com/nanoframework/nanoFramework.IoT.Device" commit="$commit$" />
18+
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
19+
<description>This package includes the .NET IoT Core binding Iot.Device.Bh1750fvi for .NET nanoFramework C# projects.</description>
20+
<summary>Iot.Device.Bh1750fvi assembly for .NET nanoFramework C# projects</summary>
21+
<tags>nanoFramework C# csharp netmf netnf Iot.Device.Bh1750fvi</tags>
22+
<dependencies>
23+
<dependency id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" />
24+
<dependency id="nanoFramework.System.Device.I2c" version="1.0.1-preview.33" />
25+
<dependency id="UnitsNet.nanoFramework.Illuminance" version="4.92.0" />
26+
</dependencies>
27+
</metadata>
28+
<files>
29+
<file src="bin\Release\Iot.Device.Bh1750fvi.dll" target="lib\Iot.Device.Bh1750fvi.dll" />
30+
<file src="bin\Release\Iot.Device.Bh1750fvi.pdb" target="lib\Iot.Device.Bh1750fvi.pdb" />
31+
<file src="bin\Release\Iot.Device.Bh1750fvi.pdbx" target="lib\Iot.Device.Bh1750fvi.pdbx" />
32+
<file src="bin\Release\Iot.Device.Bh1750fvi.pe" target="lib\Iot.Device.Bh1750fvi.pe" />
33+
<file src="bin\Release\Iot.Device.Bh1750fvi.xml" target="lib\Iot.Device.Bh1750fvi.xml" />
34+
<file src="readme.md" target="" />
35+
<file src="..\..\assets\nf-logo.png" target="images" />
36+
<file src="..\..\LICENSE.md" target="" />
37+
</files>
38+
</package>

devices/Bh1750fvi/Bh1750fvi.sln

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 16
3+
VisualStudioVersion = 16.0.30413.136
4+
MinimumVisualStudioVersion = 10.0.40219.1
5+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Bh1750fvi", "Bh1750fvi.nfproj", "{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}"
6+
EndProject
7+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Bh1750fvi.Samples", "samples\Bh1750fvi.Samples.nfproj", "{15CE15B2-AE83-4670-90A2-E36C3A9EFB03}"
8+
EndProject
9+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared Project", "Shared Project", "{DDFFEC3A-0FFB-4CF6-BB2E-30C09AFCDB1F}"
10+
EndProject
11+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "System.Device.Model", "..\..\src\System.Device.Model\System.Device.Model.shproj", "{23325B14-3651-4879-9697-9846FF123FEB}"
12+
EndProject
13+
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "BinaryPrimitives", "..\..\src\BinaryPrimitives\BinaryPrimitives.shproj", "{3F28B003-6318-4E21-A9B6-6C0DBD0BDBFD}"
14+
EndProject
15+
Global
16+
GlobalSection(SharedMSBuildProjectFiles) = preSolution
17+
..\..\src\System.Device.Model\System.Device.Model.projitems*{23325b14-3651-4879-9697-9846ff123feb}*SharedItemsImports = 13
18+
..\..\src\BinaryPrimitives\BinaryPrimitives.projitems*{3f28b003-6318-4e21-a9b6-6c0dbd0bdbfd}*SharedItemsImports = 13
19+
EndGlobalSection
20+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
21+
Debug|Any CPU = Debug|Any CPU
22+
Release|Any CPU = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
25+
{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
28+
{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}.Release|Any CPU.Build.0 = Release|Any CPU
30+
{2A6DCC87-4291-4830-8CDC-208C5BEA65FC}.Release|Any CPU.Deploy.0 = Release|Any CPU
31+
{15CE15B2-AE83-4670-90A2-E36C3A9EFB03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{15CE15B2-AE83-4670-90A2-E36C3A9EFB03}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{15CE15B2-AE83-4670-90A2-E36C3A9EFB03}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
34+
{15CE15B2-AE83-4670-90A2-E36C3A9EFB03}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{15CE15B2-AE83-4670-90A2-E36C3A9EFB03}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{15CE15B2-AE83-4670-90A2-E36C3A9EFB03}.Release|Any CPU.Deploy.0 = Release|Any CPU
37+
EndGlobalSection
38+
GlobalSection(SolutionProperties) = preSolution
39+
HideSolutionNode = FALSE
40+
EndGlobalSection
41+
GlobalSection(NestedProjects) = preSolution
42+
{23325B14-3651-4879-9697-9846FF123FEB} = {DDFFEC3A-0FFB-4CF6-BB2E-30C09AFCDB1F}
43+
{3F28B003-6318-4E21-A9B6-6C0DBD0BDBFD} = {DDFFEC3A-0FFB-4CF6-BB2E-30C09AFCDB1F}
44+
EndGlobalSection
45+
GlobalSection(ExtensibilityGlobals) = postSolution
46+
SolutionGuid = {8ECBE2FD-796C-4D37-B27C-27AB2EE0618B}
47+
EndGlobalSection
48+
EndGlobal

devices/Bh1750fvi/Command.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Bh1750fvi
5+
{
6+
internal enum Command : byte
7+
{
8+
PowerDown = 0b_0000_0000,
9+
PowerOn = 0b_0000_0001,
10+
Reset = 0b_0000_0111,
11+
MeasurementTimeHigh = 0b_0100_0000,
12+
MeasurementTimeLow = 0b_0110_0000,
13+
}
14+
}

devices/Bh1750fvi/I2cAddress.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Bh1750fvi
5+
{
6+
/// <summary>
7+
/// BH1750FVI I2C Address
8+
/// </summary>
9+
public enum I2cAddress : byte
10+
{
11+
/// <summary>
12+
/// ADD Pin connect to high power level
13+
/// </summary>
14+
AddPinHigh = 0x5C,
15+
16+
/// <summary>
17+
/// ADD Pin connect to low power level
18+
/// </summary>
19+
AddPinLow = 0x23
20+
}
21+
}

devices/Bh1750fvi/MeasuringMode.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Bh1750fvi
5+
{
6+
/// <summary>
7+
/// The measuring mode of BH1750FVI
8+
/// </summary>
9+
public enum MeasuringMode : byte
10+
{
11+
// Details in the datasheet P5
12+
13+
/// <summary>
14+
/// Start measurement at 1lx resolution
15+
/// Measurement Time is typically 120ms.
16+
/// </summary>
17+
ContinuouslyHighResolutionMode = 0b_0001_0000,
18+
19+
/// <summary>
20+
/// Start measurement at 0.5lx resolution
21+
/// Measurement Time is typically 120ms.
22+
/// </summary>
23+
ContinuouslyHighResolutionMode2 = 0b_0001_0001,
24+
25+
/// <summary>
26+
/// Start measurement at 4lx resolution
27+
/// Measurement Time is typically 16ms.
28+
/// </summary>
29+
ContinuouslyLowResolutionMode = 0b_0001_0011,
30+
31+
/// <summary>
32+
/// Start measurement at 1lx resolution once
33+
/// Measurement Time is typically 120ms.
34+
/// It is automatically set to Power Down mode after measurement.
35+
/// </summary>
36+
OneTimeHighResolutionMode = 0b_0010_0000,
37+
38+
/// <summary>
39+
/// Start measurement at 0.5lx resolution once
40+
/// Measurement Time is typically 120ms.
41+
/// It is automatically set to Power Down mode after measurement.
42+
/// </summary>
43+
OneTimeHighResolutionMode2 = 0b_0010_0001,
44+
45+
/// <summary>
46+
/// Start measurement at 4lx resolution once
47+
/// Measurement Time is typically 16ms.
48+
/// It is automatically set to Power Down mode after measurement.
49+
/// </summary>
50+
OneTimeLowResolutionMode = 0b_0010_0011
51+
}
52+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
[assembly: AssemblyTitle("Iot.Device.Bh1750fvi")]
6+
[assembly: AssemblyCompany("nanoFramework Contributors")]
7+
[assembly: AssemblyCopyright("Copyright(c).NET Foundation and Contributors")]
8+
9+
[assembly: ComVisible(false)]
10+

devices/Bh1750fvi/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# BH1750FVI - Ambient Light Sensor
2+
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.
3+
4+
## Sensor Image
5+
![](sensor.jpg)
6+
7+
## Usage
8+
```C#
9+
I2cConnectionSettings settings = new I2cConnectionSettings(busId: 1, (int)I2cAddress.AddPinLow);
10+
I2cDevice device = I2cDevice.Create(settings);
11+
12+
using (Bh1750fvi sensor = new Bh1750fvi(device))
13+
{
14+
// read illuminance(Lux)
15+
double illuminance = sensor.Illuminance;
16+
}
17+
18+
```
19+
20+
## References
21+
https://cdn.datasheetspdf.com/pdf-down/B/H/1/BH1750FVI_Rohm.pdf

devices/Bh1750fvi/category.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
light

0 commit comments

Comments
 (0)