이 영역을 누르면 첫 페이지로 이동
Puter의 잡동사니 블로그의 첫 페이지로 이동

Puter의 잡동사니

페이지 맨 위로 올라가기

Puter의 잡동사니

잡다한 것이 한데 뒤섞인 곳

라즈베리파이 HID

  • 2024.07.29 16:15
  • development
반응형
데스크탑에 연결된 라즈베리파이를 HID 설정으로 키보드처럼 사용할 수 있다.

 


 

준비물

hardware: Raspberry Pi Zero 2 W
os: Raspbian GNU/Linux 11 (bullseye)
+ 기본적인 라즈베리파이 설정 및 ssh, wifi 설정

 


 

설정

 

1. 커널 설정

echo "dtoverlay=dwc2" | sudo tee -a /boot/config.txt
echo "dwc2" | sudo tee -a /etc/modules
echo "libcomposite" | sudo tee -a /etc/modules

 

2. HID 설정 shell 생성 및 실행 권한 부여

sudo touch /usr/bin/enable_hid
sudo chmod +x /usr/bin/enable_hid

 

3. HID 명령어 설정

sudo vi /usr/bin/enable_hid
#!/bin/bash
cd /sys/kernel/config/usb_gadget/
mkdir -p g1
cd g1
echo 0x1d6b > idVendor # Linux Foundation
echo 0x0104 > idProduct # Multifunction Composite Gadget
echo 0x0100 > bcdDevice # v1.0.0
echo 0x0200 > bcdUSB # USB2
mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber
echo "Puter Park" > strings/0x409/manufacturer
echo "Raspberry Pi Zero 2 W" > strings/0x409/product
mkdir -p configs/c.1/strings/0x409
echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration
echo 250 > configs/c.1/MaxPower

# Add functions here
mkdir -p functions/hid.usb0
echo 1 > functions/hid.usb0/protocol
echo 1 > functions/hid.usb0/subclass
echo 8 > 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 > functions/hid.usb0/report_desc
ln -s functions/hid.usb0 configs/c.1/
# End functions

ls /sys/class/udc > UDC

 

4. 부팅 시 HID 설정

sudo vi /etc/rc.local
# exit 0 위에 작성
/usr/bin/enable_hid

 

5. 재기동

sudo reboot

 


 

키보드 매핑

 

이 링크에서 내용을 확인해보면, 키보드 매핑을 위한 보고서 형식은 아래 이미지와 같다.

 

키보드 매핑 보고서 형식
키별 매핑되는 코드

 

위의 데이터를 취합했을 때, 키 코드는 아래와 같은 예시가 된다.

 

키 MODIFIER BYTE RESERVED BYTE KEY CODE 1 KEY CODE 2 KEY CODE 3 KEY CODE 4 KEY CODE 5 KEY CODE 6
q 0 0 20 0 0 0 0 0
w 0 0 26 0 0 0 0 0
e 0 0 8 0 0 0 0 0
r 0 0 21 0 0 0 0 0
t 0 0 23 0 0 0 0 0
y 0 0 28 0 0 0 0 0

 


 

테스트 코드

 

test_keyboard.py

#!/usr/bin/env python3

