/
Mcp9808.cs
107 lines (91 loc) · 3.26 KB
/
Mcp9808.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using Meadow.Hardware;
using Meadow.Peripherals.Sensors;
using System.Threading.Tasks;
namespace Meadow.Foundation.Sensors.Temperature
{
/// <summary>
/// Represents a Mcp9808 temperature sensor
/// </summary>
public partial class Mcp9808 : ByteCommsSensorBase<Units.Temperature>,
ITemperatureSensor, II2cPeripheral
{
/// <summary>
/// The default I2C address for the peripheral
/// </summary>
public byte DefaultI2cAddress => (byte)Addresses.Default;
/// <summary>
/// The temperature, in degrees celsius (°C), from the last reading.
/// </summary>
public Units.Temperature? Temperature { get; protected set; }
/// <summary>
/// Creates a new Mcp9808 object
/// </summary>
/// <param name="i2CBus">The I2C bus</param>
/// <param name="address">The I2C address</param>
public Mcp9808(II2cBus i2CBus, byte address = (byte)Addresses.Default)
: base(i2CBus, address, readBufferSize: 8, writeBufferSize: 8)
{
BusComms?.WriteRegister(Registers.REG_CONFIG, (ushort)0x0);
}
/// <summary>
/// Wake the device if it's in sleep state
/// </summary>
public void Wake()
{
ushort config = BusComms?.ReadRegisterAsUShort(Registers.REG_CONFIG, ByteOrder.BigEndian) ?? 0;
config = (ushort)(config & (~Registers.CONFIG_SHUTDOWN));
BusComms?.WriteRegister(Registers.REG_CONFIG, config);
}
/// <summary>
/// Set the device into a low power sleep state
/// </summary>
public void Sleep()
{
ushort config = BusComms?.ReadRegisterAsUShort(Registers.REG_CONFIG, ByteOrder.BigEndian) ?? 0;
BusComms?.WriteRegister(Registers.REG_CONFIG, (ushort)(config | Registers.CONFIG_SHUTDOWN));
}
/// <summary>
/// Read the device ID
/// </summary>
public ushort GetDeviceId()
{
return BusComms?.ReadRegisterAsUShort(Registers.DEVICE_ID, ByteOrder.BigEndian) ?? 0;
}
/// <summary>
/// Read the manufacture ID
/// </summary>
public ushort GetManufactureId()
{
return BusComms?.ReadRegisterAsUShort(Registers.MANUFACTURER_ID, ByteOrder.BigEndian) ?? 0;
}
/// <summary>
/// Read resolution
/// </summary>
public byte GetResolution()
{
return BusComms?.ReadRegister(Registers.RESOLUTION) ?? 0;
}
/// <summary>
/// Set resolution
/// </summary>
public void SetResolution(byte resolution)
{
BusComms?.WriteRegister(Registers.RESOLUTION, resolution);
}
/// <summary>
/// Reads data from the sensor
/// </summary>
/// <returns>The latest sensor reading</returns>
protected override Task<Units.Temperature> ReadSensor()
{
ushort value = BusComms?.ReadRegisterAsUShort(Registers.AMBIENT_TEMP, ByteOrder.BigEndian) ?? 0;
double temp = value & 0x0FFF;
temp /= 16.0;
if ((value & 0x1000) != 0)
{
temp -= 256;
}
return Task.FromResult(new Units.Temperature(temp, Units.Temperature.UnitType.Celsius));
}
}
}