open file only once

This commit is contained in:
FKD13 2019-09-06 23:01:11 +02:00
parent 259f8e3506
commit 14b4d15fcd

View file

@ -7,6 +7,7 @@ import base64
NULL_CHAR = chr(0)
file = open('/dev/hidg0', 'rb+')
colors = {
"Z": "a", # Black
@ -28,8 +29,7 @@ colors = {
}
def write_report(report):
with open('/dev/hidg0', 'rb+') as fd:
fd.write(report.encode())
file.write(report.encode())
def release_keys():
@ -52,7 +52,7 @@ def printchar(c):
release_keys()
def change_color(background: str, foreground: str):
def change_color(background, foreground):
if background in colors and foreground in colors:
# DELETE keypress
write_report(NULL_CHAR*2+chr(0x2a)+NULL_CHAR*5)
@ -103,6 +103,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
else:
printstring(message + '\n')
reset_color()
file.flush()
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)