-
Notifications
You must be signed in to change notification settings - Fork 394
/
validator.py
93 lines (79 loc) · 3.34 KB
/
validator.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
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
91
92
93
# Requires Python 3.7+
import dataclasses
import struct
import sys
import argparse
import datetime
MEMORY_LIMIT = 3500
class ValidationError(Exception):
pass
@dataclasses.dataclass
class ValidationResults:
frame_count: int
step_time: int
duration_s: int
memory_usage: float
def validate(file):
"""Calculates the memory usage of the provided .fseq file"""
magic = file.read(4)
start, minor, major = struct.unpack("<HBB", file.read(4))
file.seek(10)
channel_count, frame_count, step_time = struct.unpack("<IIB", file.read(9))
file.seek(20)
compression_type, = struct.unpack("<B", file.read(1))
if (magic != b'PSEQ') or (start < 24) or (frame_count < 1) or (step_time < 15):
raise ValidationError("Unknown file format, expected FSEQ v2.0")
if channel_count != 48 and channel_count != 200:
raise ValidationError(f"Expected 48 or 200 channels, got {channel_count}")
if compression_type != 0:
raise ValidationError("Expected file format to be V2 Uncompressed")
duration_s = (frame_count * step_time / 1000)
if duration_s > 5*60:
raise ValidationError(f"Expected total duration to be less than 5 minutes, got {datetime.timedelta(seconds=duration_s)}")
if ((minor != 0) and (minor != 2)) or (major != 2):
print("")
print(f"WARNING: FSEQ version is {major}.{minor}. Only version 2.0 and 2.2 have been validated.")
print(f"If the car fails to read this file, download and older version of XLights at https://github.com/smeighan/xLights/releases")
print(f"Please report this message at https://github.com/teslamotors/light-show/issues")
print("")
file.seek(start)
prev_light = None
prev_ramp = None
prev_closure_1 = None
prev_closure_2 = None
count = 0
for frame_i in range(frame_count):
lights = file.read(30)
closures = file.read(16)
file.seek(channel_count - 30 - 16, 1)
light_state = [(b > 127) for b in lights]
ramp_state = [min((((255 - b) if (b > 127) else (b)) // 13 + 1) // 2, 3) for b in lights[:14]]
closure_state = [((b // 32 + 1) // 2) for b in closures]
if light_state != prev_light:
prev_light = light_state
count += 1
if ramp_state != prev_ramp:
prev_ramp = ramp_state
count += 1
if closure_state[:10] != prev_closure_1:
prev_closure_1 = closure_state[:10]
count += 1
if closure_state[10:] != prev_closure_2:
prev_closure_2 = closure_state[10:]
count += 1
return ValidationResults(frame_count, step_time, duration_s, count / MEMORY_LIMIT)
if __name__ == "__main__":
# Expected usage: python3 validator.py lightshow.fseq
parser = argparse.ArgumentParser(description="Validate .fseq file for Tesla Light Show use")
parser.add_argument("file")
args = parser.parse_args()
with open(args.file, "rb") as file:
try:
results = validate(file)
except ValidationError as e:
print(e)
sys.exit(1)
print(f"Found {results.frame_count} frames, step time of {results.step_time} ms for a total duration of {datetime.timedelta(seconds=results.duration_s)}.")
print(f"Used {results.memory_usage*100:.2f}% of the available memory")
if results.memory_usage > 1:
sys.exit(1)