-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathAmg88xxUtils.cs
56 lines (50 loc) · 2.53 KB
/
Amg88xxUtils.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
// 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 UnitsNet;
namespace Iot.Device.Amg88xx
{
/// <summary>
/// This class contains utilities for working with AMG88xx devices.
/// </summary>
internal static class Amg88xxUtils
{
/// <summary>
/// Temperature resolution of a pixel (in degrees celsius).
/// </summary>
private const double PixelTemperatureResolution = 0.25;
/// <summary>
/// Converts a temperature from 12-bit (in 2 bytes) two's complements representation into a floating-point reading.
/// </summary>
/// <param name="twosComplement">Reading in two's complement little endian representation.</param>
/// <returns>Temperature reading.</returns>
public static Temperature ConvertToTemperature(SpanByte twosComplement) => ConvertToTemperature(twosComplement[0], twosComplement[1]);
/// <summary>
/// Converts a temperature from two's complements representation into a floating-point reading.
/// </summary>
/// <param name="tl">Reading low byte.</param>
/// <param name="th">Reading high byte.</param>
/// <returns>Temperature reading.</returns>
public static Temperature ConvertToTemperature(byte tl, byte th)
{
// The temperature of each pixel is encoded as a 12 bit two's complement value.
int reading = (th & 0x7) << 8 | tl;
reading = th >> 3 == 0 ? reading : -(~(reading - 1) & 0x7ff);
// The LSB is equivalent to 0.25℃
return Temperature.FromDegreesCelsius(reading * PixelTemperatureResolution);
}
/// <summary>
/// Converts a temperature from floating-point representation into a two's complement representation (low- and high-byte).
/// </summary>
/// <param name="temperature">Temperature to convert from.</param>
/// <returns>Two's complement representation.</returns>
public static TemperatureLowHighBytes ConvertFromTemperature(Temperature temperature)
{
// The temperature of each pixel is encoded as a 12 bit value in two's complement form.
// The LSB is equivalent to 0.25℃
var t = (int)(temperature.DegreesCelsius / PixelTemperatureResolution);
t = temperature.DegreesCelsius < 0 ? ~(0x1000 - t) + 1 : t;
return new TemperatureLowHighBytes((byte)(t & 0xff), (byte)((t >> 8) & 0x0f));
}
}
}