Skip to content

Commit

Permalink
🔧 Fix(Sensor): Random sensor data timer cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Mar 30, 2024
1 parent 8eeb9b1 commit c583726
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class AccelerationDisplayStandState extends State<AccelerationDisplayStand> {
/// Acceleration x-axis, y-axis, z-axis values
var xValues = <FlSpot>[], yValues = <FlSpot>[], zValues = <FlSpot>[];

/// Random sensor data timer
Timer? randomSensorDataTimer;

@override
void initState() {
userAccelerometerDataListener = userAccelerometerEventStream(
Expand Down Expand Up @@ -59,7 +62,7 @@ class AccelerationDisplayStandState extends State<AccelerationDisplayStand> {

var random = Random(114514);

Timer.periodic(Duration(milliseconds: (samplingRate * 1000).toInt()), (timer) {
randomSensorDataTimer = Timer.periodic(Duration(milliseconds: (samplingRate * 1000).toInt()), (timer) {
accX.value = random.nextDouble() * 10 - 5;
accY.value = random.nextDouble() * 10 - 5;
accZ.value = random.nextDouble() * 10 - 5;
Expand All @@ -85,6 +88,7 @@ class AccelerationDisplayStandState extends State<AccelerationDisplayStand> {
@override
void dispose() {
userAccelerometerDataListener?.cancel();
randomSensorDataTimer?.cancel();
super.dispose();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class GyroscopeDisplayStandState extends State<GyroscopeDisplayStand> {
/// Painter
Painter painter = Painter();

/// Random sensor data timer
Timer? randomSensorDataTimer;

@override
void initState() {
painter.initialize();
Expand All @@ -53,7 +56,7 @@ class GyroscopeDisplayStandState extends State<GyroscopeDisplayStand> {

var random = Random(114514);

Timer.periodic(Duration(milliseconds: 50), (timer) {
randomSensorDataTimer = Timer.periodic(Duration(milliseconds: 50), (timer) {
var rad = 0.5 + random.nextDouble() / 10;

dirY.value = rad;
Expand All @@ -70,6 +73,7 @@ class GyroscopeDisplayStandState extends State<GyroscopeDisplayStand> {
@override
void dispose() {
gyroscopeDataListener?.cancel();
randomSensorDataTimer?.cancel();
super.dispose();
}

Expand Down

0 comments on commit c583726

Please sign in to comment.