Skip to content

Commit

Permalink
[sensor_plus] Implement set*SamplingPeriod method call
Browse files Browse the repository at this point in the history
  • Loading branch information
JSUYA committed Nov 29, 2023
1 parent 54f8d05 commit a36d310
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 120 deletions.
7 changes: 7 additions & 0 deletions packages/sensors_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.1.4

* Implement set*SamplingPeriod method call.
* Update deprecated API in integration_test.
* Update sensors_plus to 4.0.1.
* Update sensors_plus_platform_interface to 1.2.0.

## 1.1.3

* Update sensors_plus to 3.0.2.
Expand Down
4 changes: 2 additions & 2 deletions packages/sensors_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ This package is not an _endorsed_ implementation of 'sensors_plus'. Therefore, y

```yaml
dependencies:
sensors_plus: ^3.0.2
sensors_plus_tizen: ^1.1.3
sensors_plus: ^4.0.1
sensors_plus_tizen: ^1.1.4
```
Then you can import `sensors_plus` in your Dart code:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';
import 'package:sensors_plus/sensors_plus.dart';
Expand All @@ -14,7 +13,8 @@ void main() {
(WidgetTester tester) async {
final completer = Completer<AccelerometerEvent>();
late StreamSubscription<AccelerometerEvent> subscription;
subscription = accelerometerEvents.listen((AccelerometerEvent event) {
subscription =
accelerometerEventStream().listen((AccelerometerEvent event) {
completer.complete(event);
subscription.cancel();
});
Expand Down
226 changes: 170 additions & 56 deletions packages/sensors_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,28 +50,32 @@ class MyHomePage extends StatefulWidget {
}

class _MyHomePageState extends State<MyHomePage> {
static const Duration _ignoreDuration = Duration(milliseconds: 20);

static const int _snakeRows = 20;
static const int _snakeColumns = 20;
static const double _snakeCellSize = 10.0;

List<double>? _userAccelerometerValues;
List<double>? _accelerometerValues;
List<double>? _gyroscopeValues;
List<double>? _magnetometerValues;
UserAccelerometerEvent? _userAccelerometerEvent;
AccelerometerEvent? _accelerometerEvent;
GyroscopeEvent? _gyroscopeEvent;
MagnetometerEvent? _magnetometerEvent;

DateTime? _userAccelerometerUpdateTime;
DateTime? _accelerometerUpdateTime;
DateTime? _gyroscopeUpdateTime;
DateTime? _magnetometerUpdateTime;

int? _userAccelerometerLastInterval;
int? _accelerometerLastInterval;
int? _gyroscopeLastInterval;
int? _magnetometerLastInterval;
final _streamSubscriptions = <StreamSubscription<dynamic>>[];

Duration sensorInterval = SensorInterval.normalInterval;

@override
Widget build(BuildContext context) {
final userAccelerometer = _userAccelerometerValues
?.map((double v) => v.toStringAsFixed(1))
.toList();
final accelerometer =
_accelerometerValues?.map((double v) => v.toStringAsFixed(1)).toList();
final gyroscope =
_gyroscopeValues?.map((double v) => v.toStringAsFixed(1)).toList();
final magnetometer =
_magnetometerValues?.map((double v) => v.toStringAsFixed(1)).toList();

return Scaffold(
appBar: AppBar(
title: const Text('Sensors Plus Example'),
Expand All @@ -97,40 +101,118 @@ class _MyHomePageState extends State<MyHomePage> {
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('UserAccelerometer: $userAccelerometer'),
],
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Accelerometer: $accelerometer'),
],
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Gyroscope: $gyroscope'),
padding: const EdgeInsets.all(20.0),
child: Table(
columnWidths: const {
0: FlexColumnWidth(4),
4: FlexColumnWidth(2),
},
children: [
const TableRow(
children: [
SizedBox.shrink(),
Text('X'),
Text('Y'),
Text('Z'),
Text('Interval'),
],
),
TableRow(
children: [
const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: Text('UserAccelerometer'),
),
Text(_userAccelerometerEvent?.x.toStringAsFixed(1) ?? '?'),
Text(_userAccelerometerEvent?.y.toStringAsFixed(1) ?? '?'),
Text(_userAccelerometerEvent?.z.toStringAsFixed(1) ?? '?'),
Text(
'${_userAccelerometerLastInterval?.toString() ?? '?'} ms'),
],
),
TableRow(
children: [
const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: Text('Accelerometer'),
),
Text(_accelerometerEvent?.x.toStringAsFixed(1) ?? '?'),
Text(_accelerometerEvent?.y.toStringAsFixed(1) ?? '?'),
Text(_accelerometerEvent?.z.toStringAsFixed(1) ?? '?'),
Text('${_accelerometerLastInterval?.toString() ?? '?'} ms'),
],
),
TableRow(
children: [
const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: Text('Gyroscope'),
),
Text(_gyroscopeEvent?.x.toStringAsFixed(1) ?? '?'),
Text(_gyroscopeEvent?.y.toStringAsFixed(1) ?? '?'),
Text(_gyroscopeEvent?.z.toStringAsFixed(1) ?? '?'),
Text('${_gyroscopeLastInterval?.toString() ?? '?'} ms'),
],
),
TableRow(
children: [
const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: Text('Magnetometer'),
),
Text(_magnetometerEvent?.x.toStringAsFixed(1) ?? '?'),
Text(_magnetometerEvent?.y.toStringAsFixed(1) ?? '?'),
Text(_magnetometerEvent?.z.toStringAsFixed(1) ?? '?'),
Text('${_magnetometerLastInterval?.toString() ?? '?'} ms'),
],
),
],
),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text('Magnetometer: $magnetometer'),
],
),
Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text('Update Interval:'),
SegmentedButton(
segments: [
ButtonSegment(
value: SensorInterval.gameInterval,
label: Text('Game\n'
'(${SensorInterval.gameInterval.inMilliseconds}ms)'),
),
ButtonSegment(
value: SensorInterval.uiInterval,
label: Text('UI\n'
'(${SensorInterval.uiInterval.inMilliseconds}ms)'),
),
ButtonSegment(
value: SensorInterval.normalInterval,
label: Text('Normal\n'
'(${SensorInterval.normalInterval.inMilliseconds}ms)'),
),
const ButtonSegment(
value: Duration(milliseconds: 500),
label: Text('500ms'),
),
const ButtonSegment(
value: Duration(seconds: 1),
label: Text('1s'),
),
],
selected: {sensorInterval},
showSelectedIcon: false,
onSelectionChanged: (value) {
setState(() {
sensorInterval = value.first;
userAccelerometerEventStream(
samplingPeriod: sensorInterval);
accelerometerEventStream(samplingPeriod: sensorInterval);
gyroscopeEventStream(samplingPeriod: sensorInterval);
magnetometerEventStream(samplingPeriod: sensorInterval);
});
},
),
],
),
],
),
Expand All @@ -149,11 +231,19 @@ class _MyHomePageState extends State<MyHomePage> {
void initState() {
super.initState();
_streamSubscriptions.add(
userAccelerometerEvents.listen(
userAccelerometerEventStream(samplingPeriod: sensorInterval).listen(
(UserAccelerometerEvent event) {
final now = DateTime.now();
setState(() {
_userAccelerometerValues = <double>[event.x, event.y, event.z];
_userAccelerometerEvent = event;
if (_userAccelerometerUpdateTime != null) {
final interval = now.difference(_userAccelerometerUpdateTime!);
if (interval > _ignoreDuration) {
_userAccelerometerLastInterval = interval.inMilliseconds;
}
}
});
_userAccelerometerUpdateTime = now;
},
onError: (e) {
showDialog(
Expand All @@ -162,19 +252,27 @@ class _MyHomePageState extends State<MyHomePage> {
return const AlertDialog(
title: Text("Sensor Not Found"),
content: Text(
"It seems that your device doesn't support Accelerometer Sensor"),
"It seems that your device doesn't support User Accelerometer Sensor"),
);
});
},
cancelOnError: true,
),
);
_streamSubscriptions.add(
accelerometerEvents.listen(
accelerometerEventStream(samplingPeriod: sensorInterval).listen(
(AccelerometerEvent event) {
final now = DateTime.now();
setState(() {
_accelerometerValues = <double>[event.x, event.y, event.z];
_accelerometerEvent = event;
if (_accelerometerUpdateTime != null) {
final interval = now.difference(_accelerometerUpdateTime!);
if (interval > _ignoreDuration) {
_accelerometerLastInterval = interval.inMilliseconds;
}
}
});
_accelerometerUpdateTime = now;
},
onError: (e) {
showDialog(
Expand All @@ -183,19 +281,27 @@ class _MyHomePageState extends State<MyHomePage> {
return const AlertDialog(
title: Text("Sensor Not Found"),
content: Text(
"It seems that your device doesn't support Gyroscope Sensor"),
"It seems that your device doesn't support Accelerometer Sensor"),
);
});
},
cancelOnError: true,
),
);
_streamSubscriptions.add(
gyroscopeEvents.listen(
gyroscopeEventStream(samplingPeriod: sensorInterval).listen(
(GyroscopeEvent event) {
final now = DateTime.now();
setState(() {
_gyroscopeValues = <double>[event.x, event.y, event.z];
_gyroscopeEvent = event;
if (_gyroscopeUpdateTime != null) {
final interval = now.difference(_gyroscopeUpdateTime!);
if (interval > _ignoreDuration) {
_gyroscopeLastInterval = interval.inMilliseconds;
}
}
});
_gyroscopeUpdateTime = now;
},
onError: (e) {
showDialog(
Expand All @@ -204,19 +310,27 @@ class _MyHomePageState extends State<MyHomePage> {
return const AlertDialog(
title: Text("Sensor Not Found"),
content: Text(
"It seems that your device doesn't support User Accelerometer Sensor"),
"It seems that your device doesn't support Gyroscope Sensor"),
);
});
},
cancelOnError: true,
),
);
_streamSubscriptions.add(
magnetometerEvents.listen(
magnetometerEventStream(samplingPeriod: sensorInterval).listen(
(MagnetometerEvent event) {
final now = DateTime.now();
setState(() {
_magnetometerValues = <double>[event.x, event.y, event.z];
_magnetometerEvent = event;
if (_magnetometerUpdateTime != null) {
final interval = now.difference(_magnetometerUpdateTime!);
if (interval > _ignoreDuration) {
_magnetometerLastInterval = interval.inMilliseconds;
}
}
});
_magnetometerUpdateTime = now;
},
onError: (e) {
showDialog(
Expand Down
2 changes: 1 addition & 1 deletion packages/sensors_plus/example/lib/snake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SnakeState extends State<Snake> {
void initState() {
super.initState();
_streamSubscription =
accelerometerEvents.listen((AccelerometerEvent event) {
accelerometerEventStream().listen((AccelerometerEvent event) {
setState(() {
acceleration = event;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/sensors_plus/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
sensors_plus: ^3.0.2
sensors_plus: ^4.0.1
sensors_plus_tizen:
path: ../

Expand Down
4 changes: 2 additions & 2 deletions packages/sensors_plus/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: sensors_plus_tizen
description: Tizen implementation of the sensors plugin.
homepage: https://github.com/flutter-tizen/plugins
repository: https://github.com/flutter-tizen/plugins/tree/master/packages/sensors_plus
version: 1.1.3
version: 1.1.4

environment:
sdk: ">=2.18.0 <4.0.0"
Expand All @@ -18,7 +18,7 @@ flutter:
dependencies:
flutter:
sdk: flutter
sensors_plus_platform_interface: ^1.1.3
sensors_plus_platform_interface: ^1.2.0

dev_dependencies:
flutter_lints: ^2.0.1
Loading

0 comments on commit a36d310

Please sign in to comment.