From ff2debfae4f46ba97c6b6de8d6698fc9554fc491 Mon Sep 17 00:00:00 2001 From: Tanson Date: Sat, 24 Dec 2016 02:32:23 +0800 Subject: [PATCH] add BH1750 Sensor --- .../Sensors/Light/BH1750Connection.cs | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Raspberry.IO.Components/Sensors/Light/BH1750Connection.cs diff --git a/Raspberry.IO.Components/Sensors/Light/BH1750Connection.cs b/Raspberry.IO.Components/Sensors/Light/BH1750Connection.cs new file mode 100644 index 0000000..d840259 --- /dev/null +++ b/Raspberry.IO.Components/Sensors/Light/BH1750Connection.cs @@ -0,0 +1,53 @@ +using Raspberry.IO.InterIntegratedCircuit; +using Raspberry.Timers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Raspberry.IO.Components.Sensors.Light +{ + namespace RPI.Sensor.Sensors.Light + { + public class BH1750Connection + { + public I2cDeviceConnection Connection { get; set; } + public BH1750Connection(I2cDeviceConnection connection) + { + Connection = connection; + } + + public void SetOff() + { + Connection.Write(0x00); + } + public void SetOn() + { + Connection.Write(0x01); + } + public void Reset() + { + Connection.Write(0x07); + } + + public double GetData() + { + Connection.Write(0x10); + HighResolutionTimer.Sleep(TimeSpanUtility.FromMicroseconds(150 * 1000)); + byte[] readBuf = Connection.Read(2); + + var valf = readBuf[0] << 8; + valf |= readBuf[1]; + return valf / 1.2 * (69 / 69) / 1; + + // var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2; + // return valf; + + // return Math.Round(valf / (2 * 1.2), 2); + + } + + } + } + +}