-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathMlx90614.cs
77 lines (66 loc) · 2.38 KB
/
Mlx90614.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
// 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.Buffers.Binary;
using System.Device.I2c;
using System.Device.Model;
using UnitsNet;
namespace Iot.Device.Mlx90614
{
/// <summary>
/// Infra Red Thermometer MLX90614
/// </summary>
[Interface("Infra Red Thermometer MLX90614")]
public sealed class Mlx90614 : IDisposable
{
private I2cDevice _i2cDevice;
/// <summary>
/// MLX90614 Default I2C Address
/// </summary>
public const byte DefaultI2cAddress = 0x5A;
/// <summary>
/// Creates a new instance of the MLX90614
/// </summary>
/// <param name="i2cDevice">The I2C device used for communication.</param>
public Mlx90614(I2cDevice i2cDevice)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
}
/// <summary>
/// Read ambient temperature from MLX90614
/// </summary>
/// <returns>Temperature</returns>
[Telemetry("AmbientTemperature")]
public Temperature ReadAmbientTemperature() => Temperature.FromDegreesCelsius(ReadTemperature((byte)Register.MLX_AMBIENT_TEMP));
/// <summary>
/// Read surface temperature of object from MLX90614
/// </summary>
/// <returns>Temperature</returns>
[Telemetry("ObjectTemperature")]
public Temperature ReadObjectTemperature() => Temperature.FromDegreesCelsius(ReadTemperature((byte)Register.MLX_OBJECT1_TEMP));
/// <summary>
/// Read temperature form specified register
/// </summary>
/// <param name="register">Register</param>
/// <returns>Temperature in celsius</returns>
private double ReadTemperature(byte register)
{
SpanByte writeBuffer = new byte[]
{
register
};
SpanByte readBuffer = new byte[2];
_i2cDevice.WriteRead(writeBuffer, readBuffer);
double temp = BinaryPrimitives.ReadInt16LittleEndian(readBuffer) * 0.02 - 273.15;
return Math.Round(temp * 100) / 100;
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_i2cDevice?.Dispose();
_i2cDevice = null!;
}
}
}