#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Keyboard LED driven car control program by Elw3
# Y replays the whole action and saves it. B replays a saved action and START starts a fresh record.
#This script needs to run an root to set the LEDS
from subprocess import call
from time import time, sleep
#List of pandora keys in the binary reporting format you get by /dev/input
keys=[['Up', '670001', '670000'], ['Down', '6c0001', '6c0000'], ['Left', '690001', '690000'], ['Right', '6a0001', '6a0000'], ['Y', '680001', '680000'], ['B', '6b0001', '6b0000'], ['X', '6d0001', '6d0000'], ['A', '660001', '660000'], ['L', '360001', '360000'], ['R', '610001', '610000'], ['Start', '380001', '380000'], ['Select', '1d0001', '1d0000'], ['Pando', '8b0001', '8b0000']]
#This represents our keyboard leds, we need it two times to compare if the new state differs from the old. Of course we could always set it according to the keys but that is harder to record.
Leds,Leds2=['-','-','-'],['-','-','-']
class p:
state={'Up':0,'Right':0,'Down':0,'Left':0,'A':0,'B':0,'Start':0,'Y':0,'X':0,'L':0,'R':0,'Select':0,'Pando':0}
a,b=0,0
repeat,repeat2=[],[]
#this is the auto replay function
def repeat():
try:
#the first entry is always "nothing" so the time for it needs to be 0 too.
p.repeat[0][1]=0
except:
pass
for i in p.repeat:
call("setleds "+ i[0][0] + "num "+ i[0][1] + "caps "+ i[0][2] + "scroll </dev/tty7", shell=True)
sleep(i[1])
p.repeat2= p.repeat
p.repeat=[]
def repeat2():
for i in p.repeat2:
print i
call("setleds "+ i[0][0] + "num "+ i[0][1] + "caps "+ i[0][2] + "scroll </dev/tty7", shell=True)
sleep(i[1])
#jep i get the keys from /dev/input... whatever works is fine
data= open("/dev/input/event4" ,"rb" )
while 1:
#since read stops the script without input the thing is only active at the very moment of a button press.
button=data.read(32)
for i in keys:
#This converts the keylist into a state list.
if button.encode('hex_codec')[20:26] == i[2]:
p.state[i[0]]=0
if button.encode('hex_codec')[20:26] == i[1]:
p.state[i[0]]=1
if p.state['Y']==1:
repeat()
elif p.state['B']==1:
repeat2()
elif p.state['R']==1:
Leds=['+','+','-']
elif p.state['L']==1:
Leds=['+','-','+']
elif p.state['A']==1:
Leds=['+','-','-']
elif p.state['Right']==1:
Leds=['-','+','-']
elif p.state['Left']==1:
Leds=['-','-','+']
elif p.state['X']==1:
Leds=['-','+','+']
elif p.state['Start']==1:
p.repeat=[]
else:
Leds=['-','-','-']
if Leds == Leds2:
pass
else:
#times are actually pretty confusing
p.b=time()
p.repeat.append([Leds2,p.b-p.a])
p.a=time()
Leds2=Leds
print Leds
#this call sets the leds. Note the "</dev/tty7" part, setleds does not work in a terminal emulator so we need to use a real tty, and our tty when on the desktop is tty7.
call("setleds "+ Leds[0] + "num "+ Leds[1] + "caps "+ Leds[2] + "scroll </dev/tty7", shell=True)