Skip to content
This repository has been archived by the owner on Jan 4, 2021. It is now read-only.

Commit

Permalink
Commit for api v0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
scaryghost committed Feb 1, 2018
1 parent e5fbcca commit 5a81ec1
Show file tree
Hide file tree
Showing 80 changed files with 1,226 additions and 571 deletions.
6 changes: 3 additions & 3 deletions MetaWear.DotNet/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[assembly: AssemblyDescription("MetaWear C# API for .NET framework apps")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("MbientLab")]
[assembly: AssemblyProduct("MetaWear.DotNet")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyProduct("MetaWear")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,4 +32,4 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("0.3.0")]
[assembly: AssemblyFileVersion("0.4.0")]
6 changes: 5 additions & 1 deletion MetaWear.DotNetStandard/MetaWear.DotNetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
<AssemblyName>MbientLab.MetaWear.DotNetStandard</AssemblyName>
<RootNamespace>MbientLab.MetaWear.DotNetStandard</RootNamespace>
<DelaySign>false</DelaySign>
<FileVersion>0.3.0.0</FileVersion>
<FileVersion>0.4.0.0</FileVersion>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Description>MetaWear C# API for netstandard2.0</Description>
<Product>MetaWear</Product>
<Company>MbientLab</Company>
<Authors>MbientLab</Authors>
</PropertyGroup>

<Import Project="..\MetaWear\MetaWear.projitems" Label="Shared" />
Expand Down
2 changes: 1 addition & 1 deletion MetaWear.Impl/AccelerationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System;

