備忘錄_20160105(定位)
修改
回首頁
程式 2025-10-31 11:39:14 1761881954 100
讓 raspberry pi zero 2 w 變成鍵盤(做成 service)
讓 raspberry pi zero 2 w 變成鍵盤(做成 service)
- [micro usb] : POWER IN,就接電源
- [micro usb] : micro usb 公 - usb a 公 (接 Nintendo Switch 或 PC)
- [mini HDMI] : 空
- [micro sd] : micro sd 卡 (已安裝 raspberry pi os 64bit)
- 請用 ssh 的方式連入 pi zero 2 w 控制
[01]
sudo nano /boot/firmware/config.txt
在最後一行,通常會是 [all] 之後,加入 dtoverlay=dwc2 儲存後,重新開機 (sudo reboot)
[02]
sudo nano /home/pi/Desktop/kb.sh
# kb.sh 內文請參考後面
sudo chmod +x /home/pi/Desktop/kb.sh
[03]
sudo nano /etc/systemd/system/kb.service
# kb.service 內文請參考後面
[04]
sudo systemctl daemon-reload
sudo systemctl enable kb.service # 開機自動執行
sudo systemctl start kb.service # 立即執行測試
sudo systemctl status kb.service # 查看狀態與錯誤
[05]
sudo python test2.py
# test2.py 內文請參考後面
====== kb.sh ======
#!/bin/bash
#modprobe libcomposite
if ! lsmod | grep -q libcomposite; then
modprobe libcomposite
fi
#mount -t configfs none /sys/kernel/config
if ! mountpoint -q /sys/kernel/config; then
mount -t configfs none /sys/kernel/config
fi
G=/sys/kernel/config/usb_gadget/pi_keyboard
mkdir -p $G
echo 0x1d6b > $G/idVendor # Linux Foundation
echo 0x0104 > $G/idProduct # Multifunction Composite Gadget
echo 0x0100 > $G/bcdDevice
echo 0x0200 > $G/bcdUSB
mkdir -p $G/strings/0x409
echo "fedcba9876543210" > $G/strings/0x409/serialnumber
echo "Raspberry Pi" > $G/strings/0x409/manufacturer
echo "Pi Zero Keyboard" > $G/strings/0x409/product
mkdir -p $G/configs/c.1/strings/0x409
echo "Config 1: HID Keyboard" > $G/configs/c.1/strings/0x409/configuration
echo 120 > $G/configs/c.1/MaxPower
mkdir -p $G/functions/hid.usb0
echo 1 > $G/functions/hid.usb0/protocol
echo 1 > $G/functions/hid.usb0/subclass
echo 8 > $G/functions/hid.usb0/report_length
echo -ne \
'\x05\x01\x09\x06\xa1\x01\x05\x07\x19\xe0\x29\xe7\x15\x00\x25\x01\x75\x01\x95\x08\x81\x02\x95\x01\x75\x08\x81\x03\x95\x05\x75\x01\x05\x08\x19\x01\x29\x05\x91\x02\x95\x01\x75\x03\x91\x03\x95\x06\x75\x08\x15\x00\x25\x65\x05\x07\x19\x00\x29\x65\x81\x00\xc0' \
> $G/functions/hid.usb0/report_desc
ln -s $G/functions/hid.usb0 $G/configs/c.1/
#ls /sys/class/udc > $G/UDC
UDC=$(ls /sys/class/udc | head -n1)
if [ -z "$UDC" ]; then
echo "No UDC found, exiting"
exit 1
fi
echo $UDC > $G/UDC
====== kb.service ======
[Unit]
Description=Run keyboard gadget script at boot
After=multi-user.target
[Service]
Type=simple
ExecStart=/home/pi/Desktop/kb.sh
WorkingDirectory=/home/pi/Desktop
User=root
Restart=on-failure
[Install]
WantedBy=multi-user.target
====== test2.py ======
import random
import time
HID_DEVICE = "/dev/hidg0"
keycodes = {
'a': 0x04, 'b': 0x05, 'c': 0x06, 'd': 0x07,
'e': 0x08, 'f': 0x09, 'g': 0x0A, 'h': 0x0B,
'i': 0x0C, 'j': 0x0D, 'k': 0x0E, 'l': 0x0F,
'm': 0x10, 'n': 0x11, 'o': 0x12, 'p': 0x13,
'q': 0x14, 'r': 0x15, 's': 0x16, 't': 0x17,
'u': 0x18, 'v': 0x19, 'w': 0x1A, 'x': 0x1B,
'y': 0x1C, 'z': 0x1D,
'ENTER': 0x28, 'enter': 0x28
}
SHIFT = 0x02 # modifier mask for Shift
def send_key(dev, key, shift=False):
report = bytearray(8)
if shift:
report[0] = SHIFT
report[2] = keycodes[key]
dev.write(report)
dev.flush()
time.sleep(0.02)
dev.write(bytearray(8)) # release
dev.flush()
with open(HID_DEVICE, "wb") as dev:
while True:
key = random.choice(list("abcdefghijklmnopqrstuvwxyz") + ["ENTER"])
shift = key.isupper() # 如果需要大寫
print(key)
send_key(dev, key.lower(), shift)
time.sleep(0.1)