color formatting
This commit is contained in:
parent
04e29d7ca4
commit
4de4461947
1 changed files with 35 additions and 4 deletions
39
server.py
39
server.py
|
@ -6,15 +6,31 @@ import base64
|
||||||
|
|
||||||
|
|
||||||
NULL_CHAR = chr(0)
|
NULL_CHAR = chr(0)
|
||||||
|
last_c = None
|
||||||
|
|
||||||
|
colors = {
|
||||||
|
"Z": ["a", "a"],
|
||||||
|
"B": ["a", "b"],
|
||||||
|
"G": ["a", "c"],
|
||||||
|
"b": ["a", "d"],
|
||||||
|
"R": ["a", "e"],
|
||||||
|
"r": ["a", "f"],
|
||||||
|
"Y": ["a", "g"],
|
||||||
|
"W": ["a", "h"]
|
||||||
|
}
|
||||||
|
|
||||||
def write_report(report):
|
def write_report(report):
|
||||||
with open('/dev/hidg0', 'rb+') as fd:
|
with open('/dev/hidg0', 'rb+') as fd:
|
||||||
fd.write(report.encode())
|
fd.write(report.encode())
|
||||||
|
|
||||||
|
|
||||||
def release_keys():
|
def release_keys():
|
||||||
write_report(NULL_CHAR*8)
|
write_report(NULL_CHAR*8)
|
||||||
|
|
||||||
|
|
||||||
def printchar(c):
|
def printchar(c):
|
||||||
|
if last_c == '§':
|
||||||
|
change_color(c)
|
||||||
if c.islower():
|
if c.islower():
|
||||||
write_report(NULL_CHAR*2+chr(4 + ord(c) - ord('a'))+NULL_CHAR*5)
|
write_report(NULL_CHAR*2+chr(4 + ord(c) - ord('a'))+NULL_CHAR*5)
|
||||||
elif c.isupper():
|
elif c.isupper():
|
||||||
|
@ -24,19 +40,33 @@ def printchar(c):
|
||||||
elif c == ' ':
|
elif c == ' ':
|
||||||
write_report(NULL_CHAR*2+chr(44)+NULL_CHAR*5)
|
write_report(NULL_CHAR*2+chr(44)+NULL_CHAR*5)
|
||||||
elif c == '\n':
|
elif c == '\n':
|
||||||
write_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5)
|
write_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5)
|
||||||
else:
|
else:
|
||||||
write_report(chr(32)+NULL_CHAR+chr(0x38)+NULL_CHAR*5)
|
write_report(chr(32)+NULL_CHAR+chr(0x38)+NULL_CHAR*5)
|
||||||
release_keys()
|
release_keys()
|
||||||
|
|
||||||
|
|
||||||
def change_color(background, foreground):
|
def change_color(c: str):
|
||||||
# DELETE keypress
|
if c in colors:
|
||||||
write_report(NULL_CHAR*2+chr(0x2a)+NULL_CHAR*5)
|
# DELETE keypress
|
||||||
|
write_report(NULL_CHAR*2+chr(0x2a)+NULL_CHAR*5)
|
||||||
|
release_keys()
|
||||||
|
write_report(NULL_CHAR*2+chr(4 + ord(colors[0]) - ord('a'))+NULL_CHAR*5)
|
||||||
|
release_keys()
|
||||||
|
write_report(NULL_CHAR*2+chr(4 + ord(colors[1]) - ord('a'))+NULL_CHAR*5)
|
||||||
|
else:
|
||||||
|
write_report(chr(32)+NULL_CHAR+chr(0x38)+NULL_CHAR*5)
|
||||||
|
|
||||||
|
|
||||||
def printstring(s):
|
def printstring(s):
|
||||||
for c in s:
|
for c in s:
|
||||||
printchar(c)
|
printchar(c)
|
||||||
|
last_c = c
|
||||||
|
|
||||||
|
|
||||||
|
def reset_color():
|
||||||
|
change_color('G')
|
||||||
|
release_keys()
|
||||||
|
|
||||||
|
|
||||||
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
|
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
|
||||||
|
@ -59,6 +89,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
|
||||||
printstring('\n' * 32)
|
printstring('\n' * 32)
|
||||||
else:
|
else:
|
||||||
printstring(message + '\n')
|
printstring(message + '\n')
|
||||||
|
reset_color()
|
||||||
|
|
||||||
|
|
||||||
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
|
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
|
||||||
|
|
Loading…
Reference in a new issue