controller.py
Go to the documentation of this file.
1 import ds4drv
2 from ds4drv.eventloop import EventLoop
3 
4 from threading import Thread
5 from distutils.version import StrictVersion
6 
7 
8 # Based on DS4Controller class in __main__.py of ds4drv
9 class Controller(Thread):
10  # Reference: https://www.psdevwiki.com/ps4/DualShock_4#Specifications
11  MAX_VOLTAGE = 3.65
12  # Reference: https://www.psdevwiki.com/ps4/DS4-USB#cite_note-2
13  TOUCHPAD_MAX_X = 1919
14  TOUCHPAD_MAX_Y = 942
15  BATTERY_FULL_CHARGING = 11
16  BATTERY_MAX = 8
17 
18  def __init__(self):
19  super(Controller, self).__init__(target=self.run)
20  self.device = None
21  self.loop = EventLoop()
22 
23  self._led = (0, 0, 1)
24  self._led_flash = (0, 0)
25 
26  def fire_event(self, event, *args):
27  self.loop.fire_event(event, *args)
28 
29  def setup_device(self, device):
30  self.device = device
31  self.fire_event('device-setup', device)
32  self.loop.add_watcher(device.report_fd, self.read_report)
33 
34  def cleanup_device(self):
35  if self.device is None:
36  return
37 
38  self.fire_event('device-cleanup')
39  self.loop.remove_watcher(self.device.report_fd)
40  self.device.close()
41  self.device = None
42 
43  def read_report(self):
44  report = self.device.read_report()
45 
46  if not report:
47  if report is False:
48  return
49 
50  self.cleanup_device()
51  return
52 
53  self.fire_event('device-report', report)
54 
55  def run(self):
56  self.loop.run()
57 
58  def exit(self):
59  if self.device is not None:
60  self.cleanup_device()
61  self.loop.stop()
62  if self.is_alive():
63  self.join()
64 
65  def control(self, led_red=None, led_green=None, led_blue=None,
66  rumble_small=None, rumble_big=None,
67  flash_on=None, flash_off=None):
68  """
69  Similar to DS4Device.control but with None as default values
70  :param led_red:
71  :param led_green:
72  :param led_blue:
73  :param rumble_small:
74  :param rumble_big:
75  :param flash_on:
76  :param flash_off:
77  :return:
78  """
79  self._led = (
80  self._led[0] if led_red is None else led_red,
81  self._led[1] if led_green is None else led_green,
82  self._led[2] if led_blue is None else led_blue,
83  )
84  self._led_flash = (
85  self._led_flash[0] if flash_on is None else flash_on,
86  self._led_flash[1] if flash_off is None else flash_off,
87  )
88  # Once to change LED flashing state
89  self._control()
90  # Then to actually execute the control
91  self._control(small_rumble=rumble_small if rumble_small is not None else 0,
92  big_rumble=rumble_big if rumble_big is not None else 0)
93 
94  def _control(self, **kwargs):
95  self.device.control(led_red=self._led[0],
96  led_green=self._led[1],
97  led_blue=self._led[2],
98  flash_led1=self._led_flash[0],
99  flash_led2=self._led_flash[1],
100  **kwargs)
101 
102  @staticmethod
103  def get_imu_data(report):
104  """
105  Sets the correct IMU data in the report
106  See: https://github.com/chrippa/ds4drv/pull/168
107  :param report:
108  :return:
109  """
110  ver_with_bug = StrictVersion('0.5.1')
111  current = StrictVersion(ds4drv.__version__)
112 
113  # Bug is fixed
114  if current > ver_with_bug or not hasattr(report, 'orientation_roll'):
115  return {
116  'lin_acc': {
117  'x': report.lin_acc_x,
118  'y': report.lin_acc_y,
119  'z': report.lin_acc_z,
120  },
121  'ang_vel': {
122  'x': report.ang_vel_x,
123  'y': report.ang_vel_y,
124  'z': report.ang_vel_z,
125  }
126  }
127  else:
128  return {
129  'lin_acc': {
130  # Note: Bit is flipped in ds4drv (not sure why)
131  'x': ~report.orientation_roll,
132  'y': report.orientation_yaw,
133  'z': report.orientation_pitch,
134  },
135  'ang_vel': {
136  'x': report.motion_y,
137  'y': report.motion_x,
138  'z': report.motion_z,
139  }
140  }
def fire_event(self, event, args)
Definition: controller.py:26
def _control(self, kwargs)
Definition: controller.py:94
def control(self, led_red=None, led_green=None, led_blue=None, rumble_small=None, rumble_big=None, flash_on=None, flash_off=None)
Definition: controller.py:67
def setup_device(self, device)
Definition: controller.py:29


ds4_driver
Author(s): Naoki Mizuno
autogenerated on Fri May 1 2020 03:25:46