From b4577d0f66df5d74073f80539c94d667c5703883 Mon Sep 17 00:00:00 2001 From: Thomas Farstrike Date: Thu, 4 Dec 2025 13:51:52 +0100 Subject: [PATCH] Fix SensorManager --- CHANGELOG.md | 1 + CLAUDE.md | 3 ++- internal_filesystem/lib/mpos/board/linux.py | 5 ++++- internal_filesystem/lib/mpos/sensor_manager.py | 10 ++++++++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 05c98b1a..4dc26818 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - API: restore sys.path after starting app - API: add AudioFlinger for audio playback (i2s DAC and buzzer) - API: add LightsManager for multicolor LEDs +- API: add SensorManager for IMU/accelerometers, temperature sensors etc. - About app: add free, used and total storage space info - AppStore app: remove unnecessary scrollbar over publisher's name - Camera app: massive overhaul! diff --git a/CLAUDE.md b/CLAUDE.md index f6bacf39..e61a1e88 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1010,8 +1010,9 @@ def update_frame(self, a, b): - **Units**: Standard SI (m/s² for acceleration, deg/s for gyroscope, °C for temperature) - **Calibration**: Persistent via SharedPreferences (`data/com.micropythonos.sensors/config.json`) - **Thread-safe**: Uses locks for concurrent access -- **Auto-detection**: Identifies IMU type via chip ID registers +- **Auto-detection**: Identifies IMU type via chip ID registers (QMI8658: chip_id=0x05 at reg=0x00, WSEN_ISDS: chip_id=0x6A at reg=0x0F) - **Desktop**: Functions return `None` (graceful fallback) on desktop builds +- **Important**: Driver constants defined with `const()` cannot be imported at runtime - SensorManager uses hardcoded values instead ### Driver Locations diff --git a/internal_filesystem/lib/mpos/board/linux.py b/internal_filesystem/lib/mpos/board/linux.py index d5c3b6ee..a82a12ce 100644 --- a/internal_filesystem/lib/mpos/board/linux.py +++ b/internal_filesystem/lib/mpos/board/linux.py @@ -113,7 +113,10 @@ AudioFlinger.init( # === SENSOR HARDWARE === # Note: Desktop builds have no sensor hardware import mpos.sensor_manager as SensorManager -# Don't call init() - SensorManager functions will return None/False + +# Initialize with no I2C bus - will detect MCU temp if available +# (On Linux desktop, this will fail gracefully but set _initialized flag) +SensorManager.init(None) print("linux.py finished") diff --git a/internal_filesystem/lib/mpos/sensor_manager.py b/internal_filesystem/lib/mpos/sensor_manager.py index 4bca56e4..0f0d9563 100644 --- a/internal_filesystem/lib/mpos/sensor_manager.py +++ b/internal_filesystem/lib/mpos/sensor_manager.py @@ -98,7 +98,10 @@ def init(i2c_bus, address=0x6B): # Try QMI8658 first (Waveshare board) if i2c_bus: try: - from mpos.hardware.drivers.qmi8658 import QMI8658, _QMI8685_PARTID, _REG_PARTID + from mpos.hardware.drivers.qmi8658 import QMI8658 + # QMI8658 constants (can't import const() values) + _QMI8685_PARTID = 0x05 + _REG_PARTID = 0x00 chip_id = i2c_bus.readfrom_mem(address, _REG_PARTID, 1)[0] if chip_id == _QMI8685_PARTID: print("[SensorManager] Detected QMI8658 IMU") @@ -308,7 +311,10 @@ class _QMI8658Driver(_IMUDriver): """Wrapper for QMI8658 IMU (Waveshare board).""" def __init__(self, i2c_bus, address): - from mpos.hardware.drivers.qmi8658 import QMI8658, _ACCELSCALE_RANGE_8G, _GYROSCALE_RANGE_256DPS + from mpos.hardware.drivers.qmi8658 import QMI8658 + # QMI8658 scale constants (can't import const() values) + _ACCELSCALE_RANGE_8G = 0b10 + _GYROSCALE_RANGE_256DPS = 0b100 self.sensor = QMI8658( i2c_bus, address=address,