This commit is contained in:
Laurent Ellerbach 2021-05-24 08:55:58 +02:00 коммит произвёл GitHub
Родитель 88442b67d7
Коммит 3d44754041
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
26 изменённых файлов: 779 добавлений и 0 удалений

51
devices/Hmc5883l/Gain.cs Normal file
Просмотреть файл

@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// HMC5883L Gain Setting
/// </summary>
public enum Gain
{
/// <summary>
/// 1370, recommended sensor field range: ±0.88 Ga
/// </summary>
Gain1370 = 0x00,
/// <summary>
/// 1090, recommended sensor field range: ±1.3 Ga
/// </summary>
Gain1090 = 0x01,
/// <summary>
/// 820, recommended sensor field range: ±1.9 Ga
/// </summary>
Gain0820 = 0x02,
/// <summary>
/// 660, recommended sensor field range: ±2.5 Ga
/// </summary>
Gain0660 = 0x03,
/// <summary>
/// 440, recommended sensor field range: ±4.0 Ga
/// </summary>
Gain0440 = 0x04,
/// <summary>
/// 390, recommended sensor field range: ±4.7 Ga
/// </summary>
Gain0390 = 0x05,
/// <summary>
/// 330, recommended sensor field range: ±5.6 Ga
/// </summary>
Gain0330 = 0x06,
/// <summary>
/// 230, recommended sensor field range: ±8.1 Ga
/// </summary>
Gain0230 = 0x07,
}
}

Просмотреть файл

@ -0,0 +1,164 @@
// 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 System.Buffers.Binary;
using System.Device.I2c;
using System.Device.Model;
using System.Numerics;
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// 3-Axis Digital Compass HMC5883L
/// </summary>
[Interface("3-Axis Digital Compass HMC5883L")]
public class Hmc5883l : IDisposable
{
/// <summary>
/// HMC5883L Default I2C Address
/// </summary>
public const byte DefaultI2cAddress = 0x1E;
private readonly byte _measuringMode;
private readonly byte _outputRate;
private readonly byte _gain;
private readonly byte _samplesAmount;
private readonly byte _measurementConfig;
private I2cDevice _i2cDevice;
/// <summary>
/// HMC5883L Direction Vector
/// </summary>
[Telemetry]
public Vector3 DirectionVector => ReadDirectionVector();
/// <summary>
/// HMC5883L Heading (DEG)
/// </summary>
public double Heading => VectorToHeading(ReadDirectionVector());
/// <summary>
/// HMC5883L Status
/// </summary>
[Telemetry]
public Status DeviceStatus => GetStatus();
/// <summary>
/// Initialize a new HMC5883L device connected through I2C
/// </summary>
/// <param name="i2cDevice">The I2C device used for communication.</param>
/// <param name="gain">Gain Setting</param>
/// <param name="measuringMode">The Mode of Measuring</param>
/// <param name="outputRate">Typical Data Output Rate (Hz)</param>
/// <param name="samplesAmount">Number of samples averaged per measurement output</param>
/// <param name="measurementConfig">Measurement configuration</param>
public Hmc5883l(
I2cDevice i2cDevice,
Gain gain = Gain.Gain1090,
MeasuringMode measuringMode = MeasuringMode.Continuous,
OutputRate outputRate = OutputRate.Rate15,
SamplesAmount samplesAmount = SamplesAmount.One,
MeasurementConfiguration measurementConfig = MeasurementConfiguration.Normal)
{
_i2cDevice = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
_gain = (byte)gain;
_measuringMode = (byte)measuringMode;
_outputRate = (byte)outputRate;
_samplesAmount = (byte)samplesAmount;
_measurementConfig = (byte)measurementConfig;
Initialize();
}
/// <summary>
/// Initialize the sensor
/// </summary>
private void Initialize()
{
// Details in Datasheet P12
byte configA = (byte)(_samplesAmount | (_outputRate << 2) | _measurementConfig);
byte configB = (byte)(_gain << 5);
SpanByte commandA = new byte[]
{
(byte)Register.HMC_CONFIG_REG_A_ADDR, configA
};
SpanByte commandB = new byte[]
{
(byte)Register.HMC_CONFIG_REG_B_ADDR, configB
};
SpanByte commandMode = new byte[]
{
(byte)Register.HMC_MODE_REG_ADDR, _measuringMode
};
_i2cDevice.Write(commandA);
_i2cDevice.Write(commandB);
_i2cDevice.Write(commandMode);
}
/// <summary>
/// Read raw data from HMC5883L
/// </summary>
/// <returns>Raw Data</returns>
private Vector3 ReadDirectionVector()
{
SpanByte xRead = new byte[2];
SpanByte yRead = new byte[2];
SpanByte zRead = new byte[2];
_i2cDevice.WriteByte((byte)Register.HMC_X_MSB_REG_ADDR);
_i2cDevice.Read(xRead);
_i2cDevice.WriteByte((byte)Register.HMC_Y_MSB_REG_ADDR);
_i2cDevice.Read(yRead);
_i2cDevice.WriteByte((byte)Register.HMC_Z_MSB_REG_ADDR);
_i2cDevice.Read(zRead);
short x = BinaryPrimitives.ReadInt16BigEndian(xRead);
short y = BinaryPrimitives.ReadInt16BigEndian(yRead);
short z = BinaryPrimitives.ReadInt16BigEndian(zRead);
return new Vector3(x, y, z);
}
/// <summary>
/// Calculate heading
/// </summary>
/// <param name="vector">HMC5883L Direction Vector</param>
/// <returns>Heading (DEG)</returns>
private double VectorToHeading(Vector3 vector)
{
double deg = Math.Atan2(vector.Y, vector.X) * 180 / Math.PI;
if (deg < 0)
{
deg += 360;
}
return deg;
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_i2cDevice?.Dispose();
_i2cDevice = null!;
}
/// <summary>
/// Reads device statuses.
/// </summary>
/// <returns>Device statuses</returns>
private Status GetStatus()
{
_i2cDevice.WriteByte((byte)Register.HMC_STATUS_REG_ADDR);
byte status = _i2cDevice.ReadByte();
return (Status)status;
}
}
}

