forked from fsievers22/esphome-ble-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_cpp_usage_tables.py
27 lines (25 loc) · 1.23 KB
/
generate_cpp_usage_tables.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
import json
import re
with open("HidUsageTables.json", "r") as hid_usages_file:
data = json.load(hid_usages_file)
usage_pages = data["UsagePages"]
for page in usage_pages:
page_name : str = page["Name"]
page_name = re.sub('[^a-zA-Z0-9]+','_',page_name)
with open(f"./components/ble_client_hid/hid_usages/generated/{page_name.lower()}.h","w") as header_file:
usage_page : str = f'#define USAGE_PAGE_{page_name.upper()} {{\"{page["Name"]}\", {{\\\n'
for usage in page["UsageIds"]:
usage_name = usage["Name"]
usage_name = re.sub('[^a-zA-Z0-9]+', '_', usage_name)
usage_name = usage_name.upper()
usage_kinds : str = ""
for kind in usage["Kinds"]:
kind = kind.upper()
usage_kinds += f"\"{kind}\""
usage_kinds += ","
usage_kinds = usage_kinds[:len(usage_kinds)-1]
header_file.write(f'#define USAGE_{usage_name} \"{usage["Name"]}\"\n')
usage_page += f'\t{{{usage["Id"]}, USAGE_{usage_name}}},\\\n'
header_file.write("\n\n")
header_file.write(usage_page)
header_file.write(f'}}}}\n')