|
| 1 | +import 'dart:async'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter/services.dart'; |
| 4 | +import 'package:pslab/communication/science_lab.dart'; |
| 5 | +import 'package:pslab/others/logger_service.dart'; |
| 6 | +import '../others/science_lab_common.dart'; |
| 7 | + |
| 8 | +class RoboticArmStateProvider extends ChangeNotifier { |
| 9 | + final List<double> servoValues = [0, 0, 0, 0]; |
| 10 | + final int totalTimelineItems = 60; |
| 11 | + final double scrollAmountPerTick = 120; |
| 12 | + final List<List<double>> timelineDegrees = |
| 13 | + List.generate(60, (_) => List.filled(4, 0)); |
| 14 | + int timelinePosition = 0; |
| 15 | + bool isPlaying = false; |
| 16 | + |
| 17 | + late ScienceLab scienceLab; |
| 18 | + Timer? _debounceTimer; |
| 19 | + Timer? _timelineTimer; |
| 20 | + final ScrollController timelineScrollController = ScrollController(); |
| 21 | + |
| 22 | + Future<void> initialize() async { |
| 23 | + SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersiveSticky); |
| 24 | + SystemChrome.setPreferredOrientations([ |
| 25 | + DeviceOrientation.landscapeLeft, |
| 26 | + DeviceOrientation.landscapeRight, |
| 27 | + ]); |
| 28 | + scienceLab = |
| 29 | + ScienceLabCommon(ScienceLabCommon.communicationHandler).getScienceLab(); |
| 30 | + await scienceLab.connect(); |
| 31 | + } |
| 32 | + |
| 33 | + void disposeResources() { |
| 34 | + _timelineTimer?.cancel(); |
| 35 | + _debounceTimer?.cancel(); |
| 36 | + timelineScrollController.dispose(); |
| 37 | + SystemChrome.setPreferredOrientations([ |
| 38 | + DeviceOrientation.portraitUp, |
| 39 | + DeviceOrientation.portraitDown, |
| 40 | + ]); |
| 41 | + } |
| 42 | + |
| 43 | + void updateServoValue(int index, double value) { |
| 44 | + servoValues[index] = value; |
| 45 | + notifyListeners(); |
| 46 | + _sendAllServoCommands(); |
| 47 | + } |
| 48 | + |
| 49 | + void updateTimelineDegree(int timeIndex, int servoIndex, double value) { |
| 50 | + timelineDegrees[timeIndex][servoIndex] = value; |
| 51 | + notifyListeners(); |
| 52 | + } |
| 53 | + |
| 54 | + void _sendAllServoCommands() { |
| 55 | + _debounceTimer?.cancel(); |
| 56 | + _debounceTimer = Timer(const Duration(milliseconds: 200), () async { |
| 57 | + try { |
| 58 | + await scienceLab.servo4( |
| 59 | + servoValues[0], |
| 60 | + servoValues[1], |
| 61 | + servoValues[2], |
| 62 | + servoValues[3], |
| 63 | + ); |
| 64 | + } catch (e) { |
| 65 | + logger.e(e); |
| 66 | + } |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + void togglePlayPause() { |
| 71 | + if (isPlaying) { |
| 72 | + _timelineTimer?.cancel(); |
| 73 | + isPlaying = false; |
| 74 | + notifyListeners(); |
| 75 | + } else { |
| 76 | + _timelineTimer = |
| 77 | + Timer.periodic(const Duration(seconds: 1), (timer) async { |
| 78 | + if (timelinePosition >= totalTimelineItems) { |
| 79 | + stopScrolling(resetPosition: false); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + timelineScrollController.animateTo( |
| 84 | + timelineScrollController.offset + scrollAmountPerTick, |
| 85 | + duration: const Duration(milliseconds: 50), |
| 86 | + curve: Curves.easeInOut, |
| 87 | + ); |
| 88 | + |
| 89 | + final angles = timelineDegrees[timelinePosition]; |
| 90 | + |
| 91 | + if (scienceLab.isConnected()) { |
| 92 | + try { |
| 93 | + await scienceLab.servo4( |
| 94 | + angles[0], |
| 95 | + angles[1], |
| 96 | + angles[2], |
| 97 | + angles[3], |
| 98 | + ); |
| 99 | + } catch (e) { |
| 100 | + logger.e(e); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + timelinePosition++; |
| 105 | + notifyListeners(); |
| 106 | + }); |
| 107 | + |
| 108 | + isPlaying = true; |
| 109 | + notifyListeners(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + void stopScrolling({bool resetPosition = true}) { |
| 114 | + _timelineTimer?.cancel(); |
| 115 | + isPlaying = false; |
| 116 | + timelinePosition = 0; |
| 117 | + notifyListeners(); |
| 118 | + |
| 119 | + if (resetPosition) { |
| 120 | + timelineScrollController.animateTo( |
| 121 | + 0, |
| 122 | + duration: const Duration(milliseconds: 50), |
| 123 | + curve: Curves.easeOut, |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments