-
Notifications
You must be signed in to change notification settings - Fork 815
feat: added Accelerometer screen #2709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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 hidden or 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,146 @@ | ||
import 'dart:async'; | ||
import 'dart:math'; | ||
import 'package:fl_chart/fl_chart.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:sensors_plus/sensors_plus.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
|
||
class AccelerometerStateProvider extends ChangeNotifier { | ||
AccelerometerEvent _accelerometerEvent = | ||
AccelerometerEvent(0, 0, 0, DateTime.now()); | ||
StreamSubscription? _accelerometerSubscription; | ||
final List<double> _xData = []; | ||
final List<double> _yData = []; | ||
final List<double> _zData = []; | ||
|
||
final List<FlSpot> xData = []; | ||
final List<FlSpot> yData = []; | ||
final List<FlSpot> zData = []; | ||
|
||
final int _maxLength = 50; | ||
double _xMin = 0, _xMax = 0; | ||
double _yMin = 0, _yMax = 0; | ||
double _zMin = 0, _zMax = 0; | ||
|
||
void initializeSensors() { | ||
_accelerometerSubscription = accelerometerEventStream().listen( | ||
(event) { | ||
_accelerometerEvent = event; | ||
_updateData(); | ||
notifyListeners(); | ||
}, | ||
onError: (error) { | ||
debugPrint("Accelerometer error: $error"); | ||
}, | ||
cancelOnError: true, | ||
); | ||
} | ||
|
||
void disposeSensors() { | ||
_accelerometerSubscription?.cancel(); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
disposeSensors(); | ||
super.dispose(); | ||
} | ||
|
||
void _updateData() { | ||
final x = _accelerometerEvent.x; | ||
final y = _accelerometerEvent.y; | ||
final z = _accelerometerEvent.z; | ||
|
||
_xData.add(x); | ||
_yData.add(y); | ||
_zData.add(z); | ||
|
||
if (_xData.length > _maxLength) _xData.removeAt(0); | ||
if (_yData.length > _maxLength) _yData.removeAt(0); | ||
if (_zData.length > _maxLength) _zData.removeAt(0); | ||
|
||
_xMin = _xData.reduce(min); | ||
_xMax = _xData.reduce(max); | ||
_yMin = _yData.reduce(min); | ||
_yMax = _yData.reduce(max); | ||
_zMin = _zData.reduce(min); | ||
_zMax = _zData.reduce(max); | ||
|
||
xData.clear(); | ||
yData.clear(); | ||
zData.clear(); | ||
|
||
for (int i = 0; i < _xData.length; i++) { | ||
xData.add(FlSpot(i.toDouble(), _xData[i])); | ||
yData.add(FlSpot(i.toDouble(), _yData[i])); | ||
zData.add(FlSpot(i.toDouble(), _zData[i])); | ||
} | ||
|
||
notifyListeners(); | ||
} | ||
|
||
List<FlSpot> getAxisData(String axis) { | ||
switch (axis) { | ||
case 'x': | ||
return xData; | ||
case 'y': | ||
return yData; | ||
case 'z': | ||
return zData; | ||
default: | ||
return []; | ||
} | ||
} | ||
|
||
double getMin(String axis) { | ||
switch (axis) { | ||
case 'x': | ||
return _xMin; | ||
case 'y': | ||
return _yMin; | ||
case 'z': | ||
return _zMin; | ||
default: | ||
return 0.0; | ||
} | ||
} | ||
|
||
double getMax(String axis) { | ||
switch (axis) { | ||
case 'x': | ||
return _xMax; | ||
case 'y': | ||
return _yMax; | ||
case 'z': | ||
return _zMax; | ||
default: | ||
return 0.0; | ||
} | ||
} | ||
|
||
double getCurrent(String axis) { | ||
switch (axis) { | ||
case 'x': | ||
return _accelerometerEvent.x; | ||
case 'y': | ||
return _accelerometerEvent.y; | ||
case 'z': | ||
return _accelerometerEvent.z; | ||
default: | ||
return 0.0; | ||
} | ||
} | ||
|
||
int getDataLength(String axis) { | ||
switch (axis) { | ||
case 'x': | ||
return xData.length; | ||
case 'y': | ||
return yData.length; | ||
case 'z': | ||
return zData.length; | ||
default: | ||
return 0; | ||
} | ||
} | ||
} |
This file contains hidden or 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,38 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:provider/provider.dart'; | ||
import 'package:pslab/providers/accelerometer_state_provider.dart'; | ||
import 'package:pslab/view/widgets/common_scaffold_widget.dart'; | ||
import 'package:pslab/view/widgets/accelerometer_card.dart'; | ||
|
||
class AccelerometerScreen extends StatefulWidget { | ||
const AccelerometerScreen({super.key}); | ||
|
||
@override | ||
State<StatefulWidget> createState() => _AccelerometerScreenState(); | ||
} | ||
|
||
class _AccelerometerScreenState extends State<AccelerometerScreen> { | ||
@override | ||
Widget build(BuildContext context) { | ||
return MultiProvider( | ||
providers: [ | ||
ChangeNotifierProvider<AccelerometerStateProvider>( | ||
create: (_) => AccelerometerStateProvider()..initializeSensors(), | ||
), | ||
], | ||
child: const CommonScaffold( | ||
title: 'Accelerometer', | ||
Vidhijain20 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
body: SafeArea( | ||
child: Column( | ||
children: [ | ||
Expanded( | ||
child: AccelerometerCard(color: Colors.yellow, axis: 'x')), | ||
Expanded( | ||
child: AccelerometerCard(color: Colors.purple, axis: 'y')), | ||
Expanded( | ||
child: AccelerometerCard(color: Colors.green, axis: 'z')), | ||
], | ||
))), | ||
); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.