RetroControllerPi/spiev.py
2020-06-23 01:06:01 +01:00

55 lines
1.3 KiB
Python

import time
import spidev
from evdev import UInput, ecodes as e
bus=1
device=0
spi=spidev.SpiDev()
spi.open(bus,device)
spi.max_speed_hz = 250000
spi.mode = 0
ui = UInput()
oldstate=0
def check(diffs, state, button, evcode):
# print(hex(state))
if diffs & button:
ui.write(e.EV_KEY, evcode, 0 if state & button else 1)
# print("button %4x is %1d, aka %d"%( button, 1 if state & button else 0, evcode) )
while True:
data=spi.xfer2([0x55]*2, spi.max_speed_hz, 1000, 8)
state=(data[0]<<8) + data[1]
diffs = state ^ oldstate
if diffs:
print(' '.join([format(d,'08b') for d in data]))
check(diffs, state, 0x8000, e.KEY_Z) # B
check(diffs, state, 0x4000, e.KEY_A) # Y
check(diffs, state, 0x2000, e.KEY_LEFTSHIFT) # Select
check(diffs, state, 0x1000, e.KEY_ENTER) # Start
check(diffs, state, 0x0800, e.KEY_UP) # Up
check(diffs, state, 0x0400, e.KEY_DOWN) # Down
check(diffs, state, 0x0200, e.KEY_LEFT) # Left
check(diffs, state, 0x0100, e.KEY_RIGHT) # Right
check(diffs, state, 0x0080, e.KEY_X) # A
check(diffs, state, 0x0040, e.KEY_S) # X
check(diffs, state, 0x0020, e.KEY_Q) # L
check(diffs, state, 0x0010, e.KEY_W) # R
ui.syn()
oldstate=state
ui.close()