Skip to content

Commit 03dae0d

Browse files
committed
api v3.4.0 commit
1 parent ddf09cc commit 03dae0d

27 files changed

+324
-140
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Then, add the compile element to the dependencies closure in the module's ``buil
2020

2121
```gradle
2222
dependencies {
23-
compile 'com.mbientlab:metawear:3.3.0'
23+
compile 'com.mbientlab:metawear:3.4.0'
2424
}
2525
```
2626

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ android {
3131
defaultConfig {
3232
minSdkVersion 18
3333
targetSdkVersion 27
34-
versionCode 54
35-
versionName "3.3.0"
34+
versionCode 55
35+
versionName "3.4.0"
3636
}
3737
buildTypes {
3838
release {

library/src/main/java/com/mbientlab/metawear/Data.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@ public interface Data {
5858
* @return Array of valid classes
5959
*/
6060
Class<?>[] types();
61+
6162
/**
6263
* Converts the data bytes to a usable data type
63-
* @param clazz Class type to convert to
64+
* @param clazz Class type to convert the value to
6465
* @param <T> Runtime type the return value is casted as
6566
* @return Data value as the specified type
6667
* @throws ClassCastException if the data cannot be casted to desired type
6768
*/
6869
<T> T value(Class<T> clazz);
70+
/**
71+
* Extra information attached to this data sample
72+
* @param clazz Class type to convert the value to
73+
* @param <T> Runtime type the return value is casted as
74+
* @return Extra data casted as the specified type
75+
* @throws ClassCastException if the data cannot be casted to the desired type
76+
*/
77+
<T> T extra(Class<T> clazz);
6978
}

library/src/main/java/com/mbientlab/metawear/DataToken.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@
3030
* @author Eric Tsai
3131
*/
3232
public interface DataToken {
33+
/**
34+
* Creates a <code>DataToken</code> copy that represents a portion of the original data
35+
* @param offset Byte to start copying from
36+
* @param length Number of bytes to copy
37+
*/
38+
DataToken slice(byte offset, byte length);
3339
}

library/src/main/java/com/mbientlab/metawear/builder/RouteComponent.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package com.mbientlab.metawear.builder;
2626

2727
import com.mbientlab.metawear.CodeBlock;
28+
import com.mbientlab.metawear.Data;
2829
import com.mbientlab.metawear.DataToken;
2930
import com.mbientlab.metawear.Subscriber;
3031
import com.mbientlab.metawear.builder.filter.*;
@@ -269,9 +270,29 @@ interface Action {
269270
* @return Object representing the output of the packer
270271
*/
271272
RouteComponent pack(byte count);
273+
272274
/**
273-
* Add additional information to the payload to reconstruct timestamps from streamed data
274-
* @return Object representing the output of the accounter
275+
* Types of information the accounter processor can append to the data
276+
* @author Eric Tsai
277+
*/
278+
enum AccountType {
279+
/**
280+
* Append a looping counter to all data.
281+
* The counter's value is accessed by calling {@link Data#extra(Class)} with the <code>Long</code> type
282+
*/
283+
COUNT,
284+
/** Extra information used to calculate actual timestamps for streamed data */
285+
TIME
286+
}
287+
/**
288+
* Variant of {@link #account(AccountType)} that defaults to recalculating timestamps
289+
* @return Object representing the accounter output
275290
*/
276291
RouteComponent account();
292+
/**
293+
* Add additional information to the payload to assist in checking if streamed data is lost
294+
* @param type Type of information to append to the data<
295+
* @return Object representing the accounter output
296+
*/
297+
RouteComponent account(AccountType type);
277298
}

library/src/main/java/com/mbientlab/metawear/impl/AccelerometerBoschImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ protected float scale(MetaWearBoardPrivate mwPrivate) {
129129
}
130130

131131
@Override
132-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp) {
132+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp, DataPrivate.ClassToObject mapper) {
133133
ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
134134
short[] unscaled = new short[]{buffer.getShort(), buffer.getShort(), buffer.getShort()};
135135
final float scale= scale(mwPrivate);
136136
final Acceleration value= new Acceleration(unscaled[0] / scale, unscaled[1] / scale, unscaled[2] / scale);
137137

138-
return new DataPrivate(timestamp, data) {
138+
return new DataPrivate(timestamp, data, mapper) {
139139
@Override
140140
public float scale() {
141141
return scale;
@@ -196,11 +196,11 @@ public DataTypeBase copy(DataTypeBase input, Constant.Module module, byte regist
196196
}
197197

198198
@Override
199-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, byte[] data, Calendar timestamp) {
199+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
200200
int mask = mwPrivate.lookupModuleInfo(ACCELEROMETER).revision >= FLAT_REVISION ? 0x4 : 0x2;
201201
final boolean isFlat = (data[0] & mask) == mask;
202202

203-
return new DataPrivate(timestamp, data) {
203+
return new DataPrivate(timestamp, data, mapper) {
204204
@Override
205205
public <T> T value(Class<T> clazz) {
206206
if (clazz.equals(Boolean.class)) {
@@ -233,10 +233,10 @@ public DataTypeBase copy(DataTypeBase input, Constant.Module module, byte regist
233233
}
234234

235235
@Override
236-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, byte[] data, Calendar timestamp) {
236+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
237237
final SensorOrientation orientation = SensorOrientation.values()[((data[0] & 0x6) >> 1) + 4 * ((data[0] & 0x8) >> 3)];
238238

239-
return new DataPrivate(timestamp, data) {
239+
return new DataPrivate(timestamp, data, mapper) {
240240
@Override
241241
public <T> T value(Class<T> clazz) {
242242
if (clazz.equals(SensorOrientation.class)) {
@@ -274,7 +274,7 @@ private boolean highG(CartesianAxis axis, byte value) {
274274
}
275275

276276
@Override
277-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp) {
277+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
278278
final byte highFirst = (byte) ((data[0] & 0x1c) >> 2);
279279
final LowHighResponse castedData = new LowHighResponse(
280280
(data[0] & 0x1) == 0x1,
@@ -284,7 +284,7 @@ public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final
284284
highG(CartesianAxis.Z, highFirst),
285285
(data[0] & 0x20) == 0x20 ? Sign.NEGATIVE : Sign.POSITIVE);
286286

287-
return new DataPrivate(timestamp, data) {
287+
return new DataPrivate(timestamp, data, mapper) {
288288
@Override
289289
public <T> T value(Class<T> clazz) {
290290
if (clazz.equals(LowHighResponse.class)) {
@@ -322,15 +322,15 @@ private boolean detected(CartesianAxis axis, byte value) {
322322
}
323323

324324
@Override
325-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp) {
325+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
326326
final AnyMotion castedData = new AnyMotion(
327327
(data[0] & 0x40) == 0x40 ? Sign.NEGATIVE : Sign.POSITIVE,
328328
detected(CartesianAxis.X, data[0]),
329329
detected(CartesianAxis.Y, data[0]),
330330
detected(CartesianAxis.Z, data[0])
331331
);
332332

333-
return new DataPrivate(timestamp, data) {
333+
return new DataPrivate(timestamp, data, mapper) {
334334
@Override
335335
public <T> T value(Class<T> clazz) {
336336
if (clazz.equals(AnyMotion.class)) {
@@ -363,7 +363,7 @@ public DataTypeBase copy(DataTypeBase input, Constant.Module module, byte regist
363363
}
364364

365365
@Override
366-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp) {
366+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
367367
TapType type = null;
368368
if ((data[0] & 0x1) == 0x1) {
369369
type = TapType.DOUBLE;
@@ -372,7 +372,7 @@ public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final
372372
}
373373

374374
final Tap castedData = new Tap(type, (data[0] & 0x20) == 0x20 ? Sign.NEGATIVE : Sign.POSITIVE);
375-
return new DataPrivate(timestamp, data) {
375+
return new DataPrivate(timestamp, data, mapper) {
376376
@Override
377377
public <T> T value(Class<T> clazz) {
378378
if (clazz.equals(Tap.class)) {

library/src/main/java/com/mbientlab/metawear/impl/AccelerometerMma8452qImpl.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ public DataTypeBase[] createSplits() {
183183
}
184184

185185
@Override
186-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp) {
186+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp, DataPrivate.ClassToObject mapper) {
187187
ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
188188
final float scale= scale(mwPrivate);
189189
final Acceleration value= new Acceleration(buffer.getShort() / scale, buffer.getShort() / scale, buffer.getShort() / scale);
190190

191-
return new DataPrivate(timestamp, data) {
191+
return new DataPrivate(timestamp, data, mapper) {
192192
@Override
193193
public float scale() {
194194
return scale;
@@ -249,12 +249,12 @@ public Number convertToFirmwareUnits(MetaWearBoardPrivate mwPrivate, Number valu
249249
}
250250

251251
@Override
252-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, byte[] data, Calendar timestamp) {
252+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
253253
int offset = (data[0] & 0x06) >> 1;
254254
int index = 4 * (data[0] & 0x01) + ((offset == 2 || offset == 3) ? offset ^ 0x1 : offset);
255255
final SensorOrientation orientation = SensorOrientation.values()[index];
256256

257-
return new DataPrivate(timestamp, data) {
257+
return new DataPrivate(timestamp, data, mapper) {
258258
@Override
259259
public <T> T value(Class<T> clazz) {
260260
if (clazz.equals(SensorOrientation.class)) {
@@ -300,13 +300,13 @@ private Sign direction(CartesianAxis axis, byte value) {
300300
return (value & mask) == mask ? Sign.NEGATIVE : Sign.POSITIVE;
301301
}
302302
@Override
303-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp) {
303+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
304304
final Movement castedData = new Movement(
305305
new boolean[] {exceedsThreshold(CartesianAxis.X, data[0]), exceedsThreshold(CartesianAxis.Y, data[0]), exceedsThreshold(CartesianAxis.Z, data[0])},
306306
new Sign[] {direction(CartesianAxis.X, data[0]), direction(CartesianAxis.Y, data[0]), direction(CartesianAxis.Z, data[0])}
307307
);
308308

309-
return new DataPrivate(timestamp, data) {
309+
return new DataPrivate(timestamp, data, mapper) {
310310
@Override
311311
public <T> T value(Class<T> clazz) {
312312
if (clazz.equals(Movement.class)) {
@@ -357,13 +357,13 @@ private TapType type(byte value) {
357357
}
358358

359359
@Override
360-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp) {
360+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
361361
final Tap castedData = new Tap(
362362
new boolean[] {active(CartesianAxis.X, data[0]), active(CartesianAxis.Y, data[0]), active(CartesianAxis.Z, data[0])},
363363
new Sign[] {polarity(CartesianAxis.X, data[0]), polarity(CartesianAxis.Y, data[0]), polarity(CartesianAxis.Z, data[0])},
364364
type(data[0])
365365
);
366-
return new DataPrivate(timestamp, data) {
366+
return new DataPrivate(timestamp, data, mapper) {
367367
@Override
368368
public <T> T value(Class<T> clazz) {
369369
if (clazz.equals(Tap.class)) {
@@ -411,13 +411,13 @@ private Sign direction(CartesianAxis axis, byte value) {
411411
}
412412

413413
@Override
414-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp) {
414+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, Calendar timestamp, DataPrivate.ClassToObject mapper) {
415415
final Movement castedData = new Movement(
416416
new boolean[] {exceedsThreshold(CartesianAxis.X, data[0]), exceedsThreshold(CartesianAxis.Y, data[0]), exceedsThreshold(CartesianAxis.Z, data[0])},
417417
new Sign[] {direction(CartesianAxis.X, data[0]), direction(CartesianAxis.Y, data[0]), direction(CartesianAxis.Z, data[0])}
418418
);
419419

420-
return new DataPrivate(timestamp, data) {
420+
return new DataPrivate(timestamp, data, mapper) {
421421
@Override
422422
public <T> T value(Class<T> clazz) {
423423
if (clazz.equals(Movement.class)) {

library/src/main/java/com/mbientlab/metawear/impl/ByteArrayData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public Number convertToFirmwareUnits(MetaWearBoardPrivate mwPrivate, Number valu
5858
}
5959

6060
@Override
61-
public Data createMessage(boolean logData, final MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp) {
62-
return new DataPrivate(timestamp, data) {
61+
public Data createMessage(boolean logData, final MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp, DataPrivate.ClassToObject mapper) {
62+
return new DataPrivate(timestamp, data, mapper) {
6363
@Override
6464
public Class<?>[] types() {
6565
return new Class<?>[] { byte[].class };

library/src/main/java/com/mbientlab/metawear/impl/ColorTcs34725Impl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public Number convertToFirmwareUnits(MetaWearBoardPrivate mwPrivate, Number valu
8585
}
8686

8787
@Override
88-
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp) {
88+
public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final byte[] data, final Calendar timestamp, DataPrivate.ClassToObject mapper) {
8989
ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN);
9090
final ColorAdc wrapper= new ColorAdc(
9191
buffer.getShort() & 0xffff,
@@ -94,7 +94,7 @@ public Data createMessage(boolean logData, MetaWearBoardPrivate mwPrivate, final
9494
buffer.getShort() & 0xffff
9595
);
9696

97-
return new DataPrivate(timestamp, data) {
97+
return new DataPrivate(timestamp, data, mapper) {
9898
@Override
9999
public Class<?>[] types() {
100100
return new Class<?>[] {ColorAdc.class};

library/src/main/java/com/mbientlab/metawear/impl/DataPrivate.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,18 @@
3333
* Created by etsai on 9/4/16.
3434
*/
3535
abstract class DataPrivate implements Data {
36+
interface ClassToObject {
37+
Object apply(Class<?> clazz);
38+
}
39+
3640
private final Calendar timestamp;
3741
private final byte[] dataBytes;
42+
private final ClassToObject mapper;
3843

39-
DataPrivate(Calendar timestamp, byte[] dataBytes) {
44+
DataPrivate(Calendar timestamp, byte[] dataBytes, ClassToObject mapper) {
4045
this.timestamp = timestamp;
4146
this.dataBytes = dataBytes;
47+
this.mapper = mapper;
4248
}
4349

4450
@Override
@@ -66,6 +72,15 @@ public <T> T value(Class<T> clazz) {
6672
throw new ClassCastException(String.format(Locale.US, "Invalid input class: \'%s\'", clazz.toString()));
6773
}
6874

75+
@Override
76+
public <T> T extra(Class<T> clazz) {
77+
Object value;
78+
if (mapper == null || (value = mapper.apply(clazz)) == null) {
79+
throw new ClassCastException(String.format(Locale.US, "Invalid input class: \'%s\'", clazz.toString()));
80+
}
81+
return clazz.cast(value);
82+
}
83+
6984
@Override
7085
public String toString() {
7186
return String.format(Locale.US, "{timestamp: %s, data: %s}", formattedTimestamp(), Util.arrayToHexString(bytes()));

0 commit comments

Comments
 (0)