namespace MbientLab.MetaWear.Impl {
class AccelerationData : DataBase {
internal class AccelerationData : DataBase {
public AccelerationData(IModuleBoardBridge bridge, DataTypeBase datatype, DateTime timestamp, byte[] bytes) :
base(bridge, datatype, timestamp, bytes) {
}
Expand Down
22 changes: 11 additions & 11 deletions MetaWear.Impl/AccelerometerBosch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new BoschCartesianFloatData(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new AccelerationData(bridge, this, timestamp, data);
}

Expand Down Expand Up @@ -118,7 +118,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new BoschOrientationDataType(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new OrientationData(this, bridge, timestamp, data);
}
}
Expand All @@ -134,7 +134,7 @@ public void Configure(OrientationMode? mode = null, float? hysteresis = null) {
}

config[0] &= 0xfc;
config[0] |= (byte)(mode ?? OrientationMode.SYMMETRICAL);
config[0] |= (byte)(mode ?? OrientationMode.Symmetrical);

bridge.sendCommand(ACCELEROMETER, ORIENT_CONFIG, config);
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new BoschFlatDataType(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new FlatData(this, bridge, timestamp, data);
}
}
Expand Down Expand Up @@ -229,7 +229,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new BoschTapDataType(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new BoschTapData(this, bridge, timestamp, data);
}
}
Expand Down Expand Up @@ -287,10 +287,10 @@ internal LowHighData(DataTypeBase datatype, IModuleBoardBridge bridge, DateTime
public override T Value<T>() {
var type = typeof(T);
if (type == typeof(LowHighG)) {
Func<byte, byte, bool> CheckHighG = (axis, value) => {
bool CheckHighG(byte axis, byte value) {
byte mask = (byte)(0x1 << axis);
return (value & mask) == mask;
};
}
byte highFirst = (byte)((bytes[0] & 0x1c) >> 2);
LowHighG casted = new LowHighG(
(bytes[0] & 0x1) == 0x1,
Expand Down Expand Up @@ -319,7 +319,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new BoschLowHighGDataType(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new LowHighData(this, bridge, timestamp, data);
}
}
Expand Down Expand Up @@ -392,10 +392,10 @@ internal AnyMotionData(DataTypeBase datatype, IModuleBoardBridge bridge, DateTim
public override T Value<T>() {
var type = typeof(T);
if (type == typeof(AnyMotion)) {
Func<byte, byte, bool> detected = (axis, value) => {
bool detected(byte axis, byte value) {
byte mask = (byte)(0x1 << (axis + 3));
return (value & mask) == mask;
};
}
byte highFirst = (byte)((bytes[0] & 0x1c) >> 2);
AnyMotion casted = new AnyMotion(
(bytes[0] & 0x40) == 0x40 ? Sign.Negative : Sign.Positive,
Expand All @@ -422,7 +422,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new BoschMotionDataType(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new AnyMotionData(this, bridge, timestamp, data);
}
}
Expand Down
171 changes: 157 additions & 14 deletions MetaWear.Impl/AccelerometerMma8452q.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
using MbientLab.MetaWear.Sensor.AccelerometerMma8452q;
using System.Threading.Tasks;
using System.Collections.Generic;
using MbientLab.MetaWear.Data;

namespace MbientLab.MetaWear.Impl {
[KnownType(typeof(Mma8452QCartesianFloatData))]
[DataContract]
class AccelerometerMma8452q : ModuleImplBase, IAccelerometerMma8452q {
internal static string createIdentifier(DataTypeBase dataType) {
switch (dataType.eventConfig[1]) {
case DATA_VALUE:
return dataType.attributes.length() > 2 ? "acceleration" : string.Format("acceleration[{0}]", (dataType.attributes.offset >> 1));
case ORIENTATION_VALUE:
return "orientation";
/*
case ORIENTATION_VALUE:
return "orientation";
case SHAKE_STATUS:
return "mma8452q-shake";
case PULSE_STATUS:
return "mma8452q-tap";
case MOVEMENT_VALUE:
return "mma8452q-movement";
*/
case SHAKE_STATUS:
return "mma8452q-shake";
case PULSE_STATUS:
return "mma8452q-tap";
case MOVEMENT_VALUE:
return "mma8452q-movement";
*/
default:
return null;
}
Expand All @@ -32,12 +34,61 @@ internal static string createIdentifier(DataTypeBase dataType) {
public const byte IMPLEMENTATION = 0, PACKED_ACC_REVISION = 1;
private const byte GLOBAL_ENABLE = 1,
DATA_ENABLE = 2, DATA_CONFIG = 3, DATA_VALUE = 4,
ORIENTATION_ENABLE = 8, ORIENTATION_CONFIG = 9, ORIENTATION_VALUE = 0xa,
PACKED_ACC_DATA = 0x12;

private static readonly float[] FREQUENCIES = new float[] { 800f, 400f, 200f, 100f, 50f, 12.5f, 6.25f, 1.56f },
RANGES = new float[] { 2f, 4f, 8f };
private static readonly float[][] motionCountSteps = {
new float[] {1.25f, 2.5f, 5, 10, 20, 20, 20, 20},
new float[] {1.25f, 2.5f, 5, 10, 20, 80, 80, 80},
new float[] {1.25f, 2.5f, 2.5f, 2.5f, 2.5f, 2.5f, 2.5f, 2.5f},
new float[] {1.25f, 2.5f, 5, 10, 20, 80, 160, 160}
}, orientationSteps = motionCountSteps;
private static readonly float[][][] OS_CUTOFF_FREQS = {
new float[][] {
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {8f, 4f, 2f, 1f},
new float[] {4f, 2f, 1f, 0.5f},
new float[] {2f, 1f, 0.5f, 0.25f},
new float[] {2f, 1f, 0.5f, 0.25f},
new float[] {2f, 1f, 0.5f, 0.25f},
new float[] {2f, 1f, 0.5f, 0.25f}
},
new float[][] {
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {8f, 4f, 2f, 1f},
new float[] {4f, 2f, 1f, 0.5f},
new float[] {2f, 1f, 0.5f, 0.25f},
new float[] {0.5f, 0.25f, 0.125f, 0.063f},
new float[] {0.5f, 0.25f, 0.125f, 0.063f},
new float[] {0.5f, 0.25f, 0.125f, 0.063f}
},
new float[][] {
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f},
new float[] {16f, 8f, 4f, 2f}
},
new float[][] {
new float[] {16f, 8f, 4f, 2f},
new float[] {8f, 4f, 2f, 1f},
new float[] {4f, 2f, 1f, 0.5f},
new float[] {2f, 1f, 0.5f, 0.25f},
new float[] {1f, 0.5f, 0.25f, 0.125f},
new float[] {0.25f, 0.125f, 0.063f, 0.031f},
new float[] {0.25f, 0.125f, 0.063f, 0.031f},
new float[] {0.25f, 0.125f, 0.063f, 0.031f}
}
};

[DataContract]
[DataContract]
private class Mma8452QCartesianFloatData : FloatVectorDataType {
private Mma8452QCartesianFloatData(DataTypeBase input, Module module, byte register, byte id, DataAttributes attributes) :
base(input, module, register, id, attributes) { }
Expand All @@ -49,7 +100,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new Mma8452QCartesianFloatData(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new AccelerationData(bridge, this, timestamp, data);
}

Expand All @@ -66,12 +117,80 @@ protected override DataTypeBase[] createSplits() {
}
}

[DataContract]
private class Mma8452qOrientationDataType : DataTypeBase {
private class OrientationData : DataBase {
internal OrientationData(DataTypeBase datatype, IModuleBoardBridge bridge, DateTime timestamp, byte[] bytes) :
base(bridge, datatype, timestamp, bytes) {
}

public override Type[] Types => new Type[] { typeof(SensorOrientation) };

public override T Value<T>() {
var type = typeof(T);

if (type == typeof(SensorOrientation)) {
int offset = (bytes[0] & 0x06) >> 1;
return (T)Convert.ChangeType((SensorOrientation) (4 * (bytes[0] & 0x01) + ((offset == 2 || offset == 3) ? offset ^ 0x1 : offset)), type);
}

return base.Value<T>();
}
}

internal Mma8452qOrientationDataType() : base(ACCELEROMETER, ORIENTATION_VALUE, new DataAttributes(new byte[] { 1 }, 1, 0, false)) {
}

internal Mma8452qOrientationDataType(DataTypeBase input, Module module, byte register, byte id, DataAttributes attributes) :
base(input, module, register, id, attributes) {
}

public override DataTypeBase copy(DataTypeBase input, Module module, byte register, byte id, DataAttributes attributes) {
return new Mma8452qOrientationDataType(input, module, register, id, attributes);
}

public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new OrientationData(this, bridge, timestamp, data);
}
}
private class OrientationDataProducer : AsyncDataProducer, IOrientationDataProducer {
private int delay = 100;

internal OrientationDataProducer(DataTypeBase dataTypeBase, IModuleBoardBridge bridge) : base(ORIENTATION_ENABLE, dataTypeBase, bridge) {
}

public void Configure(int delay = 100) {
this.delay = delay;
}

public override void Start() {
AccelerometerMma8452q acc = bridge.GetModule<IAccelerometerMma8452q>() as AccelerometerMma8452q;
bridge.sendCommand(ACCELEROMETER, ORIENTATION_CONFIG, new byte[] { 0x00, 0xc0, (byte)(delay / orientationSteps[acc.PwMode][acc.Odr]), 0x44, 0x84 });

base.Start();
}

public override void Stop() {
base.Stop();

bridge.sendCommand(ACCELEROMETER, ORIENTATION_CONFIG, new byte[] { 0x00, 0x80, 0x00, 0x44, 0x84 });
}
}

[DataMember] private readonly byte[] dataSettings = new byte[] { 0x00, 0x00, 0x18, 0x00, 0x00 };
[DataMember] private Mma8452QCartesianFloatData accDataType, packedAccDataType;
[DataMember] private Mma8452qOrientationDataType orientationDataType;

private IAsyncDataProducer acceleration = null, packedAcceleration = null;
private IOrientationDataProducer orientation = null;
private TimedTask<byte[]> readConfigTask;

private int PwMode => dataSettings[3] & 0x3;

private int Odr => (dataSettings[2] & ~0xc7) >> 3;

private int PulseLpfEn => (dataSettings[1] & ~0x10) >> 4;

public AccelerometerMma8452q(IModuleBoardBridge bridge) : base(bridge) {
accDataType = new Mma8452QCartesianFloatData(DATA_VALUE, 1);
packedAccDataType = new Mma8452QCartesianFloatData(PACKED_ACC_DATA, 3);
Expand All @@ -97,6 +216,15 @@ public IAsyncDataProducer PackedAcceleration {
}
}

public IOrientationDataProducer Orientation {
get {
if (orientation == null) {
orientation = new OrientationDataProducer(orientationDataType, bridge);
}
return orientation;
}
}

internal override void aggregateDataType(ICollection<DataTypeBase> collection) {
collection.Add(accDataType);
collection.Add(packedAccDataType);
Expand All @@ -106,11 +234,26 @@ protected override void init() {
readConfigTask = new TimedTask<byte[]>();
bridge.addRegisterResponseHandler(Tuple.Create((byte)ACCELEROMETER, Util.setRead(DATA_CONFIG)),
response => readConfigTask.SetResult(response));

if (orientationDataType == null) {
orientationDataType = new Mma8452qOrientationDataType();
}
}

public void Configure(OutputDataRate odr, DataRange range) {
dataSettings[2] |= (byte) ((byte) odr << 3);
dataSettings[0] |= (byte) range;
public void Configure(OutputDataRate odr = OutputDataRate._100Hz, DataRange range = DataRange._2g, float? highPassCutoff = null,
Oversampling oversample = Oversampling.Normal) {
for(int i = 0; i < dataSettings.Length; i++) {
dataSettings[i] = 0;
}

dataSettings[3] |= (byte)oversample;
dataSettings[2] |= (byte)((byte)odr << 3);
dataSettings[0] |= (byte)range;

if (highPassCutoff != null) {
dataSettings[1] |= (byte) (Util.closestIndex(OS_CUTOFF_FREQS[(int)oversample][(int)odr], highPassCutoff.Value) & 0x3);
dataSettings[0] |= 0x10;
}

bridge.sendCommand(ACCELEROMETER, DATA_CONFIG, dataSettings);
}
Expand Down
2 changes: 1 addition & 1 deletion MetaWear.Impl/ByteArrayDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal ByteArrayDataType(DataTypeBase input, Module module, byte register, Dat
private ByteArrayDataType(DataTypeBase input, Module module, byte register, byte id, DataAttributes attributes) :
base(input, module, register, id, attributes) { }

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new ByteArrayData(bridge, this, timestamp, data);
}

Expand Down
2 changes: 1 addition & 1 deletion MetaWear.Impl/ColorTcs34725.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override DataTypeBase copy(DataTypeBase input, Module module, byte regist
return new AdcDataType(input, module, register, id, attributes);
}

public override IData createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
public override DataBase createData(bool logData, IModuleBoardBridge bridge, byte[] data, DateTime timestamp) {
return new AdcData(bridge, this, timestamp, data);
}

Expand Down
Loading

0 comments on commit 5a81ec1

Please sign in to comment.