diff --git a/wii-pair/wiiboard.py b/wii-pair/wiiboard.py index a7a3b34..c9827e7 100644 --- a/wii-pair/wiiboard.py +++ b/wii-pair/wiiboard.py @@ -26,6 +26,8 @@ EXTENSION_8BYTES = 0x32 BUTTON_DOWN_MASK = 8 +BATTERY_MAX = 0xD0 # Taken from cwiid source + TOP_RIGHT = 0 BOTTOM_RIGHT = 1 TOP_LEFT = 2 @@ -55,6 +57,7 @@ class Wiiboard: self.LED = False self.address = None self.buttonDown = False + self.reportingTypeSet = False self.battery = None for i in range(3): self.calibration.append([]) @@ -91,6 +94,7 @@ class Wiiboard: self.status = "Connected" self.address = address _thread.start_new_thread(self.receivethread, ()) + _thread.start_new_thread(self.statusthread, ()) self.calibrate() useExt = bytes([COMMAND_REGISTER, 0x04, 0xA4, 0x00, 0x40, 0x00]) self.send(useExt) @@ -181,7 +185,9 @@ class Wiiboard: data = self.datasocket.recv(25) intype = data[1] if intype == INPUT_STATUS: - self.setReportingType() + self.battery = data[7]/BATTERY_MAX + if not self.reportingTypeSet: + self.setReportingType() elif intype == INPUT_READ_DATA: if self.calibrationRequested == True: packetLength = data[4]//16 + 1 @@ -200,6 +206,11 @@ class Wiiboard: self.status = "Disconnected" self.disconnect() + def statusthread(self): + while self.status == "Connected": + time.sleep(30) + self.requestStatus() + def parseCalibrationResponse(self, bytes): index = 0 if len(bytes) == 16: @@ -213,7 +224,7 @@ class Wiiboard: index += 2 # Send to the Wiiboard - # should be an array of strings, each string representing a single hex byte + # should be of type bytes def send(self, data): if self.status != "Connected": return @@ -238,5 +249,9 @@ class Wiiboard: def setReportingType(self): msg = bytes([COMMAND_REPORTING, CONTINUOUS_REPORTING, EXTENSION_8BYTES]) self.send(msg) + self.reportingTypeSet = True + def requestStatus(self): + msg = bytes([INPUT_REQUEST_STATUS, 0x00]) + self.send(msg)