Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions lib/src/models/devices/open_ring_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class OpenRingFactory extends WearableFactory {
],
sensorHandler: sensorHandler,
),
OpenRingSensorConfiguration(
name: "PPG",
values: [
OpenRingSensorConfigurationValue(key: "On", cmd: OpenRingGatt.cmdPPGQ2, subOpcode: 0x01),
OpenRingSensorConfigurationValue(key: "Off", cmd: OpenRingGatt.cmdPPGQ2, subOpcode: 0x00),
],
sensorHandler: sensorHandler,
),
];
List<Sensor> sensors = [
OpenRingSensor(
Expand All @@ -56,6 +64,15 @@ class OpenRingFactory extends WearableFactory {
axisUnits: ["dps", "dps", "dps"],
sensorHandler: sensorHandler,
),
OpenRingSensor(
sensorId: OpenRingGatt.cmdPPGQ2,
sensorName: "PPG",
chartTitle: "PPG",
shortChartTitle: "PPG",
axisNames: ["Green", "Red", "Infrared"],
axisUnits: ["raw", "raw", "raw"],
sensorHandler: sensorHandler,
),
];

final w = OpenRing(
Expand Down
46 changes: 46 additions & 0 deletions lib/src/utils/sensor_value_parser/open_ring_value_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ class OpenRingValueParser extends SensorValueParser {
default:
throw Exception("Unknown sub-opcode for sensor data: $subOpcode");
}
case 0x32: // PPG Q2
switch (subOpcode) {
case 0x00:
result = const [];
case 0x01:
result = _parsePpg(
data: payload,
receiveTs: _lastTs,
baseHeader: baseHeader,
);
default:
throw Exception("Unknown sub-opcode for PPG data: $subOpcode");
}

default:
throw Exception("Unknown command: $cmd");
Expand Down Expand Up @@ -151,4 +164,37 @@ class OpenRingValueParser extends SensorValueParser {
'Z': data.getInt16(4, Endian.little),
};
}

List<Map<String, dynamic>> _parsePpg({
required ByteData data,
required int receiveTs,
required Map<String, dynamic> baseHeader,
}) {
if (data.lengthInBytes % 12 != 0) {
throw Exception("Invalid data length for PPG: ${data.lengthInBytes}");
}

final int nSamples = data.lengthInBytes ~/ 12;
if (nSamples == 0) return const [];

final List<Map<String, dynamic>> parsedData = [];
for (int i = 0; i < data.lengthInBytes; i += 12) {
final int sampleIndex = i ~/ 12;
final int ts = receiveTs + sampleIndex * _samplePeriodMs;

final ByteData sample = ByteData.sublistView(data, i, i + 12);

parsedData.add({
...baseHeader,
"timestamp": ts,
"PPG": {
"Green": sample.getInt32(0, Endian.little),
"Red": sample.getInt32(4, Endian.little),
"Infrared": sample.getInt32(8, Endian.little),
},
});
}

return parsedData;
}
}