Просмотреть файл

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>{21A229AF-9CEF-44CD-B3B2-52F67A478F73}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>Iot.Device.Hmc5883l</RootNamespace>
<AssemblyName>Iot.Device.Hmc5883l</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<DocumentationFile>bin\$(Configuration)\Iot.Device.Hmc5883l.xml</DocumentationFile>
<LangVersion>9.0</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Reference Include="mscorlib">
<HintPath>packages\nanoFramework.CoreLibrary.1.10.4-preview.11\lib\mscorlib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Device.I2c">
<HintPath>packages\nanoFramework.System.Device.I2c.1.0.1-preview.33\lib\System.Device.I2c.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Math">
<HintPath>packages\nanoFramework.System.Math.1.4.0-preview.7\lib\System.Math.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<Compile Include="Gain.cs" />
<Compile Include="MeasurementConfiguration.cs" />
<Compile Include="MeasuringMode.cs" />
<Compile Include="OutputRate.cs" />
<Compile Include="Register.cs" />
<Compile Include="Hmc5883l.cs" />
<Compile Include="SamplesAmount.cs" />
<Compile Include="Status.cs" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<None Include="*.md" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<Import Project="..\..\src\BinaryPrimitives\BinaryPrimitives.projitems" Label="Shared" />
<Import Project="..\..\src\System.Device.Model\System.Device.Model.projitems" Label="Shared" />
<Import Project="..\..\src\System.Numerics\System.Numerics.projitems" Label="Shared" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Import Project="packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets" Condition="Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText> This project references NuGet package(s) that are missing on this computer.Enable NuGet Package Restore to download them.For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Nerdbank.GitVersioning.3.4.194\build\Nerdbank.GitVersioning.targets'))" />
</Target>
</Project>

