Skip to content

Commit

Permalink
Introduce multiple types for sensor values
Browse files Browse the repository at this point in the history
  • Loading branch information
cadivus committed Dec 20, 2024
1 parent 9521a86 commit e668aa1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
15 changes: 12 additions & 3 deletions example/lib/widgets/sensor_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,18 @@ class SensorView extends StatelessWidget {
List<String> renderedValues = [];
if (snapshot.hasData) {
final sensorValue = snapshot.data!;
renderedValues = sensorValue.values
.map((v) => v.toStringAsFixed(2).padLeft(7, ' '))
.toList();
renderedValues = sensorValue.valueStrings;

if (sensorValue is SensorDoubleValue) {
renderedValues = sensorValue.values
.map((v) => v.toStringAsFixed(2).padLeft(7, ' '))
.toList();
} else if (sensorValue is SensorIntValue) {
renderedValues = sensorValue.values
.map((v) => v.toString().padLeft(7, ' '))
.toList();
}

} else {
renderedValues = List.generate(
sensor.axisCount, (_) => "#.##".padLeft(7, ' '));
Expand Down
46 changes: 42 additions & 4 deletions lib/src/models/capabilities/sensor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,51 @@ abstract class Sensor {
}

class SensorValue {
final List<double> values;
final List<String> _valuesStrings;
final int timestamp;

int get dimensions => values.length;
int get dimensions => _valuesStrings.length;

List<String> get valueStrings => _valuesStrings;

const SensorValue({
required this.values,
required List<String> valueStrings,
required this.timestamp,
});
}) : _valuesStrings = valueStrings;
}

class SensorDoubleValue extends SensorValue {
final List<double> values;

const SensorDoubleValue({
required this.values,
required int timestamp,
}) : super(
valueStrings: const [],
timestamp: timestamp,
);

@override
int get dimensions => values.length;

@override
List<String> get valueStrings => values.map((e) => e.toString()).toList();
}

class SensorIntValue extends SensorValue {
final List<int> values;

const SensorIntValue({
required this.values,
required int timestamp,
}) : super(
valueStrings: const [],
timestamp: timestamp,
);

@override
int get dimensions => values.length;

@override
List<String> get valueStrings => values.map((e) => e.toString()).toList();
}
8 changes: 4 additions & 4 deletions lib/src/models/devices/cosinuss_one.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class _CosinussOneSensor extends Sensor {
int accZ = bytes[18];

streamController.add(
SensorValue(
SensorDoubleValue(
values: [accX.toDouble(), accY.toDouble(), accZ.toDouble()],
timestamp: DateTime.now().millisecondsSinceEpoch - startTime,
),
Expand Down Expand Up @@ -228,7 +228,7 @@ class _CosinussOneSensor extends Sensor {
32; // ambient light sensor (e.g., if sensor is not placed correctly)

streamController.add(
SensorValue(
SensorDoubleValue(
values: [
ppgRed.toDouble(),
ppgGreen.toDouble(),
Expand Down Expand Up @@ -268,7 +268,7 @@ class _CosinussOneSensor extends Sensor {
}

streamController.add(
SensorValue(
SensorDoubleValue(
values: [temperature],
timestamp: DateTime.now().millisecondsSinceEpoch - startTime,
),
Expand Down Expand Up @@ -300,7 +300,7 @@ class _CosinussOneSensor extends Sensor {
}

streamController.add(
SensorValue(
SensorDoubleValue(
values: [bpm.toDouble()],
timestamp: DateTime.now().millisecondsSinceEpoch - startTime,
),
Expand Down
4 changes: 2 additions & 2 deletions lib/src/models/devices/open_earable_v1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ class _OpenEarableSensor extends Sensor {
_dataSubscription = _sensorManager.subscribeToSensorData(0).listen((data) {
int timestamp = data["timestamp"];

SensorValue sensorValue = SensorValue(
SensorValue sensorValue = SensorDoubleValue(
values: [
kalmanX.filtered(data[sensorName]["X"]),
kalmanY.filtered(data[sensorName]["Y"]),
Expand All @@ -436,7 +436,7 @@ class _OpenEarableSensor extends Sensor {
_dataSubscription = _sensorManager.subscribeToSensorData(1).listen((data) {
int timestamp = data["timestamp"];

SensorValue sensorValue = SensorValue(
SensorValue sensorValue = SensorDoubleValue(
values: [data[sensorName][componentName]],
timestamp: timestamp,
);
Expand Down

0 comments on commit e668aa1

Please sign in to comment.