TAp/server.py

111 lines
3 KiB
Python
Raw Normal View History

2019-08-22 01:26:15 +00:00
#!/usr/bin/env python3
2019-08-22 18:33:57 +00:00
# -*- coding: utf-8 -*-
2019-08-22 01:26:15 +00:00
import string
from http.server import HTTPServer, BaseHTTPRequestHandler
from io import BytesIO
import base64
NULL_CHAR = chr(0)
2019-09-06 21:01:11 +00:00
file = open('/dev/hidg0', 'rb+')
2019-08-22 17:43:25 +00:00
colors = {
2019-08-22 19:45:48 +00:00
"Z": "a", # Black
"B": "b", # Blue
"G": "c", # Green
"M": "d", # Magenta
"R": "e", # Red
"P": "f", # Pink
"Y": "g", # Yellow
"W": "h", # White
2019-08-23 12:16:13 +00:00
"z": "i", # Light/blink Black
"b": "j", # Light/blink Blue
"g": "k", # Light/blink Green
"m": "l", # Light/blink Magenta
"r": "m", # Light/blink Red
"p": "n", # Light/blink Pink
"y": "o", # Light/blink Yellow
"w": "p" # Light/blink White
2019-08-22 17:43:25 +00:00
}
2019-08-22 01:26:15 +00:00
def write_report(report):
2019-09-06 21:01:11 +00:00
file.write(report.encode())
2019-08-22 01:26:15 +00:00
2019-08-22 17:43:25 +00:00
2019-08-22 01:26:15 +00:00
def release_keys():
write_report(NULL_CHAR*8)
2019-08-22 17:43:25 +00:00
2019-08-22 01:26:15 +00:00
def printchar(c):
2019-08-22 18:37:42 +00:00
if c.islower():
2019-08-22 01:26:15 +00:00
write_report(NULL_CHAR*2+chr(4 + ord(c) - ord('a'))+NULL_CHAR*5)
elif c.isupper():
write_report(chr(32)+NULL_CHAR+chr(4 + ord(c) - ord('A'))+NULL_CHAR*5)
elif c.isdigit():
write_report(NULL_CHAR*2+chr(0x1e + ((ord(c) - ord('0') - 1) % 10))+NULL_CHAR*5)
elif c == ' ':
write_report(NULL_CHAR*2+chr(44)+NULL_CHAR*5)
elif c == '\n':
2019-08-22 18:37:42 +00:00
write_report(NULL_CHAR*2+chr(40)+NULL_CHAR*5)
else:
2019-08-22 01:26:15 +00:00
write_report(chr(32)+NULL_CHAR+chr(0x38)+NULL_CHAR*5)
release_keys()
2019-09-06 21:01:11 +00:00
def change_color(background, foreground):
2019-08-23 12:16:13 +00:00
if background in colors and foreground in colors:
2019-08-22 17:43:25 +00:00
# DELETE keypress
write_report(NULL_CHAR*2+chr(0x2a)+NULL_CHAR*5)
release_keys()
2019-08-22 19:45:48 +00:00
write_report(NULL_CHAR*2+chr(4 + ord(colors[background]) - ord('a'))+NULL_CHAR*5)
2019-08-22 17:43:25 +00:00
release_keys()
2019-08-22 19:45:48 +00:00
write_report(NULL_CHAR*2+chr(4 + ord(colors[foreground]) - ord('a'))+NULL_CHAR*5)
2019-08-22 17:43:25 +00:00
else:
2019-08-23 12:16:13 +00:00
print('Malformed Color code: §' + background + foreground)
2019-08-22 17:43:25 +00:00
write_report(chr(32)+NULL_CHAR+chr(0x38)+NULL_CHAR*5)
2019-08-22 18:37:42 +00:00
release_keys()
2019-08-22 17:43:25 +00:00
2019-08-22 01:26:15 +00:00
def printstring(s):
2019-08-22 18:37:42 +00:00
last_c = ''
2019-08-22 19:45:48 +00:00
last_last_c = ''
2019-08-22 01:26:15 +00:00
for c in s:
2019-08-22 19:45:48 +00:00
if last_last_c == '§':
change_color(last_c, c)
elif c != '§' and last_c != '§':
2019-08-22 18:37:42 +00:00
printchar(c)
2019-08-22 19:45:48 +00:00
last_c, last_last_c = c, last_c
2019-08-22 17:43:25 +00:00
def reset_color():
2019-08-22 19:45:48 +00:00
change_color('Z', 'g')
2019-08-22 17:43:25 +00:00
release_keys()
2019-08-22 01:26:15 +00:00
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write(b'Hello, world!')
def do_POST(self):
print(self.headers)
2019-08-22 17:45:52 +00:00
message = base64.b64decode(self.headers['X-Messages']).decode('utf8')
2019-08-22 01:26:15 +00:00
self.send_response(200)
self.end_headers()
response = BytesIO()
response.write(b'This is POST request. ')
response.write(b'Received: ')
self.wfile.write(response.getvalue())
if message == 'reset':
printstring('\n' * 32)
else:
printstring(message + '\n')
2019-08-22 17:43:25 +00:00
reset_color()
2019-09-06 21:01:11 +00:00
file.flush()
2019-08-22 01:26:15 +00:00
httpd = HTTPServer(('0.0.0.0', 8000), SimpleHTTPRequestHandler)
httpd.serve_forever()