-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRPi_Keyboard_HID.py
36 lines (29 loc) · 1.05 KB
/
RPi_Keyboard_HID.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
#SERVER
#This will be the controller board.
#Via UDP data will be received and processed to do other tasks
import sys
#sys.path.append('/home/pi/modules') #needed to import custom keymap python file
#from kb_map import keymap
import socket
test_string = 'hello there 126547'
NULL_CHAR = chr(0)
UDP_IP = '192.168.1.16' #IP address of the machine to which this script will run
UDP_PORT = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
#Print received data
while True:
data, addr = sock.recvfrom(1024) #buffer size
print ("Received data: ", data)
#HID Keyboard emulation
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
#Function to write to HID file the Usage ID
for c in data:
#write_report(NULL_CHAR*2+keymap[c]+NULL_CHAR*5)
write_report(NULL_CHAR*8) #needed to release the key, otherwise repeated letters won't be printed (ex: literaLLy)
write_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5)
# Release all keys
write_report(NULL_CHAR*8)