Просмотреть файл

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>nanoFramework.Iot.Device.Hmc5883l</id>
<version>$version$</version>
<title>nanoFramework.Iot.Device.Hmc5883l</title>
<authors>nanoFramework project contributors</authors>
<owners>nanoFramework project contributors,dotnetfoundation</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="file">LICENSE.md</license>
<releaseNotes>
</releaseNotes>
<developmentDependency>false</developmentDependency>
<projectUrl>https://github.com/nanoframework/nanoFramework.IoT.Device</projectUrl>
<iconUrl>https://secure.gravatar.com/avatar/97d0e092247f0716db6d4b47b7d1d1ad</iconUrl>
<icon>images\nf-logo.png</icon>
<repository type="git" url="https://github.com/nanoframework/nanoFramework.IoT.Device" commit="$commit$" />
<copyright>Copyright (c) .NET Foundation and Contributors</copyright>
<description>This package includes the .NET IoT Core binding Iot.Device.Hmc5883l for .NET nanoFramework C# projects.</description>
<summary>Iot.Device.Hmc5883l assembly for .NET nanoFramework C# projects</summary>
<tags>nanoFramework C# csharp netmf netnf Iot.Device.Hmc5883l</tags>
<dependencies>
<dependency id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" />
<dependency id="nanoFramework.System.Device.I2c" version="1.0.1-preview.33" />
<dependency id="nanoFramework.System.Math" version="1.4.0-preview.71" />
</dependencies>
</metadata>
<files>
<file src="bin\Release\Iot.Device.Hmc5883l.dll" target="lib\Iot.Device.Hmc5883l.dll" />
<file src="bin\Release\Iot.Device.Hmc5883l.pdb" target="lib\Iot.Device.Hmc5883l.pdb" />
<file src="bin\Release\Iot.Device.Hmc5883l.pdbx" target="lib\Iot.Device.Hmc5883l.pdbx" />
<file src="bin\Release\Iot.Device.Hmc5883l.pe" target="lib\Iot.Device.Hmc5883l.pe" />
<file src="bin\Release\Iot.Device.Hmc5883l.xml" target="lib\Iot.Device.Hmc5883l.xml" />
<file src="readme.md" target="" />
<file src="..\..\assets\nf-logo.png" target="images" />
<file src="..\..\LICENSE.md" target="" />
</files>
</package>

Просмотреть файл

@ -0,0 +1,52 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30413.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Hmc5883l", "Hmc5883l.nfproj", "{21A229AF-9CEF-44CD-B3B2-52F67A478F73}"
EndProject
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "Hmc5883l.Samples", "samples\Hmc5883l.Samples.nfproj", "{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shared Projects", "Shared Projects", "{3B3E7B17-7E07-4C0B-8523-789E4E37C088}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "BinaryPrimitives", "..\..\src\BinaryPrimitives\BinaryPrimitives.shproj", "{3F28B003-6318-4E21-A9B6-6C0DBD0BDBFD}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "System.Device.Model", "..\..\src\System.Device.Model\System.Device.Model.shproj", "{23325B14-3651-4879-9697-9846FF123FEB}"
EndProject
Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "System.Numerics", "..\..\src\System.Numerics\System.Numerics.shproj", "{F5CCB96C-70B1-4F0A-9F0E-BB912B6A996C}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
..\..\src\System.Device.Model\System.Device.Model.projitems*{23325b14-3651-4879-9697-9846ff123feb}*SharedItemsImports = 13
..\..\src\BinaryPrimitives\BinaryPrimitives.projitems*{3f28b003-6318-4e21-a9b6-6c0dbd0bdbfd}*SharedItemsImports = 13
..\..\src\System.Numerics\System.Numerics.projitems*{f5ccb96c-70b1-4f0a-9f0e-bb912b6a996c}*SharedItemsImports = 13
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Debug|Any CPU.Build.0 = Debug|Any CPU
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Release|Any CPU.ActiveCfg = Release|Any CPU
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Release|Any CPU.Build.0 = Release|Any CPU
{21A229AF-9CEF-44CD-B3B2-52F67A478F73}.Release|Any CPU.Deploy.0 = Release|Any CPU
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Release|Any CPU.Build.0 = Release|Any CPU
{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3F28B003-6318-4E21-A9B6-6C0DBD0BDBFD} = {3B3E7B17-7E07-4C0B-8523-789E4E37C088}
{23325B14-3651-4879-9697-9846FF123FEB} = {3B3E7B17-7E07-4C0B-8523-789E4E37C088}
{F5CCB96C-70B1-4F0A-9F0E-BB912B6A996C} = {3B3E7B17-7E07-4C0B-8523-789E4E37C088}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E738A85D-E77F-483B-A12E-0CD4F1543911}
EndGlobalSection
EndGlobal

