-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpre.py
More file actions
66 lines (58 loc) · 3.04 KB
/
pre.py
File metadata and controls
66 lines (58 loc) · 3.04 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
import os
import logging
import random
import time
import subprocess
logger = logging.getLogger("timeflux")
try:
version = subprocess.check_output(["git", "describe", "--tags"]).strip().decode()
logger.info(f"Application version: {version}")
except:
pass
codes = [
'110000000000000000000011000000000000000000000000000000000000001100000000000011000000000000000000000000001100000000000000001100000000',
'001100000000000000000000000011000000000000000011000000000000000000000000000000000000110000000011000000000000000000001100000000000000',
'000011000000000000000000000000000000000000110000000000000000000011000000000000001100000011000000000000000000001100000000000000000000',
'000000110000000000000000000000110000000000000000000000110000000000000011000000000000000000000000000011000000000000000000110000000000',
'000000001100000000000000000000000000110000000000110000000000000000000000110000000000000000001100000000000000000000000000000000001100',
'000000000011000000000000000000001100000000001100000000000000000000001100000000000000000000000000110000000000000000000000000000000011',
'000000000000110000000000000000000000000011000000001100000000000000000000000000110000000000000000000000000000110000110000000000000000',
'000000000000001100000000000000000011000000000000000011000000000000000000000000000000001100000000000000000011000011000000000000000000',
'000000000000000011000000001100000000000000000000000000000011000000000000000000000011000000110000000000000000000000000011000000000000',
'000000000000000000110000110000000000000000000000000000000000110000110000000000000000000000000000000000110000000000000000000011000000',
'000000000000000000001100000000000000001100000000000000001100000000000000001100000000000000000000001100000000000000000000000000110000'
]
def gen_code(length=132, bursts=6, jitter=3, offset=0):
code = ["0"] * length
for frame in range(offset, length, int(length / bursts)):
frame += random.randint(-jitter, jitter)
if frame < 0: frame = 0
if frame > length - 1: frame = length - 1
code[frame] = "1"
code = "".join(code)
return code
def gen_codes(n, length=132, bursts=6, jitter=3):
codes = []
offset = length / bursts / n
for i in range(n):
codes.append(gen_code(length, bursts, jitter, round(offset * i)))
random.shuffle(codes)
return codes
def get_codes(layout, static=False):
if layout == "single":
return codes[0] if static else gen_code(offset=10)
if layout == "simple":
return " ".join(codes[0:5]) if static else " ".join(gen_codes(5))
if layout == "grid":
return " ".join(codes[0:9]) if static else " ".join(gen_codes(9))
if layout == "keyboard":
return " ".join(codes) if static else " ".join(gen_codes(11))
# For reproducibility
seed = int(os.getenv("SEED", time.time_ns()))
logger.info(f"Random seed: {seed}")
random.seed(seed)
# Get or generate codes
static = not(int(os.getenv("DYNAMIC_CODES", "1")))
os.environ["CALIBRATION_CODES"] = get_codes(os.getenv("CALIBRATION_LAYOUT", "single"), static);
os.environ["TASK_CODES"] = get_codes(os.getenv("TASK_LAYOUT", "simple"), static);
logger.debug(os.environ["TASK_CODES"])