Skip to content

Commit d453e77

Browse files
Fix board detection by not using i2c scan
i2c scanning all the devices can confuse some of them, such as the OV* camera's. Better to selectively scan just the ones we want to know about, plus, it's also faster.
1 parent e8de453 commit d453e77

1 file changed

Lines changed: 32 additions & 11 deletions

File tree

  • internal_filesystem/lib/mpos

internal_filesystem/lib/mpos/main.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,52 @@ def init_rootscreen():
2626
img.set_blend_mode(lv.BLEND_MODE.DIFFERENCE)
2727
img.center()
2828

29+
def single_address_i2c_scan(i2c_bus, address):
30+
"""
31+
Scan a specific I2C address to check if a device is present.
32+
33+
Args:
34+
i2c_bus: An I2C bus object (machine.I2C instance)
35+
address: Integer address to scan (0-127)
36+
37+
Returns:
38+
True if a device responds at the specified address, False otherwise
39+
"""
40+
try:
41+
# Attempt to write a single byte to the address
42+
# This will raise an exception if no device responds
43+
i2c_bus.writeto(address, b'')
44+
return True
45+
except OSError:
46+
# No device at this address
47+
return False
48+
except Exception as e:
49+
# Handle any other exceptions gracefully
50+
print(f"single_address_i2c_scan: error scanning address 0x{address:02x}: {e}")
51+
return False
52+
2953
def detect_board():
3054
import sys
3155
if sys.platform == "linux" or sys.platform == "darwin": # linux and macOS
3256
return "linux"
3357
elif sys.platform == "esp32":
3458
from machine import Pin, I2C
35-
return "matouch_esp32_s3_2_8" # i2c scan confuses the camera so hard-code for now
3659

37-
i2c0 = I2C(0, sda=Pin(39), scl=Pin(38), freq=400000)
38-
devices = set(i2c0.scan()) # causes a "ghost" device to appear on 0x20 and breaks the camera
39-
if {0x14} <= devices or {0x5D} <= devices: # "ghost" device or real GT911
40-
return "matouch_esp32_s3_2_8"
41-
42-
i2c0 = I2C(0, sda=Pin(48), scl=Pin(47)) # on matouch_esp32_s3_2_8, this "finds" devices at all addresses 8-119 so only do this after matouch_esp32_s3_2_8
43-
if {0x15, 0x6B} <= set(i2c0.scan()): # touch screen and IMU (at least, possibly more)
60+
i2c0 = I2C(0, sda=Pin(48), scl=Pin(47))
61+
if single_address_i2c_scan(i2c0, 0x15) and single_address_i2c_scan(i2c0, 0x6B): # CST816S touch screen and IMU
4462
return "waveshare_esp32_s3_touch_lcd_2"
4563

64+
i2c0 = I2C(0, sda=Pin(39), scl=Pin(38))
65+
if single_address_i2c_scan(i2c0, 0x14) or single_address_i2c_scan(i2c0, 0x5D): # "ghost" or real GT911 touch screen
66+
return "matouch_esp32_s3_2_8"
67+
4668
i2c0 = I2C(0, sda=Pin(9), scl=Pin(18))
47-
if {0x6B} <= set(i2c0.scan()): # IMU (plus possibly the Communicator's LANA TNY at 0x38)
69+
if single_address_i2c_scan(i2c0, 0x6B): # IMU (plus possibly the Communicator's LANA TNY at 0x38)
4870
return "fri3d_2024"
4971

50-
# default: if {0x6A} <= set(i2c0.scan()): # IMU (plus a few others, to be added later, but this should work)
72+
# default: if single_address_i2c_scan(i2c0, 0x6A): # IMU but currently not installed
5173
return "fri3d_2026"
5274

53-
5475
board = detect_board()
5576
print(f"Initializing {board} hardware")
5677
DeviceInfo.set_hardware_id(board)

0 commit comments

Comments
 (0)