Просмотреть файл

@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// Measurement configuration.
/// This enum defines the measurement flow of the device, specifically whether or not to incorporate an applied bias to the sensor into the measurement.
/// </summary>
public enum MeasurementConfiguration : byte
{
/// <summary>
/// Normal measurement configuration (default).
/// In normal measurement configuration the device follows normal measurement flow.
/// The positive and negative pins of the resistive load are left floating and high impedance.
/// </summary>
Normal = 0b_0000_0000,
/// <summary>
/// Positive bias configuration for X and Y axes, negative bias configuration for Z axis.
/// In this configuration, a positive current is forced across the resistive load for X and Y axes, a negative current for Z axis.
/// </summary>
PositiveBiasConfiguration = 0b_0000_0001,
/// <summary>
/// Negative bias configuration for X and Y axes, positive bias configuration for Z axis.
/// In this configuration, a negative current is forced across the resistive load for X and Y axes, a positive current for Z axis.
/// </summary>
NegativeBias = 0b_0000_0010
}
}

Просмотреть файл

@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// The mode of HMC5883L measuring
/// </summary>
public enum MeasuringMode
{
/// <summary>
/// Continuous Measuring Mode
/// </summary>
Continuous = 0x00,
/// <summary>
/// Single Measuring Mode (Measure only once. In this mode, OutputRate will be invalid.)
/// </summary>
Single = 0x01
}
}

Просмотреть файл

@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// HMC5883L Typical Data Output Rate (Hz)
/// </summary>
public enum OutputRate
{
/// <summary>
/// 0.75 Hz
/// </summary>
Rate00_75 = 0x00,
/// <summary>
/// 1.5 Hz
/// </summary>
Rate01_5 = 0x01,
/// <summary>
/// 3 Hz
/// </summary>
Rate03 = 0x02,
/// <summary>
/// 7.5 Hz
/// </summary>
Rate07_5 = 0x03,
/// <summary>
/// 15 Hz
/// </summary>
Rate15 = 0x04,
/// <summary>
/// 30 Hz
/// </summary>
Rate30 = 0x05,
/// <summary>
/// 75 Hz
/// </summary>
Rate75 = 0x06,
}
}

Просмотреть файл

@ -0,0 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("Iot.Device.Hmc5883l")]
[assembly: AssemblyCompany("nanoFramework Contributors")]
[assembly: AssemblyCopyright("Copyright(c).NET Foundation and Contributors")]
[assembly: ComVisible(false)]

Просмотреть файл

