瀏覽代碼

fix maxShotSeconds and I-wind-down

Thomas Hondema 1 月之前
父節點
當前提交
dce96774af

+ 5 - 2
lib/screens/pid_settings_screen.dart

@@ -1,6 +1,7 @@
 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});
@@ -58,14 +59,16 @@ class PIDSettingsScreen extends ConsumerWidget {
               label: 'I-Wind-down Min (%)',
               value: machineState.pidIWindDownMin,
               min: 0,
-              max: 100,
+              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 : 100,
+            max: isLegacyV4
+                ? 6000
+                : MeCoffeeProtocol.maxPidLimitPercent.toDouble(),
             step: isLegacyV4 ? 250 : 1,
             onChanged: (v) => bluetoothService.updatePIDLimitSettings(max: v),
           ),

+ 3 - 2
lib/screens/settings_screen.dart

@@ -1,6 +1,7 @@
 import 'package:flutter/material.dart';
 import 'package:flutter_riverpod/flutter_riverpod.dart';
 import '../services/bluetooth_service.dart';
+import '../services/me_coffee_protocol.dart';
 import 'pid_settings_screen.dart';
 import 'statistics_screen.dart';
 import 'pressure_settings_screen.dart';
@@ -169,8 +170,8 @@ class SettingsScreen extends ConsumerWidget {
                 child: _DraftSlider(
                   value: machineState.maxShotTime.toDouble(),
                   min: 0,
-                  max: 120,
-                  divisions: 120,
+                  max: MeCoffeeProtocol.maxShotSeconds.toDouble(),
+                  divisions: MeCoffeeProtocol.maxShotSeconds,
                   format: (val) => val == 0 ? 'Off' : '${val.round()} s',
                   onSubmitted: (val) =>
                       bluetoothService.updateMaxShotTime(val.round()),

+ 38 - 9
lib/services/bluetooth_service.dart

@@ -531,8 +531,16 @@ class MeCoffeeService extends StateNotifier<MachineState> {
   Future<void> updateAutoShutoff(int minutes) =>
       _updateSettings({'tmrosd': minutes});
 
-  Future<void> updateMaxShotTime(int seconds) =>
-      _updateSettings({'shtmx': seconds});
+  Future<void> updateMaxShotTime(int seconds) async {
+    if (seconds < 0 || seconds > MeCoffeeProtocol.maxShotSeconds) {
+      _reportCommandError(
+        'Max shot time must be between 0 and '
+        '${MeCoffeeProtocol.maxShotSeconds} s.',
+      );
+      return;
+    }
+    await _updateSettings({'shtmx': seconds});
+  }
 
   Future<void> updateContinuousMode(bool enabled) =>
       _updateSettings({'tmpcntns': enabled});
@@ -561,18 +569,39 @@ class MeCoffeeService extends StateNotifier<MachineState> {
   Future<void> updatePIDSettings({double? p, double? i, double? d}) =>
       _updateSettings({'pd1p': ?p, 'pd1i': ?i, 'pd1d': ?d});
 
-  Future<void> updatePIDLimits(double min, double max, int interval) =>
-      _updateSettings({
-        'pd1imn': min,
-        'pd1imx': max,
-        'pd1sz': interval,
-      }, omitV4Minimum: true);
+  bool _pidLimitsOutOfRange(Iterable<double?> percentages) {
+    if (state.isLegacyV4Firmware) return false;
+    final outOfRange = percentages.any(
+      (value) =>
+          value != null &&
+          (value < 0 || value > MeCoffeeProtocol.maxPidLimitPercent),
+    );
+    if (outOfRange) {
+      _reportCommandError(
+        'I-wind-down limits must be between 0 and '
+        '${MeCoffeeProtocol.maxPidLimitPercent}%.',
+      );
+    }
+    return outOfRange;
+  }
+
+  Future<void> updatePIDLimits(double min, double max, int interval) async {
+    if (_pidLimitsOutOfRange([min, max])) return;
+    await _updateSettings({
+      'pd1imn': min,
+      'pd1imx': max,
+      'pd1sz': interval,
+    }, omitV4Minimum: true);
+  }
 
   Future<void> updatePIDLimitSettings({
     double? min,
     double? max,
     int? interval,
-  }) => _updateSettings({'pd1imn': ?min, 'pd1imx': ?max, 'pd1sz': ?interval});
+  }) async {
+    if (_pidLimitsOutOfRange([min, max])) return;
+    await _updateSettings({'pd1imn': ?min, 'pd1imx': ?max, 'pd1sz': ?interval});
+  }
 
   Future<void> _incrementShotCount() async {
     final prefs = await SharedPreferences.getInstance();

+ 15 - 1
lib/services/me_coffee_protocol.dart

@@ -1,6 +1,18 @@
 import '../models/machine_state.dart';
 
 class MeCoffeeProtocol {
+  /// Two-byte settings above this raw value are replaced by defaults when the
+  /// controller restores them at startup (V9/V10 firmware).
+  static const maxPersistentRaw = 32000;
+
+  /// Highest wind-down percentage whose raw value stays within
+  /// [maxPersistentRaw].
+  static const maxPidLimitPercent = 48;
+
+  /// The firmware compares against the low 16 bits of `shtmx * 1000`, so
+  /// 66 s and above wrap to a sub-second limit.
+  static const maxShotSeconds = 65;
+
   static MachineState parse(
     String msg,
     MachineState currentState, {
@@ -226,7 +238,9 @@ class MeCoffeeProtocol {
       } else if (key == 'pd1i') {
         valStr = (value * (isLegacyV4 ? 1 : 100)).round().toString();
       } else if (key == 'pd1imm' || key == 'pd1imn' || key == 'pd1imx') {
-        valStr = (value * (isLegacyV4 ? 1 : 655.36)).round().toString();
+        valStr = isLegacyV4
+            ? value.round().toString()
+            : (value * 655.36).round().clamp(0, maxPersistentRaw).toString();
       } else {
         valStr = value.round().toString();
       }

+ 1 - 1
test/screens/settings_ranges_test.dart

@@ -92,7 +92,7 @@ void main() {
       } else {
         expect(
           (sliders[4].min, sliders[4].max, sliders[4].divisions),
-          (0, 120, 120),
+          (0, 65, 65),
         );
       }
 

+ 2 - 2
test/screens/v4_pid_settings_screen_test.dart

@@ -125,8 +125,8 @@ void main() {
     expect((sliders[1].max, sliders[1].divisions), (1, 100));
     expect(sliders[2].divisions, 256);
     expect(sliders[3].divisions, 100);
-    expect(sliders[4].divisions, 100);
-    expect(sliders[5].divisions, 100);
+    expect((sliders[4].max, sliders[4].divisions), (48, 48));
+    expect((sliders[5].max, sliders[5].divisions), (48, 48));
     expect(find.text('Integral (I): 0.35'), findsOneWidget);
     expect(find.textContaining('Temperature Offset'), findsNothing);
     sliders[1].onChanged!(0.35000000000000003);

+ 13 - 0
test/services/classic_service_test.dart

@@ -243,6 +243,19 @@ void main() {
     expect(String.fromCharCodes(transport.writes.single), 'cmd set tmrpwr 1\n');
   });
 
+  test('settings outside firmware storage limits are not sent', () async {
+    await connect();
+    transport.data.add('cmd uname meCoffee  pcb V9 30 149 15 OK\n'.codeUnits);
+    transport.writes.clear();
+    await service.updateMaxShotTime(66);
+    expect(service.state.commandError, contains('65 s'));
+    await service.updatePIDLimitSettings(max: 49);
+    expect(service.state.commandError, contains('48%'));
+    expect(transport.writes, isEmpty);
+    await service.updateMaxShotTime(65);
+    expect(String.fromCharCodes(transport.writes.single), 'cmd set shtmx 65\n');
+  });
+
   test('cold temperature alone does not establish heater-off status', () async {
     await connect();
     transport.data.add('tmp 1 10100 2000\n'.codeUnits);

+ 7 - 1
test/services/me_coffee_protocol_test.dart

@@ -395,9 +395,15 @@ void main() {
             50,
           );
         }
+        expect(
+          String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(25, 48, 1000)),
+          'cmd set pd1imn 16384\ncmd set pd1imx 31457\ncmd set pd1sz 1000\n',
+        );
+        // Raw values above 32000 are reset to defaults at controller startup,
+        // and 65536 would store 0 in the 16-bit field.
         expect(
           String.fromCharCodes(MeCoffeeProtocol.encodePIDLimits(50, 100, 1000)),
-          'cmd set pd1imn 32768\ncmd set pd1imx 65536\ncmd set pd1sz 1000\n',
+          'cmd set pd1imn 32000\ncmd set pd1imx 32000\ncmd set pd1sz 1000\n',
         );
       },
     );