v1.2.0 — Phase 2: Riverpod providers wired up
- settings_provider: AppSettings (ecuType, pollingInterval) StateNotifier - bt_provider: BtNotifier (disconnected/connecting/connected/error states), btServiceProvider singleton, pairedDevicesProvider FutureProvider, internal frameStream piped through StreamController - sensor_provider: sensorStateProvider StreamProvider (auto S300/KPro), latestSensorProvider convenience Provider - theme_provider: AppThemeVariant (red/green × dark/light) StateNotifier - main.dart: fully Riverpod — ProviderScope, ConsumerWidget throughout, no setState for BT state, providers own all lifecycle
This commit is contained in:
parent
11cf7c2b63
commit
6a80d8fc1f
138
lib/core/providers/bt_provider.dart
Normal file
138
lib/core/providers/bt_provider.dart
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
import '../bluetooth/bt_poller.dart';
|
||||||
|
import '../bluetooth/bt_service.dart';
|
||||||
|
import 'settings_provider.dart';
|
||||||
|
|
||||||
|
// ─── Connection status enum ──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
enum BtStatus { disconnected, connecting, connected, error }
|
||||||
|
|
||||||
|
// ─── State ───────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
class BtState {
|
||||||
|
final BtStatus status;
|
||||||
|
final String? deviceName;
|
||||||
|
final String? error;
|
||||||
|
final int frameCount;
|
||||||
|
final int droppedFrames;
|
||||||
|
|
||||||
|
const BtState({
|
||||||
|
required this.status,
|
||||||
|
this.deviceName,
|
||||||
|
this.error,
|
||||||
|
this.frameCount = 0,
|
||||||
|
this.droppedFrames = 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const BtState.disconnected()
|
||||||
|
: this(status: BtStatus.disconnected);
|
||||||
|
|
||||||
|
BtState connecting(String name) =>
|
||||||
|
BtState(status: BtStatus.connecting, deviceName: name);
|
||||||
|
|
||||||
|
BtState connected(String name) =>
|
||||||
|
BtState(status: BtStatus.connected, deviceName: name);
|
||||||
|
|
||||||
|
BtState withError(String msg) =>
|
||||||
|
BtState(status: BtStatus.error, deviceName: deviceName, error: msg);
|
||||||
|
|
||||||
|
BtState withFrameStats(int frames, int dropped) => BtState(
|
||||||
|
status: status,
|
||||||
|
deviceName: deviceName,
|
||||||
|
frameCount: frames,
|
||||||
|
droppedFrames: dropped,
|
||||||
|
);
|
||||||
|
|
||||||
|
bool get isConnected => status == BtStatus.connected;
|
||||||
|
bool get isConnecting => status == BtStatus.connecting;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Notifier ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
class BtNotifier extends StateNotifier<BtState> {
|
||||||
|
final Ref _ref;
|
||||||
|
BtPoller? _poller;
|
||||||
|
StreamSubscription<Uint8List>? _pollSub;
|
||||||
|
final StreamController<Uint8List> _frameController =
|
||||||
|
StreamController<Uint8List>.broadcast();
|
||||||
|
|
||||||
|
BtNotifier(this._ref) : super(const BtState.disconnected());
|
||||||
|
|
||||||
|
/// Raw validated 128-byte frame stream — consumed by sensorStateProvider.
|
||||||
|
Stream<Uint8List> get frameStream => _frameController.stream;
|
||||||
|
|
||||||
|
Future<void> connect(BluetoothDevice device) async {
|
||||||
|
state = state.connecting(device.name ?? device.address);
|
||||||
|
final service = _ref.read(btServiceProvider);
|
||||||
|
final settings = _ref.read(settingsProvider);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await service.connect(device);
|
||||||
|
|
||||||
|
_poller = BtPoller(
|
||||||
|
service,
|
||||||
|
ecuType: settings.ecuType,
|
||||||
|
pollingInterval: settings.pollingInterval,
|
||||||
|
);
|
||||||
|
|
||||||
|
_pollSub = _poller!.frameStream.listen(
|
||||||
|
(frame) {
|
||||||
|
_frameController.add(frame);
|
||||||
|
state = state.withFrameStats(
|
||||||
|
_poller!.validFrames, _poller!.droppedFrames);
|
||||||
|
},
|
||||||
|
onError: (Object e) {
|
||||||
|
_frameController.addError(e);
|
||||||
|
state = state.withError(e.toString());
|
||||||
|
},
|
||||||
|
onDone: () => state = state.withError('BT stream closed'),
|
||||||
|
);
|
||||||
|
|
||||||
|
_poller!.start();
|
||||||
|
state = state.connected(device.name ?? device.address);
|
||||||
|
} catch (e) {
|
||||||
|
await service.disconnect();
|
||||||
|
state = state.withError(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> disconnect() async {
|
||||||
|
await _pollSub?.cancel();
|
||||||
|
_poller?.stop();
|
||||||
|
_poller?.dispose();
|
||||||
|
_poller = null;
|
||||||
|
await _ref.read(btServiceProvider).disconnect();
|
||||||
|
state = const BtState.disconnected();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_pollSub?.cancel();
|
||||||
|
_poller?.dispose();
|
||||||
|
_frameController.close();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Providers ───────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
/// Singleton BtService — lives as long as the app.
|
||||||
|
final btServiceProvider = Provider<BtService>((ref) {
|
||||||
|
final service = BtService();
|
||||||
|
ref.onDispose(service.dispose);
|
||||||
|
return service;
|
||||||
|
});
|
||||||
|
|
||||||
|
/// BT connection state notifier.
|
||||||
|
final btProvider =
|
||||||
|
StateNotifierProvider<BtNotifier, BtState>((ref) => BtNotifier(ref));
|
||||||
|
|
||||||
|
/// Paired BT devices list (re-fetchable via ref.refresh).
|
||||||
|
final pairedDevicesProvider = FutureProvider<List<BluetoothDevice>>((ref) {
|
||||||
|
return FlutterBluetoothSerial.instance.getBondedDevices();
|
||||||
|
});
|
||||||
29
lib/core/providers/sensor_provider.dart
Normal file
29
lib/core/providers/sensor_provider.dart
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
import '../bluetooth/bt_poller.dart';
|
||||||
|
import '../protocol/kpro_parser.dart';
|
||||||
|
import '../protocol/s300_parser.dart';
|
||||||
|
import '../protocol/sensor_state.dart';
|
||||||
|
import 'bt_provider.dart';
|
||||||
|
import 'settings_provider.dart';
|
||||||
|
|
||||||
|
/// Emits a parsed [SensorState] for every valid frame received from the ECU.
|
||||||
|
/// Automatically picks S300 or KPro parser based on [settingsProvider].
|
||||||
|
final sensorStateProvider = StreamProvider<SensorState>((ref) {
|
||||||
|
final ecuType = ref.watch(settingsProvider).ecuType;
|
||||||
|
final btNotifier = ref.watch(btProvider.notifier);
|
||||||
|
|
||||||
|
return btNotifier.frameStream.map((frame) {
|
||||||
|
if (ecuType == EcuType.s300) return parseS300(frame);
|
||||||
|
return parseKPro(frame);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Last successfully parsed sensor state (never null after first frame).
|
||||||
|
/// Falls back to SensorState.zero() before any data arrives.
|
||||||
|
final latestSensorProvider = Provider<SensorState>((ref) {
|
||||||
|
return ref
|
||||||
|
.watch(sensorStateProvider)
|
||||||
|
.whenData((s) => s)
|
||||||
|
.value ?? SensorState.zero();
|
||||||
|
});
|
||||||
31
lib/core/providers/settings_provider.dart
Normal file
31
lib/core/providers/settings_provider.dart
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import '../bluetooth/bt_poller.dart';
|
||||||
|
|
||||||
|
class AppSettings {
|
||||||
|
final EcuType ecuType;
|
||||||
|
final Duration pollingInterval;
|
||||||
|
|
||||||
|
const AppSettings({
|
||||||
|
this.ecuType = EcuType.s300,
|
||||||
|
this.pollingInterval = const Duration(milliseconds: 100),
|
||||||
|
});
|
||||||
|
|
||||||
|
AppSettings copyWith({EcuType? ecuType, Duration? pollingInterval}) =>
|
||||||
|
AppSettings(
|
||||||
|
ecuType: ecuType ?? this.ecuType,
|
||||||
|
pollingInterval: pollingInterval ?? this.pollingInterval,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class SettingsNotifier extends StateNotifier<AppSettings> {
|
||||||
|
SettingsNotifier() : super(const AppSettings());
|
||||||
|
|
||||||
|
void setEcuType(EcuType type) => state = state.copyWith(ecuType: type);
|
||||||
|
void setPollingInterval(Duration d) =>
|
||||||
|
state = state.copyWith(pollingInterval: d);
|
||||||
|
}
|
||||||
|
|
||||||
|
final settingsProvider =
|
||||||
|
StateNotifierProvider<SettingsNotifier, AppSettings>(
|
||||||
|
(ref) => SettingsNotifier(),
|
||||||
|
);
|
||||||
32
lib/core/providers/theme_provider.dart
Normal file
32
lib/core/providers/theme_provider.dart
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
|
||||||
|
enum AppThemeVariant { redDark, redLight, greenDark, greenLight }
|
||||||
|
|
||||||
|
class ThemeNotifier extends StateNotifier<AppThemeVariant> {
|
||||||
|
ThemeNotifier() : super(AppThemeVariant.redDark);
|
||||||
|
|
||||||
|
void set(AppThemeVariant v) => state = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
final themeProvider =
|
||||||
|
StateNotifierProvider<ThemeNotifier, AppThemeVariant>(
|
||||||
|
(ref) => ThemeNotifier(),
|
||||||
|
);
|
||||||
|
|
||||||
|
/// Returns the accent color for the current theme variant.
|
||||||
|
extension AppThemeVariantX on AppThemeVariant {
|
||||||
|
Color get accentColor {
|
||||||
|
switch (this) {
|
||||||
|
case AppThemeVariant.redDark:
|
||||||
|
case AppThemeVariant.redLight:
|
||||||
|
return const Color(0xFFFF4444);
|
||||||
|
case AppThemeVariant.greenDark:
|
||||||
|
case AppThemeVariant.greenLight:
|
||||||
|
return const Color(0xFF00E676);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get isDark =>
|
||||||
|
this == AppThemeVariant.redDark || this == AppThemeVariant.greenDark;
|
||||||
|
}
|
||||||
744
lib/main.dart
744
lib/main.dart
@ -1,65 +1,66 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:typed_data';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
|
||||||
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:permission_handler/permission_handler.dart';
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
||||||
|
import 'core/providers/bt_provider.dart';
|
||||||
|
import 'core/providers/sensor_provider.dart';
|
||||||
|
import 'core/providers/settings_provider.dart';
|
||||||
|
import 'core/providers/theme_provider.dart';
|
||||||
import 'core/bluetooth/bt_poller.dart';
|
import 'core/bluetooth/bt_poller.dart';
|
||||||
import 'core/bluetooth/bt_service.dart';
|
|
||||||
import 'core/protocol/kpro_parser.dart';
|
|
||||||
import 'core/protocol/s300_parser.dart';
|
|
||||||
import 'core/protocol/sensor_state.dart';
|
import 'core/protocol/sensor_state.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
runApp(const HvbtApp());
|
runApp(const ProviderScope(child: HvbtApp()));
|
||||||
}
|
}
|
||||||
|
|
||||||
class HvbtApp extends StatelessWidget {
|
class HvbtApp extends ConsumerWidget {
|
||||||
const HvbtApp({super.key});
|
const HvbtApp({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
|
final themeVariant = ref.watch(themeProvider);
|
||||||
|
final accent = themeVariant.accentColor;
|
||||||
|
|
||||||
return MaterialApp(
|
return MaterialApp(
|
||||||
title: 'HV BT Dashboard',
|
title: 'HV BT Dashboard',
|
||||||
|
debugShowCheckedModeBanner: false,
|
||||||
theme: ThemeData.dark(useMaterial3: true).copyWith(
|
theme: ThemeData.dark(useMaterial3: true).copyWith(
|
||||||
colorScheme: ColorScheme.fromSeed(
|
colorScheme: ColorScheme.fromSeed(
|
||||||
seedColor: const Color(0xFFFF4444),
|
seedColor: accent,
|
||||||
brightness: Brightness.dark,
|
brightness: Brightness.dark,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
home: const BtPickerScreen(),
|
home: const BtPickerScreen(),
|
||||||
debugShowCheckedModeBanner: false,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Bluetooth Device Picker ──────────────────────────────────────────────────
|
// ─── Bluetooth Device Picker ──────────────────────────────────────────────────
|
||||||
|
|
||||||
class BtPickerScreen extends StatefulWidget {
|
class BtPickerScreen extends ConsumerStatefulWidget {
|
||||||
const BtPickerScreen({super.key});
|
const BtPickerScreen({super.key});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<BtPickerScreen> createState() => _BtPickerScreenState();
|
ConsumerState<BtPickerScreen> createState() => _BtPickerScreenState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _BtPickerScreenState extends State<BtPickerScreen> {
|
class _BtPickerScreenState extends ConsumerState<BtPickerScreen> {
|
||||||
final BtService _btService = BtService();
|
|
||||||
List<BluetoothDevice> _devices = [];
|
|
||||||
bool _loading = true;
|
|
||||||
String? _connectingAddress;
|
|
||||||
String? _error;
|
String? _error;
|
||||||
|
bool _initDone = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_initBluetooth();
|
WidgetsBinding.instance.addPostFrameCallback((_) => _initBluetooth());
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _initBluetooth() async {
|
Future<void> _initBluetooth() async {
|
||||||
setState(() { _loading = true; _error = null; });
|
setState(() => _error = null);
|
||||||
|
|
||||||
// 1. Request all required runtime permissions
|
// 1. Runtime permissions
|
||||||
final statuses = await [
|
final statuses = await [
|
||||||
Permission.bluetoothConnect,
|
Permission.bluetoothConnect,
|
||||||
Permission.bluetoothScan,
|
Permission.bluetoothScan,
|
||||||
@ -73,60 +74,36 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
|
|
||||||
if (denied.isNotEmpty) {
|
if (denied.isNotEmpty) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_loading = false;
|
|
||||||
_error = 'Permissions denied: ${denied.join(", ")}\n\n'
|
_error = 'Permissions denied: ${denied.join(", ")}\n\n'
|
||||||
'Go to Settings → Apps → HV BT Dashboard → Permissions and allow Bluetooth + Location, then tap Refresh.';
|
'Go to Settings → Apps → HV BT Dashboard → Permissions '
|
||||||
|
'and allow Bluetooth + Location, then tap Refresh.';
|
||||||
|
_initDone = true;
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Make sure Bluetooth is enabled (shows system dialog to turn it on)
|
// 2. Auto-enable Bluetooth
|
||||||
final btState = await FlutterBluetoothSerial.instance.state;
|
final btState = await FlutterBluetoothSerial.instance.state;
|
||||||
if (btState != BluetoothState.STATE_ON) {
|
if (btState != BluetoothState.STATE_ON) {
|
||||||
await FlutterBluetoothSerial.instance.requestEnable();
|
await FlutterBluetoothSerial.instance.requestEnable();
|
||||||
// Give the radio a moment to come up
|
|
||||||
await Future<void>.delayed(const Duration(seconds: 1));
|
await Future<void>.delayed(const Duration(seconds: 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
await _loadDevices();
|
setState(() => _initDone = true);
|
||||||
}
|
ref.invalidate(pairedDevicesProvider);
|
||||||
|
|
||||||
Future<void> _loadDevices() async {
|
|
||||||
setState(() { _loading = true; _error = null; });
|
|
||||||
try {
|
|
||||||
final devices = await _btService.getPairedDevices();
|
|
||||||
setState(() {
|
|
||||||
_devices = devices;
|
|
||||||
_loading = false;
|
|
||||||
});
|
|
||||||
} catch (e) {
|
|
||||||
setState(() {
|
|
||||||
_error = 'Failed to load devices:\n$e\n\nMake sure Bluetooth is ON and tap Refresh.';
|
|
||||||
_loading = false;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _connect(BluetoothDevice device, EcuType ecuType) async {
|
Future<void> _connect(BluetoothDevice device, EcuType ecuType) async {
|
||||||
setState(() => _connectingAddress = device.address);
|
// Set protocol before connecting so the poller uses it
|
||||||
try {
|
ref.read(settingsProvider.notifier).setEcuType(ecuType);
|
||||||
await _btService.connect(device);
|
await ref.read(btProvider.notifier).connect(device);
|
||||||
|
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
final btState = ref.read(btProvider);
|
||||||
|
if (btState.status == BtStatus.connected) {
|
||||||
Navigator.of(context).pushReplacement(
|
Navigator.of(context).pushReplacement(
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(builder: (_) => const LiveDataScreen()),
|
||||||
builder: (_) => LiveDataScreen(
|
|
||||||
btService: _btService,
|
|
||||||
deviceName: device.name ?? device.address,
|
|
||||||
ecuType: ecuType,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
} catch (e) {
|
|
||||||
if (!mounted) return;
|
|
||||||
setState(() {
|
|
||||||
_connectingAddress = null;
|
|
||||||
_error = 'Connection to ${device.name ?? device.address} failed:\n$e\n\nMake sure the device is powered on and paired.';
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,21 +115,15 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
content: const Text('Select ECU protocol:'),
|
content: const Text('Select ECU protocol:'),
|
||||||
actions: [
|
actions: [
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () { Navigator.pop(ctx); _connect(device, EcuType.s300); },
|
||||||
Navigator.of(ctx).pop();
|
|
||||||
_connect(device, EcuType.s300);
|
|
||||||
},
|
|
||||||
child: const Text('S300'),
|
child: const Text('S300'),
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: () { Navigator.pop(ctx); _connect(device, EcuType.kpro); },
|
||||||
Navigator.of(ctx).pop();
|
|
||||||
_connect(device, EcuType.kpro);
|
|
||||||
},
|
|
||||||
child: const Text('KPro'),
|
child: const Text('KPro'),
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () => Navigator.of(ctx).pop(),
|
onPressed: () => Navigator.pop(ctx),
|
||||||
child: const Text('Cancel'),
|
child: const Text('Cancel'),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -162,20 +133,33 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
final btState = ref.watch(btProvider);
|
||||||
|
final devicesAsync = ref.watch(pairedDevicesProvider);
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: const Text('HV BT — Select Device'),
|
title: const Text('HV BT — Select Device'),
|
||||||
actions: [
|
actions: [
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.refresh),
|
icon: const Icon(Icons.refresh),
|
||||||
onPressed: _loading ? null : _initBluetooth,
|
onPressed: btState.isConnecting
|
||||||
|
? null
|
||||||
|
: () { setState(() => _error = null); _initBluetooth(); },
|
||||||
tooltip: 'Refresh',
|
tooltip: 'Refresh',
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: Column(
|
body: Column(
|
||||||
children: [
|
children: [
|
||||||
// Error banner
|
// BT connection error banner
|
||||||
|
if (btState.status == BtStatus.error)
|
||||||
|
_ErrorBanner(
|
||||||
|
message: 'Connection failed: ${btState.error}',
|
||||||
|
onDismiss: () =>
|
||||||
|
ref.read(btProvider.notifier).disconnect(),
|
||||||
|
),
|
||||||
|
|
||||||
|
// Permission / init error banner
|
||||||
if (_error != null)
|
if (_error != null)
|
||||||
_ErrorBanner(
|
_ErrorBanner(
|
||||||
message: _error!,
|
message: _error!,
|
||||||
@ -183,8 +167,19 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
),
|
),
|
||||||
|
|
||||||
Expanded(
|
Expanded(
|
||||||
child: _loading
|
child: !_initDone
|
||||||
? const Center(
|
? const Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
CircularProgressIndicator(),
|
||||||
|
SizedBox(height: 12),
|
||||||
|
Text('Requesting permissions…'),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: devicesAsync.when(
|
||||||
|
loading: () => const Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
@ -193,8 +188,14 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
Text('Loading paired devices…'),
|
Text('Loading paired devices…'),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
)
|
),
|
||||||
: _devices.isEmpty
|
error: (e, _) => Center(
|
||||||
|
child: _ErrorBanner(
|
||||||
|
message: 'Failed to load devices:\n$e',
|
||||||
|
onDismiss: () => ref.refresh(pairedDevicesProvider),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
data: (devices) => devices.isEmpty
|
||||||
? const Center(
|
? const Center(
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: EdgeInsets.all(24),
|
padding: EdgeInsets.all(24),
|
||||||
@ -204,14 +205,11 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
Icon(Icons.bluetooth_disabled,
|
Icon(Icons.bluetooth_disabled,
|
||||||
size: 64, color: Colors.grey),
|
size: 64, color: Colors.grey),
|
||||||
SizedBox(height: 16),
|
SizedBox(height: 16),
|
||||||
Text(
|
Text('No paired BT devices found.',
|
||||||
'No paired Bluetooth devices found.',
|
style: TextStyle(fontSize: 16)),
|
||||||
style: TextStyle(fontSize: 16),
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
|
||||||
SizedBox(height: 8),
|
SizedBox(height: 8),
|
||||||
Text(
|
Text(
|
||||||
'Go to Android Settings → Bluetooth and pair your ECU module first, then come back and tap Refresh.',
|
'Pair your ECU module in Android Settings → Bluetooth, then tap Refresh.',
|
||||||
style: TextStyle(color: Colors.grey),
|
style: TextStyle(color: Colors.grey),
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
),
|
),
|
||||||
@ -220,22 +218,19 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
: ListView.separated(
|
: ListView.separated(
|
||||||
itemCount: _devices.length,
|
itemCount: devices.length,
|
||||||
separatorBuilder: (_, __) =>
|
separatorBuilder: (_, __) =>
|
||||||
const Divider(height: 1),
|
const Divider(height: 1),
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, i) {
|
||||||
final device = _devices[index];
|
final device = devices[i];
|
||||||
final isConnecting =
|
final isConnecting = btState.isConnecting &&
|
||||||
_connectingAddress == device.address;
|
btState.deviceName == (device.name ?? device.address);
|
||||||
return ListTile(
|
return ListTile(
|
||||||
leading: Icon(
|
leading: Icon(Icons.bluetooth,
|
||||||
Icons.bluetooth,
|
|
||||||
color: isConnecting
|
color: isConnecting
|
||||||
? Colors.blue
|
? Colors.blue
|
||||||
: Colors.grey,
|
: Colors.grey),
|
||||||
),
|
title: Text(device.name ?? 'Unknown'),
|
||||||
title:
|
|
||||||
Text(device.name ?? 'Unknown Device'),
|
|
||||||
subtitle: Text(device.address),
|
subtitle: Text(device.address),
|
||||||
trailing: isConnecting
|
trailing: isConnecting
|
||||||
? const SizedBox(
|
? const SizedBox(
|
||||||
@ -244,18 +239,16 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
child: CircularProgressIndicator(
|
child: CircularProgressIndicator(
|
||||||
strokeWidth: 2),
|
strokeWidth: 2),
|
||||||
)
|
)
|
||||||
: const Icon(
|
: const Icon(Icons.arrow_forward_ios,
|
||||||
Icons.arrow_forward_ios,
|
size: 16, color: Colors.grey),
|
||||||
size: 16,
|
onTap: btState.isConnecting
|
||||||
color: Colors.grey,
|
|
||||||
),
|
|
||||||
onTap: isConnecting
|
|
||||||
? null
|
? null
|
||||||
: () => _showProtocolDialog(device),
|
: () => _showProtocolDialog(device),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -264,138 +257,15 @@ class _BtPickerScreenState extends State<BtPickerScreen> {
|
|||||||
|
|
||||||
// ─── Live Data Screen ─────────────────────────────────────────────────────────
|
// ─── Live Data Screen ─────────────────────────────────────────────────────────
|
||||||
|
|
||||||
class LiveDataScreen extends StatefulWidget {
|
class LiveDataScreen extends ConsumerWidget {
|
||||||
final BtService btService;
|
const LiveDataScreen({super.key});
|
||||||
final String deviceName;
|
|
||||||
final EcuType ecuType;
|
|
||||||
|
|
||||||
const LiveDataScreen({
|
|
||||||
super.key,
|
|
||||||
required this.btService,
|
|
||||||
required this.deviceName,
|
|
||||||
required this.ecuType,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<LiveDataScreen> createState() => _LiveDataScreenState();
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
}
|
final btState = ref.watch(btProvider);
|
||||||
|
final sensorAsync = ref.watch(sensorStateProvider);
|
||||||
class _LiveDataScreenState extends State<LiveDataScreen> {
|
final settings = ref.watch(settingsProvider);
|
||||||
late final BtPoller _poller;
|
final protocol = settings.ecuType == EcuType.s300 ? 'S300' : 'KPro';
|
||||||
StreamSubscription<Uint8List>? _frameSub;
|
|
||||||
|
|
||||||
SensorState? _state;
|
|
||||||
int _frameCount = 0;
|
|
||||||
int _dropped = 0;
|
|
||||||
DateTime? _lastFrameTime;
|
|
||||||
|
|
||||||
// Debug log — last 20 events shown on screen
|
|
||||||
final List<_LogEntry> _log = [];
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
super.initState();
|
|
||||||
_addLog('Connecting to ${widget.deviceName}…', LogLevel.info);
|
|
||||||
_addLog(
|
|
||||||
'Protocol: ${widget.ecuType == EcuType.s300 ? "S300" : "KPro"}',
|
|
||||||
LogLevel.info);
|
|
||||||
|
|
||||||
_poller = BtPoller(
|
|
||||||
widget.btService,
|
|
||||||
ecuType: widget.ecuType,
|
|
||||||
pollingInterval: const Duration(milliseconds: 100),
|
|
||||||
);
|
|
||||||
_poller.start();
|
|
||||||
_addLog('Poller started — sending requests every 100ms', LogLevel.info);
|
|
||||||
|
|
||||||
_frameSub = _poller.frameStream.listen(
|
|
||||||
(Uint8List frame) {
|
|
||||||
try {
|
|
||||||
final SensorState s = widget.ecuType == EcuType.s300
|
|
||||||
? parseS300(frame)
|
|
||||||
: parseKPro(frame);
|
|
||||||
final now = DateTime.now();
|
|
||||||
if (mounted) {
|
|
||||||
setState(() {
|
|
||||||
_state = s;
|
|
||||||
_frameCount++;
|
|
||||||
_dropped = _poller.droppedFrames;
|
|
||||||
_lastFrameTime = now;
|
|
||||||
});
|
|
||||||
// Log first frame and then every 100 frames
|
|
||||||
if (_frameCount == 1) {
|
|
||||||
_addLog('First frame received! RPM=${s.rpm.toStringAsFixed(0)}',
|
|
||||||
LogLevel.ok);
|
|
||||||
} else if (_frameCount % 100 == 0) {
|
|
||||||
_addLog(
|
|
||||||
'Frame #$_frameCount RPM=${s.rpm.toStringAsFixed(0)} ECT=${s.ect.toStringAsFixed(1)}°C',
|
|
||||||
LogLevel.info);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
_addLog('Parse error: $e', LogLevel.error);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
onError: (Object e) {
|
|
||||||
_addLog('BT stream error: $e', LogLevel.error);
|
|
||||||
},
|
|
||||||
onDone: () {
|
|
||||||
_addLog('BT stream closed (device disconnected?)', LogLevel.error);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _addLog(String msg, LogLevel level) {
|
|
||||||
if (!mounted) return;
|
|
||||||
setState(() {
|
|
||||||
_log.insert(
|
|
||||||
0,
|
|
||||||
_LogEntry(
|
|
||||||
message: msg,
|
|
||||||
level: level,
|
|
||||||
time: DateTime.now(),
|
|
||||||
));
|
|
||||||
if (_log.length > 20) _log.removeLast();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void dispose() {
|
|
||||||
_frameSub?.cancel();
|
|
||||||
_poller.dispose();
|
|
||||||
widget.btService.dispose();
|
|
||||||
super.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _disconnect() async {
|
|
||||||
_poller.stop();
|
|
||||||
await widget.btService.disconnect();
|
|
||||||
if (!mounted) return;
|
|
||||||
Navigator.of(context).pushReplacement(
|
|
||||||
MaterialPageRoute(builder: (_) => const BtPickerScreen()),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
String get _statusText {
|
|
||||||
if (_frameCount == 0) return 'Waiting for data…';
|
|
||||||
final ago = _lastFrameTime == null
|
|
||||||
? '?'
|
|
||||||
: '${DateTime.now().difference(_lastFrameTime!).inMilliseconds}ms ago';
|
|
||||||
return 'LIVE • last frame $ago';
|
|
||||||
}
|
|
||||||
|
|
||||||
Color get _statusColor {
|
|
||||||
if (_frameCount == 0) return Colors.orange;
|
|
||||||
final ms = _lastFrameTime == null
|
|
||||||
? 999
|
|
||||||
: DateTime.now().difference(_lastFrameTime!).inMilliseconds;
|
|
||||||
return ms < 500 ? Colors.green : Colors.orange;
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
final protocol =
|
|
||||||
widget.ecuType == EcuType.s300 ? 'S300' : 'KPro';
|
|
||||||
|
|
||||||
return DefaultTabController(
|
return DefaultTabController(
|
||||||
length: 2,
|
length: 2,
|
||||||
@ -406,20 +276,19 @@ class _LiveDataScreenState extends State<LiveDataScreen> {
|
|||||||
title: Column(
|
title: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(widget.deviceName,
|
Text(btState.deviceName ?? 'ECU',
|
||||||
style: const TextStyle(fontSize: 15)),
|
style: const TextStyle(fontSize: 15)),
|
||||||
Row(
|
Row(
|
||||||
children: [
|
children: [
|
||||||
Icon(_frameCount == 0
|
Icon(
|
||||||
? Icons.hourglass_empty
|
btState.isConnected ? Icons.circle : Icons.hourglass_empty,
|
||||||
: Icons.circle,
|
size: 9,
|
||||||
size: 10,
|
color: btState.isConnected ? Colors.green : Colors.orange,
|
||||||
color: _statusColor),
|
),
|
||||||
const SizedBox(width: 4),
|
const SizedBox(width: 4),
|
||||||
Text(
|
Text(
|
||||||
'$protocol • $_statusText • dropped: $_dropped',
|
'$protocol • frames: ${btState.frameCount} • dropped: ${btState.droppedFrames}',
|
||||||
style: const TextStyle(
|
style: const TextStyle(fontSize: 10, color: Colors.grey),
|
||||||
fontSize: 10, color: Colors.grey),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -427,52 +296,36 @@ class _LiveDataScreenState extends State<LiveDataScreen> {
|
|||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.bluetooth_disabled,
|
icon: const Icon(Icons.bluetooth_disabled, color: Colors.redAccent),
|
||||||
color: Colors.redAccent),
|
|
||||||
onPressed: _disconnect,
|
|
||||||
tooltip: 'Disconnect',
|
tooltip: 'Disconnect',
|
||||||
|
onPressed: () async {
|
||||||
|
await ref.read(btProvider.notifier).disconnect();
|
||||||
|
if (context.mounted) {
|
||||||
|
Navigator.of(context).pushReplacement(
|
||||||
|
MaterialPageRoute(builder: (_) => const BtPickerScreen()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
bottom: const TabBar(
|
bottom: const TabBar(
|
||||||
tabs: [
|
tabs: [Tab(text: 'LIVE DATA'), Tab(text: 'DEBUG LOG')],
|
||||||
Tab(text: 'LIVE DATA'),
|
|
||||||
Tab(text: 'DEBUG LOG'),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
body: TabBarView(
|
body: TabBarView(
|
||||||
children: [
|
children: [
|
||||||
// ── Tab 1: Live Sensor Data ──
|
// ── Tab 1: Live sensor data ──
|
||||||
_state == null
|
sensorAsync.when(
|
||||||
? Center(
|
loading: () => const _WaitingForData(),
|
||||||
child: Column(
|
error: (e, _) => _ErrorCenter(message: e.toString()),
|
||||||
mainAxisSize: MainAxisSize.min,
|
data: (state) => _SensorGrid(
|
||||||
children: [
|
state: state,
|
||||||
const CircularProgressIndicator(
|
frameCount: btState.frameCount,
|
||||||
color: Colors.orange),
|
|
||||||
const SizedBox(height: 20),
|
|
||||||
const Text('Waiting for ECU response…',
|
|
||||||
style: TextStyle(fontSize: 16)),
|
|
||||||
const SizedBox(height: 8),
|
|
||||||
Text(
|
|
||||||
'Sending ${protocol} request every 100ms\nMake sure ECU is powered on',
|
|
||||||
style: const TextStyle(
|
|
||||||
color: Colors.grey, fontSize: 13),
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
),
|
||||||
const SizedBox(height: 24),
|
|
||||||
const Text(
|
|
||||||
'Check DEBUG LOG tab for details',
|
|
||||||
style: TextStyle(
|
|
||||||
color: Colors.orange, fontSize: 13),
|
|
||||||
),
|
),
|
||||||
],
|
|
||||||
),
|
|
||||||
)
|
|
||||||
: _SensorGrid(state: _state!, frameCount: _frameCount),
|
|
||||||
|
|
||||||
// ── Tab 2: Debug Log ──
|
// ── Tab 2: Error/status log ──
|
||||||
_DebugLogView(log: _log),
|
_DebugTab(btState: btState, sensorAsync: sensorAsync),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -480,7 +333,135 @@ class _LiveDataScreenState extends State<LiveDataScreen> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Sensor Grid ─────────────────────────────────────────────────────────────
|
// ─── Waiting / Error states ───────────────────────────────────────────────────
|
||||||
|
|
||||||
|
class _WaitingForData extends StatelessWidget {
|
||||||
|
const _WaitingForData();
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return const Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
CircularProgressIndicator(color: Colors.orange),
|
||||||
|
SizedBox(height: 20),
|
||||||
|
Text('Waiting for ECU response…', style: TextStyle(fontSize: 16)),
|
||||||
|
SizedBox(height: 8),
|
||||||
|
Text(
|
||||||
|
'Sending request every 100ms\nMake sure ECU is powered on',
|
||||||
|
style: TextStyle(color: Colors.grey, fontSize: 13),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
SizedBox(height: 24),
|
||||||
|
Text('Check DEBUG LOG tab for details',
|
||||||
|
style: TextStyle(color: Colors.orange, fontSize: 13)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ErrorCenter extends StatelessWidget {
|
||||||
|
final String message;
|
||||||
|
const _ErrorCenter({required this.message});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Center(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(24),
|
||||||
|
child: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.error_outline, size: 48, color: Colors.red),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
Text(message,
|
||||||
|
style: const TextStyle(color: Colors.redAccent),
|
||||||
|
textAlign: TextAlign.center),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Debug Tab ────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
class _DebugTab extends StatelessWidget {
|
||||||
|
final BtState btState;
|
||||||
|
final AsyncValue<SensorState> sensorAsync;
|
||||||
|
|
||||||
|
const _DebugTab({required this.btState, required this.sensorAsync});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
final lines = <_DebugLine>[];
|
||||||
|
|
||||||
|
lines.add(_DebugLine(
|
||||||
|
icon: btState.isConnected ? Icons.check_circle_outline : Icons.info_outline,
|
||||||
|
color: btState.isConnected ? Colors.green : Colors.orange,
|
||||||
|
text: 'BT: ${btState.status.name.toUpperCase()} '
|
||||||
|
'${btState.deviceName != null ? "→ ${btState.deviceName}" : ""}',
|
||||||
|
));
|
||||||
|
|
||||||
|
if (btState.error != null) {
|
||||||
|
lines.add(_DebugLine(
|
||||||
|
icon: Icons.error_outline,
|
||||||
|
color: Colors.red,
|
||||||
|
text: 'Error: ${btState.error}'));
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.add(_DebugLine(
|
||||||
|
icon: Icons.analytics_outlined,
|
||||||
|
color: Colors.grey,
|
||||||
|
text:
|
||||||
|
'Frames: ${btState.frameCount} Dropped: ${btState.droppedFrames} '
|
||||||
|
'Rate: ${btState.frameCount > 0 ? "~10 Hz" : "0 Hz"}',
|
||||||
|
));
|
||||||
|
|
||||||
|
sensorAsync.whenOrNull(
|
||||||
|
error: (e, _) => lines.add(_DebugLine(
|
||||||
|
icon: Icons.error_outline, color: Colors.red, text: 'Parse error: $e')),
|
||||||
|
data: (s) => lines.add(_DebugLine(
|
||||||
|
icon: Icons.check_circle_outline,
|
||||||
|
color: Colors.green,
|
||||||
|
text: 'Last frame: RPM=${s.rpm.toStringAsFixed(0)} '
|
||||||
|
'ECT=${s.ect.toStringAsFixed(1)}°C '
|
||||||
|
'BAT=${s.bat.toStringAsFixed(2)}V')),
|
||||||
|
);
|
||||||
|
|
||||||
|
return ListView(
|
||||||
|
padding: const EdgeInsets.all(12),
|
||||||
|
children: lines
|
||||||
|
.map((l) => Padding(
|
||||||
|
padding: const EdgeInsets.symmetric(vertical: 5),
|
||||||
|
child: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Icon(l.icon, size: 15, color: l.color),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Expanded(
|
||||||
|
child: Text(l.text,
|
||||||
|
style: TextStyle(
|
||||||
|
color: l.color.withAlpha(220), fontSize: 12)),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
))
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _DebugLine {
|
||||||
|
final IconData icon;
|
||||||
|
final Color color;
|
||||||
|
final String text;
|
||||||
|
const _DebugLine({required this.icon, required this.color, required this.text});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ─── Sensor Grid ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
class _SensorGrid extends StatelessWidget {
|
class _SensorGrid extends StatelessWidget {
|
||||||
final SensorState state;
|
final SensorState state;
|
||||||
@ -523,25 +504,23 @@ class _SensorGrid extends StatelessWidget {
|
|||||||
crossAxisSpacing: 6,
|
crossAxisSpacing: 6,
|
||||||
childAspectRatio: 2.4,
|
childAspectRatio: 2.4,
|
||||||
children: [
|
children: [
|
||||||
_SensorTile('VSS', '${state.vss.toStringAsFixed(1)} km/h'),
|
_Tile('VSS', '${state.vss.toStringAsFixed(1)} km/h'),
|
||||||
_SensorTile('MAP', '${state.map.toStringAsFixed(1)} kPa'),
|
_Tile('MAP', '${state.map.toStringAsFixed(1)} kPa'),
|
||||||
_SensorTile('TPS', '${state.tps.toStringAsFixed(1)} %'),
|
_Tile('TPS', '${state.tps.toStringAsFixed(1)} %'),
|
||||||
_SensorTile('ECT', '${state.ect.toStringAsFixed(1)} °C'),
|
_Tile('ECT', '${state.ect.toStringAsFixed(1)} °C'),
|
||||||
_SensorTile('IAT', '${state.iat.toStringAsFixed(1)} °C'),
|
_Tile('IAT', '${state.iat.toStringAsFixed(1)} °C'),
|
||||||
_SensorTile('BAT', '${state.bat.toStringAsFixed(2)} V'),
|
_Tile('BAT', '${state.bat.toStringAsFixed(2)} V'),
|
||||||
_SensorTile('IGN', '${state.ign.toStringAsFixed(1)} °'),
|
_Tile('IGN', '${state.ign.toStringAsFixed(1)} °'),
|
||||||
_SensorTile('INJ', '${state.inj.toStringAsFixed(2)} ms'),
|
_Tile('INJ', '${state.inj.toStringAsFixed(2)} ms'),
|
||||||
_SensorTile('O2', state.o2.toStringAsFixed(0)),
|
_Tile('O2', state.o2.toStringAsFixed(0)),
|
||||||
_SensorTile('AFR', state.afr.toStringAsFixed(2)),
|
_Tile('AFR', state.afr.toStringAsFixed(2)),
|
||||||
_SensorTile('ETH', '${state.eth.toStringAsFixed(0)} %'),
|
_Tile('ETH', '${state.eth.toStringAsFixed(0)} %'),
|
||||||
_SensorTile('GEAR', state.gear.toString()),
|
_Tile('GEAR', state.gear.toString()),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Text(
|
Text('Frames: $frameCount',
|
||||||
'Frames received: $frameCount',
|
style: const TextStyle(color: Colors.grey, fontSize: 11)),
|
||||||
style: const TextStyle(color: Colors.grey, fontSize: 11),
|
|
||||||
),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@ -554,134 +533,11 @@ class _SensorGrid extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Debug Log View ───────────────────────────────────────────────────────────
|
// ─── Shared UI widgets ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
class _DebugLogView extends StatelessWidget {
|
|
||||||
final List<_LogEntry> log;
|
|
||||||
const _DebugLogView({required this.log});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
if (log.isEmpty) {
|
|
||||||
return const Center(
|
|
||||||
child: Text('No log entries yet.',
|
|
||||||
style: TextStyle(color: Colors.grey)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return ListView.builder(
|
|
||||||
padding: const EdgeInsets.all(8),
|
|
||||||
itemCount: log.length,
|
|
||||||
itemBuilder: (context, index) {
|
|
||||||
final entry = log[index];
|
|
||||||
return Padding(
|
|
||||||
padding: const EdgeInsets.symmetric(vertical: 2),
|
|
||||||
child: Row(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
_ts(entry.time),
|
|
||||||
style: const TextStyle(
|
|
||||||
color: Colors.grey,
|
|
||||||
fontSize: 11,
|
|
||||||
fontFamily: 'monospace'),
|
|
||||||
),
|
|
||||||
const SizedBox(width: 8),
|
|
||||||
Icon(
|
|
||||||
entry.level == LogLevel.error
|
|
||||||
? Icons.error_outline
|
|
||||||
: entry.level == LogLevel.ok
|
|
||||||
? Icons.check_circle_outline
|
|
||||||
: Icons.info_outline,
|
|
||||||
size: 14,
|
|
||||||
color: entry.level == LogLevel.error
|
|
||||||
? Colors.red
|
|
||||||
: entry.level == LogLevel.ok
|
|
||||||
? Colors.green
|
|
||||||
: Colors.grey,
|
|
||||||
),
|
|
||||||
const SizedBox(width: 6),
|
|
||||||
Expanded(
|
|
||||||
child: Text(
|
|
||||||
entry.message,
|
|
||||||
style: TextStyle(
|
|
||||||
fontSize: 12,
|
|
||||||
color: entry.level == LogLevel.error
|
|
||||||
? Colors.redAccent
|
|
||||||
: entry.level == LogLevel.ok
|
|
||||||
? Colors.greenAccent
|
|
||||||
: Colors.white70,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
String _ts(DateTime t) =>
|
|
||||||
'${t.hour.toString().padLeft(2, '0')}:${t.minute.toString().padLeft(2, '0')}:${t.second.toString().padLeft(2, '0')}';
|
|
||||||
}
|
|
||||||
|
|
||||||
enum LogLevel { info, ok, error }
|
|
||||||
|
|
||||||
class _LogEntry {
|
|
||||||
final String message;
|
|
||||||
final LogLevel level;
|
|
||||||
final DateTime time;
|
|
||||||
const _LogEntry(
|
|
||||||
{required this.message, required this.level, required this.time});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── Error Banner ─────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
class _ErrorBanner extends StatelessWidget {
|
|
||||||
final String message;
|
|
||||||
final VoidCallback onDismiss;
|
|
||||||
|
|
||||||
const _ErrorBanner({required this.message, required this.onDismiss});
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Container(
|
|
||||||
width: double.infinity,
|
|
||||||
color: const Color(0xFF4A0000),
|
|
||||||
padding: const EdgeInsets.fromLTRB(16, 12, 8, 12),
|
|
||||||
child: Row(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
const Icon(Icons.error_outline,
|
|
||||||
color: Colors.redAccent, size: 20),
|
|
||||||
const SizedBox(width: 10),
|
|
||||||
Expanded(
|
|
||||||
child: Text(
|
|
||||||
message,
|
|
||||||
style: const TextStyle(
|
|
||||||
color: Colors.redAccent, fontSize: 13),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
IconButton(
|
|
||||||
icon: const Icon(Icons.close,
|
|
||||||
color: Colors.redAccent, size: 20),
|
|
||||||
onPressed: onDismiss,
|
|
||||||
padding: EdgeInsets.zero,
|
|
||||||
constraints: const BoxConstraints(),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ─── UI helpers ──────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
class _BigValue extends StatelessWidget {
|
class _BigValue extends StatelessWidget {
|
||||||
final String label;
|
final String label, value, unit;
|
||||||
final String value;
|
|
||||||
final String unit;
|
|
||||||
final Color color;
|
final Color color;
|
||||||
|
|
||||||
const _BigValue(
|
const _BigValue(
|
||||||
{required this.label,
|
{required this.label,
|
||||||
required this.value,
|
required this.value,
|
||||||
@ -702,30 +558,23 @@ class _BigValue extends StatelessWidget {
|
|||||||
children: [
|
children: [
|
||||||
Text(label,
|
Text(label,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: color,
|
color: color, fontSize: 12, letterSpacing: 2,
|
||||||
fontSize: 12,
|
|
||||||
letterSpacing: 2,
|
|
||||||
fontWeight: FontWeight.w600)),
|
fontWeight: FontWeight.w600)),
|
||||||
const SizedBox(height: 2),
|
const SizedBox(height: 2),
|
||||||
Text(value,
|
Text(value,
|
||||||
style: TextStyle(
|
style: TextStyle(color: color, fontSize: 48,
|
||||||
color: color,
|
fontWeight: FontWeight.bold, letterSpacing: -2)),
|
||||||
fontSize: 48,
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
letterSpacing: -2)),
|
|
||||||
Text(unit,
|
Text(unit,
|
||||||
style: TextStyle(
|
style: TextStyle(color: color.withAlpha(160), fontSize: 12)),
|
||||||
color: color.withAlpha(160), fontSize: 12)),
|
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _SensorTile extends StatelessWidget {
|
class _Tile extends StatelessWidget {
|
||||||
final String label;
|
final String label, value;
|
||||||
final String value;
|
const _Tile(this.label, this.value);
|
||||||
const _SensorTile(this.label, this.value);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -733,20 +582,16 @@ class _SensorTile extends StatelessWidget {
|
|||||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
|
||||||
decoration: BoxDecoration(
|
decoration: BoxDecoration(
|
||||||
color: const Color(0xFF1A1A1A),
|
color: const Color(0xFF1A1A1A),
|
||||||
borderRadius: BorderRadius.circular(8),
|
borderRadius: BorderRadius.circular(8)),
|
||||||
),
|
|
||||||
child: Row(
|
child: Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
children: [
|
children: [
|
||||||
Text(label,
|
Text(label,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
color: Colors.grey,
|
color: Colors.grey, fontSize: 11, letterSpacing: 1)),
|
||||||
fontSize: 11,
|
|
||||||
letterSpacing: 1)),
|
|
||||||
Text(value,
|
Text(value,
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
color: Colors.white,
|
color: Colors.white, fontSize: 14,
|
||||||
fontSize: 14,
|
|
||||||
fontWeight: FontWeight.w600)),
|
fontWeight: FontWeight.w600)),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@ -769,9 +614,7 @@ class _FlagPill extends StatelessWidget {
|
|||||||
color: active ? color.withAlpha(40) : const Color(0xFF1A1A1A),
|
color: active ? color.withAlpha(40) : const Color(0xFF1A1A1A),
|
||||||
borderRadius: BorderRadius.circular(20),
|
borderRadius: BorderRadius.circular(20),
|
||||||
border: Border.all(
|
border: Border.all(
|
||||||
color: active ? color : Colors.grey.withAlpha(60),
|
color: active ? color : Colors.grey.withAlpha(60), width: 1.5),
|
||||||
width: 1.5,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
child: Text(
|
child: Text(
|
||||||
label,
|
label,
|
||||||
@ -779,8 +622,39 @@ class _FlagPill extends StatelessWidget {
|
|||||||
color: active ? color : Colors.grey,
|
color: active ? color : Colors.grey,
|
||||||
fontSize: 11,
|
fontSize: 11,
|
||||||
fontWeight: active ? FontWeight.bold : FontWeight.normal,
|
fontWeight: active ? FontWeight.bold : FontWeight.normal,
|
||||||
letterSpacing: 1,
|
letterSpacing: 1),
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _ErrorBanner extends StatelessWidget {
|
||||||
|
final String message;
|
||||||
|
final VoidCallback onDismiss;
|
||||||
|
const _ErrorBanner({required this.message, required this.onDismiss});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Container(
|
||||||
|
width: double.infinity,
|
||||||
|
color: const Color(0xFF4A0000),
|
||||||
|
padding: const EdgeInsets.fromLTRB(16, 10, 8, 10),
|
||||||
|
child: Row(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
const Icon(Icons.error_outline, color: Colors.redAccent, size: 18),
|
||||||
|
const SizedBox(width: 8),
|
||||||
|
Expanded(
|
||||||
|
child: Text(message,
|
||||||
|
style: const TextStyle(
|
||||||
|
color: Colors.redAccent, fontSize: 12))),
|
||||||
|
IconButton(
|
||||||
|
icon: const Icon(Icons.close, color: Colors.redAccent, size: 18),
|
||||||
|
onPressed: onDismiss,
|
||||||
|
padding: EdgeInsets.zero,
|
||||||
|
constraints: const BoxConstraints(),
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@ description: HV BT Automotive ECU Dashboard
|
|||||||
|
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
version: 1.0.1+2
|
version: 1.2.0+3
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: '>=3.0.0 <4.0.0'
|
sdk: '>=3.0.0 <4.0.0'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user