Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Htu31d and refactor Htu21d to Htux1d #305

Merged
merged 6 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ namespace Meadow.Foundation.Sensors.Atmospheric
{
public partial class Htu21d
{
/// <summary>
/// Valid addresses for the sensor.
/// </summary>
public enum Addresses : byte
{
/// <summary>
/// Bus address 0x40
/// </summary>
Address_0x40 = 0x40,
/// <summary>
/// Default bus address
/// </summary>
Default = Address_0x40
}

private const byte SOFT_RESET = 0xFE;

private const byte TEMPERATURE_MEASURE_NOHOLD = 0xF3;
Expand All @@ -43,10 +28,5 @@ public enum SensorResolution : byte
TEMP13_HUM10 = 0x80,
TEMP11_HUM11 = 0x81,
}

private enum Register : byte
{

}
}
}

This file was deleted.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Meadow.Foundation.Sensors.Atmospheric
{
public partial class Htu21d
{
enum Registers : byte
{
SOFT_RESET = 0xFE,

TEMPERATURE_MEASURE_NOHOLD = 0xF3,
HUMDITY_MEASURE_NOHOLD = 0xF5,

TEMPERATURE_MEASURE_HOLD = 0xE3,
HUMDITY_MEASURE_HOLD = 0xE5,
TEMPERATURE_MEASURE_PREVIOUS = 0xE0,

WRITE_USER_REGISTER = 0xE6,
READ_USER_REGISTER = 0xE7,
READ_HEATER_REGISTER = 0x11,
WRITE_HEATER_REGISTER = 0x51,
}

/// <summary>
/// Resolution of sensor data
/// </summary>
public enum SensorResolution : byte
{
/// <summary>
/// 14 bit temperature, 12 bit humidity
/// </summary>
TEMP14_HUM12 = 0x00,
/// <summary>
/// 12 bit temperature, 8 bit humidity
/// </summary>
TEMP12_HUM8 = 0x01,
/// <summary>
/// 13 bit temperature, 10 bit humidity
/// </summary>
TEMP13_HUM10 = 0x80,
/// <summary>
/// 11 bit temperature, 11 bit humidity
/// </summary>
TEMP11_HUM11 = 0x81,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Meadow.Hardware;
using Meadow.Units;
using System;
using System.Threading;
using System.Threading.Tasks;
using HU = Meadow.Units.RelativeHumidity.UnitType;

namespace Meadow.Foundation.Sensors.Atmospheric
{
/// <summary>
/// Provide access to the Htu21d(f)
/// temperature and humidity sensors
/// </summary>
public partial class Htu21d : Htux1dBase
{
/// <summary>
/// Firmware revision of the sensor.
/// </summary>
public byte FirmwareRevision { get; private set; }

/// <summary>
/// Create a new Htu21d temperature and humidity sensor
/// </summary>
/// <param name="address">Sensor address (default to 0x40)</param>
/// <param name="i2cBus">I2CBus (default to 100 KHz)</param>
/// <param name="updateInterval">Update interval, defaults to 1 sec if null</param>
public Htu21d(II2cBus i2cBus, byte address = (byte)Addresses.Default, TimeSpan? updateInterval = null)
: base(i2cBus, address, updateInterval)
{
Initialize();
}

/// <summary>
/// Initialize HTU21D
/// </summary>
protected void Initialize ()
{
Peripheral?.Write((byte)Registers.SOFT_RESET);

Thread.Sleep(100);

SetResolution(SensorResolution.TEMP11_HUM11);
}

protected override async Task<(Units.Temperature? Temperature, RelativeHumidity? Humidity)> ReadSensor()
{
(Units.Temperature Temperature, RelativeHumidity Humidity) conditions;

return await Task.Run(() =>
{
// humidity
Peripheral?.Write((byte)Registers.HUMDITY_MEASURE_NOHOLD);
Thread.Sleep(20); // Maximum conversion time is 12ms (page 5 of the datasheet)

Peripheral?.Read(ReadBuffer.Span[0..2]);// 2 data bytes plus a checksum (we ignore the checksum here)
var humidityReading = (ushort)((ReadBuffer.Span[0] << 8) + ReadBuffer.Span[1]);
var humidity = (125 * (float)humidityReading / 65536) - 6;
humidity = Math.Clamp(humidity, 0, 100);
conditions.Humidity = new RelativeHumidity(humidity, HU.Percent);

// temperature
Peripheral?.Write((byte)Registers.TEMPERATURE_MEASURE_NOHOLD);
Thread.Sleep(20); // Maximum conversion time is 12ms (page 5 of the datasheet)

Peripheral?.Read(ReadBuffer.Span[0..2]);// 2 data bytes plus a checksum (we ignore the checksum here)
var temperatureReading = (short)((ReadBuffer.Span[0] << 8) + ReadBuffer.Span[1]);
conditions.Temperature = new Units.Temperature((float)(((175.72 * temperatureReading) / 65536) - 46.85), Units.Temperature.UnitType.Celsius);

return conditions;
});
}

/// <summary>
/// Turn the heater on or off
/// </summary>
/// <param name="heaterOn">Heater status, true = turn heater on, false = turn heater off.</param>
public void Heater(bool heaterOn)
{
if (Peripheral == null) return;

var register = Peripheral.ReadRegister((byte)Registers.READ_HEATER_REGISTER);
register &= 0xfd;

if (heaterOn)
{
register |= 0x02;
}
Peripheral.WriteRegister((byte)Registers.WRITE_HEATER_REGISTER, register);
}

//Set sensor resolution
/*******************************************************************************************/
//Sets the sensor resolution to one of four levels
//Page 12:
// 0/0 = 12bit RH, 14bit Temp
// 0/1 = 8bit RH, 12bit Temp
// 1/0 = 10bit RH, 13bit Temp
// 1/1 = 11bit RH, 11bit Temp
//Power on default is 0/0
void SetResolution(SensorResolution resolution)
{
if (Peripheral == null) return;

var register = Peripheral.ReadRegister((byte)Registers.READ_USER_REGISTER);

var res = (byte)resolution;

register &= 0x73; //Turn off the resolution bits
res &= 0x81; //Turn off all other bits but resolution bits
register |= res; //Mask in the requested resolution bits

//Request a write to user register
Peripheral.WriteRegister((byte)Registers.WRITE_USER_REGISTER, register); //Write the new resolution bits
}
}
}
Loading