-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
90 lines (71 loc) · 2.35 KB
/
utils.py
File metadata and controls
90 lines (71 loc) · 2.35 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import numpy as np
from bitstruct import pack
from PyQt5.QtCore import Qt
import os
# memcpy
def my_memcpy(shared_memory, data):
for index, byte in enumerate(data):
# print(shared_memory.buf, index, byte)
shared_memory.buf[index] = byte
def bitstring_to_bytes(s):
int_list = []
for i in range(0, len(s), 8):
int_list.append(int(s[i:i+8], 2))
return bytearray(int_list)
def ndarray_bool_to_bytes(ndarray_bool: np.ndarray):
ndarray_bytes = ndarray_bool.tobytes()
new_bytes_str = ''
for byte in ndarray_bytes:
if byte:
new_bytes_str += '1'
else:
new_bytes_str += '0'
# print(new_bytes_str)
return bitstring_to_bytes(new_bytes_str)
def bytes_to_ndarray_bool(msg: bytes):
# print(bytearray(msg))
# bit_string = ['{0:08b}'.format(x, 'b') for x in msg]
bit_list = bytearray()
for m in msg:
bits = bytes('{0:08b}'.format(m, 'b'), 'utf-8')
bits_arr = bytearray(bits)
# print(bits, bits_arr)
bit_list += bits_arr
# print(bit_list)
# print(bytes(bit_list))
np_arr = np.array(bit_list, dtype=int)
np_arr = np_arr - 48 # subtracting code for zero (48). 1 is 49, so this works on my machine...
# print(np_arr)
np_bits = np.array(np_arr, dtype=bool)
# print(np_bits)
return np_bits
LOG_FOLDER = os.path.join('logs')
PACK_CODE = 'u4u28'
PACK_CODES = {
'draw': 'u4u10u10u4u4', # msg_type, x, y, draw_line, unused
'color': 'u4u8u8u8u4', # msg_type, r, g, b, a
'size': 'u4u10u18', # msg_type, size (0-1023), unused
'clear': 'u4u28' # msg_type, unused
}
MSG_CODES = {
'draw': 0,
'color': 15,
'size': 3,
'clear': 9
}
def pack_draw(x: int, y: int, draw_line: int):
return pack(PACK_CODES['draw'], MSG_CODES['draw'], x, y, draw_line, 0)
def pack_color(r: int, g: int, b: int, a: int):
return pack(PACK_CODES['color'], MSG_CODES['color'], r, g, b, a)
def pack_size(size: int):
return pack(PACK_CODES['size'], MSG_CODES['size'], size, 0)
def pack_clear():
return pack(PACK_CODES['clear'], MSG_CODES['clear'], 0)
# reference: https://doc.qt.io/qt-6/qt.html
DEFAULT_BG_COLOR = Qt.white
DEFAULT_PEN_COLOR = Qt.black
DEFAULT_BRUSH_SIZE = 3
DEFAULT_BRUSH_STYLE = Qt.SolidPattern
DEFAULT_PEN_STYLE = Qt.SolidLine
DEFAULT_PEN_CAP_STYLE = Qt.RoundCap
DEFAULT_PEN_JOIN_STYLE = Qt.RoundJoin