-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathMax44009.cs
93 lines (77 loc) · 2.94 KB
/
Max44009.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.I2c;
using System.Device.Model;
namespace Iot.Device.Max44009
{
/// <summary>
/// Ambient Light Sensor MAX44009
/// </summary>
[Interface("Ambient Light Sensor MAX44009")]
public class Max44009 : IDisposable
{
private I2cDevice _i2cDevice;
/// <summary>
/// MAX44009 Default I2C Address
/// </summary>
public const byte DefaultI2cAddress = 0x4A;
/// <summary>
/// MAX44009 Illuminance (Lux)
/// </summary>
[Telemetry]
public double Illuminance => GetIlluminance();
/// <summary>
/// Creates a new instance of the MAX44009, MAX44009 working mode is default. (Consume lowest power)
/// </summary>
/// <param name="i2cDevice">The I2C device used for communication.</param>
public Max44009(I2cDevice i2cDevice)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
// Details in the Datasheet P8
SpanByte writeBuff = new byte[2]
{
(byte)Register.MAX_CONFIG, 0b_0000_0000
};
_i2cDevice.Write(writeBuff);
}
/// <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
SpanByte writeBuff = new byte[2]
{
(byte)Register.MAX_CONFIG, (byte)(0b_1100_0000 | (byte)integrationTime)
};
_i2cDevice.Write(writeBuff);
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_i2cDevice?.Dispose();
_i2cDevice = null!;
}
/// <summary>
/// Get MAX44009 Illuminance (Lux)
/// </summary>
/// <returns>Illuminance (Lux)</returns>
private double GetIlluminance()
{
SpanByte readBuff = new byte[2];
_i2cDevice.WriteByte((byte)Register.MAX_LUX_HIGH);
_i2cDevice.Read(readBuff);
// Details in the Datasheet P9-10
byte exponent = (byte)((readBuff[0] & 0b_1111_0000) >> 4);
byte mantissa = (byte)(((readBuff[0] & 0b_0000_1111) << 4) | (readBuff[1]) & 0b_0000_1111);
double lux = Math.Pow(2.0, exponent) * mantissa * 0.045;
return Math.Round(lux * 1000) / 1000.0;
}
}
}