Skip to content
Snippets Groups Projects
Commit dd3e4251 authored by Daniel Schwietert's avatar Daniel Schwietert
Browse files

Add sensor data measurement and show it on screen

parent ac18b512
Branches
No related tags found
1 merge request!2Add sensor data measurement and show it on screen
File added
File added
File added
File added
#Thu Mar 04 15:27:51 CET 2021
gradle.version=6.1.1
File added
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "stress_detection",
"request": "launch",
"type": "dart"
}
]
}
\ No newline at end of file
org.gradle.jvmargs=-Xmx1536M
org.gradle.jvmargs=-Xmx512M
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_sensors/flutter_sensors.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool _accelAvailable = false;
bool _gyroAvailable = false;
List<double> _accelData = List.filled(3, 0.0);
List<double> _gyroData = List.filled(3, 0.0);
StreamSubscription _accelSubscription;
StreamSubscription _gyroSubscription;
@override
void initState() {
_checkAccelerometerStatus();
_checkGyroscopeStatus();
super.initState();
}
@override
void dispose() {
_stopAccelerometer();
_stopGyroscope();
super.dispose();
}
void _checkAccelerometerStatus() async {
await SensorManager()
.isSensorAvailable(Sensors.ACCELEROMETER)
.then((result) {
setState(() {
_accelAvailable = result;
});
});
}
Future<void> _startAccelerometer() async {
if (_accelSubscription != null) return;
if (_accelAvailable) {
final stream = await SensorManager().sensorUpdates(
sensorId: Sensors.ACCELEROMETER,
interval: Sensors.SENSOR_DELAY_FASTEST,
);
_accelSubscription = stream.listen((sensorEvent) {
setState(() {
_accelData = sensorEvent.data;
});
});
}
}
void main() => runApp(MaterialApp(
void _stopAccelerometer() {
if (_accelSubscription == null) return;
_accelSubscription.cancel();
_accelSubscription = null;
}
void _checkGyroscopeStatus() async {
await SensorManager().isSensorAvailable(Sensors.GYROSCOPE).then((result) {
setState(() {
_gyroAvailable = result;
});
});
}
Future<void> _startGyroscope() async {
if (_gyroSubscription != null) return;
if (_gyroAvailable) {
final stream =
await SensorManager().sensorUpdates(sensorId: Sensors.GYROSCOPE);
_gyroSubscription = stream.listen((sensorEvent) {
setState(() {
_gyroData = sensorEvent.data;
});
});
}
}
void _stopGyroscope() {
if (_gyroSubscription == null) return;
_gyroSubscription.cancel();
_gyroSubscription = null;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Diary Entry - Homepage'),
centerTitle: true ,
title: const Text('Sensor Data'),
),
body: Center(
child: Text(
'Welcome to your diary',
style: TextStyle(
fontSize: 20.0,
color: Colors.grey[600],
letterSpacing: 2.0,
fontWeight: FontWeight.bold,
body: Container(
padding: EdgeInsets.all(16.0),
alignment: AlignmentDirectional.topCenter,
child: Column(
children: <Widget>[
Text(
"Accelerometer Test",
textAlign: TextAlign.center,
),
Text(
"Accelerometer Enabled: $_accelAvailable",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[0](X) = ${_accelData[0]}",
textAlign: TextAlign.center,
),
floatingActionButton: FloatingActionButton(
child: Text('+'),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[1](Y) = ${_accelData[1]}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[2](Z) = ${_accelData[2]}",
textAlign: TextAlign.center,
),
));
Padding(padding: EdgeInsets.only(top: 16.0)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
MaterialButton(
child: Text("Start"),
color: Colors.green,
onPressed: _accelAvailable != null
? () => _startAccelerometer()
: null,
),
Padding(
padding: EdgeInsets.all(8.0),
),
MaterialButton(
child: Text("Stop"),
color: Colors.red,
onPressed: _accelAvailable != null
? () => _stopAccelerometer()
: null,
),
],
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"Gyroscope Test",
textAlign: TextAlign.center,
),
Text(
"Gyroscope Enabled: $_gyroAvailable",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[0](X) = ${_gyroData[0]}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[1](Y) = ${_gyroData[1]}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Text(
"[2](Z) = ${_gyroData[2]}",
textAlign: TextAlign.center,
),
Padding(padding: EdgeInsets.only(top: 16.0)),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
MaterialButton(
child: Text("Start"),
color: Colors.green,
onPressed:
_gyroAvailable != null ? () => _startGyroscope() : null,
),
Padding(
padding: EdgeInsets.all(8.0),
),
MaterialButton(
child: Text("Stop"),
color: Colors.red,
onPressed:
_gyroAvailable != null ? () => _stopGyroscope() : null,
),
],
),
],
),
),
),
);
}
}
\ No newline at end of file
......@@ -62,6 +62,13 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_sensors:
dependency: "direct main"
description:
name: flutter_sensors
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.6"
flutter_test:
dependency: "direct dev"
description: flutter
......
......@@ -25,6 +25,9 @@ dependencies:
sdk: flutter
flutter_sensors: ^0.1.5
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.0
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment