async def write(self, input_report: InputReport): """ Sets timer byte and current button state in the input report and sends it. Fires sig_is_send event in the controller state afterwards. Raises NotConnected exception if the transport is not connected or the connection was lost. """ if self.transport is None: raise NotConnectedError('Transport not registered.') # set button and stick and 6-axis data of input report input_report.set_button_status(self._controller_state.button_state) if self._controller_state.l_stick_state is None: l_stick = [0x00, 0x00, 0x00] else: l_stick = self._controller_state.l_stick_state if self._controller_state.r_stick_state is None: r_stick = [0x00, 0x00, 0x00] else: r_stick = self._controller_state.r_stick_state input_report.set_stick_status(l_stick, r_stick) # set timer byte of input report input_report.set_timer(self._input_report_timer) self._input_report_timer = (self._input_report_timer + 1) % 0x100 await self.transport.write(input_report) self._controller_state.sig_is_send.set()
async def send_controller_state(self): """ Waits for the controller state to be send. Raises NotConnected exception if the transport is not connected or the connection was lost. """ # TODO: Call write directly if in continuously sending input report mode if self.transport is None: raise NotConnectedError('Transport not registered.') self._controller_state.sig_is_send.clear() # wrap into a future to be able to set an exception in case of a disconnect self._controller_state_sender = asyncio.ensure_future(self._controller_state.sig_is_send.wait()) await self._controller_state_sender self._controller_state_sender = None
async def _write(self, input_report): """ Fires sig_is_send event in the controller state afterwards. Raises NotConnected exception if the transport is not connected or the connection was lost. """ if self.transport is None: raise NotConnectedError('Transport not registered.') if self._is_pairing and (int.from_bytes(input_report.data[4:7], "big") & close_pairing_masks[self.controller]): # this is a bit too early, but so far no logger.info('left change Grip/Order menu') self._is_pairing = False self._set_mode(self._input_report_mode) if not self._not_paused.is_set(): logger.warning("Write while paused") await self.transport.write(input_report) self._controller_state.sig_is_send.set()