-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
126 lines (99 loc) · 3.38 KB
/
generate.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/which python3
try:
from pyflipper.pyflipper import PyFlipper
except:
PyFlipper = None
print("no flipping library")
import time
from dataclasses import dataclass
# Number of times to broadcast a color change
BROADCAST_TIMES = 5
@dataclass
class ColorCommand:
red: int
green: int
blue: int
brightness: int = 15
delay_time: int = 0
def __post_init__(self):
assert 0 <= self.red <= 15
assert 0 <= self.green <= 15
assert 0 <= self.blue <= 15
assert 0 <= self.brightness <= 15
assert 0 <= self.delay_time <= 0xffffffff
self.red = int(self.red * (self.brightness/15.0))
self.green = int(self.green * (self.brightness/15.0))
self.blue = int(self.blue * (self.brightness/15.0))
def _calc_checksum(self) -> bytes:
checksum_nibble = 5 # has to be 5
assert 0 <= checksum_nibble <= 15
return bytes([((self.red ^ self.green ^ self.blue ^ checksum_nibble) << 4) | checksum_nibble])
def _encode_body(self) -> bytes:
return b'\xaa\xff' + bytes([self.red << 4, self.green << 4, self.blue << 4]) + self._calc_checksum() + b'\x00'
def encode(self) -> bytes:
start_delay = b''
if self.delay_time:
start_delay = b'\xcd' + self.delay_time.to_bytes(4) + b'\xcd'
return start_delay + b'\x80\xcc'.join(self._encode_body() for _ in range(BROADCAST_TIMES))
@dataclass
class ColorCommands:
colors: list[ColorCommand]
def encode(self):
return b'\x50\xcc' + b'\x80\xcc'.join(i.encode() for i in self.colors)
header = '''Filetype: Flipper SubGhz RAW File
Version: 1
Frequency: 433920000
Preset: FuriHalSubGhzPresetOok650Async
Protocol: RAW
'''
input = ColorCommands(
colors=[ColorCommand(red=15, green=0, blue=0, brightness=i, delay_time=250) for i in range(15, 0, -1)] + [ColorCommand(red=15, green=0, blue=0, brightness=i, delay_time=250) for i in range(1, 16)]
).encode()
print(input.hex(' '))
current_byte_is_delay = False
delay = 0
pairs: list[tuple[int, int]] = []
for b in input:
if b == 0xcc:
pairs.append((60, -1600))
continue
elif b == 0xcd:
if current_byte_is_delay:
pairs.append((200, -650*delay))
current_byte_is_delay = False
else:
current_byte_is_delay = True
delay = 0
continue
elif current_byte_is_delay:
delay = (delay << 8) | b
continue
for bit in f'{b:08b}':
if bit == '1':
pairs.append((200, -600))
else:
pairs.append((600, -200))
# 256 pairs per line
for i in range(0, len(pairs), 256):
header += 'RAW_Data: ' + ' '.join(f'{a} {b}' for a,b in pairs[i:i+256]) + '\n'
header += f"# {input.hex(' ')}"
if PyFlipper:
with open('.flipper', 'r') as file:
flippername = file.read().strip()
modem = f"/dev/tty.usbmodemflip_{flippername}1"
flipper = PyFlipper(com=modem)
outputpath = "/ext/subghz/rgb/output.sub"
try:
# In case the file doesn't exist
flipper.storage.remove(file=outputpath)
except:
pass
flipper.storage.write.start(outputpath)
time.sleep(1)
flipper.storage.write.send(header)
time.sleep(1)
flipper.storage.write.stop()
time.sleep(1)
flipper._serial_wrapper.send(f"subghz tx_from_file /ext/subghz/rgb/output.sub 2 0")
with open('captures/output.sub', 'w') as f:
f.write(header)