forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.dart
170 lines (141 loc) · 6.38 KB
/
sensors.dart
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
// Copyright 2013 The Flutter Authors. 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/services.dart';
const EventChannel _accelerometerEventChannel =
EventChannel('plugins.flutter.io/sensors/accelerometer');
const EventChannel _userAccelerometerEventChannel =
EventChannel('plugins.flutter.io/sensors/user_accel');
const EventChannel _gyroscopeEventChannel =
EventChannel('plugins.flutter.io/sensors/gyroscope');
/// Discrete reading from an accelerometer. Accelerometers measure the velocity
/// of the device. Note that these readings include the effects of gravity. Put
/// simply, you can use accelerometer readings to tell if the device is moving in
/// a particular direction.
class AccelerometerEvent {
/// Contructs an instance with the given [x], [y], and [z] values.
AccelerometerEvent(this.x, this.y, this.z);
/// Acceleration force along the x axis (including gravity) measured in m/s^2.
///
/// When the device is held upright facing the user, positive values mean the
/// device is moving to the right and negative mean it is moving to the left.
final double x;
/// Acceleration force along the y axis (including gravity) measured in m/s^2.
///
/// When the device is held upright facing the user, positive values mean the
/// device is moving towards the sky and negative mean it is moving towards
/// the ground.
final double y;
/// Acceleration force along the z axis (including gravity) measured in m/s^2.
///
/// This uses a right-handed coordinate system. So when the device is held
/// upright and facing the user, positive values mean the device is moving
/// towards the user and negative mean it is moving away from them.
final double z;
@override
String toString() => '[AccelerometerEvent (x: $x, y: $y, z: $z)]';
}
/// Discrete reading from a gyroscope. Gyroscopes measure the rate or rotation of
/// the device in 3D space.
class GyroscopeEvent {
/// Contructs an instance with the given [x], [y], and [z] values.
GyroscopeEvent(this.x, this.y, this.z);
/// Rate of rotation around the x axis measured in rad/s.
///
/// When the device is held upright, this can also be thought of as describing
/// "pitch". The top of the device will tilt towards or away from the
/// user as this value changes.
final double x;
/// Rate of rotation around the y axis measured in rad/s.
///
/// When the device is held upright, this can also be thought of as describing
/// "yaw". The lengthwise edge of the device will rotate towards or away from
/// the user as this value changes.
final double y;
/// Rate of rotation around the z axis measured in rad/s.
///
/// When the device is held upright, this can also be thought of as describing
/// "roll". When this changes the face of the device should remain facing
/// forward, but the orientation will change from portrait to landscape and so
/// on.
final double z;
@override
String toString() => '[GyroscopeEvent (x: $x, y: $y, z: $z)]';
}
/// Like [AccelerometerEvent], this is a discrete reading from an accelerometer
/// and measures the velocity of the device. However, unlike
/// [AccelerometerEvent], this event does not include the effects of gravity.
class UserAccelerometerEvent {
/// Contructs an instance with the given [x], [y], and [z] values.
UserAccelerometerEvent(this.x, this.y, this.z);
/// Acceleration force along the x axis (excluding gravity) measured in m/s^2.
///
/// When the device is held upright facing the user, positive values mean the
/// device is moving to the right and negative mean it is moving to the left.
final double x;
/// Acceleration force along the y axis (excluding gravity) measured in m/s^2.
///
/// When the device is held upright facing the user, positive values mean the
/// device is moving towards the sky and negative mean it is moving towards
/// the ground.
final double y;
/// Acceleration force along the z axis (excluding gravity) measured in m/s^2.
///
/// This uses a right-handed coordinate system. So when the device is held
/// upright and facing the user, positive values mean the device is moving
/// towards the user and negative mean it is moving away from them.
final double z;
@override
String toString() => '[UserAccelerometerEvent (x: $x, y: $y, z: $z)]';
}
AccelerometerEvent _listToAccelerometerEvent(List<double> list) {
return AccelerometerEvent(list[0], list[1], list[2]);
}
UserAccelerometerEvent _listToUserAccelerometerEvent(List<double> list) {
return UserAccelerometerEvent(list[0], list[1], list[2]);
}
GyroscopeEvent _listToGyroscopeEvent(List<double> list) {
return GyroscopeEvent(list[0], list[1], list[2]);
}
Stream<AccelerometerEvent>? _accelerometerEvents;
Stream<GyroscopeEvent>? _gyroscopeEvents;
Stream<UserAccelerometerEvent>? _userAccelerometerEvents;
/// A broadcast stream of events from the device accelerometer.
Stream<AccelerometerEvent> get accelerometerEvents {
Stream<AccelerometerEvent>? accelerometerEvents = _accelerometerEvents;
if (accelerometerEvents == null) {
accelerometerEvents =
_accelerometerEventChannel.receiveBroadcastStream().map(
(dynamic event) =>
_listToAccelerometerEvent(event.cast<double>()),
);
_accelerometerEvents = accelerometerEvents;
}
return accelerometerEvents;
}
/// A broadcast stream of events from the device gyroscope.
Stream<GyroscopeEvent> get gyroscopeEvents {
Stream<GyroscopeEvent>? gyroscopeEvents = _gyroscopeEvents;
if (gyroscopeEvents == null) {
gyroscopeEvents = _gyroscopeEventChannel.receiveBroadcastStream().map(
(dynamic event) => _listToGyroscopeEvent(event.cast<double>()),
);
_gyroscopeEvents = gyroscopeEvents;
}
return gyroscopeEvents;
}
/// Events from the device accelerometer with gravity removed.
Stream<UserAccelerometerEvent> get userAccelerometerEvents {
Stream<UserAccelerometerEvent>? userAccelerometerEvents =
_userAccelerometerEvents;
if (userAccelerometerEvents == null) {
userAccelerometerEvents =
_userAccelerometerEventChannel.receiveBroadcastStream().map(
(dynamic event) =>
_listToUserAccelerometerEvent(event.cast<double>()),
);
_userAccelerometerEvents = userAccelerometerEvents;
}
return userAccelerometerEvents;
}