xref: /btstack/port/esp32/create_examples.py (revision 73cbd4d97f92d480088752e70324cf243f97d616)
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
22component_mk_gatt_add_on = '''
23# app depends on compiled gatt db
24EXAMPLE.o: EXAMPLE.h
25
26# rule to compile gatt db
27EXAMPLE.h: $(COMPONENT_PATH)/EXAMPLE.gatt
28\t$(IDF_PATH)/components/btstack/tool/compile_gatt.py $^ $@
29
30# remove compiled gatt db on clean
31COMPONENT_EXTRA_CLEAN = EXAMPLE.h
32'''
33
34def create_examples(script_path, suffix):
35    # path to examples
36    examples_embedded = script_path + "/../../example/"
37
38    # path to samples
39    example_folder = script_path + "/example" + suffix + "/"
40
41    print("Creating examples folder")
42    if not os.path.exists(example_folder):
43        os.makedirs(example_folder)
44
45    print("Creating examples in examples folder")
46
47    # iterate over btstack examples
48    for file in os.listdir(examples_embedded):
49        if not file.endswith(".c"):
50            continue
51        if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c']:
52            continue
53
54        example = file[:-2]
55        gatt_path = examples_embedded + example + ".gatt"
56
57        # create folder
58        apps_folder = example_folder + example + "/"
59        if os.path.exists(apps_folder):
60            shutil.rmtree(apps_folder)
61        os.makedirs(apps_folder)
62
63        # copy files
64        for item in ['sdkconfig', 'set_port.sh']:
65            src = script_path + '/template/' + item
66            if item == 'sdkconfig':
67                src = src + suffix
68            dst = apps_folder + '/' + item
69            shutil.copyfile(src, dst)
70
71        # mark set_port.sh as executable
72        os.chmod(apps_folder + '/set_port.sh', 0o755)
73
74        # create Makefile file
75        with open(apps_folder + "Makefile", "wt") as fout:
76            fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c")))
77
78        # create main folder
79        main_folder = apps_folder + "main/"
80        if not os.path.exists(main_folder):
81            os.makedirs(main_folder)
82
83        # copy example file
84        shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c")
85
86        # add sco_demo_util.c for audio examples
87        if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']:
88            shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/')
89            shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/')
90
91        # add component.mk file to main folder
92        main_component_mk = apps_folder + "/main/component.mk"
93        shutil.copyfile(script_path + '/template/main/component.mk', main_component_mk)
94
95        # add rules to compile gatt db if .gatt file is present
96        gatt_path = examples_embedded + example + ".gatt"
97        if os.path.exists(gatt_path):
98            shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt")
99            with open(main_component_mk, "a") as fout:
100                fout.write(component_mk_gatt_add_on.replace("EXAMPLE", example))
101            print("- %s including GATT DB compilation rules" % example)
102        else:
103            print("- %s" % example)
104
105if __name__ == '__main__':
106    # get script path
107    script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
108    suffix = ''
109    if len(sys.argv) > 1:
110        suffix = sys.argv[1]
111    create_examples(script_path, suffix)
112
113
114