Skip to content

Commit

Permalink
💾 Feat(Rotation): Better UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Mar 30, 2024
1 parent 03a7a6d commit 2881c29
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:collection';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sensors_plus/sensors_plus.dart';
Expand All @@ -15,16 +17,46 @@ class AccelerationDisplayStandState extends State<AccelerationDisplayStand> {
/// Acceleration x-axis, y-axis, z-axis
final accX = 0.0.obs, accY = 0.0.obs, accZ = 0.0.obs;

/// Sampling Rage
var samplingRate = 0.05.obs;

/// User accelerometer sensor data listener
StreamSubscription<UserAccelerometerEvent>? userAccelerometerDataListener;

/// Is listener paused
var listenerPaused = false.obs;

/// Chart related data
var minX = 0.0.obs, maxX = 0.05.obs, xCount = 10.obs;

/// Acceleration x-axis, y-axis, z-axis values
var xValues = <FlSpot>[], yValues = <FlSpot>[], zValues = <FlSpot>[];

@override
void initState() {
userAccelerometerDataListener = userAccelerometerEventStream(samplingPeriod: Duration(milliseconds: 50)).listen((event) {
accX.value = event.x;
accY.value = event.y;
accZ.value = event.z;
});
userAccelerometerDataListener = userAccelerometerEventStream(
samplingPeriod: Duration(milliseconds: (samplingRate * 1000).toInt()),
).listen(
(event) {
accX.value = event.x;
accY.value = event.y;
accZ.value = event.z;
maxX.value += samplingRate.value;
if ((maxX.value - minX.value) >= samplingRate.value * xCount.value) {
minX.value += samplingRate.value;
}
xValues.add(FlSpot(maxX.value, event.x));
yValues.add(FlSpot(maxX.value, event.y));
zValues.add(FlSpot(maxX.value, event.z));
if (xValues.length > xCount.value + 1) {
xValues.removeAt(0);
yValues.removeAt(0);
zValues.removeAt(0);
}
},
onError: (error) {},
cancelOnError: true,
);
super.initState();
}

Expand All @@ -42,14 +74,118 @@ class AccelerationDisplayStandState extends State<AccelerationDisplayStand> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Acceleration Data', style: TextStyle(fontSize: 32)),
Container(
height: 300,
margin: EdgeInsets.fromLTRB(0, 30, 20, 30),
child: Obx(
() => LineChart(
LineChartData(
minX: minX.value,
maxX: maxX.value,
minY: -5,
maxY: 5,
lineBarsData: [
LineChartBarData(
spots: xValues,
color: Colors.redAccent,
isStrokeCapRound: true,
isStrokeJoinRound: true,
),
LineChartBarData(
spots: yValues,
color: Colors.greenAccent,
isStrokeCapRound: true,
isStrokeJoinRound: true,
),
LineChartBarData(
spots: zValues,
color: Colors.blueAccent,
isStrokeCapRound: true,
isStrokeJoinRound: true,
),
],
titlesData: FlTitlesData(
show: true,
rightTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
topTitles: const AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
bottomTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
reservedSize: 40,
interval: 1,
getTitlesWidget: (a, b) => Padding(
padding: EdgeInsets.only(top: 10),
child: Text(a.toStringAsFixed(3).toString()),
),
),
),
leftTitles: AxisTitles(
sideTitles: SideTitles(
showTitles: true,
interval: 1,
getTitlesWidget: (a, b) => Text(a.toInt().toString()),
reservedSize: 30,
),
),
),
gridData: FlGridData(
show: true,
drawVerticalLine: true,
horizontalInterval: 1,
verticalInterval: samplingRate.value,
getDrawingHorizontalLine: (value) {
return const FlLine(
color: Colors.indigo,
strokeWidth: 1,
);
},
getDrawingVerticalLine: (value) {
return const FlLine(
color: Colors.indigo,
strokeWidth: 1,
);
},
),
borderData: FlBorderData(
show: true,
border: Border.all(color: Colors.indigo),
),
),
duration: const Duration(milliseconds: 0),
),
),
),
Obx(
() => Text('${accX > 0 ? "⏪" : "⏩"} \tx: ${accX.value}', style: TextStyle(fontSize: 16, color: Colors.redAccent)),
),
Obx(
() => Text('${accY > 0 ? "⏬" : "⏫"} \ty: ${accY.value}', style: TextStyle(fontSize: 16, color: Colors.greenAccent)),
),
Obx(
() => Text('x: ${accX.value}', style: TextStyle(fontSize: 14)),
() => Text('${accZ > 0 ? "⬇" : "⬆"} \tz: ${accZ.value}', style: TextStyle(fontSize: 16, color: Colors.blueAccent)),
),
Obx(
() => Text('y: ${accY.value}', style: TextStyle(fontSize: 14)),
() => Text('\tSampling Rate: ${samplingRate.value} s', style: TextStyle(fontSize: 16)),
),
const Text('↔ \tUnit: m/s^2', style: TextStyle(fontSize: 16)),
const SizedBox(height: 20),
Obx(
() => Text('z: ${accZ.value}', style: TextStyle(fontSize: 14)),
() => ElevatedButton(
onPressed: () {
if (userAccelerometerDataListener?.isPaused ?? true) {
userAccelerometerDataListener?.resume();
listenerPaused.value = false;
} else {
userAccelerometerDataListener?.pause();
listenerPaused.value = true;
}
},
child: listenerPaused.value ? const Text('Resume') : const Text("Pause"),
),
),
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class GyroscopeDisplayStandState extends State<GyroscopeDisplayStand> {
/// Gyroscope x-axis, y-axis, z-axis
final dirX = 0.0.obs, dirY = 0.0.obs, dirZ = 0.0.obs;

/// Gyroscope direction x, y, z
final directionX = 'none'.obs, directionY = 'none'.obs, directionZ = 'none'.obs;

/// Drawing canvas size
static double canvasWidth = 400, canvasHeight = 300;

/// Is drawing paused
var rotationPaused = false.obs;

/// Sampling Rage
var samplingRate = 0.05.obs;

/// Gyroscope sensor data listener
StreamSubscription<GyroscopeEvent>? gyroscopeDataListener;

Expand All @@ -36,21 +36,19 @@ class GyroscopeDisplayStandState extends State<GyroscopeDisplayStand> {
void initState() {
painter.initialize();

gyroscopeDataListener = gyroscopeEventStream(samplingPeriod: Duration(milliseconds: 50)).listen(
gyroscopeDataListener = gyroscopeEventStream(
samplingPeriod: Duration(milliseconds: (samplingRate.value * 1000).toInt()),
).listen(
(event) {
DeviceRotationHost.rotateWithAcceleration(event.x, event.y, event.z, 0.05);
DeviceRotationHost.rotateWithAcceleration(event.x, event.y, event.z, samplingRate.value);

dirX.value = event.x;
dirY.value = event.y;
dirZ.value = event.z;

directionX.value = dirX >= 0 ? '⇊' : '⇈';
directionY.value = dirY >= 0 ? '↻' : '↺';
directionZ.value = dirZ >= 0 ? '↶' : '↷';
},
onError: (error) {
Timer.periodic(Duration(milliseconds: 50), (timer) {
DeviceRotationHost.rotateWithAcceleration(0, 0, 0.5, 0.05);
DeviceRotationHost.rotateWithAcceleration(0.5, 0.5, 0.5, samplingRate.value);
});
},
cancelOnError: true,
Expand Down Expand Up @@ -122,14 +120,18 @@ class GyroscopeDisplayStandState extends State<GyroscopeDisplayStand> {
],
),
Obx(
() => Text('${directionX.value} x: ${dirX.value}', style: TextStyle(fontSize: 16)),
() => Text('${dirX >= 0 ? '⏬' : '⏫'} \tx: ${dirX.value}', style: TextStyle(fontSize: 16)),
),
Obx(
() => Text('${dirY >= 0 ? '⤵' : '⤴'} \ty: ${dirY.value}', style: TextStyle(fontSize: 16)),
),
Obx(
() => Text('${directionY.value} y: ${dirY.value}', style: TextStyle(fontSize: 16)),
() => Text('${dirZ >= 0 ? '⏪' : '⏩'} \tz: ${dirZ.value}', style: TextStyle(fontSize: 16)),
),
Obx(
() => Text('${directionZ.value} z: ${dirZ.value}', style: TextStyle(fontSize: 16)),
() => Text('\tSampling Rate: ${samplingRate.value} s', style: TextStyle(fontSize: 16)),
),
const Text('↔ \tUnit: rad/s', style: TextStyle(fontSize: 16)),
],
),
);
Expand Down
4 changes: 4 additions & 0 deletions kitx_mobile/lib/utils/emulators/rotation_emulator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class DeviceRotationHost {
np = zDirR * np * zDirRInv;
points[i] = np;
}

lastXDirR = xDirR;
lastYDirR = yDirR;
lastZDirR = zDirR;
}

/// Set points
Expand Down
8 changes: 8 additions & 0 deletions kitx_mobile/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
fl_chart:
dependency: "direct main"
description:
name: fl_chart
sha256: "2b7c1f5d867da9a054661641c8f499c55c47c39acccb97b3bc673f5fa9a39e74"
url: "https://pub.dev"
source: hosted
version: "0.67.0"
flutter:
dependency: "direct main"
description: flutter
Expand Down
1 change: 1 addition & 0 deletions kitx_mobile/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
crypto: ^3.0.3
device_info_plus: ^9.1.1
f_logs: ^2.0.1
fl_chart: ^0.67.0
flutter:
sdk: flutter
flutter_blue_plus: ^1.31.15
Expand Down

0 comments on commit 2881c29

Please sign in to comment.