Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable controls when connected via bluetooth #169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions ds4drv/device.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from binascii import crc32
from struct import Struct
from sys import version_info as sys_version

Expand Down Expand Up @@ -118,17 +119,21 @@ def control(self, big_rumble=0, small_rumble=0,
led_red=0, led_green=0, led_blue=0,
flash_led1=0, flash_led2=0):
if self.type == "bluetooth":
pkt = bytearray(77)
pkt[0] = 128
pkt[2] = 255
offset = 2
pkt = bytearray(79)
pkt[:5] = [0xa2, 0x11, 0x80, 0x00, 0xff]
offset = 4
report_id = 0x11
send_from = 2

elif self.type == "usb":
pkt = bytearray(31)
pkt[0] = 255
offset = 0
report_id = 0x05
send_from = 0

else:
raise ValueError("Type is neither bluetooth nor usb")

# Rumble
pkt[offset+3] = min(small_rumble, 255)
Expand All @@ -145,7 +150,14 @@ def control(self, big_rumble=0, small_rumble=0,
# Time to flash dark (255 = 2.5 seconds)
pkt[offset+9] = min(flash_led2, 255)

self.write_report(report_id, pkt)
# Last 4 bytes are CRC32
crc = crc32(pkt[:-4])
pkt[-4] = (crc & 0x000000ff)
pkt[-3] = (crc & 0x0000ff00) >> 8
pkt[-2] = (crc & 0x00ff0000) >> 16
pkt[-1] = (crc & 0xff000000) >> 24

self.write_report(report_id, pkt[send_from:])

def parse_report(self, buf):
"""Parse a buffer containing a HID report."""
Expand Down