-
Notifications
You must be signed in to change notification settings - Fork 0
/
evdev-rumble.py
executable file
·78 lines (65 loc) · 2.07 KB
/
evdev-rumble.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
"""
Very simple rumble P4 controller in python.
"""
import fcntl, struct, array, time
import evdev
from evdev import ecodes, InputDevice, ff
for name in evdev.list_devices():
dev = InputDevice(name)
print("===========")
print(name)
print(dev.capabilities())
print(ecodes.EV_FF)
if ecodes.EV_FF in dev.capabilities():
print (dev.path + " is EV_FF capable")
break
print("===========")
EVIOCRMFF = 0x40044581
EVIOCSFF = 0x40304580
LOG_CLASS_ON = False
TIME_DELTA = 250
TIME_DELTA = 1000
class Vibrate:
def __init__(self, file):
self.ff_joy = open(file, "r+")
def close(self):
self.ff_joy.close()
def new_effect(self, strong, weak, length):
effect = struct.pack('HhHHHHHxHH', 0x50, -1, 0, 0, 0, length, 0, int(strong * 0xFFFF), int(weak * 0xFFFF))
a = array.array('h', effect)
fcntl.ioctl(self.ff_joy, EVIOCSFF, a, True)
return a[1]
id = a[1]
return (ev_play, ev_stop)
def play_efect(self, id):
if type(id) == tuple or type(id) == list:
ev_play = ''
for i in id:
ev_play = ev_play + struct.pack('LLHHi', 0, 0, 0x15, i, 1)
else:
ev_play = struct.pack('LLHHi', 0, 0, 0x15, id, 1)
self.ff_joy.write(ev_play)
self.ff_joy.flush()
def stop_effect(self, id):
if type(id) == tuple or type(id) == list:
ev_stop = ''
for i in id:
ev_stop = ev_stop + struct.pack('LLHHi', 0, 0, 0x15, i, 0)
else:
ev_stop = struct.pack('LLHHi', 0, 0, 0x15, id, 0)
self.ff_joy.write(ev_stop)
self.ff_joy.flush()
def forget_effect(self, id):
if type(id) == tuple or type(id) == list:
for i in id:
fcntl.ioctl(self.ff_joy, EVIOCRMFF, i)
else:
fcntl.ioctl(self.ff_joy, EVIOCRMFF, id)
# f = Vibrate("/dev/input/event20")
f = Vibrate(dev.path)
p = f.new_effect(1.0, 1.0, TIME_DELTA )
f.play_efect((p))
time.sleep(TIME_DELTA / 1000.0)
f.stop_effect((p))
f.forget_effect((p))