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

Add controller "splash" on connection #92

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions ds4drv/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from . import input
from . import led
from . import status
from . import splash
86 changes: 86 additions & 0 deletions ds4drv/actions/splash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from ..action import Action

from collections import namedtuple
from time import sleep


Action.add_option("--no-splash", action="store_true",
help="Disable rumbling controller and flashing LEDs on connection.")


class ActionSplash(Action):
"""Rumble controller and flash LEDs on connection."""

def __init__(self, *args, **kwargs):
super(ActionSplash, self).__init__(*args, **kwargs)

self.led = (0, 0, 1)
self.no_splash = False

@staticmethod
def interpolate_leds(l1, l2, n):
diff = (
(l2[0] - l1[0])/n,
(l2[1] - l1[1])/n,
(l2[2] - l1[2])/n
)

for i in range(n):
yield (
l1[0] + diff[0]*i,
l1[1] + diff[1]*i,
l1[2] + diff[2]*i
)

def setup(self, device):
if self.no_splash == True:
return

splash_time = 0.5

# Define key frames
high_rumble = (255, 63) # (small_rumble, big_rumble)
low_rumble = (0, 0)

Frame = namedtuple('Frame', ['led', 'rumble'])

splash_key_frames = [
Frame(led = self.led, rumble = high_rumble),
Frame(led = (255, 255, 0), rumble = low_rumble),
Frame(led = (0, 255, 255), rumble = high_rumble),
Frame(led = (0, 0, 0), rumble = low_rumble),
Frame(led = self.led, rumble = low_rumble)
]
splash_frame_counts = [4, 6, 8, 4]


# Build all frames
splash_frames = []
for i, nframes in enumerate(splash_frame_counts):
frame_leds = self.interpolate_leds(
splash_key_frames[i].led, splash_key_frames[i+1].led,
nframes
)
frame_rumbles = [splash_key_frames[i].rumble] * nframes

for led, rumble in zip(frame_leds, frame_rumbles):
splash_frames.append(Frame(led = led, rumble = rumble))


# Play frames
splash_frame_duration = splash_time/len(splash_frames)

for splash_frame in splash_frames:
frame_led = tuple(map(int, splash_frame.led))
frame_rumble = tuple(map(int, splash_frame.rumble))

self.controller.device.set_led(*frame_led)
self.controller.device.rumble(*frame_rumble)

sleep(splash_frame_duration)

self.controller.device.set_led(*(self.led))

def load_options(self, options):
self.led = options.led
self.no_splash = options.no_splash
12 changes: 10 additions & 2 deletions ds4drv/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,24 @@ def __init__(self, device_name, device_addr, type):
self._led_flash = (0, 0)
self._led_flashing = False

self._small_rumble = 0
self._big_rumble = 0

self.set_operational()

def _control(self, **kwargs):
self.control(led_red=self._led[0], led_green=self._led[1],
led_blue=self._led[2], flash_led1=self._led_flash[0],
flash_led2=self._led_flash[1], **kwargs)
flash_led2=self._led_flash[1],
big_rumble = self._big_rumble,
small_rumble = self._small_rumble,
**kwargs)

def rumble(self, small=0, big=0):
"""Sets the intensity of the rumble motors. Valid range is 0-255."""
self._control(small_rumble=small, big_rumble=big)
self._big_rumble = big
self._small_rumble = small
self._control()

def set_led(self, red=0, green=0, blue=0):
"""Sets the LED color. Values are RGB between 0-255."""
Expand Down