| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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),
- ),
- ListTile(
- title: const Text('Polling Interval (ms)'),
- subtitle: Text('${machineState.pidPollingInterval} ms'),
- trailing: SizedBox(
- width: 200,
- child: Slider(
- value: machineState.pidPollingInterval.toDouble().clamp(
- 1000.0,
- 10000.0,
- ),
- min: 1000,
- max: 10000,
- divisions: 18, // 500 ms steps
- label: '${machineState.pidPollingInterval} ms',
- onChanged: (v) {
- bluetoothService.updatePIDLimitSettings(interval: v.round());
- },
- ),
- ),
- ),
- ],
- ),
- );
- }
- }
- class _PIDSlider extends StatelessWidget {
- 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
- Widget build(BuildContext context) {
- final rounded = value.toStringAsFixed(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('$label: $reading'),
- ),
- Slider(
- value: value.clamp(min, max),
- min: min,
- max: max,
- divisions: ((max - min) / step).round(),
- label: reading,
- onChanged: (v) => onChanged(
- double.parse(
- (min + ((v - min) / step).round() * step).toStringAsFixed(
- decimalPlaces,
- ),
- ),
- ),
- ),
- ],
- );
- }
- }
|