Skip to content

Commit 0b63200

Browse files
authored
Merge d2cbe88 into 38efb1c
2 parents 38efb1c + d2cbe88 commit 0b63200

2 files changed

Lines changed: 36 additions & 14 deletions

File tree

source/bdDetect.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -462,14 +462,14 @@ def getConnectedUsbDevicesForDriver(driver: str) -> Iterator[DeviceMatch]:
462462
DeviceMatch(DeviceType.CUSTOM, port["usbID"], port["devicePath"], port)
463463
for port in deviceInfoFetcher.usbDevices
464464
),
465-
(
466-
DeviceMatch(DeviceType.HID, port["usbID"], port["devicePath"], port)
467-
for port in deviceInfoFetcher.hidDevices if port["provider"] == "usb"
468-
),
469465
(
470466
DeviceMatch(DeviceType.SERIAL, port["usbID"], port["port"], port)
471467
for port in deviceInfoFetcher.usbComPorts
472-
)
468+
),
469+
(
470+
DeviceMatch(DeviceType.HID, port["usbID"], port["devicePath"], port)
471+
for port in deviceInfoFetcher.hidDevices if port["provider"] == "usb"
472+
)
473473
)
474474
for match in usbDevs:
475475
if driver == _getStandardHidDriverName():

source/brailleDisplayDrivers/hims.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,26 +272,23 @@ def registerAutomaticDetection(cls, driverRegistrar: bdDetect.DriverRegistrar):
272272

273273
@classmethod
274274
def getManualPorts(cls):
275-
return braille.getSerialPorts(filterFunc=lambda info: "bluetoothName" in info)
275+
return braille.getSerialPorts()
276276

277277
def __init__(self, port="auto"):
278278
super(BrailleDisplayDriver, self).__init__()
279279
self.numCells = 0
280280
self._model = None
281+
self._serData = b''
281282

282283
for match in self._getTryPorts(port):
283284
portType, portId, port, portInfo = match
284285
self.isBulk = portType == bdDetect.DeviceType.CUSTOM
285286
self.isHID = portType == bdDetect.DeviceType.HID
287+
self.isSerial = portType == bdDetect.DeviceType.SERIAL
286288
# Try talking to the display.
287289
try:
288290
match portType:
289-
case bdDetect.DeviceType.HID:
290-
self._dev = hwIo.Hid(port, onReceive=self._hidOnReceive)
291-
case bdDetect.DeviceType.CUSTOM:
292-
# onReceiveSize based on max packet size according to USB endpoint information.
293-
self._dev = hwIo.Bulk(port, 0, 1, self._onReceive, onReceiveSize=64)
294-
case _:
291+
case bdDetect.DeviceType.SERIAL:
295292
self._dev = hwIo.Serial(
296293
port,
297294
baudrate=BAUD_RATE,
@@ -300,6 +297,11 @@ def __init__(self, port="auto"):
300297
writeTimeout=self.timeout,
301298
onReceive=self._onReceive
302299
)
300+
case bdDetect.DeviceType.HID:
301+
self._dev = hwIo.Hid(port, onReceive=self._hidOnReceive)
302+
case bdDetect.DeviceType.CUSTOM:
303+
# onReceiveSize based on max packet size according to USB endpoint information.
304+
self._dev = hwIo.Bulk(port, 0, 1, self._onReceive, onReceiveSize=64)
303305
except EnvironmentError:
304306
log.debugWarning("", exc_info=True)
305307
continue
@@ -497,15 +499,35 @@ def _onReceive(self, data: bytes):
497499
firstByte = data
498500
# data only contained the first byte. Read the rest from the device.
499501
stream = self._dev
502+
503+
# sometimes serial data received splited data. so accmulate data until it reaches 10 bytes.
504+
if self._serData:
505+
self._serData += data
506+
if len(self._serData) == 10:
507+
firstByte = b'\xfa'
508+
else:
509+
return
510+
500511
if firstByte == b"\x1c":
501512
# A device is identifying itself
502513
deviceId: bytes = stream.read(2)
503514
# When a device identifies itself, the packets ends with 0x1f
504515
assert stream.read(1) == b"\x1f"
505516
self._handleIdentification(deviceId)
506517
elif firstByte == b"\xfa":
507-
# Command packets are ten bytes long
508-
packet = firstByte + stream.read(9)
518+
# serial data first received
519+
if not self._serData:
520+
try:
521+
# Command packets are ten bytes long
522+
packet = firstByte + stream.read(9)
523+
except:
524+
# remained data will be received next onReceive
525+
self._serData = firstByte
526+
return
527+
else:
528+
packet = self._serData
529+
self._serData = b''
530+
509531
assert packet[2] == 0x01 # Fixed value
510532
CHECKSUM_INDEX = 8
511533
checksum: int = packet[CHECKSUM_INDEX]

0 commit comments

Comments
 (0)