Skip to content

Commit e194756

Browse files
imutest: make it work on desktop
1 parent 1e6f7a5 commit e194756

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

  • internal_filesystem/apps/com.example.imutest/assets

internal_filesystem/apps/com.example.imutest/assets/imutest.py

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from machine import Pin, I2C
2-
from qmi8658 import QMI8658
3-
import machine
4-
51
def map_nonlinear(value: float) -> int:
62
# Preserve sign and work with absolute value
73
sign = 1 if value >= 0 else -1
@@ -16,21 +12,34 @@ def map_nonlinear(value: float) -> int:
1612
return int(50.0 + (sign * scaled)) # Shift to [0, 100]
1713

1814
def refresh(timer):
19-
#print(f"""{sensor.temperature=} {sensor.acceleration=} {sensor.gyro=}""")
20-
templabel.set_text(f"IMU chip temperature: {sensor.temperature:.2f}°C")
21-
ax = sensor.acceleration[0]
22-
axp = int((ax * 100 + 100)/2)
23-
ay = sensor.acceleration[1]
24-
ayp = int((ay * 100 + 100)/2)
25-
az = sensor.acceleration[2]
26-
azp = int((az * 100 + 100)/2)
15+
if have_imu:
16+
#print(f"""{sensor.temperature=} {sensor.acceleration=} {sensor.gyro=}""")
17+
temp = sensor.temperature
18+
ax = sensor.acceleration[0]
19+
axp = int((ax * 100 + 100)/2)
20+
ay = sensor.acceleration[1]
21+
ayp = int((ay * 100 + 100)/2)
22+
az = sensor.acceleration[2]
23+
azp = int((az * 100 + 100)/2)
24+
# values between -200 and 200 => /4 becomes -50 and 50 => +50 becomes 0 and 100
25+
gx = map_nonlinear(sensor.gyro[0])
26+
gy = map_nonlinear(sensor.gyro[1])
27+
gz = map_nonlinear(sensor.gyro[2])
28+
else:
29+
temp = 12.34
30+
axp = 25
31+
ayp = 50
32+
azp = 75
33+
gx = 45
34+
gy = 50
35+
gz = 55
36+
templabel.set_text(f"IMU chip temperature: {temp:.2f}°C")
2737
sliderx.set_value(axp, lv.ANIM.OFF)
2838
slidery.set_value(ayp, lv.ANIM.OFF)
2939
sliderz.set_value(azp, lv.ANIM.OFF)
30-
# values between -200 and 200 => /4 becomes -50 and 50 => +50 becomes 0 and 100
31-
slidergx.set_value(map_nonlinear(sensor.gyro[0]), lv.ANIM.OFF)
32-
slidergy.set_value(map_nonlinear(sensor.gyro[1]), lv.ANIM.OFF)
33-
slidergz.set_value(map_nonlinear(sensor.gyro[2]), lv.ANIM.OFF)
40+
slidergx.set_value(gx, lv.ANIM.OFF)
41+
slidergy.set_value(gy, lv.ANIM.OFF)
42+
slidergz.set_value(gz, lv.ANIM.OFF)
3443

3544

3645
def janitor_cb(timer):
@@ -39,7 +48,16 @@ def janitor_cb(timer):
3948
janitor.delete()
4049
refresh_timer.delete()
4150

42-
sensor = QMI8658(I2C(0, sda=machine.Pin(48), scl=machine.Pin(47)))
51+
have_imu = True
52+
try:
53+
from machine import Pin, I2C
54+
from qmi8658 import QMI8658
55+
import machine
56+
sensor = QMI8658(I2C(0, sda=machine.Pin(48), scl=machine.Pin(47)))
57+
except Exception as e:
58+
print(f"Warning: could not initialize IMU hardware: {e}")
59+
have_imu=False
60+
4361

4462
appscreen = lv.screen_active()
4563
templabel = lv.label(appscreen)

0 commit comments

Comments
 (0)