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

Mma7660fc driver #275

Merged
merged 3 commits into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Meadow.Foundation.Sensors.Motion
{
public partial class Mma7660fc
{
/// <summary>
/// Valid addresses for the sensor.
/// </summary>
public enum Addresses : byte
{
/// <summary>
/// Bus address 0x4C
/// </summary>
Address_0x4c = 0x4C,
/// <summary>
/// Bus address 0x4C
/// </summary>
Default = Address_0x4c
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Meadow.Foundation.Sensors.Motion
{
public partial class Mma7660fc
{
public enum SensorMode : byte
adrianstevens marked this conversation as resolved.
Show resolved Hide resolved
{
Standby = 0,
Active = 1,
}

public enum SampleRate : byte
{
_120 = 0,
_64 = 1,
_32 =2,
_16 = 3,
_8 = 4,
_4 = 5,
_2 = 6,
_1 = 7,
}

//check the TILT registe
public enum DirectionType : byte
{
Unknown = 0,
Up = 0b00011000,
Down = 0b00010100,
Right = 0b00001000,
Left = 0b00000100,
}

public enum OrientationType : byte
{
Unknown = 0,
Back = 0b00000010,
Front = 0b00000001,
}

public enum Tilt : byte
{
Shake = 0b10000000,
Alert = 0b01000000,
Tap = 0b00100000,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Meadow.Foundation.Sensors.Motion
{
public partial class Mma7660fc
{
public enum Registers : byte
{
XOUT = 0x00,
YOUT = 0x01,
ZOUT = 0x02,
TILT = 0x03,
Mode = 0x07,
SleepRate = 0x08
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Threading.Tasks;
using Meadow.Hardware;
using Meadow.Peripherals.Sensors.Motion;
using Meadow.Units;

namespace Meadow.Foundation.Sensors.Motion
{
/// <summary>
/// Represents Mma7660fc 3-axis acclerometer
/// </summary>
public partial class Mma7660fc : ByteCommsSensorBase<Acceleration3D>, IAccelerometer
{
/// <summary>
/// Raised when new acceleration data is processed
/// </summary>
public event EventHandler<IChangeResult<Acceleration3D>> Acceleration3DUpdated = delegate { };

/// <summary>
/// Current Acceleration3d value
/// </summary>
public Acceleration3D? Acceleration3D => Conditions;

public DirectionType Direction { get; set; } = DirectionType.Unknown;
public OrientationType Orientation { get; set; } = OrientationType.Unknown;

/// <summary>
/// Create a new instance of the Mma7660fc communicating over the I2C interface.
/// </summary>
/// <param name="address">Address of the I2C sensor</param>
/// <param name="i2cBus">I2C bus</param>
public Mma7660fc(II2cBus i2cBus, Addresses address = Addresses.Default)
: this(i2cBus, (byte)address)
{
}

/// <summary>
/// Create a new instance of the Mma7660fc communicating over the I2C interface.
/// </summary>
/// <param name="address">Address of the I2C sensor</param>
/// <param name="i2cBus">I2C bus</param>
public Mma7660fc(II2cBus i2cBus, byte address)
: base(i2cBus, address)
{
Initialize();
}

void Initialize()
{
SetMode(SensorMode.Standby);
SetSampleRate(SampleRate._32);
SetMode(SensorMode.Active);
}

void SetMode(SensorMode mode)
{
Peripheral.WriteRegister((byte)Registers.Mode, (byte)mode);
}

/// <summary>
/// Set sample rate in samples per second
/// </summary>
/// <param name="rate">sample rate</param>
public void SetSampleRate(SampleRate rate)
{
Peripheral.WriteRegister((byte)Registers.SleepRate, (byte)rate);
}

/// <summary>
/// Read sensor data from registers
/// </summary>
/// <returns></returns>
protected override Task<Acceleration3D> ReadSensor()
{
return Task.Run(() =>
{
//let's start with the tilt/orientation
// var value = (Peripheral.ReadRegister((byte)Registers.TILT));
adrianstevens marked this conversation as resolved.
Show resolved Hide resolved

// Console.WriteLine($"TILT: {value}");

Direction = (DirectionType)(Peripheral.ReadRegister((byte)Registers.TILT) & 0x1C);

Orientation = (OrientationType)(Peripheral.ReadRegister((byte)Registers.TILT) & 0x03);

int xAccel, yAccel, zAccel;
byte x, y, z;

//Signed byte 6-bit 2’s complement data with allowable range of +31 to -32
//[5] is 0 if the g direction is positive, 1 if the g direction is negative.
do
{
x = Peripheral.ReadRegister((byte)Registers.XOUT);
}
//ensure bit 6 isn't set - if so, it means there was a read/write collision ... try again
while (x >= 64);

//check bit 5 and flip to negative
if ((x & (1 << 5)) != 0) xAccel = x - 64;
else xAccel = x;

do
{
y = Peripheral.ReadRegister((byte)Registers.YOUT);
}
//ensure bit 6 isn't set - if so, it means there was a read/write collision ... try again
while (y >= 64);

if ((y & (1 << 5)) != 0) yAccel = y - 64;
else yAccel = y;

do
{
z = Peripheral.ReadRegister((byte)Registers.ZOUT);
}
//ensure bit 6 isn't set - if so, it means there was a read/write collision ... try again
while (y >= 64);

if ((z & (1 << 5)) != 0) zAccel = z - 64;
else zAccel = z;

return new Acceleration3D(
new Acceleration(xAccel * 3.0 / 64.0, Acceleration.UnitType.Gravity),
new Acceleration(yAccel * 3.0 / 64.0, Acceleration.UnitType.Gravity),
new Acceleration(zAccel * 3.0 / 64.0, Acceleration.UnitType.Gravity)
);
});
}

/// <summary>
/// Raise event and notify subscribers
/// </summary>
/// <param name="changeResult">Acceleration3d data</param>
protected override void RaiseEventsAndNotify(IChangeResult<Acceleration3D> changeResult)
{
Acceleration3DUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageIcon>icon.png</PackageIcon>
<Authors>Wilderness Labs, Inc</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<AssemblyName>Mma7660fc</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl>
<PackageId>Meadow.Foundation.Sensors.Motion.Adxl3xx</PackageId>
adrianstevens marked this conversation as resolved.
Show resolved Hide resolved
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<PackageTags>Meadow.Foundation, Mma7660fc, Accelerometer, Motion</PackageTags>
<Version>0.1.0</Version>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Description>Mma7660fc I2C 3-axis accelerometer</Description>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\..\..\icon.png" Pack="true" PackagePath="" />
<ProjectReference Include="..\..\..\..\Meadow.Foundation.Core\Meadow.Foundation.Core.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Threading.Tasks;
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.Sensors.Motion;
using Meadow.Units;
using AU = Meadow.Units.Acceleration.UnitType;

namespace Sensors.Motion.Adxl335_Sample
adrianstevens marked this conversation as resolved.
Show resolved Hide resolved
{
public class MeadowApp : App<F7MicroV2, MeadowApp>
{
//<!—SNIP—>

Mma7660fc sensor;

public MeadowApp()
{
Console.WriteLine("Initializing");

// create the sensor driver
sensor = new Mma7660fc(Device.CreateI2cBus());

// classical .NET events can also be used:
sensor.Updated += (sender, result) => {
Console.WriteLine($"Accel: [X:{result.New.X.MetersPerSecondSquared:N2}," +
$"Y:{result.New.Y.MetersPerSecondSquared:N2}," +
$"Z:{result.New.Z.MetersPerSecondSquared:N2} (m/s^2)]" +
$" Direction: {sensor.Direction}" +
$" Orientation: {sensor.Orientation}");
};

// Example that uses an IObersvable subscription to only be notified when the filter is satisfied
var consumer = Mma7660fc.CreateObserver(
handler: result => Console.WriteLine($"Observer: [x] changed by threshold; new [x]: X:{result.New.X:N2}, old: X:{result.Old?.X:N2}"),
// only notify if there's a greater than 1G change in the Z direction
filter: result => {
if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
return ((result.New - old).Z > new Acceleration(1, AU.Gravity));
}
return false;
});
sensor.Subscribe(consumer);

//==== one-off read
ReadConditions().Wait();

// start updating
sensor.StartUpdating(TimeSpan.FromMilliseconds(1000));
}

protected async Task ReadConditions()
{
var result = await sensor.Read();
Console.WriteLine("Initial Readings:");
Console.WriteLine($"Accel: [X:{result.X.MetersPerSecondSquared:N2}," +
$"Y:{result.Y.MetersPerSecondSquared:N2}," +
$"Z:{result.Z.MetersPerSecondSquared:N2} (m/s^2)]");
}

//<!—SNOP—>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<Company>Wilderness Labs, Inc</Company>
<Authors>Wilderness Labs, Inc</Authors>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Exe</OutputType>
<AssemblyName>App</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\Meadow.Core\source\Meadow.F7\Meadow.F7.csproj" />
<ProjectReference Include="..\..\Driver\Sensors.Motion.Mma7660fc\Sensors.Motion.Mma7660fc.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Threading;
using Meadow;

namespace Sensors.Motion.Adxl335_Sample
{
class Program
{
static IApp app;

public static void Main(string[] args)
{
app = new MeadowApp();

Thread.Sleep(Timeout.Infinite);
}
}
}
28 changes: 26 additions & 2 deletions Source/Meadow.Foundation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sensors.Weather.SwitchingRa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SwitchingRainGauge_Sample", "Meadow.Foundation.Peripherals\Sensors.Weather.SwitchingRainGauge\Samples\SwitchingRainGauge_Sample\SwitchingRainGauge_Sample.csproj", "{AFCF1EBC-B7FC-445A-A8E5-AF3724DA8F78}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Meadow.Contracts", "..\..\Meadow.Contracts\Source\Meadow.Contracts\Meadow.Contracts.csproj", "{150569EE-4053-4097-98EE-A455287610B5}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Meadow.Contracts", "..\..\Meadow.Contracts\Source\Meadow.Contracts\Meadow.Contracts.csproj", "{150569EE-4053-4097-98EE-A455287610B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Meadow.Units", "..\..\Meadow.Units\Source\Meadow.Units\Meadow.Units.csproj", "{46394970-4495-408F-8A9C-31BEBC47E899}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Meadow.Units", "..\..\Meadow.Units\Source\Meadow.Units\Meadow.Units.csproj", "{46394970-4495-408F-8A9C-31BEBC47E899}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sensors.Motion.Mma7660fc", "Sensors.Motion.Mma7660fc", "{8C70DC80-3DD8-40F7-B781-CDF6D9F8EA42}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sensors.Motion.Mma7660fc", "Meadow.Foundation.Peripherals\Sensors.Motion.Mma7660fc\Driver\Sensors.Motion.Mma7660fc\Sensors.Motion.Mma7660fc.csproj", "{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{9D13043C-8A59-461A-9A4D-3735D06CED77}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mma7660fc_Sample", "Meadow.Foundation.Peripherals\Sensors.Motion.Mma7660fc\Samples\Mma7660fc_Sample\Mma7660fc_Sample.csproj", "{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -2430,6 +2438,18 @@ Global
{46394970-4495-408F-8A9C-31BEBC47E899}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46394970-4495-408F-8A9C-31BEBC47E899}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46394970-4495-408F-8A9C-31BEBC47E899}.Release|Any CPU.Build.0 = Release|Any CPU
{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9}.Release|Any CPU.Build.0 = Release|Any CPU
{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9}.Release|Any CPU.Deploy.0 = Release|Any CPU
{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED}.Release|Any CPU.Build.0 = Release|Any CPU
{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2951,6 +2971,10 @@ Global
{AFCF1EBC-B7FC-445A-A8E5-AF3724DA8F78} = {A29C80C8-E116-4344-A49F-7E58529DC338}
{150569EE-4053-4097-98EE-A455287610B5} = {65C50059-6C22-43E9-88DE-AD73F7F108C8}
{46394970-4495-408F-8A9C-31BEBC47E899} = {65C50059-6C22-43E9-88DE-AD73F7F108C8}
{8C70DC80-3DD8-40F7-B781-CDF6D9F8EA42} = {64623FCA-6086-4F0A-A59D-2BF372EA38AA}
{4924CFE6-44B7-4C9D-AE02-1DD2A63E37D9} = {8C70DC80-3DD8-40F7-B781-CDF6D9F8EA42}
{9D13043C-8A59-461A-9A4D-3735D06CED77} = {8C70DC80-3DD8-40F7-B781-CDF6D9F8EA42}
{A2F68D9C-A462-4E7A-A71C-CB773BBB5EED} = {9D13043C-8A59-461A-9A4D-3735D06CED77}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF7CA16F-8C38-4546-87A2-5DAAF58A1520}
Expand Down