Skip to content

Commit

Permalink
Add handle usb-hid gadget
Browse files Browse the repository at this point in the history
Tunr your handheld into usb gamepad for PC
  • Loading branch information
tiopex committed Nov 23, 2023
1 parent b7a8ba5 commit 719a756
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 15 deletions.
4 changes: 0 additions & 4 deletions board/miyoo/main/apps/tvoff/tvout-off.sh

This file was deleted.

5 changes: 0 additions & 5 deletions board/miyoo/main/apps/tvon/tvout-on.sh

This file was deleted.

9 changes: 9 additions & 0 deletions board/miyoo/main/apps/tvout/tvout.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/busybox sh
FILE=/mnt/tvout
if [ -f "$FILE" ]; then
rm "$FILE"
else
touch "$FILE"
fi
sync
reboot
43 changes: 43 additions & 0 deletions board/miyoo/main/apps/usb-hid/usb-hid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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:29, #b mapped to z
56:27, #a mapped to x
57:4, #y mapped to a
42:22, #x mapped to s
1:42, #select mapped to backspace
28:40, #start mapped to enter
15:20, #lpad1 mapped to q
14:26, #rpad1 mapped to w
104:75, #lpad2 mapped to pg_up
109:78, #rpad2 mapped to pg_down
97:0, #reset not mapped, used to quit
}

NULL_CHAR = chr(0)
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):
line = ''.join(chr(translate(code,hidCodeDict)) for code in keyboard._pressed_events)
if line == '':
write_report(NULL_CHAR*8)
return
print('\r', end='')
write_report(NULL_CHAR*2+line+NULL_CHAR*5)

print("Press RESET button to quit")
keyboard.hook(write_pressed_keys)
keyboard.wait(97)
12 changes: 12 additions & 0 deletions board/miyoo/main/apps/usb-hid/usb-hid.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/busybox sh
if [ `cat /sys/devices/platform/soc/1c13000.usb/musb-hdrc.1.auto/mode` != "b_peripheral" ]; then
echo -e "\e[31mFirst connect handheld to device!\e[0m"&>$(tty)
sleep 2
exit
fi
gadget-vid-pid-remove 0x1d6b:0x0104
gadget-hid
echo -e "\e[32mStarting USB HID mode...\e[0m"&>$(tty)
python /mnt/apps/usb-hid/usb-hid.py&>$(tty)
gadget-vid-pid-remove 0x1d6b:0x0104
gadget-ms /dev/mmcblk0p1 /dev/mmcblk0p4
3 changes: 0 additions & 3 deletions board/miyoo/main/gmenu2x/sections/applications/tvoff

This file was deleted.

3 changes: 0 additions & 3 deletions board/miyoo/main/gmenu2x/sections/applications/tvon

This file was deleted.

3 changes: 3 additions & 0 deletions board/miyoo/main/gmenu2x/sections/applications/tvout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title=TVout ON/OFF
description=TV output ON/OFF
exec=/mnt/apps/tvout/tvout.sh
3 changes: 3 additions & 0 deletions board/miyoo/main/gmenu2x/sections/applications/usb-hid
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title=USB HID
description=USB HID PC gamepad
exec=/mnt/apps/usb-hid/usb-hid.sh
1 change: 1 addition & 0 deletions configs/miyoo_musl_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,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
Expand Down
1 change: 1 addition & 0 deletions configs/miyoo_uclibc_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,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
Expand Down
1 change: 1 addition & 0 deletions package/Config.in
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions package/python-keyboard/Config.in
Original file line number Diff line number Diff line change
@@ -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

17 changes: 17 additions & 0 deletions package/python-keyboard/python-keyboard.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
################################################################################
#
# python-keyboard
#
################################################################################

PYTHON_KEYBOARD_VERSION = 0.13.5
PYTHON_KEYBOARD_SOURCE = keyboard-$(PYTHON_KEYBOARD_VERSION).zip
PYTHON_KEYBOARD_SITE = https://files.pythonhosted.org/packages/79/75/c969f2258e908c39aadfc57d1cb78247dc49e6d36371bb3a48c194640c01
PYTHON_KEYBOARD_LICENSE = MIT
PYTHON_KEYBOARD_LICENSE_FILES = LICENSE
PYTHON_KEYBOARD_SETUP_TYPE = setuptools
define PYTHON_KEYBOARD_EXTRACT_CMDS
$(UNZIP) -d $(@D) $(PYTHON_KEYBOARD_DL_DIR)/$(PYTHON_KEYBOARD_SOURCE)
yes | cp -rf $(@D)/keyboard-0.13.5/* $(@D)/
endef
$(eval $(python-package))

0 comments on commit 719a756

Please sign in to comment.