diff --git a/meshtastic/stream_interface.py b/meshtastic/stream_interface.py index ec0670d6..6c53509f 100644 --- a/meshtastic/stream_interface.py +++ b/meshtastic/stream_interface.py @@ -8,7 +8,7 @@ from meshtastic.mesh_interface import MeshInterface -from meshtastic.util import stripnl +from meshtastic.util import stripnl, is_windows11 START1 = 0x94 @@ -38,6 +38,8 @@ def __init__(self, debugOut=None, noProto=False, connectNow=True): self._rxBuf = bytes() # empty self._wantExit = False + self.is_windows11 = is_windows11() + # FIXME, figure out why daemon=True causes reader thread to exit too early self._rxThread = threading.Thread(target=self.__reader, args=(), daemon=True) @@ -88,9 +90,12 @@ def _writeBytes(self, b): if self.stream: # ignore writes when stream is closed self.stream.write(b) self.stream.flush() - # we sleep here to give the TBeam a chance to work - # also win11 might need a bit more time, too - time.sleep(1.0) + # win11 might need a bit more time, too + if self.is_windows11: + time.sleep(1.0) + else: + # we sleep here to give the TBeam a chance to work + time.sleep(0.1) def _readBytes(self, length): """Read an array of bytes from our stream""" diff --git a/meshtastic/tests/test_util.py b/meshtastic/tests/test_util.py index fe326d91..399959c2 100644 --- a/meshtastic/tests/test_util.py +++ b/meshtastic/tests/test_util.py @@ -11,7 +11,8 @@ quoteBooleans, catchAndIgnore, remove_keys_from_dict, Timeout, hexstr, ipstr, readnet_u16, findPorts, convert_mac_addr, - snake_to_camel, camel_to_snake, eliminate_duplicate_port) + snake_to_camel, camel_to_snake, eliminate_duplicate_port, + is_windows11) @pytest.mark.unit @@ -284,3 +285,44 @@ def test_eliminate_duplicate_port(): assert eliminate_duplicate_port(['/dev/cu.usbserial-1430', '/dev/cu.wchusbserial1430']) == ['/dev/cu.wchusbserial1430'] assert eliminate_duplicate_port(['/dev/cu.SLAB_USBtoUART', '/dev/cu.usbserial-0001']) == ['/dev/cu.usbserial-0001'] assert eliminate_duplicate_port(['/dev/cu.usbmodem11301', '/dev/cu.wchusbserial11301']) == ['/dev/cu.wchusbserial11301'] + +@patch('platform.version', return_value='10.0.22000.194') +@patch('platform.release', return_value='10') +@patch('platform.system', return_value='Windows') +def test_is_windows11_true(patched_platform, patched_release, patched_version): + """Test is_windows11()""" + assert is_windows11() is True + patched_platform.assert_called() + patched_release.assert_called() + patched_version.assert_called() + + +@patch('platform.version', return_value='10.0.a2200.foo') # made up +@patch('platform.release', return_value='10') +@patch('platform.system', return_value='Windows') +def test_is_windows11_true2(patched_platform, patched_release, patched_version): + """Test is_windows11()""" + assert is_windows11() is False + patched_platform.assert_called() + patched_release.assert_called() + patched_version.assert_called() + + +@patch('platform.version', return_value='10.0.17763') # windows 10 home +@patch('platform.release', return_value='10') +@patch('platform.system', return_value='Windows') +def test_is_windows11_false(patched_platform, patched_release, patched_version): + """Test is_windows11()""" + assert is_windows11() is False + patched_platform.assert_called() + patched_release.assert_called() + patched_version.assert_called() + + +@patch('platform.release', return_value='8.1') +@patch('platform.system', return_value='Windows') +def test_is_windows11_false_win8_1(patched_platform, patched_release): + """Test is_windows11()""" + assert is_windows11() is False + patched_platform.assert_called() + patched_release.assert_called() diff --git a/meshtastic/util.py b/meshtastic/util.py index 21258f1c..e3d8a28f 100644 --- a/meshtastic/util.py +++ b/meshtastic/util.py @@ -413,3 +413,21 @@ def eliminate_duplicate_port(ports): else: new_ports = ports return new_ports + + +def is_windows11(): + """Detect if Windows 11""" + is_win11 = False + if platform.system() == "Windows": + if float(platform.release()) >= 10.0: + patch = platform.version().split('.')[2] + print(f'patch:{patch}') + # in case they add some number suffix later, just get first 5 chars of patch + patch = patch[:5] + try: + if int(patch) >= 22000: + is_win11 = True + except Exception as e: + print(f'problem detecting win11 e:{e}') + print(f'is_win11:{is_win11}') + return is_win11