hardware_settings_screen.dart 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. 'Requires Bluetooth Classic, live telemetry, heating off, no shot, and a boiler '
  78. 'at or below 50 C. BLE flashing is not available.',
  79. ),
  80. enabled: bluetoothService.canUpdateFirmware,
  81. onTap: () async {
  82. final confirmed = await showDialog<bool>(
  83. context: context,
  84. builder: (context) => AlertDialog(
  85. title: const Text('Update controller firmware?'),
  86. content: const Text(
  87. 'This replaces the controller firmware with the bundled V10 '
  88. 'image. Only ATmega328P controllers with a compatible STK500 '
  89. 'bootloader are supported. Settings may change after updating.\n\n'
  90. 'Do not operate the machine, close the app, or interrupt '
  91. 'power/Bluetooth. Failure may require manual bootloader '
  92. 'recovery. Continue only if you can recover your controller.',
  93. ),
  94. actions: [
  95. TextButton(
  96. onPressed: () => Navigator.pop(context, false),
  97. child: const Text('Cancel'),
  98. ),
  99. FilledButton(
  100. onPressed: () => Navigator.pop(context, true),
  101. child: const Text('I understand - update'),
  102. ),
  103. ],
  104. ),
  105. );
  106. if (confirmed == true) await bluetoothService.updateFirmware();
  107. },
  108. ),
  109. if (machineState.firmwareUpdateStatus.isNotEmpty)
  110. Padding(
  111. padding: const EdgeInsets.all(16),
  112. child: Text(machineState.firmwareUpdateStatus),
  113. ),
  114. if (machineState.firmwareUpdating)
  115. LinearProgressIndicator(value: machineState.firmwareProgress),
  116. ],
  117. ),
  118. );
  119. }
  120. }
  121. class _OutputTile extends StatelessWidget {
  122. final String label;
  123. final int value;
  124. final ValueChanged<int?>? onChanged;
  125. const _OutputTile({
  126. required this.label,
  127. required this.value,
  128. required this.onChanged,
  129. });
  130. @override
  131. Widget build(BuildContext context) {
  132. return ListTile(
  133. title: Text(label),
  134. trailing: DropdownButton<int>(
  135. value: value,
  136. items: const [
  137. DropdownMenuItem(value: 112, child: Text('Pump')),
  138. DropdownMenuItem(value: 98, child: Text('Boiler')),
  139. DropdownMenuItem(value: 118, child: Text('Valve')),
  140. DropdownMenuItem(value: 105, child: Text('Indicator')),
  141. DropdownMenuItem(value: 103, child: Text('Grinder')),
  142. DropdownMenuItem(value: 110, child: Text('None')),
  143. ],
  144. onChanged: onChanged,
  145. ),
  146. );
  147. }
  148. }
  149. class _SectionHeader extends StatelessWidget {
  150. final String title;
  151. const _SectionHeader({required this.title});
  152. @override
  153. Widget build(BuildContext context) {
  154. return Padding(
  155. padding: const EdgeInsets.symmetric(vertical: 8.0),
  156. child: Text(
  157. title.toUpperCase(),
  158. style: TextStyle(
  159. color: Theme.of(context).colorScheme.primary,
  160. fontWeight: FontWeight.bold,
  161. fontSize: 12,
  162. ),
  163. ),
  164. );
  165. }
  166. }