pid_settings_screen.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../services/bluetooth_service.dart';
  4. import '../services/me_coffee_protocol.dart';
  5. class PIDSettingsScreen extends ConsumerWidget {
  6. const PIDSettingsScreen({super.key});
  7. @override
  8. Widget build(BuildContext context, WidgetRef ref) {
  9. final machineState = ref.watch(bluetoothServiceProvider);
  10. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  11. final isLegacyV4 = machineState.isLegacyV4Firmware;
  12. return Scaffold(
  13. appBar: AppBar(title: const Text('PID Tuning')),
  14. body: ListView(
  15. padding: const EdgeInsets.all(16),
  16. children: [
  17. Text(
  18. isLegacyV4
  19. ? 'V4 defaults: P 25, I 3, D 128, I limit 3750.'
  20. : 'Defaults: P 20, I 0.30, D 128, I limit 20%.',
  21. ),
  22. _PIDSlider(
  23. label: 'Proportional (P)',
  24. value: machineState.pidP,
  25. min: 0,
  26. max: 100,
  27. onChanged: (v) => bluetoothService.updatePIDSettings(p: v),
  28. ),
  29. _PIDSlider(
  30. label: 'Integral (I)',
  31. value: machineState.pidI,
  32. min: 0,
  33. max: isLegacyV4 ? 10 : 1,
  34. step: isLegacyV4 ? 1 : 0.01,
  35. decimalPlaces: isLegacyV4 ? 0 : 2,
  36. onChanged: (v) => bluetoothService.updatePIDSettings(i: v),
  37. ),
  38. _PIDSlider(
  39. label: 'Derivative (D)',
  40. value: machineState.pidD,
  41. min: 0,
  42. max: 256,
  43. onChanged: (v) => bluetoothService.updatePIDSettings(d: v),
  44. ),
  45. const Divider(),
  46. _PIDSlider(
  47. label: 'Pro-Active Percentage',
  48. value: machineState.proActivePercentage.toDouble(),
  49. min: 0,
  50. max: 100,
  51. onChanged: (v) => bluetoothService.updateProActive(v.toInt()),
  52. ),
  53. const Divider(),
  54. if (!isLegacyV4)
  55. _PIDSlider(
  56. label: 'I-Wind-down Min (%)',
  57. value: machineState.pidIWindDownMin,
  58. min: 0,
  59. max: MeCoffeeProtocol.maxPidLimitPercent.toDouble(),
  60. onChanged: (v) => bluetoothService.updatePIDLimitSettings(min: v),
  61. ),
  62. _PIDSlider(
  63. label: isLegacyV4 ? 'I-Wind-down Max (raw)' : 'I-Wind-down Max (%)',
  64. value: machineState.pidIWindDownMax,
  65. min: 0,
  66. max: isLegacyV4
  67. ? 6000
  68. : MeCoffeeProtocol.maxPidLimitPercent.toDouble(),
  69. step: isLegacyV4 ? 250 : 1,
  70. onChanged: (v) => bluetoothService.updatePIDLimitSettings(max: v),
  71. ),
  72. _PIDSlider(
  73. label: 'Polling Interval (ms)',
  74. value: machineState.pidPollingInterval.toDouble(),
  75. min: 1000,
  76. max: 10000,
  77. step: 500,
  78. onChanged: (v) =>
  79. bluetoothService.updatePIDLimitSettings(interval: v.round()),
  80. ),
  81. ],
  82. ),
  83. );
  84. }
  85. }
  86. /// Shows the dragged value immediately but sends it only on release: every
  87. /// `cmd set` rewrites controller EEPROM, so per-tick writes wear it out.
  88. class _PIDSlider extends StatefulWidget {
  89. final String label;
  90. final double value;
  91. final double min;
  92. final double max;
  93. final double step;
  94. final int decimalPlaces;
  95. final ValueChanged<double> onChanged;
  96. const _PIDSlider({
  97. required this.label,
  98. required this.value,
  99. required this.min,
  100. required this.max,
  101. this.step = 1,
  102. this.decimalPlaces = 0,
  103. required this.onChanged,
  104. });
  105. @override
  106. State<_PIDSlider> createState() => _PIDSliderState();
  107. }
  108. class _PIDSliderState extends State<_PIDSlider> {
  109. double? _draft;
  110. double _snap(double v) => double.parse(
  111. (widget.min + ((v - widget.min) / widget.step).round() * widget.step)
  112. .toStringAsFixed(widget.decimalPlaces),
  113. );
  114. @override
  115. void didUpdateWidget(covariant _PIDSlider oldWidget) {
  116. super.didUpdateWidget(oldWidget);
  117. if (oldWidget.value != widget.value) _draft = null;
  118. }
  119. @override
  120. Widget build(BuildContext context) {
  121. final value = _draft ?? widget.value;
  122. final rounded = value.toStringAsFixed(widget.decimalPlaces);
  123. final reading = double.tryParse(rounded) == value
  124. ? rounded
  125. : value.toString();
  126. return Column(
  127. crossAxisAlignment: CrossAxisAlignment.start,
  128. children: [
  129. Padding(
  130. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  131. child: Text('${widget.label}: $reading'),
  132. ),
  133. Slider(
  134. value: value.clamp(widget.min, widget.max),
  135. min: widget.min,
  136. max: widget.max,
  137. divisions: ((widget.max - widget.min) / widget.step).round(),
  138. label: reading,
  139. onChanged: (v) => setState(() => _draft = _snap(v)),
  140. onChangeEnd: (v) {
  141. setState(() => _draft = null);
  142. widget.onChanged(_snap(v));
  143. },
  144. ),
  145. ],
  146. );
  147. }
  148. }