@ -0,0 +1,25 @@
# HMC5883L - 3 Axis Digital Compass
HMC5883L is a surface-mount, multi-chip module designed for low-field magnetic sensing with a digital interface for applications such as lowcost compassing and magnetometry.
## Sensor Image
![](sensor.jpg)
## Usage
```C#
I2cConnectionSettings settings = new I2cConnectionSettings(1, Hmc5883l.DefaultI2cAddress);
I2cDevice device = I2cDevice.Create(settings);
using (Hmc5883l sensor = new Hmc5883l(device))
{
// read direction vector
Vector3 directionVector = sensor.DirectionVector;
// read heading
double heading = sensor.Heading;
// read status
Status status = sensor.DeviceStatus;
}
```
## References
https://cdn.datasheetspdf.com/pdf-down/H/M/C/HMC5883L-Honeywell.pdf

Просмотреть файл

@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// Register of HMC5883L
/// </summary>
internal enum Register : byte
{
HMC_CONFIG_REG_A_ADDR = 0x00,
HMC_CONFIG_REG_B_ADDR = 0x01,
HMC_MODE_REG_ADDR = 0x02,
HMC_X_MSB_REG_ADDR = 0x03,
HMC_Z_MSB_REG_ADDR = 0x05,
HMC_Y_MSB_REG_ADDR = 0x07,
HMC_STATUS_REG_ADDR = 0x09
}
}

Просмотреть файл

@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// Number of samples averaged (1 to 8) per measurement output.
/// </summary>
public enum SamplesAmount : byte
{
/// <summary>
/// 1 (Default) samples per measurement output.
/// </summary>
One = 0b_0000_0000,
/// <summary>
/// 2 samples per measurement output.
/// </summary>
Two = 0b_0010_0000,
/// <summary>
/// 4 samples per measurement output.
/// </summary>
Four = 0b_0100_0000,
/// <summary>
/// 8 samples per measurement output.
/// </summary>
Eight = 0b_0110_0000
}
}

Просмотреть файл

@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
namespace Iot.Device.Hmc5883l
{
/// <summary>
/// The status of HMC5883L device
/// </summary>
[Flags]
public enum Status : byte
{
/// <summary>
/// Ready Bit. Set when data is written to all six data registers. Cleared when device initiates a write to the data output
/// registers, when in off mode, and after one or more of the data output registers are written to.
/// When RDY bit is clear it shall remain cleared for a minimum of a 250 μs.
/// </summary>
Ready = 0b_0000_0001,
/// <summary>
/// Data output register lock. This bit is set when some but not all for of the six data output registers have been read.
/// When this bit is set, the six data output registers are locked and any new data will not be placed in these register until
/// one of four conditions are met: all six have been read or the mode changed, a POR is issued, the mode is changed, the measurement is changed.
/// </summary>
Lock = 0b_0000_0010,
/// <summary>
/// Regulator Enabled Bit. This bit is set when the internal voltage regulator is enabled.
/// This bit is cleared when the internal regulator is disabled.
/// </summary>
RegulatorEnabled = 0b_0000_0100
}
}

Просмотреть файл

@ -0,0 +1,2 @@
magnetometer
compass

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Device.I2c" version="1.0.1-preview.33" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Math" version="1.4.0-preview.7" targetFramework="netnanoframework10" />
<package id="Nerdbank.GitVersioning" version="3.4.194" developmentDependency="true" targetFramework="netnanoframework10" />
</packages>

Двоичные данные
devices/Hmc5883l/samples/HMC5883L_circuit.fzz Normal file

Двоичный файл не отображается.

Двоичные данные
devices/Hmc5883l/samples/HMC5883L_circuit_bb.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 90 KiB

Просмотреть файл

@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>{5D42428B-6D66-4D3C-AEEF-DEE78FA0B939}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>Iot.Device.Hmc5883l.Samples</RootNamespace>
<AssemblyName>Iot.Device.Hmc5883l.Samples</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<DocumentationFile>bin\$(Configuration)\Iot.Device.Hmc5883l.Samples.xml</DocumentationFile>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Reference Include="mscorlib">
<HintPath>..\packages\nanoFramework.CoreLibrary.1.10.4-preview.11\lib\mscorlib.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Device.I2c">
<HintPath>..\packages\nanoFramework.System.Device.I2c.1.0.1-preview.33\lib\System.Device.I2c.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<!-- INSERT FILE REFERENCES HERE -->
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="*.cs" />
<None Include="*.md" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Hmc5883l.nfproj" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<!-- INSERT IMPORTS HERE -->
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<!-- INSERT NBGV IMPORT HERE -->
</Project>

