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