-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsrcConverter.py
More file actions
47 lines (41 loc) · 1.69 KB
/
Copy pathsrcConverter.py
File metadata and controls
47 lines (41 loc) · 1.69 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
import requests
input_url = "https://raw.githubusercontent.com/suaveolent/hoymiles-wifi/main/hoymiles_wifi/const.py"
output_file = "include/dtuConst.h"
# Download the input file
response = requests.get(input_url)
if response.status_code == 200:
input_lines = response.text.split('\n')
else:
print(f"Failed to download input file from {input_url}")
exit()
output_lines = []
output_lines.append("// constants converted from python constants file: " + input_url + "\n//")
for line in input_lines:
if '=' in line:
# If the line contains an '=', it might define either a byte sequence or a numeric constant
parts = line.split('=')
name = parts[0].strip()
value = parts[1].strip()
if value.startswith('b"') and value.endswith('"'):
# This is a byte sequence
bytes_str = value.lstrip('b').strip().strip('"')
hex_values = bytes_str.split('\\x')[1:]
# Convert hex values to integers
int_values = [int(value, 16) for value in hex_values]
# Construct Arduino C++ line
output_line = f"const byte {name}[] = {{{','.join([f'0x{value:02x}' for value in int_values])}}};"
output_lines.append(output_line)
elif value.isdigit():
# This is a numeric constant
output_line = f"#define {name} {value}"
output_lines.append(output_line)
else:
# Treat as a comment
output_lines.append(f"// {line}")
else:
# Treat as a comment
output_lines.append(f"// {line}")
# Write output to file
with open(output_file, 'w') as file:
file.write('\n'.join(output_lines))
print(f"Output written to {output_file}")