[PACKAGE/BOARD] Add handle usb-hid gadget (#55)

* Add handle usb-hid gadget (Tunr your handheld into usb gamepad for PC)

* Add special keys handling

* add Manual for USB-HID app

* use st terminal to execute script

---------

Co-authored-by: Apaczer <94932128+Apaczer@users.noreply.github.com>
This commit is contained in:
tiopex
2023-11-28 23:15:18 +01:00
committed by GitHub
parent f7fbe79d2f
commit 10d3f8c37a
15 changed files with 151 additions and 15 deletions

View File

@@ -1,4 +0,0 @@
#!/bin/busybox sh
rm /mnt/tvout
sync
reboot

View File

@@ -1,5 +0,0 @@
touch /mnt/tvout
#killall -9 main
sync
#we need to reboot because we're dependant of /dev/fb0 in all cases
reboot

View File

@@ -0,0 +1,9 @@
#!/bin/busybox sh
FILE=/mnt/tvout
if [ -f "$FILE" ]; then
rm "$FILE"
else
touch "$FILE"
fi
sync
reboot

View File

@@ -0,0 +1,21 @@
Turn your handheld into USB gamepad for PC/Android
Usage:
- Connect handheld to PC via USB
- Run this USB HID app
- Now you should be able to control PC/phone via handheld buttons
- Press RESET button to leave USB HID mode
Mappings:
DPAD mapped to ARROWS
B mapped to L_CTRL
A mapped to SPACE
Y mapped to L_SHIFT
X mapped to X
SELECT mapped to BACKSPACE
START mapped to ENTER
L1 mapped to L_ALT
R1 mapped to TAB
L2 mapped to PG_UP
R2 mapped to PG_DOWN
RESET not mapped, press to QUIT

View File

@@ -0,0 +1,68 @@
import sys
import keyboard
## usb hid codes: https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf
hidCodeDict = {
103:82, #up
108:81, #down
105:80, #left
106:79, #right
29:224, #b mapped to l_ctrl
56:44, #a mapped to space
57:225, #y mapped to l_shift
42:27, #x mapped to x
1:42, #select mapped to backspace
28:40, #start mapped to enter
15:226, #lpad1 mapped to l_alt
14:43, #rpad1 mapped to tab
104:75, #lpad2 mapped to pg_up
109:78, #rpad2 mapped to pg_down
97:0, #reset not mapped, used to quit
}
specialCodeDict = {
224:1,
225:2,
226:4,
227:8,
228:16,
229:32,
230:64,
231:128,
}
NULL_CHAR = chr(0)
SPECIAL_KEY_CODES=[224,225,226,227,228,229,230,231]
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
def translate(code, dictonary):
if code in dictonary:
return dictonary[code]
return 0
def write_pressed_keys(e):
print('\r', end='')
all_keys=[translate(code,hidCodeDict) for code in keyboard._pressed_events]
if not len(all_keys):
write_report(NULL_CHAR*8)
return
special_keys=0
for s_key in SPECIAL_KEY_CODES:
if s_key in all_keys:
all_keys.remove(s_key)
special_keys += translate(s_key,specialCodeDict)
if not len(all_keys):
write_report(chr(special_keys)+NULL_CHAR*7)
return
keys =''.join(chr(key) for key in all_keys)
write_report(chr(special_keys)+NULL_CHAR+keys+NULL_CHAR*5)
print("Press RESET button to quit")
keyboard.hook(write_pressed_keys)
keyboard.wait(97)

View File

@@ -0,0 +1,24 @@
#!/bin/busybox sh
st_error_func(){
st -k -e "/bin/sh" "-c" "echo -e \"\e[31m${1}\e[0m\"; sleep 2"
}
st_exec_func(){
st -k -e "/bin/sh" "-c" "${1}"
}
MUSB_MODE=$(cat /sys/devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/mode)
if ! (test "${MUSB_MODE}" = "b_peripheral"); then
st_error_func "\n\n\n\n\n\n\n\n First connect handheld to device!"
exit
else
gadget-vid-pid-remove 0x1d6b:0x0104
gadget-hid
st_exec_func "\
echo -e \"\e[32m\n\n\n\n\n\n Starting USB-HID mode\e[0m\n\n\n\"; \
sleep 1; \
python /mnt/apps/usb-hid/usb-hid.py"
gadget-vid-pid-remove 0x1d6b:0x0104
gadget-ms /dev/mmcblk0p1 /dev/mmcblk0p4
fi

View File

@@ -1,3 +0,0 @@
title=TVout OFF
description=TV output disabled
exec=/mnt/apps/tvoff/tvout-off.sh

View File

@@ -1,3 +0,0 @@
title=TVout ON
description=TV output enabled
exec=/mnt/apps/tvon/tvout-on.sh

View File

@@ -0,0 +1,3 @@
title=TVout ON/OFF
description=TV output ON/OFF
exec=/mnt/apps/tvout/tvout.sh

View File

@@ -0,0 +1,3 @@
title=USB HID
description=USB HID PC gamepad
exec=/mnt/apps/usb-hid/usb-hid.sh

View File

@@ -165,6 +165,7 @@ BR2_PACKAGE_UMTPRD=y
BR2_PACKAGE_LUA=y
BR2_PACKAGE_LUA_5_1=y
BR2_PACKAGE_LUA_ICONV=y
BR2_PACKAGE_PYTHON_KEYBOARD=y
BR2_PACKAGE_LIBASPLIB=y
BR2_PACKAGE_LIBID3TAG=y
BR2_PACKAGE_LIBMAD=y

View File

@@ -163,6 +163,7 @@ BR2_PACKAGE_PARTED=y
BR2_PACKAGE_UMTPRD=y
BR2_PACKAGE_LUA=y
BR2_PACKAGE_LUA_5_1=y
BR2_PACKAGE_PYTHON_KEYBOARD=y
BR2_PACKAGE_LIBASPLIB=y
BR2_PACKAGE_LIBID3TAG=y
BR2_PACKAGE_LIBMAD=y

View File

@@ -1152,6 +1152,7 @@ menu "External python modules"
source "package/python-json-schema-validator/Config.in"
source "package/python-jsonmodels/Config.in"
source "package/python-jsonschema/Config.in"
source "package/python-keyboard/Config.in"
source "package/python-keyring/Config.in"
source "package/python-kiwisolver/Config.in"
source "package/python-libconfig/Config.in"

View File

@@ -0,0 +1,7 @@
config BR2_PACKAGE_PYTHON_KEYBOARD
select BR2_PACKAGE_KBD
bool "python-keyboard"
help
Hook and simulate keyboard events on Windows and Linux
https://pypi.org/project/keyboard

View File

@@ -0,0 +1,13 @@
################################################################################
#
# python-keyboard
#
################################################################################
PYTHON_KEYBOARD_VERSION = 0.13.5
PYTHON_KEYBOARD_SITE = $(call github,boppreh,keyboard,v$(PYTHON_KEYBOARD_VERSION))
PYTHON_KEYBOARD_LICENSE = MIT
PYTHON_KEYBOARD_LICENSE_FILES = LICENSE
PYTHON_KEYBOARD_SETUP_TYPE = setuptools
$(eval $(python-package))