Skip to content

Instantly share code, notes, and snippets.

@pinscript
Created May 24, 2019 19:59
Show Gist options
  • Save pinscript/119e5a12965d50e9b4f3c39bbc67469a to your computer and use it in GitHub Desktop.
Save pinscript/119e5a12965d50e9b4f3c39bbc67469a to your computer and use it in GitHub Desktop.
Flask application to write the specified value to a HID device (RPI Zero in my case)
import time
import sys
from flask import Flask
from flask import request
NULL_CHAR = chr(0)
EOL_SEQ = NULL_CHAR*5
RELEASE_SEQ = NULL_CHAR*8
KEYMAP = {
'a': 0x04, 'A': 0x04, # Keyboard a and A
'b': 0x05, 'B': 0x05, # Keyboard b and B
'c': 0x06, 'C': 0x06, # Keyboard c and C
'd': 0x07, 'D': 0x07, # Keyboard d and D
'e': 0x08, 'E': 0x08, # Keyboard e and E
'f': 0x09, 'F': 0x09, # Keyboard f and F
'g': 0x0a, 'G': 0x0a, # Keyboard g and G
'h': 0x0b, 'H': 0x0b, # Keyboard h and H
'i': 0x0c, 'I': 0x0c, # Keyboard i and I
'j': 0x0d, 'J': 0x0d, # Keyboard j and J
'k': 0x0e, 'K': 0x0e, # Keyboard k and K
'l': 0x0f, 'L': 0x0f, # Keyboard l and L
'm': 0x10, 'M': 0x10, # Keyboard m and M
'n': 0x11, 'N': 0x11, # Keyboard n and N
'o': 0x12, 'O': 0x12, # Keyboard o and O
'p': 0x13, 'P': 0x13, # Keyboard p and P
'q': 0x14, 'Q': 0x14, # Keyboard q and Q
'r': 0x15, 'R': 0x15, # Keyboard r and R
's': 0x16, 'S': 0x16, # Keyboard s and S
't': 0x17, 'T': 0x17, # Keyboard t and T
'u': 0x18, 'U': 0x18, # Keyboard u and U
'v': 0x19, 'V': 0x19, # Keyboard v and V
'w': 0x1a, 'W': 0x1a, # Keyboard w and W
'x': 0x1b, 'X': 0x1b, # Keyboard x and X
'y': 0x1c, 'Y': 0x1c, # Keyboard y and Y
'z': 0x1d, 'Z': 0x1d, # Keyboard z and Z
'1': 0x1e, '!': 0x1e, # Keyboard 1 and !
'2': 0x1f, '@': 0x1f, # Keyboard 2 and @
'3': 0x20, '#': 0x20, # Keyboard 3 and #
'4': 0x21, '$': 0x21, # Keyboard 4 and $
'5': 0x22, '%': 0x22, # Keyboard 5 and %
'6': 0x23, '^': 0x23, # Keyboard 6 and ^
'7': 0x24, '&': 0x24, # Keyboard 7 and &
'8': 0x25, '*': 0x25, # Keyboard 8 and *
'9': 0x26, '(': 0x26, # Keyboard 9 and (
'0': 0x27, ')': 0x27, # Keyboard 0 and )
'ENTER': 0x28, # Keyboard Return (ENTER)
'ESC': 0x29, # Keyboard ESCAPE
'BACKSPACE': 0x2a, # Keyboard DELETE (Backspace)
'TAB': 0x2b, # Keyboard Tab
'SPACE': 0x2c, ' ': 0x2c, # Keyboard Spacebar
'MINUS': 0x2d, '-': 0x2d,# Keyboard - and _
'EQUAL': 0x2e, '=': 0x2e, # Keyboard = and +
'LEFTBRACE': 0x2f, # Keyboard [ and {
'RIGHTBRACE': 0x30, # Keyboard ] and }
'BACKSLASH': 0x31, # Keyboard \ and |
'HASHTILDE': 0x32, # Keyboard Non-US # and ~
'SEMICOLON': 0x33, # Keyboard ; and :
'APOSTROPHE': 0x34, # Keyboard ' and "
'GRAVE': 0x35, # Keyboard ` and ~
'COMMA': 0x36, # Keyboard , and <
'DOT': 0x37, '.': 0x37, # Keyboard . and >
'SLASH': 0x38, '/': 0x38, # Keyboard / and ?
'CAPSLOCK': 0x39, # Keyboard Caps Lock
}
# Get the keyboard ASCII character for the provided character.
def get_chr(input):
if input in KEYMAP:
return chr(KEYMAP[input])
return None
# Get the key sequence for a string.
def get_seq(value):
result = ""
for v in value:
k = v
if(v.isupper()):
result += chr(32)+NULL_CHAR # Uppercase character, press shift
k = k.lower()
else:
result += NULL_CHAR*2
val = get_chr(k)
result += val
result += EOL_SEQ
return result
# Write the specified sequence to the HID.
def write_hid(seq):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(seq.encode())
app = Flask(__name__)
@app.route("/")
def index():
msg = request.args.get('msg')
seq = get_seq(msg)
if request.args.get('crlf'):
seq += NULL_CHAR*2 + chr(0x28) + EOL_SEQ
write_hid(seq + RELEASE_SEQ)
return msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment