55from pathlib import Path
66
77
8- def load_driver_mock (driver_name , fake_i2c ):
8+ def load_driver_mock (driver_name , fake_i2c , module_name = None ):
99 """Load a driver using FakeI2C on CPython.
1010
1111 Patches machine and micropython modules, imports the driver,
1212 and returns an instance configured with the fake I2C bus.
13+
14+ Args:
15+ driver_name: directory name under lib/ (e.g. 'wsen-pads')
16+ fake_i2c: FakeI2C instance with pre-loaded registers
17+ module_name: Python module name if different from driver_name
18+ (e.g. 'wsen_pads' when dir is 'wsen-pads')
1319 """
20+ if module_name is None :
21+ module_name = driver_name
1422 from tests .fake_machine import FakeI2C , FakePin
1523 from tests .fake_machine import micropython_stub
1624
@@ -32,6 +40,17 @@ def load_driver_mock(driver_name, fake_i2c):
3240 if not hasattr (time , "sleep_us" ):
3341 time .sleep_us = lambda us : time .sleep (us / 1000000 )
3442
43+ # Create utime module as alias for time (MicroPython compatibility)
44+ if "utime" not in sys .modules :
45+ utime = types .ModuleType ("utime" )
46+ utime .sleep_ms = time .sleep_ms
47+ utime .sleep_us = time .sleep_us
48+ utime .sleep = time .sleep
49+ utime .ticks_ms = lambda : int (time .time () * 1000 )
50+ utime .ticks_us = lambda : int (time .time () * 1000000 )
51+ utime .ticks_diff = lambda a , b : a - b
52+ sys .modules ["utime" ] = utime
53+
3554 # Add driver lib path to sys.path
3655 root = Path (__file__ ).parent .parent .parent
3756 driver_lib = root / "lib" / driver_name
@@ -40,17 +59,18 @@ def load_driver_mock(driver_name, fake_i2c):
4059
4160 # Force reimport of the driver module
4261 for mod_name in list (sys .modules ):
43- if mod_name .startswith (driver_name ):
62+ if mod_name .startswith (module_name ):
4463 del sys .modules [mod_name ]
4564
46- driver_module = importlib .import_module (f"{ driver_name } .device" )
65+ driver_module = importlib .import_module (f"{ module_name } .device" )
4766 return driver_module , fake_i2c
4867
4968
50- def cleanup_driver (driver_name ):
69+ def cleanup_driver (driver_name , module_name = None ):
5170 """Remove patched modules after test."""
71+ mod_prefix = module_name or driver_name
5272 for mod_name in list (sys .modules ):
53- if mod_name .startswith (driver_name ):
73+ if mod_name .startswith (mod_prefix ):
5474 del sys .modules [mod_name ]
5575 sys .modules .pop ("machine" , None )
5676 sys .modules .pop ("micropython" , None )
0 commit comments