diff options
author | NeilBrown <neilb@suse.de> | 2012-07-23 10:42:59 +1000 |
---|---|---|
committer | NeilBrown <neilb@suse.de> | 2013-05-17 17:48:20 +1000 |
commit | 586eeb58c97b5faff6e0d24d2bd9439d703028ba (patch) | |
tree | 6627de428ce5fe82a84eee82eaee10b75c2b8488 /GTA04 | |
parent | 4f6dfd765ec7b741eac46b38d307a1c4d6c2b9ed (diff) | |
download | kernel_goldelico_gta04-586eeb58c97b5faff6e0d24d2bd9439d703028ba.zip kernel_goldelico_gta04-586eeb58c97b5faff6e0d24d2bd9439d703028ba.tar.gz kernel_goldelico_gta04-586eeb58c97b5faff6e0d24d2bd9439d703028ba.tar.bz2 |
Improve vibra.py demo script
Add a 'multi_vibe' which rumbles several times in quick succession.
Can the interface a bit.
Signed-off-by: NeilBrown <neilb@suse.de>
Diffstat (limited to 'GTA04')
-rw-r--r-- | GTA04/scripts/vibra.py | 83 |
1 files changed, 62 insertions, 21 deletions
diff --git a/GTA04/scripts/vibra.py b/GTA04/scripts/vibra.py index c33f0a0..ecc00c9 100644 --- a/GTA04/scripts/vibra.py +++ b/GTA04/scripts/vibra.py @@ -22,10 +22,22 @@ import fcntl, struct, time, array # An effect is created with f.new_vibe(strength, duration, delay) # That effect can then be started with 'play' and stopped with 'stop'. +# EVIOCRMFF = _IOW('E', 0x81, int) +# dir: 2 WRITE = 1 == 0x40000 +# size 14 4 +# type 8 'E' == 0x45 +# nr: 8 0x81 +# +EVIOCRMFF = 0x40044581 +# EVIOCSFF _IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect)) +EVIOCSFF = 0x402c4580 class Vibra: def __init__(self, file = "/dev/input/rumble"): self.f = open(file, "r+") + def close(self): + self.f.close() + def new_vibe(self, strength, length, delay): # strength is from 0 to 1 # length and delay are in millisecs @@ -36,35 +48,64 @@ class Vibra: length, delay, int(strength * 0xFFFF), 0) a = array.array('h', effect) - # this is EVIOCSFF _IOC(_IOC_WRITE, 'E', 0x80, sizeof(struct ff_effect)) - fcntl.ioctl(self.f, 0x402c4580, a, True) + fcntl.ioctl(self.f, EVIOCSFF, a, True) + return a[1] id = a[1] - # this is 'struct input_event': sec, nsec, type, code, value - ev_play = struct.pack('LLHHi', 0, 0, 0x15, id, 1) - ev_stop = struct.pack('LLHHi', 0, 0, 0x15, id, 0) return (ev_play, ev_stop) - def play(self, ev): - p,s = ev; - self.f.write(p) + def multi_vibe(self, length, repeats = 1, delay = None, strength = 1): + start = 0 + if delay == None: + delay = length + v = [] + for i in range(0, repeats): + v.append(self.new_vibe(strength, length, start)) + start += length + delay + return v + + def play(self, id): + # this is 'struct input_event': sec, nsec, type, code, value + 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.f.write(ev_play) self.f.flush() - def stop(self, ev): - p,s = ev; - self.f.write(s) + def stop(self, id): + # this is 'struct input_event': sec, nsec, type, code, value + 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.f.write(ev_stop) self.f.flush() + def forget(self, id): + if type(id) == tuple or type(id) == list: + for i in id: + fcntl.ioctl(self.f, EVIOCRMFF, i) + else: + fcntl.ioctl(self.f, EVIOCRMFF, id) + +if __name__ == '__main__': + f = Vibra("/dev/input/rumble") + + # rumble for 300ms, pause for 100ms, rumble for 300ms, pause for 200ms + # then half-speed rumble for 600ms + p1 = f.new_vibe(1, 300, 0) + p2 = f.new_vibe(1, 300,400) + p3 = f.new_vibe(0.5, 600, 900) + f.play((p1, p2, p3)) -f = Vibra("/dev/input/rumble") + time.sleep(2) + f.forget((p1, p2, p3)) -# rumble for 300ms, pause for 100ms, rumble for 300ms, pause for 200ms -# then half-speed rumble for 600ms -p1 = f.new_vibe(1, 300, 0) -p2 = f.new_vibe(1, 300,400) -p3 = f.new_vibe(0.5, 600, 900) -f.play(p1) -f.play(p2) -f.play(p3) + f.play(f.multi_vibe(200, 14, delay=100)) -time.sleep(1.5) + time.sleep(5) |