keymap = {
    "a": bytearray([0, 0, 0x04, 0, 0, 0, 0, 0]),
    "b": bytearray([0, 0, 0x05, 0, 0, 0, 0, 0]),
    "c": bytearray([0, 0, 0x06, 0, 0, 0, 0, 0]),
    "d": bytearray([0, 0, 0x07, 0, 0, 0, 0, 0]),
    "e": bytearray([0, 0, 0x08, 0, 0, 0, 0, 0]),
    "f": bytearray([0, 0, 0x09, 0, 0, 0, 0, 0]),
    "g": bytearray([0, 0, 0x0A, 0, 0, 0, 0, 0]),
    "h": bytearray([0, 0, 0x0B, 0, 0, 0, 0, 0]),
    "i": bytearray([0, 0, 0x0C, 0, 0, 0, 0, 0]),
    "j": bytearray([0, 0, 0x0D, 0, 0, 0, 0, 0]),
    "k": bytearray([0, 0, 0x0E, 0, 0, 0, 0, 0]),
    "l": bytearray([0, 0, 0x0F, 0, 0, 0, 0, 0]),
    "m": bytearray([0, 0, 0x10, 0, 0, 0, 0, 0]),
    "n": bytearray([0, 0, 0x11, 0, 0, 0, 0, 0]),
    "o": bytearray([0, 0, 0x12, 0, 0, 0, 0, 0]),
    "p": bytearray([0, 0, 0x13, 0, 0, 0, 0, 0]),
    "q": bytearray([0, 0, 0x14, 0, 0, 0, 0, 0]),
    "r": bytearray([0, 0, 0x15, 0, 0, 0, 0, 0]),
    "s": bytearray([0, 0, 0x16, 0, 0, 0, 0, 0]),
    "t": bytearray([0, 0, 0x17, 0, 0, 0, 0, 0]),
    "u": bytearray([0, 0, 0x18, 0, 0, 0, 0, 0]),
    "v": bytearray([0, 0, 0x19, 0, 0, 0, 0, 0]),
    "w": bytearray([0, 0, 0x1A, 0, 0, 0, 0, 0]),
    "x": bytearray([0, 0, 0x1B, 0, 0, 0, 0, 0]),
    "y": bytearray([0, 0, 0x1C, 0, 0, 0, 0, 0]),
    "z": bytearray([0, 0, 0x1D, 0, 0, 0, 0, 0]),
    "A": bytearray([0x02, 0, 0x04, 0, 0, 0, 0, 0]),
    "B": bytearray([0x02, 0, 0x05, 0, 0, 0, 0, 0]),
    "C": bytearray([0x02, 0, 0x06, 0, 0, 0, 0, 0]),
    "D": bytearray([0x02, 0, 0x07, 0, 0, 0, 0, 0]),
    "E": bytearray([0x02, 0, 0x08, 0, 0, 0, 0, 0]),
    "F": bytearray([0x02, 0, 0x09, 0, 0, 0, 0, 0]),
    "G": bytearray([0x02, 0, 0x0A, 0, 0, 0, 0, 0]),
    "H": bytearray([0x02, 0, 0x0B, 0, 0, 0, 0, 0]),
    "I": bytearray([0x02, 0, 0x0C, 0, 0, 0, 0, 0]),
    "J": bytearray([0x02, 0, 0x0D, 0, 0, 0, 0, 0]),
    "K": bytearray([0x02, 0, 0x0E, 0, 0, 0, 0, 0]),
    "L": bytearray([0x02, 0, 0x0F, 0, 0, 0, 0, 0]),
    "M": bytearray([0x02, 0, 0x10, 0, 0, 0, 0, 0]),
    "N": bytearray([0x02, 0, 0x11, 0, 0, 0, 0, 0]),
    "O": bytearray([0x02, 0, 0x12, 0, 0, 0, 0, 0]),
    "P": bytearray([0x02, 0, 0x13, 0, 0, 0, 0, 0]),
    "Q": bytearray([0x02, 0, 0x14, 0, 0, 0, 0, 0]),
    "R": bytearray([0x02, 0, 0x15, 0, 0, 0, 0, 0]),
    "S": bytearray([0x02, 0, 0x16, 0, 0, 0, 0, 0]),
    "T": bytearray([0x02, 0, 0x17, 0, 0, 0, 0, 0]),
    "U": bytearray([0x02, 0, 0x18, 0, 0, 0, 0, 0]),
    "V": bytearray([0x02, 0, 0x19, 0, 0, 0, 0, 0]),
    "W": bytearray([0x02, 0, 0x1A, 0, 0, 0, 0, 0]),
    "X": bytearray([0x02, 0, 0x1B, 0, 0, 0, 0, 0]),
    "Y": bytearray([0x02, 0, 0x1C, 0, 0, 0, 0, 0]),
    "Z": bytearray([0x02, 0, 0x1D, 0, 0, 0, 0, 0]),
    "1": bytearray([0, 0, 0x1E, 0, 0, 0, 0, 0]),
    "2": bytearray([0, 0, 0x1F, 0, 0, 0, 0, 0]),
    "3": bytearray([0, 0, 0x20, 0, 0, 0, 0, 0]),
    "4": bytearray([0, 0, 0x21, 0, 0, 0, 0, 0]),
    "5": bytearray([0, 0, 0x22, 0, 0, 0, 0, 0]),
    "6": bytearray([0, 0, 0x23, 0, 0, 0, 0, 0]),
    "7": bytearray([0, 0, 0x24, 0, 0, 0, 0, 0]),
    "8": bytearray([0, 0, 0x25, 0, 0, 0, 0, 0]),
    "9": bytearray([0, 0, 0x26, 0, 0, 0, 0, 0]),
    "0": bytearray([0, 0, 0x27, 0, 0, 0, 0, 0]),
    "!": bytearray([0x02, 0, 0x1E, 0, 0, 0, 0, 0]),
    "#": bytearray([0x02, 0, 0x20, 0, 0, 0, 0, 0]),
    "$": bytearray([0x02, 0, 0x21, 0, 0, 0, 0, 0]),
    "%": bytearray([0x02, 0, 0x22, 0, 0, 0, 0, 0]),
    "&": bytearray([0x02, 0, 0x24, 0, 0, 0, 0, 0]),
    ".": bytearray([0, 0, 0x37, 0, 0, 0, 0, 0]),
    "?": bytearray([0x02, 0, 0x38, 0, 0, 0, 0, 0]),
    "/": bytearray([0, 0, 0x38, 0, 0, 0, 0, 0]),
    "\\": bytearray([0, 0, 0x31, 0, 0, 0, 0, 0]),
    ":": bytearray([0x02, 0, 0x33, 0, 0, 0, 0, 0]),
    "-": bytearray([0, 0, 0x2D, 0, 0, 0, 0, 0]),
    "_": bytearray([0x02, 0, 0x2D, 0, 0, 0, 0, 0]),
    " ": bytearray([0, 0, 0x2C, 0, 0, 0, 0, 0]),
    "~": bytearray([0x02, 0, 0x35, 0, 0, 0, 0, 0]),
    "esc": bytearray([0, 0, 0x29, 0, 0, 0, 0, 0]),
    "win": bytearray([0, 0, 0xE3, 0, 0, 0, 0, 0]),
    "win-r": bytearray([0x08, 0, 0x15, 0, 0, 0, 0, 0]),
    "win-l": bytearray([0x08, 0, 0x0F, 0, 0, 0, 0, 0]),
    "keyup": bytearray([0, 0, 0, 0, 0, 0, 0, 0]),
    "return": bytearray([0, 0, 0x28, 0, 0, 0, 0, 0]),
    "right_arrow": bytearray([0, 0, 0x4F, 0, 0, 0, 0, 0]),
    "left_arrow": bytearray([0, 0, 0x50, 0, 0, 0, 0, 0]),
    "down_arrow": bytearray([0, 0, 0x51, 0, 0, 0, 0, 0]),
    "up_arrow": bytearray([0, 0, 0, 0x52, 0, 0, 0, 0]),
}


