Skip to content

Commit

Permalink
resolve hidapi unhandled exception issue
Browse files Browse the repository at this point in the history
-added try except around thread code to handle the IOError raised by hidapi.
-added 'connected' property to indicate when controller is disconnected.
  • Loading branch information
dalethomas81 committed Apr 1, 2024
1 parent 5238a1f commit a5ed419
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions pydualsense/pydualsense.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def init(self) -> None:
self.battery = DSBattery()
self.conType = self.determineConnectionType() # determine USB or BT connection
self.ds_thread = True
self.connected = True
self.report_thread = threading.Thread(target=self.sendReport)
self.report_thread.start()
self.states = None
Expand Down Expand Up @@ -227,18 +228,22 @@ def setRightMotor(self, intensity: int) -> None:
def sendReport(self) -> None:
"""background thread handling the reading of the device and updating its states"""
while self.ds_thread:
# read data from the input report of the controller
inReport = self.device.read(self.input_report_length)
if self.verbose:
logger.debug(inReport)
# decrypt the packet and bind the inputs
self.readInput(inReport)

# prepare new report for device
outReport = self.prepareReport()

# write the report to the device
self.writeReport(outReport)
try:
# read data from the input report of the controller
inReport = self.device.read(self.input_report_length)
if self.verbose:
logger.debug(inReport)
# decrypt the packet and bind the inputs
self.readInput(inReport)

# prepare new report for device
outReport = self.prepareReport()

# write the report to the device
self.writeReport(outReport)
except IOError:
self.connected = False
break

def readInput(self, inReport) -> None:
"""
Expand Down

0 comments on commit a5ed419

Please sign in to comment.