pid_settings_screen.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. ListTile(
  73. title: const Text('Polling Interval (ms)'),
  74. subtitle: Text('${machineState.pidPollingInterval} ms'),
  75. trailing: SizedBox(
  76. width: 200,
  77. child: Slider(
  78. value: machineState.pidPollingInterval.toDouble().clamp(
  79. 1000.0,
  80. 10000.0,
  81. ),
  82. min: 1000,
  83. max: 10000,
  84. divisions: 18, // 500 ms steps
  85. label: '${machineState.pidPollingInterval} ms',
  86. onChanged: (v) {
  87. bluetoothService.updatePIDLimitSettings(interval: v.round());
  88. },
  89. ),
  90. ),
  91. ),
  92. ],
  93. ),
  94. );
  95. }
  96. }
  97. class _PIDSlider extends StatelessWidget {
  98. final String label;
  99. final double value;
  100. final double min;
  101. final double max;
  102. final double step;
  103. final int decimalPlaces;
  104. final ValueChanged<double> onChanged;
  105. const _PIDSlider({
  106. required this.label,
  107. required this.value,
  108. required this.min,
  109. required this.max,
  110. this.step = 1,
  111. this.decimalPlaces = 0,
  112. required this.onChanged,
  113. });
  114. @override
  115. Widget build(BuildContext context) {
  116. final rounded = value.toStringAsFixed(decimalPlaces);
  117. final reading = double.tryParse(rounded) == value
  118. ? rounded
  119. : value.toString();
  120. return Column(
  121. crossAxisAlignment: CrossAxisAlignment.start,
  122. children: [
  123. Padding(
  124. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  125. child: Text('$label: $reading'),
  126. ),
  127. Slider(
  128. value: value.clamp(min, max),
  129. min: min,
  130. max: max,
  131. divisions: ((max - min) / step).round(),
  132. label: reading,
  133. onChanged: (v) => onChanged(
  134. double.parse(
  135. (min + ((v - min) / step).round() * step).toStringAsFixed(
  136. decimalPlaces,
  137. ),
  138. ),
  139. ),
  140. ),
  141. ],
  142. );
  143. }
  144. }