From 990ccdb2687d7e6960c5fa7ac7815f01d3db0934 Mon Sep 17 00:00:00 2001 From: Ingo Jache Date: Mon, 12 Aug 2019 09:38:04 +0200 Subject: [PATCH] New LEGO-Boost Firmware 2.0.00.0017 breaks pylgbst... (#33) * - Updated Port-Numbers (New Firmware 2.0.00.0017) - Skipping Device without Mac-Address (Seems to occur randomly after Firmware-Update, referring to https://github.com/LLK/scratch-vm/issues/2230 ) - Fixed racecondition in Hub.send() (Reply could come before entering _sync_lock) * - fixed Unit-Tests (Changed Port-Numbers) - moved the write-call in hub.send() outside the lock --- .gitignore | 3 +- pylgbst/comms/cpygatt.py | 9 +++--- pylgbst/hub.py | 14 +++++---- tests/test_hub.py | 18 ++++++------ tests/test_peripherals.py | 62 +++++++++++++++++++-------------------- 5 files changed, 55 insertions(+), 51 deletions(-) diff --git a/.gitignore b/.gitignore index ef136e8..8980129 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ *.pyc build *.avi -test_real.py \ No newline at end of file +test_real.py +.vscode/settings.json diff --git a/pylgbst/comms/cpygatt.py b/pylgbst/comms/cpygatt.py index ab91dce..52e6209 100644 --- a/pylgbst/comms/cpygatt.py +++ b/pylgbst/comms/cpygatt.py @@ -33,10 +33,11 @@ class GattoolConnection(Connection): for dev in devices: address = dev['address'] name = dev['name'] - if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac == address: - logging.info("Found %s at %s", name, address) - self._conn_hnd = adapter.connect(address) - break + if address != "00:00:00:00:00:00": + if (not hub_mac and name == LEGO_MOVE_HUB) or hub_mac == address: + logging.info("Found %s at %s", name, address) + self._conn_hnd = adapter.connect(address) + break if self._conn_hnd: break diff --git a/pylgbst/hub.py b/pylgbst/hub.py index 04a65a8..c8ec0cb 100644 --- a/pylgbst/hub.py +++ b/pylgbst/hub.py @@ -61,19 +61,21 @@ class Hub(object): :rtype: pylgbst.messages.UpstreamMsg """ log.debug("Send message: %r", msg) - self.connection.write(self.HUB_HARDWARE_HANDLE, msg.bytes()) + msgbytes = msg.bytes() if msg.needs_reply: with self._sync_lock: assert not self._sync_request, "Pending request %r while trying to put %r" % (self._sync_request, msg) self._sync_request = msg log.debug("Waiting for sync reply to %r...", msg) + self.connection.write(self.HUB_HARDWARE_HANDLE, msgbytes) resp = self._sync_replies.get() log.debug("Fetched sync reply: %r", resp) if isinstance(resp, MsgGenericError): raise RuntimeError(resp.message()) return resp else: + self.connection.write(self.HUB_HARDWARE_HANDLE, msgbytes) return None def _notify(self, handle, data): @@ -183,12 +185,12 @@ class MoveHub(Hub): """ # PORTS - PORT_C = 0x01 - PORT_D = 0x02 + PORT_A = 0x00 + PORT_B = 0x01 + PORT_C = 0x02 + PORT_D = 0x03 + PORT_AB = 0x10 PORT_LED = 0x32 - PORT_A = 0x37 - PORT_B = 0x38 - PORT_AB = 0x39 PORT_TILT_SENSOR = 0x3A PORT_CURRENT = 0x3B PORT_VOLTAGE = 0x3C diff --git a/tests/test_hub.py b/tests/test_hub.py index c3ef5a8..faf5527 100644 --- a/tests/test_hub.py +++ b/tests/test_hub.py @@ -122,20 +122,20 @@ class HubTest(unittest.TestCase): class MoveHubTest(unittest.TestCase): def test_capabilities(self): conn = ConnectionMock() - conn.notifications.append('0f00 04 01 0125000000001000000010') - conn.notifications.append('0f00 04 02 0126000000001000000010') - conn.notifications.append('0f00 04 37 0127000100000001000000') - conn.notifications.append('0f00 04 38 0127000100000001000000') - conn.notifications.append('0900 04 39 0227003738') + conn.notifications.append('0f00 04 02 0125000000001000000010') + conn.notifications.append('0f00 04 03 0126000000001000000010') + conn.notifications.append('0f00 04 00 0127000100000001000000') + conn.notifications.append('0f00 04 01 0127000100000001000000') + conn.notifications.append('0900 04 10 0227003738') conn.notifications.append('0f00 04 32 0117000100000001000000') conn.notifications.append('0f00 04 3a 0128000000000100000001') conn.notifications.append('0f00 04 3b 0115000200000002000000') conn.notifications.append('0f00 04 3c 0114000200000002000000') - conn.notification_delayed('12000101064c45474f204d6f766520487562', 0.1) - conn.notification_delayed('0b00010d06001653a0d1d4', 0.3) - conn.notification_delayed('060001060600', 0.5) - conn.notification_delayed('0600030104ff', 0.7) + conn.notification_delayed('12000101064c45474f204d6f766520487562', 1.1) + conn.notification_delayed('0b00010d06001653a0d1d4', 1.3) + conn.notification_delayed('060001060600', 1.5) + conn.notification_delayed('0600030104ff', 1.7) MoveHub(conn.connect()) time.sleep(1) conn.wait_notifications_handled() diff --git a/tests/test_peripherals.py b/tests/test_peripherals.py index aef8e6b..ed1ea3d 100644 --- a/tests/test_peripherals.py +++ b/tests/test_peripherals.py @@ -148,49 +148,49 @@ class PeripheralsTest(unittest.TestCase): motor = EncodedMotor(hub, MoveHub.PORT_D) hub.peripherals[MoveHub.PORT_D] = motor - hub.connection.notification_delayed('050082020a', 0.1) + hub.connection.notification_delayed('050082030a', 0.1) motor.start_power(1.0) - self.assertEqual(b"0800810211510164", hub.writes.pop(1)[1]) + self.assertEqual(b"0800810311510164", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('050082020a', 0.1) + hub.connection.notification_delayed('050082030a', 0.1) motor.stop() - self.assertEqual(b"090081021107006403", hub.writes.pop(1)[1]) + self.assertEqual(b"090081031107006403", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('050082020a', 0.1) + hub.connection.notification_delayed('050082030a', 0.1) motor.set_acc_profile(1.0) - self.assertEqual(b"090081021105e80300", hub.writes.pop(1)[1]) + self.assertEqual(b"090081031105e80300", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('050082020a', 0.1) + hub.connection.notification_delayed('050082030a', 0.1) motor.set_dec_profile(1.0) - self.assertEqual(b"090081021106e80300", hub.writes.pop(1)[1]) + self.assertEqual(b"090081031106e80300", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('050082020a', 0.1) + hub.connection.notification_delayed('050082030a', 0.1) motor.start_speed(1.0) - self.assertEqual(b"090081021107646403", hub.writes.pop(1)[1]) + self.assertEqual(b"090081031107646403", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('050082020a', 0.1) + hub.connection.notification_delayed('050082030a', 0.1) motor.stop() - self.assertEqual(b"090081021107006403", hub.writes.pop(1)[1]) + self.assertEqual(b"090081031107006403", hub.writes.pop(1)[1]) logging.debug("\n\n") - hub.connection.notification_delayed('0500820201', 0.1) - hub.connection.notification_delayed('050082020a', 0.2) + hub.connection.notification_delayed('0500820301', 0.1) + hub.connection.notification_delayed('050082030a', 0.2) motor.timed(1.0) - self.assertEqual(b"0c0081021109e80364647f03", hub.writes.pop(1)[1]) + self.assertEqual(b"0c0081031109e80364647f03", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('0500820201', 0.1) - hub.connection.notification_delayed('050082020a', 0.2) + hub.connection.notification_delayed('0500820301', 0.1) + hub.connection.notification_delayed('050082030a', 0.2) motor.angled(180) - self.assertEqual(b"0e008102110bb400000064647f03", hub.writes.pop(1)[1]) + self.assertEqual(b"0e008103110bb400000064647f03", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('050082020a', 0.2) + hub.connection.notification_delayed('050082030a', 0.2) motor.preset_encoder(-180) - self.assertEqual(b"0b0081021151024cffffff", hub.writes.pop(1)[1]) + self.assertEqual(b"0b0081031151024cffffff", hub.writes.pop(1)[1]) - hub.connection.notification_delayed('0500820201', 0.1) - hub.connection.notification_delayed('050082020a', 0.2) + hub.connection.notification_delayed('0500820301', 0.1) + hub.connection.notification_delayed('050082030a', 0.2) motor.goto_position(0) - self.assertEqual(b"0e008102110d00000000647f6403", hub.writes.pop(1)[1]) + self.assertEqual(b"0e008103110d00000000647f6403", hub.writes.pop(1)[1]) hub.connection.wait_notifications_handled() @@ -204,15 +204,15 @@ class PeripheralsTest(unittest.TestCase): def callback(*args): vals.append(args) - hub.connection.notification_delayed('0a004701020100000001', 0.1) + hub.connection.notification_delayed('0a004702020100000001', 0.1) motor.subscribe(callback) - hub.connection.notification_delayed("0800450100000000", 0.1) - hub.connection.notification_delayed("08004501ffffffff", 0.2) - hub.connection.notification_delayed("08004501feffffff", 0.3) + hub.connection.notification_delayed("0800450200000000", 0.1) + hub.connection.notification_delayed("08004502ffffffff", 0.2) + hub.connection.notification_delayed("08004502feffffff", 0.3) time.sleep(0.4) - hub.connection.notification_delayed('0a004701020000000000', 0.1) + hub.connection.notification_delayed('0a004702020000000000', 0.1) motor.unsubscribe(callback) hub.connection.wait_notifications_handled() @@ -228,13 +228,13 @@ class PeripheralsTest(unittest.TestCase): def callback(*args): vals.append(args) - hub.connection.notification_delayed('0a00 4701080100000001', 0.1) + hub.connection.notification_delayed('0a00 4702080100000001', 0.1) cds.subscribe(callback) - hub.connection.notification_delayed("08004501ff0aff00", 0.1) + hub.connection.notification_delayed("08004502ff0aff00", 0.1) time.sleep(0.2) - hub.connection.notification_delayed('0a00 4701090100000001', 0.1) + hub.connection.notification_delayed('0a00 4702090100000001', 0.1) cds.unsubscribe(callback) hub.connection.wait_notifications_handled()