xref: /btstack/port/esp32/create_examples.py (revision f936392b8506e7240b72f4bffe985b4b31a51497)
1#!/usr/bin/env python
2#
3# Create project files for all BTstack embedded examples in local port/esp32 folder
4
5import os
6import shutil
7import sys
8import time
9import subprocess
10
11mk_template = '''#
12# BTstack example 'EXAMPLE' for ESP32 port
13#
14# Generated by TOOL
15# On DATE
16
17PROJECT_NAME := EXAMPLE
18
19include $(IDF_PATH)/make/project.mk
20'''
21
22gatt_update_template = '''#!/usr/bin/env python
23#
24# Update EXAMPLE.h from EXAMPLE.gatt
25import os
26import sys
27
28script_path  = os.path.abspath(os.path.dirname(sys.argv[0]))
29compile_gatt = os.environ['IDF_PATH'] + '/components/btstack/tool/compile_gatt.py'
30print("Creating src/gatt_battery_query.h from src/gatt_battery_query.gatt")
31sys.argv= [compile_gatt, script_path + "/main/EXAMPLE.gatt", script_path + "/main/EXAMPLE.h"]
32exec(open(compile_gatt).read(), globals())
33'''
34
35# get script path
36script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
37
38# path to examples
39examples_embedded = script_path + "/../../example/"
40
41# path to samples
42examples_folder = script_path + "/examples/"
43
44print("Creating examples folder")
45if not os.path.exists(examples_folder):
46    os.makedirs(examples_folder)
47
48print("Creating examples in examples folder")
49
50# iterate over btstack examples
51for file in os.listdir(examples_embedded):
52    if not file.endswith(".c"):
53        continue
54    if file in ['panu_demo.c', 'sco_demo_util.c']:
55        continue
56
57    example = file[:-2]
58    gatt_path = examples_embedded + example + ".gatt"
59
60    # create folder
61    apps_folder = examples_folder + example + "/"
62    if os.path.exists(apps_folder):
63        shutil.rmtree(apps_folder)
64    os.makedirs(apps_folder)
65
66    # copy files
67    for item in ['sdkconfig', 'set_port.sh']:
68        shutil.copyfile(script_path + '/template/' + item, apps_folder + '/' + item)
69
70    # mark set_port.sh as executable
71    os.chmod(apps_folder + '/set_port.sh', 0o755)
72
73    # create Makefile file
74    with open(apps_folder + "Makefile", "wt") as fout:
75        fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c")))
76
77    # create main folder
78    main_folder = apps_folder + "main/"
79    if not os.path.exists(main_folder):
80        os.makedirs(main_folder)
81
82    # copy example file
83    shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c")
84
85    # add sco_demo_util.c for audio examples
86    if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hf_demo']:
87        shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/')
88        shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/')
89
90    # add component.mk file to main folder
91    shutil.copyfile(script_path + '/template/main/component.mk', apps_folder + "/main/component.mk")
92
93    # create update_gatt.sh if .gatt file is present
94    gatt_path = examples_embedded + example + ".gatt"
95    if os.path.exists(gatt_path):
96        shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt")
97        update_gatt_script = apps_folder + "update_gatt_db.py"
98        with open(update_gatt_script, "wt") as fout:
99            fout.write(gatt_update_template.replace("EXAMPLE", example))
100        os.chmod(update_gatt_script, 0o755)
101        subprocess.call(update_gatt_script + "> /dev/null", shell=True)
102        print("- %s including compiled GATT DB" % example)
103    else:
104        print("- %s" % example)
105