blob: 7b41a4959f94fe847df25829bcfb18e71f92722a [file] [log] [blame]
// Copyright 2019 The Chromium 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');
class AccelerometerEvent {
AccelerometerEvent(this.x, this.y, this.z);
/// Acceleration force along the x axis (including gravity) measured in m/s^2.
final double x;
/// Acceleration force along the y axis (including gravity) measured in m/s^2.
final double y;
/// Acceleration force along the z axis (including gravity) measured in m/s^2.
final double z;
@override
String toString() => '[AccelerometerEvent (x: $x, y: $y, z: $z)]';
}
class GyroscopeEvent {
GyroscopeEvent(this.x, this.y, this.z);
/// Rate of rotation around the x axis measured in rad/s.
final double x;
/// Rate of rotation around the y axis measured in rad/s.
final double y;
/// Rate of rotation around the z axis measured in rad/s.
final double z;
@override
String toString() => '[GyroscopeEvent (x: $x, y: $y, z: $z)]';
}
class UserAccelerometerEvent {
UserAccelerometerEvent(this.x, this.y, this.z);
/// Acceleration force along the x axis (excluding gravity) measured in m/s^2.
final double x;
/// Acceleration force along the y axis (excluding gravity) measured in m/s^2.
final double y;
/// Acceleration force along the z axis (excluding gravity) measured in m/s^2.
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 {
if (_accelerometerEvents == null) {
_accelerometerEvents = _accelerometerEventChannel
.receiveBroadcastStream()
.map(
(dynamic event) => _listToAccelerometerEvent(event.cast<double>()));
}
return _accelerometerEvents;
}
/// A broadcast stream of events from the device gyroscope.
Stream<GyroscopeEvent> get gyroscopeEvents {
if (_gyroscopeEvents == null) {
_gyroscopeEvents = _gyroscopeEventChannel
.receiveBroadcastStream()
.map((dynamic event) => _listToGyroscopeEvent(event.cast<double>()));
}
return _gyroscopeEvents;
}
/// Events from the device accelerometer with gravity removed.
Stream<UserAccelerometerEvent> get userAccelerometerEvents {
if (_userAccelerometerEvents == null) {
_userAccelerometerEvents = _userAccelerometerEventChannel
.receiveBroadcastStream()
.map((dynamic event) =>
_listToUserAccelerometerEvent(event.cast<double>()));
}
return _userAccelerometerEvents;
}