-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from UteSpiske/main
Implementation Start/Stop Button
- Loading branch information
Showing
7 changed files
with
210 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ class MyApp extends StatelessWidget{ | |
home: Home(), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_sensors/flutter_sensors.dart'; | ||
import 'package:fl_chart/fl_chart.dart'; | ||
import 'dart:math'; | ||
import '../utils/datatypes.dart'; | ||
import '../utils/constants.dart' as _constants; | ||
|
||
|
||
class DataRecordingPage extends StatefulWidget { | ||
@override | ||
State<DataRecordingPage> createState() => _DataRecordingPageState(); | ||
} | ||
|
||
class _DataRecordingPageState extends State<DataRecordingPage> { | ||
double x = 0, y = 0, z = 0; | ||
String direction = "none"; | ||
|
||
// chart plotter variables | ||
var xPoints = <FlSpot>[const FlSpot(0, 0)]; | ||
var yPoints = <FlSpot>[const FlSpot(0, 0)]; | ||
var zPoints = <FlSpot>[const FlSpot(0, 0)]; | ||
|
||
double xValue = 0; | ||
double step = 0.05; | ||
|
||
var accelSubscription; | ||
var isStarted = false; | ||
|
||
void stream_accelerometer_data() async { | ||
final _stream = await SensorManager().sensorUpdates( | ||
sensorId: Sensors.ACCELEROMETER, | ||
interval: Sensors.SENSOR_DELAY_GAME, | ||
); | ||
|
||
accelSubscription = _stream.listen((sensorEvent) { | ||
final _accelData = sensorEvent.data; | ||
|
||
// update counter | ||
xValue++; | ||
|
||
// get coordinates | ||
x = _accelData[0]; | ||
y = _accelData[1]; | ||
z = _accelData[2]; | ||
|
||
// store result in history | ||
xPoints.add(FlSpot(xValue, x)); | ||
yPoints.add(FlSpot(xValue, y)); | ||
zPoints.add(FlSpot(xValue, z)); | ||
|
||
while ((xPoints.length > _constants.limitCount) && (xPoints.isNotEmpty)) { | ||
xPoints.removeAt(0); | ||
yPoints.removeAt(0); | ||
zPoints.removeAt(0); | ||
} | ||
|
||
setState(() { | ||
//this.isStarted = true; | ||
}); | ||
}); | ||
} | ||
|
||
void reset_variables() { | ||
accelSubscription.cancel(); | ||
|
||
this.xValue = 0; | ||
this.step = 0.05; | ||
|
||
this.x = 0; | ||
this.y = 0; | ||
this.z = 0; | ||
this.direction = "none"; | ||
|
||
// chart plotter variables | ||
this.xPoints = <FlSpot>[const FlSpot(0, 0)]; | ||
this.yPoints = <FlSpot>[const FlSpot(0, 0)]; | ||
this.zPoints = <FlSpot>[const FlSpot(0, 0)]; | ||
} | ||
|
||
SizedBox makeLineChart(List<FlSpot> points, Color currColor) { | ||
return SizedBox( | ||
width: 350, | ||
height: 150, | ||
child: LineChart( | ||
LineChartData( | ||
minY: points.map((abc) => abc.y).reduce(min) - 0.05, | ||
maxY: points.map((abc) => abc.y).reduce(max) + 0.05, | ||
minX: points.first.x, | ||
maxX: points.last.x, | ||
lineTouchData: LineTouchData(enabled: false), | ||
clipData: FlClipData.all(), | ||
gridData: FlGridData( | ||
show: true, | ||
drawVerticalLine: false, | ||
), | ||
lineBarsData: [ | ||
historyLine(points, currColor), | ||
], | ||
titlesData: FlTitlesData( | ||
show: true, | ||
), | ||
), | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SingleChildScrollView( | ||
child: Column( | ||
children: <Widget>[ | ||
const SizedBox( | ||
height: 30, | ||
width: 20, | ||
), | ||
SizedBox( | ||
width: double.infinity, | ||
child: RichText( | ||
textAlign: TextAlign.center, | ||
text: const TextSpan( | ||
style: TextStyle( | ||
fontSize: 20.0, | ||
color: Colors.black, | ||
), | ||
children: <TextSpan>[ | ||
TextSpan(text: "Real time charts: "), | ||
TextSpan( | ||
text: 'x ', style: TextStyle(color: _constants.xColor)), | ||
TextSpan( | ||
text: 'y ', style: TextStyle(color: _constants.yColor)), | ||
TextSpan( | ||
text: 'z ', style: TextStyle(color: _constants.zColor)), | ||
], | ||
), | ||
), | ||
), | ||
const SizedBox( | ||
height: 10, | ||
width: 20, | ||
), | ||
makeLineChart(xPoints, _constants.xColor), | ||
makeLineChart(yPoints, _constants.yColor), | ||
makeLineChart(zPoints, _constants.zColor), | ||
ElevatedButton( | ||
onPressed: () { | ||
|
||
this.isStarted = !this.isStarted; | ||
this.isStarted ? stream_accelerometer_data() : reset_variables(); | ||
|
||
setState(() {}); | ||
}, | ||
style: ElevatedButton.styleFrom( | ||
backgroundColor: this.isStarted ? Colors.red : Colors.green, | ||
padding: EdgeInsets.symmetric(horizontal: 50, vertical: 20), | ||
textStyle: | ||
TextStyle(fontSize: 30, fontWeight: FontWeight.bold)), | ||
child: | ||
this.isStarted ? Text('Stop recording') : Text('Start Recording')) | ||
], | ||
), | ||
); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
// need to close listener when class is inactive | ||
accelSubscription.cancel(); | ||
|
||
super.dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters