Skip to content
This repository was archived by the owner on Jul 20, 2025. It is now read-only.

Commit 6536793

Browse files
committed
usb: reduced read overhead for performance
1 parent 5586b69 commit 6536793

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

mbientlab/metawear/metawear.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class MetaWearUSB(object):
5858
GATT_MW_CHAR_COMMAND = '326a9001-85cb-9195-d9dd-464cfbbae75a'
5959
GATT_MW_CHAR_NOTIFICATION = '326a9006-85cb-9195-d9dd-464cfbbae75a'
6060

61+
SERIAL_XFER_SIZE = 1024
62+
SERIAL_BYTE_START = b'\x1f'
63+
SERIAL_BYTE_STOP = b'\n'
64+
6165
@staticmethod
6266
def scan():
6367
"""List MetaWear devices attached to USB"""
@@ -202,10 +206,10 @@ def _bin_cmd_decode(self, c):
202206
elif self._cmd_recv_len < self._cmd_len:
203207
self._cmd_recv_len += 1
204208
self._cmd_buffer += c
205-
elif c == b'\n':
209+
elif c == MetaWearUSB.SERIAL_BYTE_STOP:
206210
self._cmd_started = False
207211
return self._cmd_buffer
208-
elif c == b'\x1f':
212+
elif c == MetaWearUSB.SERIAL_BYTE_START:
209213
self._cmd_started = True
210214
self._cmd_len = 0
211215
self._cmd_recv_len = 0
@@ -217,18 +221,20 @@ def _read_poller(self):
217221
self._cmd_started = False
218222
while self._read_poll:
219223
try:
220-
c = self.ser.read()
224+
read_len = max(1, min(MetaWearUSB.SERIAL_XFER_SIZE, self.ser.in_waiting))
225+
line_bytes = self.ser.read(read_len)
221226
except serial.SerialException:
222227
self._read_poll = False
223228
self.disconnect()
224229
return
225230

226-
if len(c) < 1:
231+
if len(line_bytes) < 1:
227232
continue
228-
cmd = self._bin_cmd_decode(c)
229-
if len(cmd) > 0:
230-
if self._notify_handler is not None:
231-
self._notify_handler(cmd)
233+
for i in range(len(line_bytes)):
234+
cmd = self._bin_cmd_decode(line_bytes[i:i+1])
235+
if len(cmd) > 0:
236+
if self._notify_handler is not None:
237+
self._notify_handler(cmd)
232238

233239
def _write_poller(self):
234240
"""Write poller enabling async writes and write response callbacks."""

0 commit comments

Comments
 (0)