Better precision

This commit is contained in:
Thomas Farstrike
2025-04-23 22:50:26 +02:00
parent 54d7864f5f
commit ed839d0620
+14 -24
View File
@@ -3,23 +3,11 @@ from qmi8658 import QMI8658
import time
import machine
sensor = QMI8658(I2C(0, sda=machine.Pin(48), scl=machine.Pin(47)))
templabel = lv.label(subwindow)
templabel.align(lv.ALIGN.TOP_MID, 0, 10)
#scale = lv.scale(subwindow)
#scale.set_size(lv.pct(80), 50)
#scale.align(lv.ALIGN.TOP_MID, 0, 40)
#acclabelx = lv.label(subwindow)
#acclabelx.align(lv.ALIGN.CENTER, -100, 0)
#acclabely = lv.label(subwindow)
#acclabely.align(lv.ALIGN.CENTER, 0, 0)
#acclabelz = lv.label(subwindow)
#acclabelz.align(lv.ALIGN.CENTER, 100, 0)
sliderx = lv.slider(subwindow)
sliderx.align(lv.ALIGN.CENTER, 0, -60)
slidery = lv.slider(subwindow)
@@ -35,20 +23,22 @@ slidergz = lv.slider(subwindow)
slidergz.align(lv.ALIGN.CENTER, 0, 90)
def map_nonlinear(value: float) -> int:
"""Convert a value from [-200, 200] to [0, 100] with non-linear stretching around 0."""
# Step 1: Normalize input from [-200, 200] to [-1, 1]
normalized = value / 200.0
# Step 2: Apply non-linear transformation (preserve sign)
sign = 1 if normalized >= 0 else -1
abs_normalized = abs(normalized)
# Use square root (or another power < 1) to stretch small values
non_linear = sign * (abs_normalized ** 0.5)
# Step 3: Scale and shift to [0, 100]
# Map [-1, 1] to [0, 100] (i.e., -1 -> 0, 0 -> 50, 1 -> 100)
return int((non_linear + 1) * 50.0)
# Preserve sign and work with absolute value
sign = 1 if value >= 0 else -1
abs_value = abs(value)
# Apply non-linear transformation (square root) to absolute value
# Scale input range [0, 200] to [0, sqrt(200)] first
sqrt_value = (abs_value ** 0.5)
# Scale to output range [0, 100]
# Map [0, sqrt(200)] to [50, 100] for positive, [0, 50] for negative
max_sqrt = 200.0 ** 0.5 # Approx 14.142
scaled = (sqrt_value / max_sqrt) * 50.0 # Scale to [0, 50]
return int(50.0 + (sign * scaled)) # Shift to [0, 100]
canary = lv.obj(subwindow)
canary.add_flag(0x0001) # LV_OBJ_FLAG_HIDDEN is 0x0001
canary.add_flag(lv.obj.FLAG.HIDDEN)
while canary.get_class():
#print(f"""{sensor.temperature=} {sensor.acceleration=} {sensor.gyro=}""")
templabel.set_text(f"Temperature: {sensor.temperature:.2f}")