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