def register_callback(sdRef, flags, errorCode, name, regtype, domain): if errorCode == pybonjour.kDNSServiceErr_NoError: print 'Registered service:' print ' name =', name print ' regtype =', regtype print ' domain =', domain host, port = "", 0 # setup the tcp server and prepare the uinput devices server = iBeagle((host, port), iBeagleHandler) port = server.socket.getsockname()[1] mouse = uinput.Device([ uinput.BTN_LEFT, uinput.BTN_RIGHT, uinput.REL_X, uinput.REL_Y, uinput.REL_WHEEL ]) kb = uinput.Device([ uinput.KEY_A, uinput.KEY_B, uinput.KEY_C, uinput.KEY_D, uinput.KEY_E, uinput.KEY_F, uinput.KEY_G, uinput.KEY_H, uinput.KEY_I, uinput.KEY_J, uinput.KEY_K, uinput.KEY_L, uinput.KEY_M, uinput.KEY_N, uinput.KEY_O, uinput.KEY_P, uinput.KEY_Q, uinput.KEY_R, uinput.KEY_S, uinput.KEY_T, uinput.KEY_U, uinput.KEY_V, uinput.KEY_W, uinput.KEY_X, uinput.KEY_Y, uinput.KEY_Z, uinput.KEY_ENTER, uinput.KEY_BACKSPACE, uinput.KEY_SPACE, uinput.KEY_LEFTSHIFT ]) # now split off into two processes # parent: serves the iBeagle requests # child: advertises the iBeagle service via bonjour pid = os.fork()
'KEY_LEFT': array('B', [8, 0, 85, 92]), 'KEY_ENTER': array('B', [16, 0, 85, 92]), 'KEY_ESC': array('B', [1, 0, 85, 92]), 'KEY_VOLUMEUP': array('B', [0, 2, 85, 92]), 'KEY_VOLUMEDOWN': array('B', [0, 4, 85, 92]), 'KEY_RELEASE': array('B', [0, 0, 85, 92]) } brightsign_keys = [ uinput.KEY_UP, uinput.KEY_RIGHT, uinput.KEY_DOWN, uinput.KEY_LEFT, uinput.KEY_ENTER, uinput.KEY_ESC, uinput.KEY_VOLUMEUP, uinput.KEY_VOLUMEDOWN ] key_pressed = False last_key = "KEY_ESC" touchboard = uinput.Device(brightsign_keys) while True: try: data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize) for key, code in keys.items(): if code == data[0:4]: if 'KEY_RELEASE' != key: touchboard.emit(eval('uinput.' + key), value=1) # press key last_key = key else: touchboard.emit(eval('uinput.' + last_key), value=0) except usb.core.USBError as e: data = None if e.args == ('Operation timed out', ): continue
# other libraries import uinput import time from math import ceil ## setup device via uinput # define possible keyboard/mouse key presses events = ( uinput.REL_X, uinput.REL_Y, uinput.BTN_LEFT, uinput.BTN_RIGHT, ) # load device with events device = uinput.Device(events) # define functions of pressing to pass to buttons l_click_press = lambda: device.emit(uinput.BTN_LEFT, 1) l_click_release = lambda: device.emit(uinput.BTN_LEFT, 0) ## create buttons # home = buttons.Button_struct(3, "home", button_test) # vol_min = buttons.Button_struct(5, "vol_min", button_test) # vol_add = buttons.Button_struct(7, "vol_add", button_test) # display = buttons.Button_struct(8, "display", button_test) # mute = buttons.Button_struct(10, "mute", button_test) # start = buttons.Button_struct(11, "start", button_test) # select = buttons.Button_struct(12, "select", button_test) green_r = buttons.Button_struct(13, "green_right", l_click_press, l_click_release) # red_r = buttons.Button_struct(15, "red_right", button_test) # blue_r = buttons.Button_struct(16, "blue_right", button_test)
Read a specific InputDevice (my_trackpoint), monitoring for special thumb button Use uinput (virtual driver) to create a mini keyboard Send ctrl keystroke on that keyboard test """ from evdev import InputDevice, categorize, ecodes import uinput import os # Initialize keyboard, choosing used keys ctrl_keyboard = uinput.Device([ uinput.KEY_KEYBOARD, uinput.KEY_LEFTCTRL, uinput.KEY_LEFTALT, uinput.KEY_LEFTMETA, uinput.KEY_F4, ]) # Sort of initialization click (not sure if mandatory) # ( "I'm-a-keyboard key" ) ctrl_keyboard.emit_click(uinput.KEY_KEYBOARD) # Useful to list input devices #for i in range(0,15): # dev = InputDevice('/dev/input/event{}'.format(i)) # print(dev) # Declare device patch. # I made a udev rule to assure it's always the same name
uinput.BTN_TR, uinput.BTN_THUMBL, uinput.BTN_THUMBR, uinput.BTN_SELECT, uinput.BTN_START, uinput.BTN_TL2, uinput.BTN_TR2, uinput.ABS_X + (0, 255, 0, 0), uinput.ABS_Y + (0, 255, 0, 0), uinput.BTN_DPAD_UP, uinput.BTN_DPAD_DOWN, uinput.BTN_DPAD_LEFT, uinput.BTN_DPAD_RIGHT, ]) gamepad = uinput.Device(events, "RetroStone2 Controle", 0x00) gamepad.emit(uinput.ABS_X, 128, syn=False) gamepad.emit(uinput.ABS_Y, 128) #--------------- define botoes ----------------- # ---- BRIGHTNESS CONTROL ---------# button_plus = port.PC18 button_minus = port.PC22 # ---- PLAYER 1 ---------# bt_up_p1 = port.PH19 bt_down_p1 = port.PH7 bt_left_p1 = port.PH4 bt_right_p1 = port.PH16
# ==== Serial Port Config ==== ser = serial.Serial() ser.baudrate = 115200 ser.port = '/dev/ttyUSB0' # "ser.port = X-1" if using windows, where X is the number of the COM-port, -1 is because the .port function counts from 0 and not 1 like the com names in windows ser.timeout = 10 # Specify the TimeOut in seconds, so that SerialPort doesn't hangs ser.open() # Opens SerialPort # ==== Keyboard buttons configurations ==== device = uinput.Device([ uinput.KEY_NEXTSONG, uinput.KEY_PREVIOUSSONG, uinput.KEY_PLAYPAUSE, uinput.KEY_VOLUMEUP, uinput.KEY_VOLUMEDOWN, uinput.KEY_MUTE, uinput.KEY_M, uinput.KEY_O, ]) # Check if the connection is open while not ser.isOpen(): print('Could not connect to Serialport ' + ser.portstr) time.sleep(1) print('Success! Opened Serial Connection with ' + ser.portstr) while True: msg = ser.readline() #Read from Serial Port
showOverlay = False lowbattery = 0 overrideCounter = threading.Event() joystick = False if joystickConfig['ENABLE_ON_BOOT'] == 'True': joystick = True # TO DO REPLACE A LOT OF OLD CALLS WITH THE CHECK_OUTPUT if monitoring_enabled == 'True': adc = Adafruit_ADS1x15.ADS1015() else: adc = False # Create virtual HID for Joystick device = uinput.Device(KEYS.values()) time.sleep(1) def hotkeyAction(key): if not gpio.input(HOTKEY): if key in HOTKEYS: logging.debug("hotkey action!") return True return False def handle_button(pin): global showOverlay key = KEYS[pin]
"Start": uinput.BTN_START, "Select": uinput.BTN_SELECT, "Tap": uinput.BTN_MODE, } device = uinput.Device([ uinput.ABS_X + (-159, 159, 0, 10), uinput.ABS_Y + (-159, 159, 0, 10), uinput.ABS_RX + (-146, 146, 0, 16), uinput.ABS_RY + (-146, 146, 0, 16), uinput.ABS_VOLUME + (0, 63, 0, 0), uinput.BTN_A, uinput.BTN_B, uinput.BTN_X, uinput.BTN_Y, uinput.BTN_TL, uinput.BTN_TR, uinput.BTN_THUMBL, uinput.BTN_THUMBR, uinput.BTN_DPAD_LEFT, uinput.BTN_DPAD_RIGHT, uinput.BTN_DPAD_UP, uinput.BTN_DPAD_DOWN, uinput.BTN_START, uinput.BTN_SELECT, uinput.BTN_MODE, ]) ######################################################## # CONFIGURABLE REGION END - Don't touch anything below # ######################################################## def pprint(obj):
IRQ_PIN = 26 # Don't change the below values unless you know what you're doing. These help # adjust the load on the CPU vs. responsiveness of the key detection. MAX_EVENT_WAIT_SECONDS = 0.5 EVENT_WAIT_SLEEP_SECONDS = 0.1 # Uncomment to enable debug message logging (might slow down key detection). #logging.basicConfig(level=logging.DEBUG) # Make sure uinput kernel module is loaded. subprocess.check_call(['modprobe', 'uinput']) # Configure virtual keyboard. device = uinput.Device(KEY_MAPPING.values()) # Setup the MPR121 device. cap = MPR121.MPR121() if not cap.begin(): print('Failed to initialize MPR121, check your wiring!') sys.exit(1) # Configure GPIO library to listen on IRQ pin for changes. # Be sure to configure pin with a pull-up because it is open collector when not # enabled. GPIO.setmode(GPIO.BCM) GPIO.setup(IRQ_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.add_event_detect(IRQ_PIN, GPIO.FALLING) atexit.register(GPIO.cleanup)
# End of Timeout Object Definition #-----------------------------------------# #Check if we're on a Raspberry Pi/Linux and do do appropriate initialisation/setup. #This allows some testing of the menu operation to be done without having a Pi if sys.platform == "linux2": #Import Ras Pi specific libraries for the hardware. import RPi.GPIO as GPIO import uinput #Set up "Device" object from the Uinput library so we can generate keypresses - with the type listed below. device = uinput.Device([ #These are the keypresses we want to generate to operate the menu using switches attached #to the GPIO pins, rather than using a real keyboard etc. uinput.KEY_UP, uinput.KEY_DOWN, uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_SPACE, uinput.KEY_TAB, uinput.KEY_ENTER, uinput.KEY_LEFTSHIFT ]) #======================================================= # Key Scan Object # Sets up and scans GPIO pins to generate keypresses #======================================================= #This object will be instantiated to scan the GPIO pins for state changes. class KeyScanObj(): #Initialise def __init__(self):
# Must do "sudo modprobe uinput" before running this program. There's also # a one-time setup of "sudo pip3 install python-uinput". This program itself # must be run as "sudo ./automateV35.py". "V35E" goes into whatever window # has the focus, so you have to move the cursor into the 272x480 DSKY display # and focus it before you'll see anything happen. import uinput import time device = uinput.Device([ uinput.KEY_V, uinput.KEY_N, uinput.KEY_0, uinput.KEY_1, uinput.KEY_2, uinput.KEY_3, uinput.KEY_4, uinput.KEY_5, uinput.KEY_6, uinput.KEY_7, uinput.KEY_8, uinput.KEY_9, uinput.KEY_ENTER ]) interKeyDelay = 0.25 time.sleep(5) count = 0 while True: count += 1 print("Count " + str(count))
count = 20 while count: if gp.input(self.gpio): # button is not pressed return True sleep(0.1) count -= 1 # if we get here it is time to exit return False # create uinput device events = list() for (key, gpio) in bindings: events.append(key) device = ui.Device(events) # create KeyBtn objects for (key, gpio) in bindings: KeyBtn(device, key, gpio) # This button causes program to exit exit_button = ExitBtn(SHUTDOWN_GPIO) # check if we should exit every half second while exit_button.check_continue(): #print(gp.input(23)) sleep(0.5) # All done so exit device.destroy()
def main(): def Idle(): if connected == True: # Ping to keep connection of joy-cons alive... # this is a dirty trick outerwise controllers disconnect to early # when not in use if chkcontroller == 1: joymacL = "l2ping -c 1 " + JoyConMacL response = os.system(joymacL) if chkcontroller == 2: joymacR = "l2ping -c 1 " + JoyConMacR response = os.system(joymacR) time.sleep(1.0) Idle() t = threading.Timer(1.0, Idle) t.start() signal.signal(signal.SIGINT, signal_handler) # joy-con bluetooth id codes controller JL_BUTTON_UP = 306 JL_BUTTON_DOWN = 305 JL_BUTTON_LEFT = 304 JL_BUTTON_RIGHT = 307 JL_BUTTON_SELECT = 317 JL_BUTTON_MINUS = 312 JL_BUTTON_L1 = 318 JL_BUTTON_L2 = 319 JL_BUTTON_TUMB = 314 JL_STICK_UP = 16 JL_STICK_DOWN = 16 JL_STICK_LEFT = 17 JL_STICK_RIGHT = 17 JR_BUTTON_A = 304 JR_BUTTON_X = 305 JR_BUTTON_B = 306 JR_BUTTON_Y = 307 JR_BUTTON_START = 316 JR_BUTTON_PLUS = 313 JR_BUTTON_R1 = 318 JR_BUTTON_R2 = 319 JR_BUTTON_TUMB = 315 JR_STICK_UP = 16 JR_STICK_DOWN = 16 JR_STICK_LEFT = 17 JR_STICK_RIGHT = 17 # sub set/reset virtual key def handle_button(ev, keystroke): #print "TYPE:" + str(event.type) #print "CODE:" + str(event.code) #print "VALUE:" + str(event.value) # DIGITAL # SET KEY if event.type == 1 and event.code == ev and event.value == 1: ui.write(e.EV_KEY, keystroke, 1) ui.syn() # RELEASE KEY if event.type == 1 and event.code == ev and event.value == 0: ui.write(e.EV_KEY, keystroke, 0) ui.syn() def handle_stick(ev1, ev2, keystroke1, keystroke2): # ANALOG # SET KEY SIDE 1 if event.type == 3 and event.code == ev1 and event.value == 1: ui.write(e.EV_KEY, keystroke1, 1) ui.syn() # SET KEY SIDE 2 if event.type == 3 and event.code == ev2 and event.value == -1: ui.write(e.EV_KEY, keystroke2, 1) ui.syn() # RELEASE SIDES 1,2 if event.type == 3 and (event.code == ev1 or event.code == ev2) and event.value == 0: ui.write(e.EV_KEY, keystroke1, 0) ui.write(e.EV_KEY, keystroke2, 0) ui.syn() # Search for joy-con device print("JoyCon program startup") print print("Finding Joy-con controller...") connected = False notfound = False try: while not connected: devices = devices = [ evdev.InputDevice(fn) for fn in evdev.list_devices() ] for device in devices: #print device.name #if device.name == "Nintendo Gamepad": if device.name == "Joy-Con (L)" and chkcontroller == 1: #print device.info.product # print device.info.bustype #if device.info.product == 8198 and chkcontroller==1: print 'Joy-con left found' joycondevL = device.fn print device.info gamepadL = evdev.InputDevice(joycondevL) connected = True # Set LED left controller connected GPIO.output(26, 1) if device.name == "Joy-Con (R)" and chkcontroller == 2: #if device.info.product == 8199 and chkcontroller==2: print 'Joy-con right found' joycondevR = device.fn print device.info gamepadR = evdev.InputDevice(joycondevR) connected = True # Set LED right controller connected GPIO.output(16, 1) #time.sleep(1) except NameError: if not notfound: print "Nothing found. <polling>" notfound = True pass except KeyboardInterrupt: t.cancel() print 'Connected!' """ Devices for keystrokes """ device = uinput.Device([uinput.KEY_E]) ui = UInput() # reading input from selected controller # Left controller if chkcontroller == 1: while True: try: for event in gamepadL.read_loop(): #this loops infinitely time.sleep(0.02) # Left controller handle_button(JL_BUTTON_UP, e.KEY_UP) handle_button(JL_BUTTON_DOWN, e.KEY_DOWN) handle_button(JL_BUTTON_LEFT, e.KEY_LEFT) handle_button(JL_BUTTON_RIGHT, e.KEY_RIGHT) handle_button(JL_BUTTON_SELECT, e.KEY_LEFTALT) handle_button(JL_BUTTON_MINUS, e.KEY_1) handle_button(JL_BUTTON_L1, e.KEY_O) handle_button(JL_BUTTON_L2, e.KEY_P) handle_button(JL_BUTTON_TUMB, e.KEY_M) handle_stick(JL_STICK_UP, JL_STICK_DOWN, e.KEY_3, e.KEY_4) handle_stick(JL_STICK_LEFT, JL_STICK_RIGHT, e.KEY_5, e.KEY_6) except IOError: print "Connection lost" connected = False # Reset LED left controller disconnected GPIO.output(26, 0) main() except KeyboardInterrupt: t.cancel() # reading input from selected controller # Right controller if chkcontroller == 2: while True: try: for event in gamepadR.read_loop(): # this loops infinitely time.sleep(0.02) # Right controller handle_button(JR_BUTTON_A, e.KEY_Q) handle_button(JR_BUTTON_B, e.KEY_B) handle_button(JR_BUTTON_X, e.KEY_X) handle_button(JR_BUTTON_Y, e.KEY_Y) handle_button(JR_BUTTON_START, e.KEY_RIGHTALT) handle_button(JR_BUTTON_PLUS, e.KEY_ESC) handle_button(JR_BUTTON_R1, e.KEY_U) handle_button(JR_BUTTON_R2, e.KEY_I) handle_button(JR_BUTTON_TUMB, e.KEY_N) handle_stick(JR_STICK_UP, JR_STICK_DOWN, e.KEY_7, e.KEY_8) handle_stick(JR_STICK_LEFT, JR_STICK_RIGHT, e.KEY_9, e.KEY_0) except IOError: print "Connection lost" connected = False # Reset LED right controller disconnected GPIO.output(16, 0) main() except KeyboardInterrupt: t.cancel()
device = uinput.Device([uinput.KEY_A, uinput.KEY_B, uinput.KEY_C, uinput.KEY_D, uinput.KEY_E, uinput.KEY_F, uinput.KEY_G, uinput.KEY_H, uinput.KEY_I, uinput.KEY_J, uinput.KEY_K, uinput.KEY_L, uinput.KEY_M, uinput.KEY_N, uinput.KEY_O, uinput.KEY_P, uinput.KEY_Q, uinput.KEY_R, uinput.KEY_S, uinput.KEY_T, uinput.KEY_U, uinput.KEY_V, uinput.KEY_W, uinput.KEY_X, uinput.KEY_Y, uinput.KEY_Z, uinput.KEY_LEFTSHIFT, uinput.KEY_ENTER, uinput.KEY_SPACE, uinput.KEY_BACKSPACE, uinput.KEY_UP, uinput.KEY_DOWN, uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_TAB ])
import RPi.GPIO as GPIO from os.path import isdir from os import remove, rmdir from glob import glob from time import sleep from shutil import copyfile import uinput libc = ctypes.CDLL("libc.so.6") #Log to syslog log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) log.addHandler(SysLogHandler(address='/dev/log', facility=SysLogHandler.LOG_DAEMON)) #Create a virtual keyboard keyboard = uinput.Device([uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_F5]) def left(_): log.info("Left button pressed") keyboard.emit_click(uinput.KEY_LEFT) def right(_): log.info("Right button pressed") keyboard.emit_click(uinput.KEY_RIGHT) def setup(): #Register edge detection inteturpts using the CPUs hardware debounce GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(LEFT_BUTTON, GPIO.IN, GPIO.PUD_UP) GPIO.setup(RIGHT_BUTTON, GPIO.IN, GPIO.PUD_UP)
system("modprobe uinput") joyMaxVal = 1024 joyMidVal = int(joyMaxVal / 2) device = uinput.Device([ uinput.BTN_THUMBL, uinput.BTN_THUMBR, uinput.ABS_X + (0, joyMaxVal, 0, 0), uinput.ABS_Y + (0, joyMaxVal, 0, 0), uinput.ABS_RX + (0, joyMaxVal, 0, 0), uinput.ABS_RY + (0, joyMaxVal, 0, 0), uinput.BTN_X, uinput.BTN_Y, uinput.BTN_Z, uinput.BTN_A, uinput.BTN_B, uinput.BTN_C, uinput.BTN_DPAD_UP, uinput.BTN_DPAD_DOWN, uinput.BTN_DPAD_LEFT, uinput.BTN_DPAD_RIGHT, uinput.BTN_SELECT, uinput.BTN_START, ]) class DigitalKey(object): def __init__(self, pin, key, val=0): self.pin = pin self.key = key
PREVIOUS: uinput.KEY_P, NEXT: uinput.KEY_N, STOP: uinput.KEY_S, SONG_1: uinput.KEY_KP1, SONG_2: uinput.KEY_KP2, SONG_3: uinput.KEY_KP3, SONG_4: uinput.KEY_KP4, SONG_5: uinput.KEY_KP5, SONG_6: uinput.KEY_KP6, SONG_7: uinput.KEY_KP7, SONG_8: uinput.KEY_KP8 } callbacks = [] device = uinput.Device(keys) pi = pigpio.pi() # Connect to local Pi. def init_pin_mapping(pin): pi.set_pull_up_down(pin, pigpio.PUD_UP) pi.set_mode(pin, pigpio.INPUT) pi.set_glitch_filter(pin, GLITCH_FILTER) callbacks.append(pi.callback(pin, pigpio.EITHER_EDGE, emitKey)) def init_hold_stop(): callbacks.append(pi.callback(STOP, pigpio.EITHER_EDGE, holdStop))
from evdev import UInput, ecodes as e from datetime import datetime import autopy from Xlib import X, display import uinput import time import math device = uinput.Device([ uinput.BTN_LEFT, uinput.BTN_RIGHT, uinput.REL_X, uinput.REL_Y, ]) def click(): capabilities = { e.EV_REL: (e.REL_X, e.REL_Y), e.EV_KEY: (e.BTN_LEFT, e.BTN_RIGHT), } with UInput(capabilities) as ui: ui.write(e.EV_KEY, e.BTN_LEFT, 1) ui.syn() def click2(): device = uinput.Device([ uinput.BTN_LEFT, uinput.BTN_RIGHT,
selection_6_led = LED(16) selection_7_button = Button(26) selection_7_led = LED(20) selection_8_button = Button(27) selection_8_led = LED(10) coin_a_button = Button(5) coin_b_button = Button(7) device = uinput.Device([ uinput.KEY_1, uinput.KEY_2, uinput.KEY_3, uinput.KEY_4, uinput.KEY_5, uinput.KEY_R, uinput.KEY_S, uinput.KEY_C, ]) # setup complete def button_1_pressed(): device.emit_click(uinput.KEY_1) return def button_2_pressed(): device.emit_click(uinput.KEY_2)
def main(): uinput_events = (uinput.BTN_EAST, uinput.BTN_SOUTH, uinput.BTN_NORTH, uinput.BTN_WEST, uinput.BTN_DPAD_UP, uinput.BTN_DPAD_DOWN, uinput.BTN_DPAD_LEFT, uinput.BTN_DPAD_RIGHT, uinput.BTN_SELECT, uinput.BTN_START, uinput.BTN_MODE, uinput.BTN_TL, uinput.BTN_TL2, uinput.BTN_TR, uinput.BTN_TR2, uinput.BTN_THUMBL, uinput.BTN_THUMBR, uinput.ABS_X, uinput.ABS_Y, uinput.ABS_RX, uinput.ABS_RY) uinput_buttons_map = { procon.ProCon.Button.A: uinput.BTN_EAST, procon.ProCon.Button.B: uinput.BTN_SOUTH, procon.ProCon.Button.X: uinput.BTN_NORTH, procon.ProCon.Button.Y: uinput.BTN_WEST, procon.ProCon.Button.UP: uinput.BTN_DPAD_UP, procon.ProCon.Button.DOWN: uinput.BTN_DPAD_DOWN, procon.ProCon.Button.LEFT: uinput.BTN_DPAD_LEFT, procon.ProCon.Button.RIGHT: uinput.BTN_DPAD_RIGHT, procon.ProCon.Button.MINUS: uinput.BTN_SELECT, procon.ProCon.Button.PLUS: uinput.BTN_START, procon.ProCon.Button.SCREENSHOT: None, procon.ProCon.Button.HOME: uinput.BTN_MODE, procon.ProCon.Button.L: uinput.BTN_TL, procon.ProCon.Button.ZL: uinput.BTN_TL2, procon.ProCon.Button.R: uinput.BTN_TR, procon.ProCon.Button.ZR: uinput.BTN_TR2, procon.ProCon.Button.LS: uinput.BTN_THUMBL, procon.ProCon.Button.RS: uinput.BTN_THUMBR } buttons_prev = {} try: print('Initializing uinput device... ', end='', flush=True) uinput_dev = uinput.Device(uinput_events, 'Nintendo Switch Pro Controller') print('done') except OSError as e: panic( 'Unable to open the uinput device. Make sure you have inserted the uinput kernel module (by `sudo modprobe uinput`) and have sufficient permission to open it (either as root or with udev rules): {}' .format(e)) def send_to_uinput(buttons, l_stick, r_stick, _, __, ___): nonlocal buttons_prev if not buttons_prev: buttons_prev = buttons return for k, v in buttons.items(): if buttons_prev[k] != v: uinput_button = uinput_buttons_map[k] if not uinput_button: continue if v: uinput_dev.emit(uinput_button, 1) else: uinput_dev.emit(uinput_button, 0) buttons_prev = buttons uinput_dev.emit(uinput.ABS_X, l_stick[0]) uinput_dev.emit(uinput.ABS_Y, -l_stick[1]) uinput_dev.emit(uinput.ABS_RX, r_stick[0]) uinput_dev.emit(uinput.ABS_RY, -r_stick[1]) print('Initializing Nintendo Switch Pro Controller... ', end='', flush=True) try: con = procon.ProCon() except OSError as e: panic( 'Unable to open the controller. Make sure you have plugged in the controller and have sufficient permission to open it (either as root or with udev rules): {}' .format(e)) print('done\nEnjoy!') try: con.start(send_to_uinput) except KeyboardInterrupt: print('\rGoodbye!') except OSError: panic('IO failed. Did you just unplugged the controller?')
def emulate(controllerName="WiimoteController", keyboardName="WiimoteButtons", horizontal=False): global running padEvents = [ uinput.ABS_X + (0, 1023, 0, 0), uinput.ABS_Y + (0, 1023, 0, 0), uinput.BTN_A, uinput.BTN_B, uinput.BTN_C, uinput.BTN_X, uinput.BTN_Y, uinput.BTN_Z ] kbdEvents = [(uinput.KEY_ESC[0], i) for i in range(uinput.KEY_ESC[1], uinput.KEY_MICMUTE[1] + 1)] def updateLEDs(): if horizontal: wm.led = cwiid.LED2_ON | cwiid.LED3_ON else: wm.led = cwiid.LED1_ON | cwiid.LED4_ON nunchuck = False with uinput.Device(padEvents, name=controllerName) as controller: controller.emit(uinput.ABS_X, 512, syn=False) controller.emit(uinput.ABS_Y, 512) with uinput.Device(kbdEvents, name=keyboardName) as kbd: try: prevButtons = 0 prevNunchukX = 128 prevNunchukY = 128 uinputPressed = set() CONNECTED_EVENT.wait() updateLEDs() def press(dev, u): if u not in uinputPressed: dev.emit(u, 1) uinputPressed.add(u) def release(dev, u): if u in uinputPressed: dev.emit(u, 0) uinputPressed.remove(u) while running: wiimoteWait() buttons = getButtons(wm.state) pressed = buttons & ~prevButtons released = ~buttons & prevButtons prevButtons = buttons if buttons & cwiid.BTN_MINUS: if pressed & cwiid.BTN_PLUS: horizontal = not horizontal updateLEDs() map = minusVerticalMap if not horizontal else minusHorizontalMap for wii, u in map: if pressed & wii: press(kbd, u) elif released & wii: release(kbd, u) elif pressed or released: map = verticalMap if not horizontal else horizontalMap for wii, u in map: if pressed & wii: press(kbd, u) elif released & wii: release(kbd, u) if 'nunchuk' in wm.state: if not nunchuck: nunchuck = True horizontal = False updateLEDs() x, y = wm.state['nunchuk']['stick'] def fix(v): v = int(512 + 512 * 1.35 * (v - 128) / 128) if v < 0: v = 0 elif v > 1023: v = 1023 return v controller.emit(uinput.ABS_X, fix(x), syn=False) controller.emit(uinput.ABS_Y, fix(y)) except KeyboardInterrupt: pass finally: for u in uinputPressed: kbd.emit(u, 0)
import time ## rpi drivers for sonic the hedgehog - sega megadrive ## using the dgen emulator ## uinput used for simulated key presses import os import RPi.GPIO as GPIO import uinput import math device = uinput.Device([uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_SPACE]) GPIO.setmode(GPIO.BCM) DEBUG = 1 # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) def readadc(adcnum, clockpin, mosipin, misopin, cspin): if ((adcnum > 7) or (adcnum < 0)): return -1 GPIO.output(cspin, True) GPIO.output(clockpin, False) # start clock low GPIO.output(cspin, False) # bring CS low commandout = adcnum commandout |= 0x18 # start bit + single-ended bit commandout <<= 3 # we only need to send 5 bits here for i in range(5): if (commandout & 0x80):
import time import subprocess import os import uinput import serial ser = serial.Serial('/dev/ttyUSB0', 9600) device = uinput.Device([ uinput.KEY_1, uinput.KEY_A, uinput.KEY_2, uinput.KEY_B, uinput.KEY_3, uinput.KEY_C, uinput.KEY_4, uinput.KEY_D ]) #Tombol panggil LCD 1 def loket_1(): path = '/var/www/html/audio_counter/data1.txt' loket_1 = open(path, 'r') lihat = loket_1.read() data = int(lihat) loket_1.close() return data def sisa_1(): path = '/var/www/html/audio_counter/loket1.txt' sisa_1 = open(path, 'r') ceksisa1 = sisa_1.read() left_1 = int(ceksisa1) sisa_1.close() return left_1
showOverlay = False lowbattery = 0 overrideCounter = Event() if ON_BY_DEFAULT == 'True': joystick = True # TO DO REPLACE A LOT OF OLD CALLS WITH THE CHECK_OUTPUT if monitoring_enabled == 'True': adc = Adafruit_ADS1x15.ADS1015() else: adc = False # Create virtual HID for Joystick if JOYSTICK_DISABLED == 'False': device = uinput.Device(KEYS.values(), name="OneForAll-GP", version=0x3) else: device = uinput.Device(KEYS.values(), name="OneForAll", version=0x3) time.sleep(1) def hotkeyAction(key): if not gpio.input(SHOW_OSD_KEY): if key in HOTKEYS: return True return False def handle_button(pin):
#!/usr/bin/env python import skywriter import signal import uinput import time mouse = uinput.Device([uinput.REL_X, uinput.REL_Y, uinput.BTN_LEFT]) v_x = 0 v_y = 0 v_x = 0 @skywriter.move() def move(x, y, z): global v_x, v_y v_z = int(z * 30.0) v_x = int((x - 0.5) * v_z) v_y = int((y - 0.5) * v_z) print(v_x, v_y) while True: time.sleep(0.01) mouse.emit(uinput.REL_X, v_x) mouse.emit(uinput.REL_Y, -v_y) signal.pause()
Keyboard = uinput.Device([ uinput.KEY_RESERVED, uinput.KEY_1, uinput.KEY_2, uinput.KEY_3, uinput.KEY_4, uinput.KEY_5, uinput.KEY_6, uinput.KEY_7 #8 , uinput.KEY_8 #9 , uinput.KEY_9 #10 , uinput.KEY_0 #11 , uinput.KEY_MINUS #12 , uinput.KEY_EQUAL #13 , uinput.KEY_BACKSPACE # , uinput.KEY_TAB # , uinput.KEY_Q # , uinput.KEY_W # , uinput.KEY_E # , uinput.KEY_R # , uinput.KEY_T # , uinput.KEY_Y # , uinput.KEY_U # , uinput.KEY_I # , uinput.KEY_O # , uinput.KEY_P # , uinput.KEY_LEFTBRACE # , uinput.KEY_RIGHTBRACE # , uinput.KEY_ENTER # , uinput.KEY_LEFTCTRL # , uinput.KEY_A # , uinput.KEY_S # , uinput.KEY_D # , uinput.KEY_F # , uinput.KEY_G # , uinput.KEY_H # , uinput.KEY_J # , uinput.KEY_K # , uinput.KEY_L # , uinput.KEY_SEMICOLON # , uinput.KEY_APOSTROPHE # , uinput.KEY_GRAVE # , uinput.KEY_LEFTSHIFT # , uinput.KEY_BACKSLASH # , uinput.KEY_Z # , uinput.KEY_X # , uinput.KEY_C # , uinput.KEY_V # , uinput.KEY_B # , uinput.KEY_N # , uinput.KEY_M # , uinput.KEY_COMMA # , uinput.KEY_DOT # , uinput.KEY_SLASH, uinput.KEY_RIGHTSHIFT, uinput.KEY_KPASTERISK, uinput.KEY_LEFTALT, uinput.KEY_SPACE, uinput.KEY_CAPSLOCK, uinput.KEY_F1, uinput.KEY_F2, uinput.KEY_F3, uinput.KEY_F4, uinput.KEY_F5, uinput.KEY_F6, uinput.KEY_F7, uinput.KEY_F8, uinput.KEY_F9, uinput.KEY_F10, uinput.KEY_NUMLOCK, uinput.KEY_SCROLLLOCK, uinput.KEY_KP7, uinput.KEY_KP8, uinput.KEY_KP9, uinput.KEY_KPMINUS, uinput.KEY_KP4, uinput.KEY_KP5, uinput.KEY_KP6, uinput.KEY_KPPLUS, uinput.KEY_KP1, uinput.KEY_KP2, uinput.KEY_KP3, uinput.KEY_KP0, uinput.KEY_KPDOT, uinput.KEY_ZENKAKUHANKAKU, uinput.KEY_102ND, uinput.KEY_F11, uinput.KEY_F12, uinput.KEY_RO, uinput.KEY_KATAKANA, uinput.KEY_HIRAGANA, uinput.KEY_HENKAN, uinput.KEY_KATAKANAHIRAGANA, uinput.KEY_MUHENKAN, uinput.KEY_KPJPCOMMA, uinput.KEY_KPENTER, uinput.KEY_RIGHTCTRL, uinput.KEY_KPSLASH, uinput.KEY_SYSRQ, uinput.KEY_RIGHTALT, uinput.KEY_LINEFEED, uinput.KEY_HOME, uinput.KEY_UP, uinput.KEY_PAGEUP, uinput.KEY_LEFT, uinput.KEY_RIGHT, uinput.KEY_END, uinput.KEY_DOWN, uinput.KEY_PAGEDOWN, uinput.KEY_INSERT, uinput.KEY_DELETE, uinput.KEY_MACRO, uinput.KEY_MUTE, uinput.KEY_VOLUMEDOWN, uinput.KEY_VOLUMEUP, uinput.KEY_POWER, uinput.KEY_KPEQUAL, uinput.KEY_KPPLUSMINUS, uinput.KEY_PAUSE, uinput.KEY_SCALE, uinput.KEY_KPCOMMA, uinput.KEY_HANGEUL, uinput.KEY_HANGUEL, uinput.KEY_HANJA, uinput.KEY_YEN, uinput.KEY_LEFTMETA, uinput.KEY_RIGHTMETA, uinput.KEY_COMPOSE #127 , uinput.KEY_F13 #183 , uinput.KEY_F14, uinput.KEY_F15, uinput.KEY_F16, uinput.KEY_F17, uinput.KEY_F18, uinput.KEY_F19, uinput.KEY_F20, uinput.KEY_F21, uinput.KEY_F22, uinput.KEY_F23, uinput.KEY_F24, uinput.KEY_PLAYCD, uinput.KEY_PAUSECD, uinput.KEY_PROG3, uinput.KEY_PROG4, uinput.KEY_DASHBOARD, uinput.KEY_SUSPEND, uinput.KEY_CLOSE, uinput.KEY_PLAY, uinput.KEY_FASTFORWARD, uinput.KEY_BASSBOOST, uinput.KEY_PRINT, uinput.KEY_HP, uinput.KEY_CAMERA, uinput.KEY_SOUND, uinput.KEY_QUESTION, uinput.KEY_EMAIL, uinput.KEY_CHAT, uinput.KEY_SEARCH, uinput.KEY_CONNECT, uinput.KEY_FINANCE, uinput.KEY_SPORT, uinput.KEY_SHOP, uinput.KEY_ALTERASE, uinput.KEY_CANCEL, uinput.KEY_BRIGHTNESSDOWN, uinput.KEY_BRIGHTNESSUP #225 , uinput.KEY_UNKNOWN #240 , uinput.KEY_VIDEO_NEXT #241 , uinput.KEY_VIDEO_PREV #242 # uinput.KEY_ ])
def register_device(keylist): return uinput.Device(keylist)
"Down": uinput.KEY_DOWN, "Start": uinput.KEY_ENTER, "Select": uinput.KEY_ESC } gamepad_device = uinput.Device([ uinput.ABS_X + (-159, 159, 0, 10), uinput.ABS_Y + (-159, 159, 0, 10), uinput.ABS_RX + (-146, 146, 0, 16), uinput.ABS_RY + (-146, 146, 0, 16), uinput.BTN_A, uinput.BTN_B, uinput.BTN_X, uinput.BTN_Y, uinput.BTN_TL, uinput.BTN_TR, uinput.BTN_THUMBL, uinput.BTN_THUMBR, uinput.BTN_DPAD_LEFT, uinput.BTN_DPAD_RIGHT, uinput.BTN_DPAD_UP, uinput.BTN_DPAD_DOWN, uinput.BTN_START, uinput.BTN_SELECT, uinput.BTN_MODE, ]) mouse_device = uinput.Device( [uinput.REL_X, uinput.REL_Y, uinput.BTN_LEFT, uinput.BTN_RIGHT]) keyboard_device = uinput.Device(keboard_matrix + btn_keyboard_map.values())
_KEYPRESS_ACTION = { '\x19': KEY_VOLUMEDOWN, '\x18': KEY_VOLUMEUP, '\x5b': KEY_MUTE, } # Server config PORT = 2999 # Default port that the app uses ADDRESS = "0.0.0.0" # Devices controller EV = [ BTN_LEFT, BTN_RIGHT, REL_X, REL_Y, KEY_VOLUMEDOWN, KEY_VOLUMEUP, KEY_MUTE ] + _CHAR_MAP.values() device = uinput.Device(EV) # Handles packets received class incoming(SocketServer.BaseRequestHandler): def handle(self): req = self.request # Sending Sync. First null byte is important. Avoids password checking req.sendall("\x00\x00\x00\x00\x00") # Each packet sent by the android application is 5 bytes of length def recvcmd(): return req.recv(5) #never say never
def emulateMouse(mouseName="LightgunMouse", controllerName="WiimoteButtons", horizontal=False, rumble=False): global running size = WINDOW_SIZE or (1920, int(0.5 + 1920 / CONFIG.aspect)) events = [ uinput.ABS_X + (0, size[0], 0, 0), uinput.ABS_Y + (0, size[1], 0, 0), uinput.BTN_LEFT, uinput.BTN_RIGHT ] events2 = [(uinput.KEY_ESC[0], i) for i in range(uinput.KEY_ESC[1], uinput.KEY_MICMUTE[1] + 1)] def updateLEDs(): if horizontal: wm.led = cwiid.LED2_ON | cwiid.LED3_ON else: wm.led = cwiid.LED1_ON | cwiid.LED4_ON rumbleStarted = None with uinput.Device(events, name=mouseName) as device: with uinput.Device(events2, name=controllerName) as device2: try: prevButtons = 0 prevNunchukX = 128 prevNunchukY = 128 uinputPressed = set() CONNECTED_EVENT.wait() updateLEDs() def press(dev, u): if u not in uinputPressed: dev.emit(u, 1) uinputPressed.add(u) def release(dev, u): if u in uinputPressed: dev.emit(u, 0) uinputPressed.remove(u) while running: wiimoteWait() buttons = getButtons(wm.state) updateAcceleration(wm.state['acc']) pressed = buttons & ~prevButtons released = ~buttons & prevButtons prevButtons = buttons if buttons & cwiid.BTN_MINUS: if pressed & cwiid.BTN_PLUS: horizontal = not horizontal updateLEDs() map = minusVerticalMap if not horizontal else minusHorizontalMap for wii, u in map: if pressed & wii: press(device2, u) elif released & wii: release(device2, u) elif pressed or released: map = verticalMap if not horizontal else horizontalMap for wii, u in map: dev = device if (u == uinput.BTN_LEFT or u == uinput.BTN_RIGHT) else device2 if pressed & wii: press(dev, u) if rumble: wm.rumble = True rumbleStarted = time.time() elif released & wii: release(dev, u) if rumble and rumbleStarted and rumbleStarted + RUMBLE_TIME <= time.time( ): wm.rumble = False if 'nunchuk' in wm.state: def stick(offset, prevOffset, key): if offset < NUNCHUK_DEADZONE - NUNCHUK_HYSTERESIS and prevOffset >= NUNCHUK_DEADZONE - NUNCHUK_HYSTERESIS: release(device2, key) elif offset >= NUNCHUK_DEADZONE: press(device2, key) x, y = wm.state['nunchuk']['stick'] stick(x - 128, prevNunchukX - 128, uinput.KEY_RIGHT) stick(128 - x, 128 - prevNunchukX, uinput.KEY_LEFT) stick(y - 128, prevNunchukY - 128, uinput.KEY_UP) stick(128 - y, 128 - prevNunchukY, uinput.KEY_DOWN) prevNunchukX, prevNunchukY = x, y if not horizontal: ir = wm.state['ir_src'] irQuad = getIRQuad(ir) if irQuad: x, y = CONFIG.pointerPosition(irQuad) x1 = int(size[0] * x) y1 = int(size[1] * (1 - y)) device.emit(uinput.ABS_X, x1, syn=False) device.emit(uinput.ABS_Y, y1) except KeyboardInterrupt: pass finally: for u in uinputPressed: (device if u == uinput.BTN_LEFT or u == uinput.BTN_RIGHT else device2).emit(u, 0)