-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBleMovementService.cs
More file actions
346 lines (293 loc) · 11.3 KB
/
Copy pathBleMovementService.cs
File metadata and controls
346 lines (293 loc) · 11.3 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.Storage.Streams;
using Buffer = Windows.Storage.Streams.Buffer;
namespace SensorTag
{
[FlagsAttribute]
public enum MovementFlags
{
None = 0,
/// <summary>
/// Enable Gyro X-Axis
/// </summary>
GyroX = 1,
/// <summary>
/// Enable Gyro Y-Axis
/// </summary>
GyroY = 2,
/// <summary>
/// Enable Gyro Z-Axis
/// </summary>
GyroZ = 4,
/// <summary>
/// Enable Accelerometer X-Axis
/// </summary>
AccelX = 8,
/// <summary>
/// Enable Accelerometer Y-Axis
/// </summary>
///
AccelY = 0x10,
/// <summary>
/// Enable Accelerometer Z-Axis
/// </summary>
AccelZ = 0x20,
/// <summary>
/// Enable Magnetometer
/// </summary>
Mag = 0x40,
/// <summary>
/// The Wake-On-Motion (WOM) feature allows the MPU to operate with only the accelerometer enabled, but will give an interrupt to the CC2650 when motion is detected.
/// After a shake is detected, the SensorTag will provide movement data for 10 seconds before entering the MPU re-enters low power WOM state
/// </summary>
WakeOnMotion = 0x80,
/// <summary>
/// Accelerometer range (2G)
/// </summary>
Accel2G = 0,
/// <summary>
/// Accelerometer range (4G)
/// </summary>
Accel4G = 0x100,
/// <summary>
/// Accelerometer range (8G)
/// </summary>
Accel8G = 0x200,
/// <summary>
/// Accelerometer range (16G)
/// </summary>
Accel16G = 0x300
}
/// <summary>
/// This class provides access to the SensorTag Movement BLE data
/// </summary>
public class BleMovementService : BleGenericGattService
{
public BleMovementService()
{
}
/// <summary>
/// The version of the SensorTag device. 2=CC2650. Version 1 does not support this service.
/// </summary>
public int Version { get; set; }
static Guid MovementServiceUUid = Guid.Parse("f000aa80-0451-4000-b000-000000000000");
static Guid MovementCharacteristicUuid = Guid.Parse("f000aa81-0451-4000-b000-000000000000");
static Guid MovementCharacteristicConfigUuid = Guid.Parse("f000aa82-0451-4000-b000-000000000000");
static Guid MovementCharacteristicPeriodUuid = Guid.Parse("f000aa83-0451-4000-b000-000000000000");
Delegate _movementValueChanged;
public event EventHandler<MovementEventArgs> MovementMeasurementValueChanged
{
add
{
if (_movementValueChanged != null)
{
_movementValueChanged = Delegate.Combine(_movementValueChanged, value);
}
else
{
_movementValueChanged = value;
RegisterForValueChangeEvents(MovementCharacteristicUuid);
}
}
remove
{
if (_movementValueChanged != null)
{
_movementValueChanged = Delegate.Remove(_movementValueChanged, value);
if (_movementValueChanged == null)
{
UnregisterForValueChangeEvents(MovementCharacteristicUuid);
}
}
}
}
private async Task<MovementFlags> GetConfig()
{
var ch = GetCharacteristic(MovementCharacteristicConfigUuid);
if (ch != null)
{
var properties = ch.CharacteristicProperties;
if ((properties & GattCharacteristicProperties.Read) != 0)
{
var result = await ch.ReadValueAsync();
IBuffer buffer = result.Value;
DataReader reader = DataReader.FromBuffer(buffer);
short value = ReadBigEndian16bit(reader);
MovementFlags flags = (MovementFlags)value;
Debug.WriteLine("Acceleration config = " + flags);
return flags;
}
}
return MovementFlags.None;
}
bool isReading;
public async Task StartReading(MovementFlags flags)
{
if (!isReading)
{
// One bit for each gyro and accelerometer axis (6), magnetometer (1), wake-on-motion enable (1), accelerometer range (2).
// Write any bit combination top enable the desired features
DataWriter writer = new DataWriter();
WriteBigEndian16bit(writer, (ushort)flags);
IBuffer buffer = writer.DetachBuffer();
await WriteCharacteristicBytes(MovementCharacteristicConfigUuid, buffer);
isReading = true;
}
}
public async Task StopReading()
{
if (isReading)
{
// Writing 0x0000 powers the unit off.
isReading = false;
DataWriter writer = new DataWriter();
WriteBigEndian16bit(writer, 0);
IBuffer buffer = writer.DetachBuffer();
await WriteCharacteristicBytes(MovementCharacteristicConfigUuid, buffer);
}
}
/// <summary>
/// Get the rate at which accelerometer is being polled, in milliseconds.
/// </summary>
/// <returns>Returns the value read from the sensor or -1 if something goes wrong.</returns>
public async Task<int> GetPeriod()
{
// Resolution 10 ms. Range 100 ms (0x0A) to 2.55 sec (0xFF). Default 1 second (0x64).
byte v = await ReadCharacteristicByte(MovementCharacteristicPeriodUuid, Windows.Devices.Bluetooth.BluetoothCacheMode.Uncached);
return (int)(v * 10);
}
/// <summary>
/// Set the rate at which accelerometer is being polled, in milliseconds.
/// </summary>
/// <param name="milliseconds">The delay between updates, accurate only to 10ms intervals. Maximum value is 2550.</param>
public async Task SetPeriod(int milliseconds)
{
int delay = milliseconds / 10;
byte p = (byte)delay;
if (p < 1)
{
p = 1;
}
await WriteCharacteristicByte(MovementCharacteristicPeriodUuid, p);
}
private void OnMovementMeasurementValueChanged(MovementEventArgs args)
{
if (_movementValueChanged != null)
{
((EventHandler<MovementEventArgs>)_movementValueChanged)(this, args);
}
}
public async Task<bool> ConnectAsync(string deviceContainerId)
{
if (Version == 1)
{
throw new NotSupportedException();
}
return await this.ConnectAsync(MovementServiceUUid, deviceContainerId);
}
protected override void OnCharacteristicValueChanged(GattCharacteristic sender, GattValueChangedEventArgs eventArgs)
{
if (sender.Uuid == MovementCharacteristicUuid)
{
if (_movementValueChanged != null)
{
uint dataLength = eventArgs.CharacteristicValue.Length;
using (DataReader reader = DataReader.FromBuffer(eventArgs.CharacteristicValue))
{
if (dataLength == 18)
{
MovementMeasurement measurement = new MovementMeasurement();
short gx = ReadBigEndian16bit(reader);
short gy = ReadBigEndian16bit(reader);
short gz = ReadBigEndian16bit(reader);
short ax = ReadBigEndian16bit(reader);
short ay = ReadBigEndian16bit(reader);
short az = ReadBigEndian16bit(reader);
short mx = ReadBigEndian16bit(reader);
short my = ReadBigEndian16bit(reader);
short mz = ReadBigEndian16bit(reader);
measurement.GyroX = ((double)gx * 500.0) / 65536.0;
measurement.GyroY = ((double)gy * 500.0) / 65536.0;
measurement.GyroZ = ((double)gz * 500.0) / 65536.0;
measurement.AccelX = ((double)ax / 32768);
measurement.AccelY = ((double)ay / 32768);
measurement.AccelZ = ((double)az / 32768);
// on SensorTag CC2650 the conversion to micro tesla's is done in the firmware.
measurement.MagX = (double)mx;
measurement.MagY = (double)my;
measurement.MagZ = (double)mz;
OnMovementMeasurementValueChanged(new MovementEventArgs(measurement, eventArgs.Timestamp));
}
}
}
}
}
}
public class MovementMeasurement
{
/// <summary>
/// Get/Set X accelerometer in units of 1 g (9.81 m/s^2).
/// </summary>
public double AccelX { get; set;}
/// <summary>
/// Get/Set Y accelerometer in units of 1 g (9.81 m/s^2).
/// </summary>
public double AccelY { get; set;}
/// <summary>
/// Get/Set Z accelerometer in units of 1 g (9.81 m/s^2).
/// </summary>
public double AccelZ { get; set;}
/// <summary>
/// Get/Set X twist in degrees per second.
/// </summary>
public double GyroX { get; set; }
/// <summary>
/// Get/Set Y twist in degrees per second.
/// </summary>
public double GyroY { get; set; }
/// <summary>
/// Get/Set Z twist in degrees per second.
/// </summary>
public double GyroZ { get; set; }
/// <summary>
/// Get/Set X direction in units of 1 micro tesla.
/// </summary>
public double MagX { get; set; }
/// <summary>
/// Get/Set Y direction in units of 1 micro tesla.
/// </summary>
public double MagY { get; set; }
/// <summary>
/// Get/Set Z direction in units of 1 micro tesla.
/// </summary>
public double MagZ { get; set; }
public MovementMeasurement()
{
}
}
public class MovementEventArgs : EventArgs
{
public MovementEventArgs(MovementMeasurement measurement, DateTimeOffset timestamp)
{
Measurement = measurement;
Timestamp = timestamp;
}
public MovementMeasurement Measurement
{
get;
private set;
}
public DateTimeOffset Timestamp
{
get;
private set;
}
}
}