-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tool.py
57 lines (40 loc) · 1.78 KB
/
tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from socket import socket
from time import sleep
from typing import List
from goodix import (FLAGS_TRANSPORT_LAYER_SECURITY, Device, check_message_pack,
encode_message_pack)
def warning(text: str) -> str:
decorator = "#" * len(max(text.split("\n"), key=len))
return f"\033[31;5m{decorator}\n{text}\n{decorator}\033[0m"
def connect_device(device: Device, tls_client: socket) -> None:
tls_client.sendall(device.request_tls_connection())
device.protocol.write(
encode_message_pack(tls_client.recv(1024),
FLAGS_TRANSPORT_LAYER_SECURITY))
tls_client.sendall(
check_message_pack(device.protocol.read(),
FLAGS_TRANSPORT_LAYER_SECURITY))
tls_client.sendall(
check_message_pack(device.protocol.read(),
FLAGS_TRANSPORT_LAYER_SECURITY))
tls_client.sendall(
check_message_pack(device.protocol.read(),
FLAGS_TRANSPORT_LAYER_SECURITY))
device.protocol.write(
encode_message_pack(tls_client.recv(1024),
FLAGS_TRANSPORT_LAYER_SECURITY))
sleep(0.01) # Important otherwise an USBTimeout error occur
def decode_image(data: bytes) -> List[int]:
image = []
for i in range(0, len(data), 6):
chunk = data[i:i + 6]
image.append(((chunk[0] & 0xf) << 8) + chunk[1])
image.append((chunk[3] << 4) + (chunk[0] >> 4))
image.append(((chunk[5] & 0xf) << 8) + chunk[2])
image.append((chunk[4] << 4) + (chunk[5] >> 4))
return image
def write_pgm(image: List[int], width: int, height: int, filename: str) -> None:
file = open(filename, "w")
file.write(f"P2\n{height} {width}\n4095\n")
file.write("\n".join(map(str, image)))
file.close()