| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import '../services/bluetooth_service.dart';
- import '../services/me_coffee_protocol.dart';
- class PIDSettingsScreen extends ConsumerWidget {
- const PIDSettingsScreen({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final machineState = ref.watch(bluetoothServiceProvider);
- final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
- final isLegacyV4 = machineState.isLegacyV4Firmware;
- return Scaffold(
- appBar: AppBar(title: const Text('PID Tuning')),
- body: ListView(
- padding: const EdgeInsets.all(16),
- children: [
- Text(
- isLegacyV4
- ? 'V4 defaults: P 25, I 3, D 128, I limit 3750.'
- : 'Defaults: P 20, I 0.30, D 128, I limit 20%.',
- ),
- _PIDSlider(
- label: 'Proportional (P)',
- value: machineState.pidP,
- min: 0,
- max: 100,
- onChanged: (v) => bluetoothService.updatePIDSettings(p: v),
- ),
- _PIDSlider(
- label: 'Integral (I)',
- value: machineState.pidI,
- min: 0,
- max: isLegacyV4 ? 10 : 1,
- step: isLegacyV4 ? 1 : 0.01,
- decimalPlaces: isLegacyV4 ? 0 : 2,
- onChanged: (v) => bluetoothService.updatePIDSettings(i: v),
- ),
- _PIDSlider(
- label: 'Derivative (D)',
- value: machineState.pidD,
- min: 0,
- max: 256,
- onChanged: (v) => bluetoothService.updatePIDSettings(d: v),
- ),
- const Divider(),
- _PIDSlider(
- label: 'Pro-Active Percentage',
- value: machineState.proActivePercentage.toDouble(),
- min: 0,
- max: 100,
- onChanged: (v) => bluetoothService.updateProActive(v.toInt()),
- ),
- const Divider(),
- if (!isLegacyV4)
- _PIDSlider(
- label: 'I-Wind-down Min (%)',
- value: machineState.pidIWindDownMin,
- min: 0,
- max: MeCoffeeProtocol.maxPidLimitPercent.toDouble(),
- onChanged: (v) => bluetoothService.updatePIDLimitSettings(min: v),
- ),
- _PIDSlider(
- label: isLegacyV4 ? 'I-Wind-down Max (raw)' : 'I-Wind-down Max (%)',
- value: machineState.pidIWindDownMax,
- min: 0,
- max: isLegacyV4
- ? 6000
- : MeCoffeeProtocol.maxPidLimitPercent.toDouble(),
- step: isLegacyV4 ? 250 : 1,
- onChanged: (v) => bluetoothService.updatePIDLimitSettings(max: v),
- ),
- _PIDSlider(
- label: 'Polling Interval (ms)',
- value: machineState.pidPollingInterval.toDouble(),
- min: 1000,
- max: 10000,
- step: 500,
- onChanged: (v) =>
- bluetoothService.updatePIDLimitSettings(interval: v.round()),
- ),
- ],
- ),
- );
- }
- }
- /// Shows the dragged value immediately but sends it only on release: every
- /// `cmd set` rewrites controller EEPROM, so per-tick writes wear it out.
- class _PIDSlider extends StatefulWidget {
- final String label;
- final double value;
- final double min;
- final double max;
- final double step;
- final int decimalPlaces;
- final ValueChanged<double> onChanged;
- const _PIDSlider({
- required this.label,
- required this.value,
- required this.min,
- required this.max,
- this.step = 1,
- this.decimalPlaces = 0,
- required this.onChanged,
- });
- @override
- State<_PIDSlider> createState() => _PIDSliderState();
- }
- class _PIDSliderState extends State<_PIDSlider> {
- double? _draft;
- double _snap(double v) => double.parse(
- (widget.min + ((v - widget.min) / widget.step).round() * widget.step)
- .toStringAsFixed(widget.decimalPlaces),
- );
- @override
- void didUpdateWidget(covariant _PIDSlider oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (oldWidget.value != widget.value) _draft = null;
- }
- @override
- Widget build(BuildContext context) {
- final value = _draft ?? widget.value;
- final rounded = value.toStringAsFixed(widget.decimalPlaces);
- final reading = double.tryParse(rounded) == value
- ? rounded
- : value.toString();
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Padding(
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
- child: Text('${widget.label}: $reading'),
- ),
- Slider(
- value: value.clamp(widget.min, widget.max),
- min: widget.min,
- max: widget.max,
- divisions: ((widget.max - widget.min) / widget.step).round(),
- label: reading,
- onChanged: (v) => setState(() => _draft = _snap(v)),
- onChangeEnd: (v) {
- setState(() => _draft = null);
- widget.onChanged(_snap(v));
- },
- ),
- ],
- );
- }
- }
|