-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress_dictionary.py
40 lines (33 loc) · 1.01 KB
/
compress_dictionary.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
import zlib
filename = "./src/config_dictionary.json"
outfile = "./src/compressed_dict.h"
with open(filename,"r") as f:
string = ""
for line in f.readlines():
string+=line.lstrip().rstrip()
print(string)
print(f'uncompressed length: {len(string)}')
compressed = zlib.compress(string.encode())
print(f'compressed length: {len(compressed)}')
byte_string = ""
for b in compressed:
byte_string+=f"{hex(b)}, "
decl_string = f"#define CONFIG_DICT_LENGTH {len(compressed)}\n#define CONFIG_DICT const uint8_t config[CONFIG_DICT_LENGTH] = {{{byte_string.strip(', ')}}};\n\n"
header = \
'''\
//Do not edit this file
//This file is machine generated by compress_dictionary.py
//Any changes made here will be lost
//Make any changes in either the compress_dictionary.py script, config_dictionary.json, or KlipperCommander.h
#ifndef _COMPRESSED_DICT
#define _COMPRESSED_DICT
#include "Arduino.h"\n
'''
footer = \
'''\
#endif
'''
with open(outfile, 'w') as f:
f.write(header)
f.write(decl_string)
f.write(footer)