Update diagnostics/os_diagnostics.py
This commit is contained in:
@@ -1,5 +1,29 @@
|
|||||||
from pybricks.parameters import Port
|
from pybricks.parameters import Port
|
||||||
from uerrno import EAGAIN, EBUSY, ECANCELED, EINVAL, EIO, ENODEV, EOPNOTSUPP, EPERM, ETIMEDOUT
|
from uerrno import EAGAIN, EBUSY, ECANCELED, EINVAL, EIO, ENODEV, EOPNOTSUPP, EPERM, ETIMEDOUT
|
||||||
|
from pybricks.iodevices import UARTDevice as _UARTDevice
|
||||||
|
from pybricks.tools import wait
|
||||||
|
|
||||||
|
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)
|
||||||
class OSDiagnostics:
|
class OSDiagnostics:
|
||||||
def __init__(self, hub, motorclass):
|
def __init__(self, hub, motorclass):
|
||||||
self.motorclass = motorclass
|
self.motorclass = motorclass
|
||||||
@@ -23,6 +47,21 @@ class OSDiagnostics:
|
|||||||
else:
|
else:
|
||||||
print(f"Another error occurred with code: {ex.errno}.\nCompleted Test 6/9: ENODEV - FAILED")
|
print(f"Another error occurred with code: {ex.errno}.\nCompleted Test 6/9: ENODEV - FAILED")
|
||||||
self.failedtests["ENODEV"] = ex.errno
|
self.failedtests["ENODEV"] = ex.errno
|
||||||
|
def testetimedout(self):
|
||||||
|
uart = UARTDevice(Port.A, baudrate=9600, timeout=1000) # 1000ms timeout
|
||||||
|
# Test ETIMEDOUT
|
||||||
|
try:
|
||||||
|
data = uart.read(10) # FakeUART will wait `timeout` ms, then raise OSError(ETIMEDOUT)
|
||||||
|
except OSError as ex:
|
||||||
|
if ex.errno == ETIMEDOUT:
|
||||||
|
print("Timed out with synthetic UART device. ETIMEDOUT can be thrown and caught.\nCompleted Test 9/9: ETIMEDOUT - SUCCESSFUL")
|
||||||
|
self.successfultests += 1
|
||||||
|
elif ex.errno == EIO:
|
||||||
|
print("An unspecified error occurred (EIO).\nCompleted Test 9/9: ETIMEDOUT - FAILED")
|
||||||
|
self.failedtests["ETIMEDOUT"] = "EIO - Unspecified Error"
|
||||||
|
else:
|
||||||
|
print(f"Another error occurred with code: {ex.errno}.\nCompleted Test 9/9: ETIMEDOUT - FAILED")
|
||||||
|
self.failedtests["ETIMEDOUT"] = ex.errno
|
||||||
def print_results(self):
|
def print_results(self):
|
||||||
for key, value in self.failedtests():
|
for key, value in self.failedtests():
|
||||||
print(f"{key}: {value}")
|
print(f"{key}: {value}")
|
||||||
Reference in New Issue
Block a user