32 lines
927 B
Dart
32 lines
927 B
Dart
const double gearRatioMin = 0.25;
|
|
const double gearRatioMax = 5.75;
|
|
const int gearRatioEmptyRaw = 0;
|
|
const int gearRatioMinRaw = 1;
|
|
const int gearRatioMaxRaw = 255;
|
|
|
|
const double gearRatioStep =
|
|
(gearRatioMax - gearRatioMin) / (gearRatioMaxRaw - gearRatioMinRaw);
|
|
|
|
int encodeGearRatioByte(double value) {
|
|
if (value <= 0) {
|
|
return gearRatioEmptyRaw;
|
|
}
|
|
final clamped = value.clamp(gearRatioMin, gearRatioMax);
|
|
final scaled = ((clamped - gearRatioMin) / gearRatioStep).round();
|
|
return (gearRatioMinRaw + scaled)
|
|
.clamp(gearRatioMinRaw, gearRatioMaxRaw)
|
|
.toInt();
|
|
}
|
|
|
|
double decodeGearRatioByte(int raw) {
|
|
if (raw <= gearRatioEmptyRaw) {
|
|
return 0;
|
|
}
|
|
final clamped = raw.clamp(gearRatioMinRaw, gearRatioMaxRaw).toInt();
|
|
return gearRatioMin + (clamped - gearRatioMinRaw) * gearRatioStep;
|
|
}
|
|
|
|
double quantizeGearRatio(double value) {
|
|
return decodeGearRatioByte(encodeGearRatioByte(value));
|
|
}
|