Add dev-tests/test-allow-missing-uart.py
This commit is contained in:
25
dev-tests/test-allow-missing-uart.py
Normal file
25
dev-tests/test-allow-missing-uart.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from pybricks.iodevices import UARTDevice as _UARTDevice
|
||||
from pybricks.tools import wait
|
||||
from uerrno import ETIMEDOUT
|
||||
|
||||
class FakeUART:
|
||||
def __init__(self, port, baudrate, timeout):
|
||||
self.timeout = timeout
|
||||
print("Warning: No physical UART detected. Using simulator.")
|
||||
|
||||
def read(self, length=1):
|
||||
if self.timeout is not None:
|
||||
wait(self.timeout)
|
||||
raise OSError(ETIMEDOUT)
|
||||
else:
|
||||
while True:
|
||||
wait(1000)
|
||||
|
||||
def write(self, data):
|
||||
pass
|
||||
|
||||
def UARTDevice(port, baudrate=9600, timeout=None):
|
||||
try:
|
||||
return _UARTDevice(port, baudrate, timeout)
|
||||
except OSError:
|
||||
return FakeUART(port, baudrate, timeout)
|
||||
Reference in New Issue
Block a user