From 10d3f8c37a4ea1852532fb01c0ac3017e42a0775 Mon Sep 17 00:00:00 2001 From: tiopex <67048640+tiopex@users.noreply.github.com> Date: Tue, 28 Nov 2023 23:15:18 +0100 Subject: [PATCH] [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> --- board/miyoo/main/apps/tvoff/tvout-off.sh | 4 -- board/miyoo/main/apps/tvon/tvout-on.sh | 5 -- board/miyoo/main/apps/tvout/tvout.sh | 9 +++ board/miyoo/main/apps/usb-hid/usb-hid.man.txt | 21 ++++++ board/miyoo/main/apps/usb-hid/usb-hid.py | 68 +++++++++++++++++++ board/miyoo/main/apps/usb-hid/usb-hid.sh | 24 +++++++ .../main/gmenu2x/sections/applications/tvoff | 3 - .../main/gmenu2x/sections/applications/tvon | 3 - .../main/gmenu2x/sections/applications/tvout | 3 + .../gmenu2x/sections/applications/usb-hid | 3 + configs/miyoo_musl_defconfig | 1 + configs/miyoo_uclibc_defconfig | 1 + package/Config.in | 1 + package/python-keyboard/Config.in | 7 ++ package/python-keyboard/python-keyboard.mk | 13 ++++ 15 files changed, 151 insertions(+), 15 deletions(-) delete mode 100644 board/miyoo/main/apps/tvoff/tvout-off.sh delete mode 100644 board/miyoo/main/apps/tvon/tvout-on.sh create mode 100644 board/miyoo/main/apps/tvout/tvout.sh create mode 100644 board/miyoo/main/apps/usb-hid/usb-hid.man.txt create mode 100644 board/miyoo/main/apps/usb-hid/usb-hid.py create mode 100644 board/miyoo/main/apps/usb-hid/usb-hid.sh delete mode 100644 board/miyoo/main/gmenu2x/sections/applications/tvoff delete mode 100644 board/miyoo/main/gmenu2x/sections/applications/tvon create mode 100644 board/miyoo/main/gmenu2x/sections/applications/tvout create mode 100644 board/miyoo/main/gmenu2x/sections/applications/usb-hid create mode 100644 package/python-keyboard/Config.in create mode 100644 package/python-keyboard/python-keyboard.mk diff --git a/board/miyoo/main/apps/tvoff/tvout-off.sh b/board/miyoo/main/apps/tvoff/tvout-off.sh deleted file mode 100644 index d86df04f..00000000 --- a/board/miyoo/main/apps/tvoff/tvout-off.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/busybox sh -rm /mnt/tvout -sync -reboot diff --git a/board/miyoo/main/apps/tvon/tvout-on.sh b/board/miyoo/main/apps/tvon/tvout-on.sh deleted file mode 100644 index 86020785..00000000 --- a/board/miyoo/main/apps/tvon/tvout-on.sh +++ /dev/null @@ -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 diff --git a/board/miyoo/main/apps/tvout/tvout.sh b/board/miyoo/main/apps/tvout/tvout.sh new file mode 100644 index 00000000..0733e7c3 --- /dev/null +++ b/board/miyoo/main/apps/tvout/tvout.sh @@ -0,0 +1,9 @@ +#!/bin/busybox sh +FILE=/mnt/tvout +if [ -f "$FILE" ]; then + rm "$FILE" +else + touch "$FILE" +fi +sync +reboot diff --git a/board/miyoo/main/apps/usb-hid/usb-hid.man.txt b/board/miyoo/main/apps/usb-hid/usb-hid.man.txt new file mode 100644 index 00000000..da140c91 --- /dev/null +++ b/board/miyoo/main/apps/usb-hid/usb-hid.man.txt @@ -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 \ No newline at end of file diff --git a/board/miyoo/main/apps/usb-hid/usb-hid.py b/board/miyoo/main/apps/usb-hid/usb-hid.py new file mode 100644 index 00000000..8ab4591c --- /dev/null +++ b/board/miyoo/main/apps/usb-hid/usb-hid.py @@ -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) \ No newline at end of file diff --git a/board/miyoo/main/apps/usb-hid/usb-hid.sh b/board/miyoo/main/apps/usb-hid/usb-hid.sh new file mode 100644 index 00000000..6c98de9e --- /dev/null +++ b/board/miyoo/main/apps/usb-hid/usb-hid.sh @@ -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 diff --git a/board/miyoo/main/gmenu2x/sections/applications/tvoff b/board/miyoo/main/gmenu2x/sections/applications/tvoff deleted file mode 100644 index e5574e46..00000000 --- a/board/miyoo/main/gmenu2x/sections/applications/tvoff +++ /dev/null @@ -1,3 +0,0 @@ -title=TVout OFF -description=TV output disabled -exec=/mnt/apps/tvoff/tvout-off.sh diff --git a/board/miyoo/main/gmenu2x/sections/applications/tvon b/board/miyoo/main/gmenu2x/sections/applications/tvon deleted file mode 100644 index 456fdfda..00000000 --- a/board/miyoo/main/gmenu2x/sections/applications/tvon +++ /dev/null @@ -1,3 +0,0 @@ -title=TVout ON -description=TV output enabled -exec=/mnt/apps/tvon/tvout-on.sh diff --git a/board/miyoo/main/gmenu2x/sections/applications/tvout b/board/miyoo/main/gmenu2x/sections/applications/tvout new file mode 100644 index 00000000..e39f6aa2 --- /dev/null +++ b/board/miyoo/main/gmenu2x/sections/applications/tvout @@ -0,0 +1,3 @@ +title=TVout ON/OFF +description=TV output ON/OFF +exec=/mnt/apps/tvout/tvout.sh diff --git a/board/miyoo/main/gmenu2x/sections/applications/usb-hid b/board/miyoo/main/gmenu2x/sections/applications/usb-hid new file mode 100644 index 00000000..86980a4f --- /dev/null +++ b/board/miyoo/main/gmenu2x/sections/applications/usb-hid @@ -0,0 +1,3 @@ +title=USB HID +description=USB HID PC gamepad +exec=/mnt/apps/usb-hid/usb-hid.sh diff --git a/configs/miyoo_musl_defconfig b/configs/miyoo_musl_defconfig index 2ad9ca76..f1f80b60 100644 --- a/configs/miyoo_musl_defconfig +++ b/configs/miyoo_musl_defconfig @@ -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 diff --git a/configs/miyoo_uclibc_defconfig b/configs/miyoo_uclibc_defconfig index e47894c5..e20972ff 100644 --- a/configs/miyoo_uclibc_defconfig +++ b/configs/miyoo_uclibc_defconfig @@ -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 diff --git a/package/Config.in b/package/Config.in index c19487c9..cdbb99cc 100644 --- a/package/Config.in +++ b/package/Config.in @@ -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" diff --git a/package/python-keyboard/Config.in b/package/python-keyboard/Config.in new file mode 100644 index 00000000..7d5df061 --- /dev/null +++ b/package/python-keyboard/Config.in @@ -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 + diff --git a/package/python-keyboard/python-keyboard.mk b/package/python-keyboard/python-keyboard.mk new file mode 100644 index 00000000..f0bb03c6 --- /dev/null +++ b/package/python-keyboard/python-keyboard.mk @@ -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))