Просмотреть файл

@ -0,0 +1,40 @@
// 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 System.Device.I2c;
using System.Diagnostics;
using System.Threading;
using Iot.Device.Hmc5883l;
I2cConnectionSettings settings = new(1, Hmc5883l.DefaultI2cAddress);
using I2cDevice device = I2cDevice.Create(settings);
using Hmc5883l sensor = new(device);
while (true)
{
// read heading
Debug.WriteLine($"Heading: {sensor.Heading.ToString("0.00")} °");
var status = sensor.DeviceStatus;
Debug.Write("Statuses: ");
switch (status)
{
case Status.Ready:
Debug.Write($"Ready ");
break;
case Status.Lock:
Debug.Write($"Lock ");
break;
case Status.RegulatorEnabled:
Debug.Write($"RegulatorEnabled ");
break;
default:
break;
}
Debug.WriteLine("");
Debug.WriteLine("");
// wait for a second
Thread.Sleep(1000);
}

Просмотреть файл

@ -0,0 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("Iot.Device.Hmc5883l.Samples")]
[assembly: AssemblyCompany("nanoFramework Contributors")]
[assembly: AssemblyCopyright("Copyright(c).NET Foundation and Contributors")]
[assembly: ComVisible(false)]

Просмотреть файл

@ -0,0 +1,36 @@
# HMC5883L - Samples
## Hardware Required
* HMC5883L
* Male/Female Jumper Wires
## Circuit
![](HMC5883L_circuit_bb.png)
* SCL - SCL
* SDA - SDA
* VCC - 5V
* GND - GND
## Code
```C#
I2cConnectionSettings settings = new I2cConnectionSettings(1, Hmc5883l.DefaultI2cAddress);
I2cDevice device = I2cDevice.Create(settings);
using (Hmc5883l sensor = new Hmc5883l(device))
{
while (true)
{
// read heading
Console.WriteLine($"Heading: {sensor.Heading.ToString("0.00")} °");
Console.WriteLine();
// wait for a second
Thread.Sleep(1000);
}
}
```
## Result
![](RunningResult.jpg)

Двоичные данные
devices/Hmc5883l/samples/RunningResult.jpg Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 31 KiB

Просмотреть файл

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nanoFramework.CoreLibrary" version="1.10.4-preview.11" targetFramework="netnanoframework10" />
<package id="nanoFramework.System.Device.I2c" version="1.0.1-preview.33" targetFramework="netnanoframework10" />
</packages>

Просмотреть файл

@ -0,0 +1,19 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0",
"assemblyVersion": {
"precision": "minor"
},
"semVer1NumericIdentifierPadding": 3,
"nuGetPackageVersion": {
"semVer": 2.0
},
"publicReleaseRefSpec": [
"^refs/heads/develop$",
"^refs/heads/main$",
"^refs/heads/v\\d+(?:\\.\\d+)?$"
],
"cloudBuild": {
"setAllVariables": true
}
}

Двоичные данные
devices/Hmc5883l/sensor.jpg Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 64 KiB

Просмотреть файл

@ -0,0 +1,19 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "1.0",
"assemblyVersion": {
"precision": "minor"
},
"semVer1NumericIdentifierPadding": 3,
"nuGetPackageVersion": {
"semVer": 2.0
},
"publicReleaseRefSpec": [
"^refs/heads/develop$",
"^refs/heads/main$",
"^refs/heads/v\\d+(?:\\.\\d+)?$"
],
"cloudBuild": {
"setAllVariables": true
}
}