-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.py
56 lines (36 loc) · 1.22 KB
/
encoder.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
#!/usr/bin/python3
import zlib
# Byte structure: based on the num_lights we need to create a list of lists frames = [[RGB values for num_ligts]]
# RGB values will be encoded via chunks of 3 bytes
# So the size of the individual arrays will be of length 3 * num_lights
class Encoder:
def __init__(self, num_lights, num_colors):
self.num_lights = num_lights
self.num_colors = num_colors
def int_arr_to_byte_arr(self, frame):
if max(frame) > 255:
return None
return bytearray(frame)
def byte_arr_to_frame(self, byte_arr):
frame = []
for byte in range(0, len(byte_arr), 3):
r = byte_arr[byte]
g = byte_arr[byte+1]
b = byte_arr[byte+2]
color = (r, g, b)
frame.append(color)
return frame
def flatten(self, arr):
return [i for sub in arr for i in sub]
def encode(self, frames):
byte_arrs = []
frames = self.flatten(self.flatten(frames))
byte_arr = self.int_arr_to_byte_arr(frames)
return zlib.compress(byte_arr)
def decode(self, compressed):
frames = []
byte_arr = zlib.decompress(compressed)
num_bytes_in_frame = self.num_lights*3
for i in range(0, len(byte_arr), num_bytes_in_frame):
frames.append(self.byte_arr_to_frame(byte_arr[i:i+num_bytes_in_frame]))
return frames