- Notifications
You must be signed in to change notification settings - Fork135
Htu21df driver#94
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
ChrisDodgeRR wants to merge11 commits intoraspberry-sharp:masterChoose a base branch fromJTrotta:HTU21DF-Driver
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
Show all changes
11 commits Select commitHold shift + click to select a range
f0a9983 RPi3
JTrotta94233f2 Added try catch
JTrotta88fb1ab Added support for RPi3
JTrotta37f1e2a Adding RP3/CM3 support
JTrotta4951cc6 Addeb to nuget
JTrotta27de18d Updated file version
JTrotta1b53c71 Refactoring
JTrotta527be4d I2c Repeated Start now works
JTrotta2aa9e70 Some updates
JTrotta6eb0a0f Added HTU21DF driver
ChrisDodgeRR6a32c38 Uses V3.1.2 of Raspberry.System.
ChrisDodgeRRFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.gitignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -14,6 +14,7 @@ TestResults | ||
| **.suo | ||
| **.sdf | ||
| *.sln.docstates | ||
| .vs/* | ||
| # resharper | ||
| /_[Rr]e[Ss]harper.* | ||
5 changes: 4 additions & 1 deletionREADME.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion...mperature/Dht/InvalidChecksumException.cs → ...IO.Components/InvalidChecksumException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
63 changes: 51 additions & 12 deletionsRaspberry.IO.Components/Raspberry.IO.Components.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
183 changes: 183 additions & 0 deletionsRaspberry.IO.Components/Sensors/Humidity/Htu21df/Htu21dfConnection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using Raspberry.IO.Components.Sensors.Temperature.Dht; | ||
| using Raspberry.IO.InterIntegratedCircuit; | ||
| namespace Raspberry.IO.Components.Sensors.Humidity.Htu21df | ||
| { | ||
| /// <summary> | ||
| /// Connection to the HTU21D-F Adafruit humidity and temperature breakout board. | ||
| /// </summary> | ||
| /// <remarks>See <see cref="https://www.adafruit.com/datasheets/1899_HTU21D.pdf"/> for more information.</remarks>/// | ||
| public class Htu21dfConnection | ||
| { | ||
| #region Helpers | ||
| public enum I2cDefs : byte | ||
| { | ||
| HTU21DF_I2CADDR = 0x40, | ||
| HTU21DF_READTEMP = 0xE3, | ||
| HTU21DF_READHUM = 0xE5, | ||
| HTU21DF_READTEMP_NOHOLDMASTER = 0xF3, | ||
| HTU21DF_READHUM_NOHOLDMASTER = 0xF5, | ||
| HTU21DF_WRITEREG = 0xE6, | ||
| HTU21DF_READREG = 0xE7, | ||
| HTU21DF_RESET = 0xFE, | ||
| } | ||
| public enum I2cReadMode | ||
| { | ||
| /// <summary> | ||
| /// Default. In this mode, the SCK line is blocked during the measurement process. When measurement is complete | ||
| /// the sensor indicates by releasing SCK and the read can continue. | ||
| /// </summary> | ||
| HoldMaster, | ||
| /// <summary> | ||
| /// The sensor does not hold SCK so other I2C comms can take place on the bus. The MCU (R-Pi) has to then | ||
| /// poll for the data. NOT CURRENTLY SUPPORTED. | ||
| /// </summary> | ||
| NoHoldMaster, | ||
| } | ||
| #endregion | ||
| #region Fields | ||
| private readonly I2cDeviceConnection connection; | ||
| #endregion | ||
| #region Instance Management | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="Htu21dfConnection"/> class. | ||
| /// </summary> | ||
| /// <param name="connection">The connection.</param> | ||
| public Htu21dfConnection( I2cDeviceConnection connection ) | ||
| { | ||
| this.connection = connection; | ||
| ReadMode = I2cReadMode.HoldMaster; | ||
| } | ||
| #endregion | ||
| #region Methods | ||
| public I2cReadMode ReadMode { get; set; } | ||
| /// <summary> | ||
| /// Init's the sensor and checks its status is OK. | ||
| /// </summary> | ||
| public void Begin() | ||
| { | ||
| Reset(); | ||
| connection.WriteByte( (byte) I2cDefs.HTU21DF_READREG ); | ||
| var status = connection.ReadByte(); | ||
| if ( status != 0x02 ) | ||
| { | ||
| throw new Exception( $"Status following reset should be 0x02. Have {status}" ); | ||
| } | ||
| } | ||
| /// <summary> | ||
| /// Resets the sensor by power cycling. Takes about 15ms. | ||
| /// </summary> | ||
| private void Reset() | ||
| { | ||
| connection.WriteByte((byte)I2cDefs.HTU21DF_RESET); | ||
| Thread.Sleep(15); | ||
| } | ||
| /// <summary> | ||
| /// Reads the temperature value from the sensor. | ||
| /// </summary> | ||
| /// <returns>Temperature in degrees Centigrade.</returns> | ||
| public double ReadTemperature() | ||
| { | ||
| if ( ReadMode == I2cReadMode.NoHoldMaster ) | ||
| { | ||
| throw new NotSupportedException( "No-Hold-Master read mode not supported." ); | ||
| } | ||
| connection.WriteByte( (byte) I2cDefs.HTU21DF_READTEMP ); | ||
| // Add delay between request and actual read | ||
| Thread.Sleep( 50 ); | ||
| // Read 3 bytes; 2 bytes temp data and one byte checksum. | ||
| var readBytes = connection.Read( 3 ); | ||
| CheckCrc( readBytes ); | ||
| // Get data value from bytes 0 and 1. | ||
| var tVal = ( readBytes[0] << 8 ) + readBytes[1]; | ||
| // Compute temp using formula from datasheet. | ||
| return ( ( tVal * 175.72 ) / 65536 ) - 46.85; | ||
| } | ||
| /// <summary> | ||
| /// Reads the humidity value from the sensor. | ||
| /// </summary> | ||
| /// <returns>The relative humidity value as a percentage.</returns> | ||
| public double ReadHumidity() | ||
| { | ||
| if (ReadMode == I2cReadMode.NoHoldMaster) | ||
| { | ||
| throw new NotSupportedException("No-Hold-Master read mode not supported."); | ||
| } | ||
| connection.WriteByte((byte)I2cDefs.HTU21DF_READHUM); | ||
| // Add delay between request and actual read | ||
| Thread.Sleep( 50 ); | ||
| // Read 3 bytes; 2 bytes temp data and one byte checksum. | ||
| var readBytes = connection.Read( 3 ); | ||
| CheckCrc( readBytes ); | ||
| // Get data value from bytes 0 and 1. | ||
| var hVal = ( readBytes[0] << 8 ) + readBytes[1]; | ||
| // Compute temp using formula from datasheet. | ||
| return ( ( (double)hVal * 125 ) / 65536 ) - 6; | ||
| } | ||
| /// <summary> | ||
| /// Calculate the CRC checksum. The result should be zero. | ||
| /// Thanks to the IoT-Playground - https://github.com/iot-playground. | ||
| /// </summary> | ||
| public static void CheckCrc(byte[] readData) | ||
| { | ||
| // Test cases from datasheet: | ||
| // sensor value = 0xDC, checkvalue is 0x79. readData = 0x00DC79. | ||
| // message = 0x683A, checkvalue is 0x7C. readData = 0x683A7C | ||
| // message = 0x4E85, checkvalue is 0x6B. readData = 0x45856B | ||
| // Create a 32-bit value with bytes of {0, val-msb, val-lsb, checksum} | ||
| var remainder = (uint) ( ( readData[0] << 16 ) + ( readData[1] << 8 ) + readData[2] ); | ||
| // This is the x^8 + x^5 + x^4 + 1 polynomial (100110001), but shifted up to start at the left-hand | ||
| // side of the 24-bit value. | ||
| uint divisor = 0x988000; | ||
| for ( var i = 0; i < 16; i++ ) | ||
| { | ||
| if ( (remainder & (1<<(23-i))) != 0 ) | ||
| { | ||
| remainder ^= divisor; | ||
| } | ||
| divisor >>= 1; | ||
| } | ||
| if ( remainder != 0 ) | ||
| { | ||
| throw new InvalidChecksumException( 0x00, remainder ); | ||
| } | ||
| } | ||
| #endregion | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletionRaspberry.IO.Components/app.config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletionsRaspberry.IO.Components/packages.config
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <packages> | ||
| <package id="Common.Logging" version="3.4.1" targetFramework="net40" /> | ||
| <package id="Common.Logging.Core" version="3.4.1" targetFramework="net40" /> | ||
| <package id="Raspberry.System3" version="3.1.1" targetFramework="net40" /> | ||
| <package id="UnitsNet" version="3.77.0" targetFramework="net40" /> | ||
| </packages> |
32 changes: 0 additions & 32 deletionsRaspberry.IO.GeneralPurpose.nuspec
This file was deleted.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.