def write_report(key):
    try:
        with open("/dev/hidg0", "rb+") as fd:
            fd.write(key)
    except Exception:
        pass


def press_key(key):
    write_report(keymap[key])
    write_report(keymap["keyup"])


press_key("1")
press_key("2")
press_key("3")
press_key("4")
press_key("5")

 


 

끝.

반응형
저작자표시 비영리 동일조건 (새창열림)

'development' 카테고리의 다른 글

postgres primary/replica  (0) 2024.09.08
JOOQ 비관적 잠금  (0) 2024.09.06
PKIX path building failed  (0) 2023.05.01
centos8에서 mariadb 설치 rpm 문제  (0) 2023.05.01
UnsatisfiedDependency  (0) 2022.09.15

댓글

이 글 공유하기

  • 구독하기

    구독하기

  • 카카오톡

    카카오톡

  • 라인

    라인

  • 트위터

    트위터

  • Facebook

    Facebook

  • 카카오스토리

    카카오스토리

  • 밴드

    밴드

  • 네이버 블로그

    네이버 블로그

  • Pocket

    Pocket

  • Evernote

    Evernote

다른 글

  • postgres primary/replica

    postgres primary/replica

    2024.09.08
  • JOOQ 비관적 잠금

    JOOQ 비관적 잠금

    2024.09.06
  • PKIX path building failed

    PKIX path building failed

    2023.05.01
  • centos8에서 mariadb 설치 rpm 문제

    centos8에서 mariadb 설치 rpm 문제

    2023.05.01
다른 글 더 둘러보기

정보

Puter의 잡동사니 블로그의 첫 페이지로 이동

Puter의 잡동사니

  • Puter의 잡동사니의 첫 페이지로 이동

검색

메뉴

  • ALL
  • #TAG

카테고리

  • Puter의 잡동사니 (164)
    • creation (5)
    • tagging (42)
    • product (63)
    • toy (18)
    • game (2)
    • clothes (4)
    • useful (3)
    • development (27)
반응형

정보

Puter의 Puter의 잡동사니

Puter의 잡동사니

Puter

블로그 구독하기

  • 구독하기
  • 네이버 이웃 맺기
  • RSS 피드

나의 외부 링크

  • kakao pay
  • 구글 Search Console
  • 구글 Analytics
  • 네이버 웹마스터 도구
  • 네이버 Analytics

방문자

  • 전체 방문자
  • 오늘
  • 어제
Powered by Tistory / Kakao. Copyright © Puter.

티스토리툴바