Skip to content

Commit 23b8296

Browse files
authored
fix: implemented accelerometer gain (fossasia#3357)
1 parent 501515f commit 23b8296

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

lib/models/accelerometer_config.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ class AccelerometerConfig {
33
final int highLimit;
44
final int lowLimit;
55
final String activeSensor;
6-
final int sensorGain;
6+
final double sensorGain;
77
final bool includeLocationData;
88

99
const AccelerometerConfig({
1010
this.updatePeriod = 1000,
1111
this.highLimit = 200,
1212
this.lowLimit = 200,
1313
this.activeSensor = 'In-built Sensor',
14-
this.sensorGain = 1,
14+
this.sensorGain = 1.0,
1515
this.includeLocationData = true,
1616
});
1717

@@ -20,7 +20,7 @@ class AccelerometerConfig {
2020
int? highLimit,
2121
int? lowLimit,
2222
String? activeSensor,
23-
int? sensorGain,
23+
double? sensorGain,
2424
bool? includeLocationData,
2525
}) {
2626
return AccelerometerConfig(
@@ -50,7 +50,7 @@ class AccelerometerConfig {
5050
highLimit: json['highLimit'] ?? 200,
5151
lowLimit: json['lowLimit'] ?? json['depthLimit'] ?? 200,
5252
activeSensor: json['activeSensor'] ?? 'In-built Sensor',
53-
sensorGain: json['sensorGain'] ?? 1,
53+
sensorGain: json['sensorGain'] ?? 1.0,
5454
includeLocationData: json['includeLocationData'] ?? true,
5555
);
5656
}

lib/providers/accelerometer_config_provider.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class AccelerometerConfigProvider extends ChangeNotifier {
5858
_saveConfigToPrefs();
5959
}
6060

61-
void updateSensorGain(int sensorGain) {
61+
void updateSensorGain(double sensorGain) {
6262
_config = _config.copyWith(sensorGain: sensorGain);
6363
notifyListeners();
6464
_saveConfigToPrefs();

lib/providers/accelerometer_state_provider.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,21 +214,21 @@ class AccelerometerStateProvider extends ChangeNotifier {
214214
void _updateData() {
215215
final highLimit = _currentHighLimit;
216216
final lowLimit = _currentLowLimit;
217-
217+
final gain = (_configProvider?.config.sensorGain ?? 1.0).toDouble();
218218
final bool shouldClip = !_isPlayingBack;
219219

220220
final double x;
221221
final double y;
222222
final double z;
223223

224224
if (shouldClip && highLimit != null && lowLimit != null) {
225-
x = _accelerometerEvent.x.clamp(-lowLimit, highLimit).toDouble();
226-
y = _accelerometerEvent.y.clamp(-lowLimit, highLimit).toDouble();
227-
z = _accelerometerEvent.z.clamp(-lowLimit, highLimit).toDouble();
225+
x = (_accelerometerEvent.x * gain).clamp(-lowLimit, highLimit).toDouble();
226+
y = (_accelerometerEvent.y * gain).clamp(-lowLimit, highLimit).toDouble();
227+
z = (_accelerometerEvent.z * gain).clamp(-lowLimit, highLimit).toDouble();
228228
} else {
229-
x = _accelerometerEvent.x;
230-
y = _accelerometerEvent.y;
231-
z = _accelerometerEvent.z;
229+
x = _accelerometerEvent.x * gain;
230+
y = _accelerometerEvent.y * gain;
231+
z = _accelerometerEvent.z * gain;
232232
}
233233

234234
_accelerometerEvent = AccelerometerEvent(x, y, z, DateTime.now());

lib/view/accelerometer_config_screen.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,20 @@ class _AccelerometerConfigScreenState extends State<AccelerometerConfigScreen> {
185185
value: provider.config.sensorGain.toString(),
186186
controller: _sensorGainController,
187187
onChanged: (value) {
188-
final intValue = int.tryParse(value);
189-
if (intValue != null) {
190-
provider.updateSensorGain(intValue);
188+
final doubleValue = double.tryParse(value);
189+
if (doubleValue != null &&
190+
doubleValue >= 0 &&
191+
doubleValue <= 100) {
192+
provider.updateSensorGain(doubleValue);
193+
} else {
194+
ScaffoldMessenger.of(context).showSnackBar(
195+
SnackBar(
196+
content: Text(
197+
appLocalizations.sensorGainErrorMessage,
198+
style: TextStyle(color: snackBarContentColor),
199+
),
200+
backgroundColor: snackBarBackgroundColor),
201+
);
191202
}
192203
},
193204
hint: appLocalizations.sensorGainHint,

0 commit comments

Comments
 (0)