Skip to content

Commit

Permalink
Merge pull request #18 from UteSpiske/main
Browse files Browse the repository at this point in the history
Implementation Start/Stop Button
  • Loading branch information
andreped authored Feb 1, 2023
2 parents bc4105b + caf2a60 commit fb8262c
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Test_Flutter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '2.10.4'
flutter-version: '3.7.0'

- name: Install dependencies
run: cd sw_app/ && flutter pub get
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_APK.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '2.10.4'
flutter-version: '3.7.0'

- name: Build APK
run: |
Expand Down
2 changes: 1 addition & 1 deletion sw_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ class MyApp extends StatelessWidget{
home: Home(),
);
}
}
}
176 changes: 176 additions & 0 deletions sw_app/lib/widgets/datarecording.dart
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();
}
}
17 changes: 9 additions & 8 deletions sw_app/lib/widgets/home.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:flutter/material.dart';
import 'data.dart';
import 'charts.dart';
import 'datarecording.dart';

void main() {
runApp(const Home());
}

class Home extends StatelessWidget {
const Home({key});
Expand All @@ -13,26 +11,29 @@ class Home extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
home: DefaultTabController(
length: 2,
length: 3,
child: Scaffold(
appBar: AppBar(
bottom: const TabBar(
tabs: [
Tab(icon: Icon(Icons.sensors)),
Tab(icon: Icon(Icons.show_chart_rounded)),
Tab(icon: Icon(Icons.access_alarm)),
],
),
title: const Text('DSS: Demo app'),
backgroundColor: Colors.redAccent,
),
body: const TabBarView(
body: TabBarView(
children: [
DataStream(),
Charts(),
const DataStream(),
const Charts(),
DataRecordingPage(),
],
),
),
),
);
}
}
}

24 changes: 20 additions & 4 deletions sw_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ packages:
dependency: "direct dev"
description:
name: flutter_lints
sha256: b543301ad291598523947dc534aaddc5aaad597b709d2426d3a0e0d44c5cb493
sha256: aeb0b80a8b3709709c9cc496cdc027c5b3216796bc0af0ce1007eaf24464fd4c
url: "https://pub.dev"
source: hosted
version: "1.0.4"
version: "2.0.1"
flutter_sensors:
dependency: "direct main"
description:
Expand Down Expand Up @@ -196,10 +196,10 @@ packages:
dependency: transitive
description:
name: lints
sha256: a2c3d198cb5ea2e179926622d433331d8b58374ab8f29cdda6e863bd62fd369c
sha256: "5e4a9cd06d447758280a8ac2405101e0e2094d2a1dbdd3756aec3fe7775ba593"
url: "https://pub.dev"
source: hosted
version: "1.0.1"
version: "2.0.1"
matcher:
dependency: transitive
description:
Expand All @@ -224,6 +224,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.0"
nested:
dependency: transitive
description:
name: nested
sha256: "03bac4c528c64c95c722ec99280375a6f2fc708eec17c7b3f07253b626cd2a20"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
path:
dependency: transitive
description:
Expand All @@ -248,6 +256,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.6.2"
provider:
dependency: "direct dev"
description:
name: provider
sha256: cdbe7530b12ecd9eb455bdaa2fcb8d4dad22e80b8afb4798b41479d5ce26847f
url: "https://pub.dev"
source: hosted
version: "6.0.5"
quiver:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion sw_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ dev_dependencies:
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
flutter_lints: ^2.0.0
provider: ^6.0.0

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
Expand Down

0 comments on commit fb8262c

Please sign in to comment.