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

RB3/CM3 Support to all libraries, nuget added #93

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TestResults
**.suo
**.sdf
*.sln.docstates
.vs/*

# resharper
/_[Rr]e[Ss]harper.*
Expand Down
Binary file modified Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Raspberry# IO
This project is discontinued, try https://github.com/JTrotta/RaspberrySharp

Raspberry# IO
=============

See the **[Raspberry\# IO Wiki](https://github.com/raspberry-sharp/raspberry-sharp-io/wiki)** for full documentation and samples.
Expand All @@ -23,7 +25,7 @@ Features

### Raspberry.IO.GeneralPurpose
Raspberry.IO.GeneralPurpose provides a convenient way to use Raspberry Pi GPIO pins, while using .NET concepts, syntax and case.
You can easily add a reference to it in your Visual Studio projects using the **[Raspberry.IO.GeneralPurpose Nuget](https://www.nuget.org/packages/Raspberry.IO.GeneralPurpose)**.
You can easily add a reference to it in your Visual Studio projects using the **[Raspberry.IO.GeneralPurpose Nuget](https://www.nuget.org/packages/Raspberry.IO.GeneralPurpose3)**.

It currently support the following features:

Expand All @@ -43,12 +45,15 @@ High-level:
+ **High-level behaviors** for output pins, including *blink*, *pattern* and *chaser*

### Raspberry.IO.SerialPeripheralInterface
You can easily add a reference to it in your Visual Studio projects using the **[Raspberry.IO.Raspberry.IO.SerialPeripheralInterface Nuget](https://www.nuget.org/packages/Raspberry.IO.SerialPeripheralInterface3)**.


+ Preliminary support for SPI through Raspberry.IO.SerialPeripheralInterface assembly
+ Includes SPI samples for MCP3008 ADC and MCP4822 DAC
+ Includes support for Linux's kernel SPI module driver spi-bcm2708 (/dev/spidev0.0)

### Raspberry.IO.InterIntegratedCircuit
You can easily add a reference to it in your Visual Studio projects using the **[Raspberry.IO.Raspberry.IO.InterIntegratedCircuit Nuget](https://www.nuget.org/packages/Raspberry.IO.InterIntegratedCircuit3)**.

+ Preliminary support for I2C through Raspberry.IO.InterIntegratedCircuit assembly
+ Includes I2C sample for MCP23017 I/O expander
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Globalization;

namespace Raspberry.IO.Components.Sensors.Temperature.Dht
namespace Raspberry.IO.Components
{
public class InvalidChecksumException : Exception
{
Expand Down
63 changes: 51 additions & 12 deletions Raspberry.IO.Components/Raspberry.IO.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,21 +45,23 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Raspberry.System, Version=2.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\Raspberry.System.2.1\lib\net40\Raspberry.System.dll</HintPath>
<Reference Include="Common.Logging, Version=3.4.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
<HintPath>..\packages\Common.Logging.3.4.1\lib\net40\Common.Logging.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Common.Logging.Core, Version=3.4.1.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
<HintPath>..\packages\Common.Logging.Core.3.4.1\lib\net40\Common.Logging.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Raspberry.System, Version=3.1.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Raspberry.System3.3.1.1\lib\net40\Raspberry.System.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Configuration" />
<Reference Include="Common.Logging.Core">
<HintPath>$(SolutionDir)packages\Common.Logging.Core.3.3.1\lib\net40\Common.Logging.Core.dll</HintPath>
</Reference>
<Reference Include="Common.Logging">
<HintPath>$(SolutionDir)packages\Common.Logging.3.3.1\lib\net40\Common.Logging.dll</HintPath>
</Reference>
<Reference Include="UnitsNet, Version=3.46.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>$(SolutionDir)packages\UnitsNet.3.46.1\lib\net35\UnitsNet.dll</HintPath>
<Reference Include="UnitsNet, Version=3.77.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\UnitsNet.3.77.0\lib\net35\UnitsNet.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
Expand Down Expand Up @@ -127,14 +144,15 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Sensors\Distance\HcSr04\HcSr04Connection.cs" />
<Compile Include="Sensors\Distance\HcSr04\Units.cs" />
<Compile Include="Sensors\Humidity\Htu21df\Htu21dfConnection.cs" />
<Compile Include="Sensors\Pressure\Bmp085\Bmp085Data.cs" />
<Compile Include="Sensors\Pressure\Bmp085\Bmp085I2cConnectionExtensionMethods.cs" />
<Compile Include="Sensors\Pressure\Bmp085\Bmp085Precision.cs" />
<Compile Include="Sensors\Temperature\Dht\Dht11Connection.cs" />
<Compile Include="Sensors\Temperature\Dht\Dht22Connection.cs" />
<Compile Include="Sensors\Temperature\Dht\DhtConnection.cs" />
<Compile Include="Sensors\Temperature\Dht\DhtData.cs" />
<Compile Include="Sensors\Temperature\Dht\InvalidChecksumException.cs" />
<Compile Include="InvalidChecksumException.cs" />
<Compile Include="Sensors\Temperature\Ds18b20\Ds18b20Connection.cs" />
<Compile Include="Sensors\VariableResistiveDividerConnection.cs" />
<Compile Include="Sensors\Temperature\Tmp36\Tmp36Connection.cs" />
Expand Down Expand Up @@ -181,7 +199,28 @@
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.4.5">
<Visible>False</Visible>
<ProductName>Windows Installer 4.5</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
183 changes: 183 additions & 0 deletions Raspberry.IO.Components/Sensors/Humidity/Htu21df/Htu21dfConnection.cs
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
}
}
12 changes: 10 additions & 2 deletions Raspberry.IO.Components/Sensors/Temperature/Dht/Dht11Connection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region References

#region References

using System;
using UnitsNet;
Expand Down Expand Up @@ -34,6 +35,13 @@ protected override TimeSpan WakeupInterval
{
get { return TimeSpan.FromMilliseconds(18); }
}

protected override TimeSpan HostReleaseBusInterval
{
get { throw new ArgumentException("DHT11 sensor does not have the host release time"); }
}

protected override bool HaveHostReleaseBus => false;

protected override DhtData GetDhtData(int temperatureValue, int humidityValue)
{
Expand All @@ -46,4 +54,4 @@ protected override DhtData GetDhtData(int temperatureValue, int humidityValue)

#endregion
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ protected override TimeSpan WakeupInterval
{
get { return TimeSpan.FromMilliseconds(1); }
}

protected override TimeSpan HostReleaseBusInterval
{
get { return new TimeSpan(10 * 20); }
}

protected override bool HaveHostReleaseBus => true;

protected override DhtData GetDhtData(int temperatureValue, int humidityValue)
{
Expand All @@ -46,4 +53,4 @@ protected override DhtData GetDhtData(int temperatureValue, int humidityValue)

#endregion
}
}
}
Loading