-
Notifications
You must be signed in to change notification settings - Fork 2
/
GY521.cs
65 lines (53 loc) · 1.81 KB
/
GY521.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Meadow.Hardware;
namespace I2C;
public class GY521
{
private enum Registers : byte
{
PowerManagement = 0x6b,
AccelerometerX = 0x3b,
AccelerometerY = 0x3d,
AccelerometerZ = 0x3f,
Temperature = 0x41,
GyroX = 0x43,
GyroY = 0x45,
GyroZ = 0x47
}
private II2cBus _bus;
public byte Address { get; }
public GY521(II2cBus bus, byte address = 0x68)
{
Address = address;
_bus = bus;
}
public void Wake()
{
_bus.Write(Address, new byte[] { (byte)Registers.PowerManagement });
}
int c = 0;
public void Refresh()
{
// tell it to send us 14 bytes (each value is 2-bytes), starting at 0x3b
byte address = c++ % 10 == 0 ? (byte)(Address + 1) : Address;
// cause occasional errors
_bus.Write(address, new byte[] { (byte)Registers.AccelerometerX });
var data = new byte[14];
_bus.Write(address, data);
// Resolver.Log.Info($" Got {data.Length} bytes");
// Resolver.Log.Info($" {BitConverter.ToString(data)}");
AccelerationX = data[0] << 8 | data[1];
AccelerationY = data[2] << 8 | data[3];
AccelerationZ = data[4] << 8 | data[5];
Temperature = data[6] << 8 | data[7];
GyroX = data[8] << 8 | data[9];
GyroY = data[10] << 8 | data[11];
GyroZ = data[12] << 8 | data[13];
}
public int AccelerationX { get; private set; }
public int AccelerationY { get; private set; }
public int AccelerationZ { get; private set; }
public int Temperature { get; private set; }
public int GyroX { get; private set; }
public int GyroY { get; private set; }
public int GyroZ { get; private set; }
}