hardware_settings_screen.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../services/bluetooth_service.dart';
  4. class HardwareSettingsScreen extends ConsumerWidget {
  5. const HardwareSettingsScreen({super.key});
  6. @override
  7. Widget build(BuildContext context, WidgetRef ref) {
  8. final machineState = ref.watch(bluetoothServiceProvider);
  9. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  10. return Scaffold(
  11. appBar: AppBar(title: const Text('Hardware Configuration')),
  12. body: ListView(
  13. padding: const EdgeInsets.all(16),
  14. children: [
  15. if (machineState.isLegacyV4Firmware)
  16. const Padding(
  17. padding: EdgeInsets.all(16),
  18. child: Text(
  19. 'V4 firmware does not support configurable output mapping or a momentary power button.',
  20. ),
  21. ),
  22. if (!machineState.isLegacyV4Firmware) ...[
  23. _SectionHeader(title: 'Output Mapping'),
  24. _OutputTile(
  25. label: 'Output 1',
  26. value: machineState.output0,
  27. onChanged: (v) =>
  28. bluetoothService.updateHardwareSettings(output0: v!),
  29. ),
  30. ],
  31. _OutputTile(
  32. label: 'Output 2',
  33. value: machineState.output1,
  34. onChanged: machineState.isLegacyV4Firmware
  35. ? null
  36. : (v) => bluetoothService.updateHardwareSettings(output1: v!),
  37. ),
  38. _OutputTile(
  39. label: 'Output 3',
  40. value: machineState.output2,
  41. onChanged: machineState.isLegacyV4Firmware
  42. ? null
  43. : (v) => bluetoothService.updateHardwareSettings(output2: v!),
  44. ),
  45. const Divider(),
  46. _SectionHeader(title: 'Machine Quirks'),
  47. SwitchListTile(
  48. title: const Text('Installed as Timer'),
  49. subtitle: const Text('Prevent warmup after power outage'),
  50. value: machineState.installedAsTimer,
  51. onChanged: bluetoothService.updateInstalledAsTimer,
  52. ),
  53. if (!machineState.isLegacyV4Firmware)
  54. SwitchListTile(
  55. title: const Text('Power Button Flips Back'),
  56. subtitle: const Text('Enable for Silvia V5 or similar'),
  57. value: machineState.powerButtonFlipsBack,
  58. onChanged: (v) =>
  59. bluetoothService.updateHardwareSettings(flips: v),
  60. ),
  61. const Divider(),
  62. _SectionHeader(title: 'Firmware'),
  63. ListTile(
  64. leading: const Icon(Icons.memory_outlined),
  65. title: const Text('Firmware Version'),
  66. subtitle: Text(
  67. machineState.firmwareVersion.isEmpty
  68. ? 'Unknown (connect to device)'
  69. : machineState.firmwareVersion,
  70. ),
  71. ),
  72. ListTile(
  73. leading: const Icon(Icons.system_update),
  74. title: const Text('Install bundled V10 firmware'),
  75. subtitle: const Text(
  76. 'Experimental STK500 update with read-back verification. '
  77. 'Only for "pcb V9" controllers (not V4 / pcb V3). '
  78. 'Requires Bluetooth Classic, live telemetry, the machine switched off at '
  79. 'its power switch, and a boiler at or below 50 C. BLE flashing is not available.',
  80. ),
  81. enabled: bluetoothService.canUpdateFirmware,
  82. onTap: () async {
  83. final confirmed = await showDialog<bool>(
  84. context: context,
  85. builder: (context) => AlertDialog(
  86. title: const Text('Update controller firmware?'),
  87. content: const Text(
  88. 'This replaces the controller firmware with the bundled V10 '
  89. 'image. Only ATmega328P controllers with a compatible STK500 '
  90. 'bootloader are supported. Settings may change after updating.\n\n'
  91. 'Do not operate the machine, close the app, or interrupt '
  92. 'power/Bluetooth. Failure may require manual bootloader '
  93. 'recovery. Continue only if you can recover your controller.',
  94. ),
  95. actions: [
  96. TextButton(
  97. onPressed: () => Navigator.pop(context, false),
  98. child: const Text('Cancel'),
  99. ),
  100. FilledButton(
  101. onPressed: () => Navigator.pop(context, true),
  102. child: const Text('I understand - update'),
  103. ),
  104. ],
  105. ),
  106. );
  107. if (confirmed == true) await bluetoothService.updateFirmware();
  108. },
  109. ),
  110. if (machineState.firmwareUpdateStatus.isNotEmpty)
  111. Padding(
  112. padding: const EdgeInsets.all(16),
  113. child: Text(machineState.firmwareUpdateStatus),
  114. ),
  115. if (machineState.firmwareUpdating)
  116. LinearProgressIndicator(value: machineState.firmwareProgress),
  117. ],
  118. ),
  119. );
  120. }
  121. }
  122. class _OutputTile extends StatelessWidget {
  123. final String label;
  124. final int value;
  125. final ValueChanged<int?>? onChanged;
  126. const _OutputTile({
  127. required this.label,
  128. required this.value,
  129. required this.onChanged,
  130. });
  131. @override
  132. Widget build(BuildContext context) {
  133. return ListTile(
  134. title: Text(label),
  135. trailing: DropdownButton<int>(
  136. value: value,
  137. items: const [
  138. DropdownMenuItem(value: 112, child: Text('Pump')),
  139. DropdownMenuItem(value: 98, child: Text('Boiler')),
  140. DropdownMenuItem(value: 118, child: Text('Valve')),
  141. DropdownMenuItem(value: 105, child: Text('Indicator')),
  142. DropdownMenuItem(value: 103, child: Text('Grinder')),
  143. DropdownMenuItem(value: 110, child: Text('None')),
  144. ],
  145. onChanged: onChanged,
  146. ),
  147. );
  148. }
  149. }
  150. class _SectionHeader extends StatelessWidget {
  151. final String title;
  152. const _SectionHeader({required this.title});
  153. @override
  154. Widget build(BuildContext context) {
  155. return Padding(
  156. padding: const EdgeInsets.symmetric(vertical: 8.0),
  157. child: Text(
  158. title.toUpperCase(),
  159. style: TextStyle(
  160. color: Theme.of(context).colorScheme.primary,
  161. fontWeight: FontWeight.bold,
  162. fontSize: 12,
  163. ),
  164. ),
  165. );
  166. }
  167. }