71 lines
2.7 KiB
Dart
71 lines
2.7 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
final calibrationServiceProvider = Provider<CalibrationService>((ref) {
|
|
return CalibrationService();
|
|
});
|
|
|
|
class CalibrationService {
|
|
static const _pitchKey = 'calibration_pitch';
|
|
static const _rollKey = 'calibration_roll';
|
|
static const _gyroBiasXKey = 'calibration_gyro_bias_x';
|
|
static const _gyroBiasYKey = 'calibration_gyro_bias_y';
|
|
static const _gyroBiasZKey = 'calibration_gyro_bias_z';
|
|
|
|
Future<void> saveCalibrationOffsets(
|
|
double pitch,
|
|
double roll, {
|
|
double gyroBiasX = 0.0,
|
|
double gyroBiasY = 0.0,
|
|
double gyroBiasZ = 0.0,
|
|
int? motorcycleId,
|
|
}) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
if (motorcycleId != null) {
|
|
await prefs.setDouble('calibration_pitch_$motorcycleId', pitch);
|
|
await prefs.setDouble('calibration_roll_$motorcycleId', roll);
|
|
await prefs.setDouble('calibration_gyro_bias_x_$motorcycleId', gyroBiasX);
|
|
await prefs.setDouble('calibration_gyro_bias_y_$motorcycleId', gyroBiasY);
|
|
await prefs.setDouble('calibration_gyro_bias_z_$motorcycleId', gyroBiasZ);
|
|
}
|
|
// Also save global fallback
|
|
await prefs.setDouble(_pitchKey, pitch);
|
|
await prefs.setDouble(_rollKey, roll);
|
|
await prefs.setDouble(_gyroBiasXKey, gyroBiasX);
|
|
await prefs.setDouble(_gyroBiasYKey, gyroBiasY);
|
|
await prefs.setDouble(_gyroBiasZKey, gyroBiasZ);
|
|
}
|
|
|
|
Future<Map<String, double>> loadCalibrationOffsets({int? motorcycleId}) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
if (motorcycleId != null) {
|
|
final bikePitch = prefs.getDouble('calibration_pitch_$motorcycleId');
|
|
final bikeRoll = prefs.getDouble('calibration_roll_$motorcycleId');
|
|
final bikeGbx = prefs.getDouble('calibration_gyro_bias_x_$motorcycleId') ?? 0.0;
|
|
final bikeGby = prefs.getDouble('calibration_gyro_bias_y_$motorcycleId') ?? 0.0;
|
|
final bikeGbz = prefs.getDouble('calibration_gyro_bias_z_$motorcycleId') ?? 0.0;
|
|
if (bikePitch != null && bikeRoll != null) {
|
|
return {
|
|
'pitch': bikePitch,
|
|
'roll': bikeRoll,
|
|
'gyroBiasX': bikeGbx,
|
|
'gyroBiasY': bikeGby,
|
|
'gyroBiasZ': bikeGbz,
|
|
};
|
|
}
|
|
}
|
|
final pitch = prefs.getDouble(_pitchKey) ?? 0.0;
|
|
final roll = prefs.getDouble(_rollKey) ?? 0.0;
|
|
final gbx = prefs.getDouble(_gyroBiasXKey) ?? 0.0;
|
|
final gby = prefs.getDouble(_gyroBiasYKey) ?? 0.0;
|
|
final gbz = prefs.getDouble(_gyroBiasZKey) ?? 0.0;
|
|
return {
|
|
'pitch': pitch,
|
|
'roll': roll,
|
|
'gyroBiasX': gbx,
|
|
'gyroBiasY': gby,
|
|
'gyroBiasZ': gbz,
|
|
};
|
|
}
|
|
}
|