def _set_hidKeyboardInput(self, state): rawState = not state if self.isHid: # Make sure the device settings are up to date. keySettings: int = self._dev.getFeature( ALVA_KEY_SETTINGS_REPORT)[ALVA_KEY_SETTINGS_POS] # Try to update the state if rawState: newKeySettings = intToByte(keySettings | ALVA_KEY_RAW_INPUT_MASK) elif keySettings & ALVA_KEY_RAW_INPUT_MASK: newKeySettings = intToByte(keySettings ^ ALVA_KEY_RAW_INPUT_MASK) else: newKeySettings = intToByte(keySettings) self._dev.setFeature(ALVA_KEY_SETTINGS_REPORT + newKeySettings) # Check whether the state has been changed successfully. # If not, this device does not support this feature. keySettings: int = self._dev.getFeature( ALVA_KEY_SETTINGS_REPORT)[ALVA_KEY_SETTINGS_POS] # Save the new state self._rawKeyboardInput = bool(keySettings & ALVA_KEY_RAW_INPUT_MASK) else: self._ser6SendMessage(cmd=b"r", value=boolToByte(rawState)) self._ser6SendMessage(b"r", b"?") for i in range(3): self._dev.waitForRead(self.timeout) if rawState is self._rawKeyboardInput: break
def _hidDisplay(self, cellBytes: bytes) -> None: for offset in range(0, len(cellBytes), ALVA_BRAILLE_OUTPUT_MAX_SIZE): cellsToWrite = cellBytes[offset:offset + ALVA_BRAILLE_OUTPUT_MAX_SIZE] data = b"".join([ ALVA_BRAILLE_OUTPUT_REPORT, intToByte(offset), intToByte(len(cellsToWrite)), cellsToWrite ]) self._dev.write(data)
def _serSendMessage(self, msgId: bytes, payload: Union[bytes, int, bool] = b""): if not isinstance(payload, bytes): if isinstance(payload, int): payload: bytes = intToByte(payload) elif isinstance(payload, bool): payload: bytes = boolToByte(payload) else: raise TypeError( "Expected arg 'payload' to be of type 'bytes, int, or bool'" ) data = b''.join([HEADER, msgId, intToByte(len(payload)), payload]) self._dev.write(data)
def _sendPacket(self, packetType: bytes, arg1: bytes = FS_BYTE_NULL, arg2: bytes = FS_BYTE_NULL, arg3: bytes = FS_BYTE_NULL, data: bytes = FS_DATA_EMPTY): """Send a packet to the display @param packetType: Type of packet (first byte), use one of the FS_PKT constants @param arg1: First argument (second byte of packet) @param arg2: Second argument (third byte of packet) @param arg3: Third argument (fourth byte of packet) @param data: Data to send if this is an extended packet, required checksum will be added automatically """ def handleArg(arg: bytes) -> bytes: if isinstance(arg, bytes): return arg else: raise TypeError("Expected arg to be bytes") arg1 = handleArg(arg1) arg2 = handleArg(arg2) arg3 = handleArg(arg3) packet = b"".join([packetType, arg1, arg2, arg3, data]) if data: checksum = BrailleDisplayDriver._calculateChecksum(packet) packet += intToByte(checksum) self._dev.write(packet)
def display(self, cells: List[int]): # ESCAPE must be quoted because it is a control character cellBytesList = [ intToByte(cell).replace(ESCAPE, ESCAPE * 2) for cell in cells ] cellBytesList.insert(0, DISPLAY_TAG) self._serial.write(b"".join(cellBytesList))
def display(self, cells: List[int]): # cells will already be padded up to numCells. cellBytes = b"".join(intToByte(cell) for cell in cells) if self.isHid: outputReport: bytes = b"".join([ HR_BRAILLE, # id b"\x01\x00", # Module 1, offset 0 intToByte(self.numCells), # length cellBytes ]) #: Humanware HID devices require the use of HidD_SetOutputReport when # sending data to the device via HID, as WriteFile seems to block forever # or fail to reach the device at all. self._dev.setOutputReport(outputReport) else: self._serSendMessage(MSG_DISPLAY, cellBytes)
def _sendRequest(self, command: bytes, arg: Union[bytes, bool, int] = b""): """ :type command: bytes :type arg: bytes | bool | int """ typeErrorString = "Expected param '{}' to be of type '{}', got '{}'" if not isinstance(arg, bytes): if isinstance(arg, bool): arg = boolToByte(arg) elif isinstance(arg, int): arg = intToByte(arg) else: raise TypeError( typeErrorString.format("arg", "bytes, bool, or int", type(arg).__name__)) if not isinstance(command, bytes): raise TypeError( typeErrorString.format("command", "bytes", type(command).__name__)) if self.isHid: self._dev.write(command + arg) else: arg = arg.replace(ESCAPE, ESCAPE * 2) data = b"".join([ESCAPE, command, arg]) self._dev.write(data)
def _set_dotFirmness(self, value): if self._dotFirmness is value: return if isinstance(self._model,TimeSyncFirmnessMixin): self.sendExtendedPacket(HT_EXTPKT_SET_FIRMNESS, intToByte(value)) else: log.debugWarning("Changing dot firmness setting for unsupported device %s"%self._model.name) # Regardless whether this setting is supported or not, we want to safe its state. self._dotFirmness = value
def display(self, cells: List[int]): if self.translationTable: cells = _translate(cells, FOCUS_1_TRANSLATION_TABLE) if not self._awaitingAck: self._sendPacket(FS_PKT_WRITE, intToByte(self.numCells), FS_BYTE_NULL, FS_BYTE_NULL, bytes(cells)) self._pendingCells = [] else: self._pendingCells = cells
def _ser6Display(self, cellBytes: bytes) -> None: if not isinstance(cellBytes, bytes): raise TypeError("Expected param 'cells' to be of type 'bytes'") value = b"".join([ b"\x00", intToByte(len(cellBytes)), cellBytes ]) self._ser6SendMessage(b"B", value)
def sendExtendedPacket(self, packetType: bytes, data: bytes = b""): if self._sleepcounter > 0: log.debug("Packet discarded as driver was requested to sleep") return packetBytes: bytes = b"".join([ intToByte(len(data) + len(packetType)), packetType, data, b"\x16" ]) if self._model: packetBytes = self._model.deviceId + packetBytes self.sendPacket(HT_PKT_EXTENDED, packetBytes)
def _sendHidPacket(self, packet: bytes): assert self.isHid maxBlockSize = self._dev._writeSize-3 # When the packet length exceeds C{writeSize}, the packet is split up into several packets. # They contain C{HT_HID_RPT_InData}, the length of the data block, # the data block itself and a terminating null character. for offset in range(0, len(packet), maxBlockSize): block = packet[offset:offset+maxBlockSize] hidPacket = HT_HID_RPT_InData + intToByte(len(block)) + block + b"\x00" self._dev.write(hidPacket)
def display(self, cells: List[int]): # cells will already be padded up to numCells. cellBytes = b"".join(intToByte(cell) for cell in cells) report = hwIo.hid.HidOutputReport( self._dev, reportID=self._cellValueCaps.ReportID) report.setUsageValueArray(HID_USAGE_PAGE_BRAILLE, self._cellValueCaps.LinkCollection, self._cellValueCaps.u1.NotRange.Usage, cellBytes) self._dev.write(report.data)
def display(self, cells: List[int]): # every transmitted line consists of the preamble 'sendHeader' and the Cells if 80 == self.numCells: lineBytes: bytes = self.sendHeader + bytes(cells) elif 40 == self.numCells: cellData = (b"\x00" + intToByte(cell) for cell in cells) lineBytes = self.sendHeader + b"".join(cellData) else: log.error("Unsupported cell count for seika braille device") return self._ser.write(lineBytes)
def display(self, cells: List[int]): writeBytes: List[bytes] = [DISPLAY_TAG, ] for cell in cells: writeBytes.append(b"\x00") writeBytes.append(intToByte(cell)) self._dev.write(b"".join(writeBytes))