Browse Source

Added reading of battery level

feature/balanceutils
klonfish 7 years ago committed by klonfish
parent
commit
081fb40436
  1. 19
      wii-pair/wiiboard.py

19
wii-pair/wiiboard.py

@ -26,6 +26,8 @@ EXTENSION_8BYTES = 0x32 @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -213,7 +224,7 @@ class Wiiboard:
index += 2
# Send <data> to the Wiiboard
# <data> should be an array of strings, each string representing a single hex byte
# <data> should be of type bytes
def send(self, data):
if self.status != "Connected":
return
@ -238,5 +249,9 @@ class Wiiboard: @@ -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)

Loading…
Cancel
Save