Last active
July 12, 2024 17:40
-
-
Save rmed/0d11b7225b3b772bb0dd89108ee93df0 to your computer and use it in GitHub Desktop.
RPi Zero keyboard test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
sleep 15 | |
# Create gadget | |
mkdir /sys/kernel/config/usb_gadget/mykeyboard | |
cd /sys/kernel/config/usb_gadget/mykeyboard | |
# Add basic information | |
echo 0x0100 > bcdDevice # Version 1.0.0 | |
echo 0x0200 > bcdUSB # USB 2.0 | |
echo 0x00 > bDeviceClass | |
echo 0x00 > bDeviceProtocol | |
echo 0x00 > bDeviceSubClass | |
echo 0x08 > bMaxPacketSize0 | |
echo 0x0104 > idProduct # Multifunction Composite Gadget | |
echo 0x1d6b > idVendor # Linux Foundation | |
# Create English locale | |
mkdir strings/0x409 | |
echo "My manufacturer" > strings/0x409/manufacturer | |
echo "My virtual keyboard" > strings/0x409/product | |
echo "0123456789" > strings/0x409/serialnumber | |
# Create HID function | |
mkdir functions/hid.usb0 | |
echo 1 > functions/hid.usb0/protocol | |
echo 8 > functions/hid.usb0/report_length # 8-byte reports | |
echo 1 > functions/hid.usb0/subclass | |
# Write report descriptor | |
echo "05010906a101050719e029e71500250175019508810275089501810175019503050819012903910275019505910175089506150026ff00050719002aff008100c0" | xxd -r -ps > functions/hid.usb0/report_desc | |
# Create configuration | |
mkdir configs/c.1 | |
mkdir configs/c.1/strings/0x409 | |
echo 0x80 > configs/c.1/bmAttributes | |
echo 200 > configs/c.1/MaxPower # 200 mA | |
echo "Example configuration" > configs/c.1/strings/0x409/configuration | |
# Link HID function to configuration | |
ln -s functions/hid.usb0 configs/c.1 | |
# Enable gadget | |
ls /sys/class/udc > UDC | |
sleep 15 | |
/usr/local/bin/tester.sh & | |
# /usr/local/bin/tester.py & |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Python file to test the gadget by writing "Hello World!" | |
# If used with the configfs_test.sh, place this file under /usr/local/bin/tester.py | |
# and uncomment the appropriate line | |
NULL_CHAR = chr(0) | |
def write_report(report): | |
with open('/dev/hidg0', 'rb+') as fd: | |
fd.write(report.encode()) | |
# H (press shift and H) | |
write_report(chr(32)+NULL_CHAR+chr(11)+NULL_CHAR*5) | |
# e | |
write_report(NULL_CHAR*2+chr(8)+NULL_CHAR*5) | |
# ll | |
write_report(NULL_CHAR*2+chr(15)+NULL_CHAR*5) | |
write_report(NULL_CHAR*8) | |
write_report(NULL_CHAR*2+chr(15)+NULL_CHAR*5) | |
# o | |
write_report(NULL_CHAR*2+chr(18)+NULL_CHAR*5) | |
# SPACE | |
write_report(NULL_CHAR*2+chr(44)+NULL_CHAR*5) | |
# W (press shift and W) | |
write_report(chr(32)+NULL_CHAR+chr(26)+NULL_CHAR*5) | |
# o | |
write_report(NULL_CHAR*2+chr(18)+NULL_CHAR*5) | |
# r | |
write_report(NULL_CHAR*2+chr(21)+NULL_CHAR*5) | |
# l | |
write_report(NULL_CHAR*2+chr(15)+NULL_CHAR*5) | |
# d | |
write_report(NULL_CHAR*2+chr(7)+NULL_CHAR*5) | |
# ! (press shift and 1) | |
write_report(chr(32)+NULL_CHAR+chr(30)+NULL_CHAR*5) | |
# Release all keys | |
write_report(NULL_CHAR*8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Bash file to test the gadget by writing "Hello World!" | |
# If used with the configfs_test.sh, place this file under /usr/local/bin/tester.sh | |
# and uncomment the appropriate line | |
function write_report { | |
echo -ne $1 > /dev/hidg0 | |
} | |
# H (press shift and H) | |
write_report "\x20\0\xb\0\0\0\0\0" | |
# e | |
write_report "\0\0\x8\0\0\0\0\0" | |
# ll | |
write_report "\0\0\xf\0\0\0\0\0" | |
write_report "\0\0\0\0\0\0\0\0" | |
write_report "\0\0\xf\0\0\0\0\0" | |
# o | |
write_report "\0\0\x12\0\0\0\0\0" | |
# SPACE | |
write_report "\0\0\x2c\0\0\0\0\0" | |
# W (press shift and W) | |
write_report "\x20\0\x1a\0\0\0\0\0" | |
# o | |
write_report "\0\0\x12\0\0\0\0\0" | |
# r | |
write_report "\0\0\x21\0\0\0\0\0" | |
# l | |
write_report "\0\0\xf\0\0\0\0\0" | |
# d | |
write_report "\0\0\x7\0\0\0\0\0" | |
# ! (press shift and 1) | |
write_report "\x20\0\x1e\0\0\0\0\0" | |
# Release al keys | |
write_report "\0\0\0\0\0\0\0\0" |
any idea how to write code to move the mouse? I have this but I can only move the mouse to the right and down as it's a syntax error to use negative values for the chr?
#!/usr/bin/env python3 NULL_CHAR = chr(0) import time
def ms_write(report): with open("/dev/hidg1", "wb+") as fd: fd.write(report.encode())
ms_write(chr(1)+chr(0)+chr(0)) ms_write(NULL_CHAR*3)
While chr()
is good for simple cases (or those that don't need negative values), I'd say you need to use the int.to_bytes()
method (https://docs.python.org/3/library/stdtypes.html#int.to_bytes), which will allow you to specify that the value to convert is signed and needs to be stored in a single byte.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
any idea how to write code to move the mouse? I have this but I can only move the mouse to the right and down as it's a syntax error to use negative values for the chr?
#!/usr/bin/env python3
NULL_CHAR = chr(0)
import time
def ms_write(report):
with open("/dev/hidg1", "wb+") as fd:
fd.write(report.encode())
ms_write(chr(1)+chr(0)+chr(0))
ms_write(NULL_CHAR*3)