xref: /btstack/port/esp32/create_examples.py (revision 6c099da017fdc786e6cea93a272d1635a8ab2cf5)
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):
35    # path to examples
36    examples_embedded = script_path + "/../../example/"
37
38    # path to samples
39    example_folder = script_path + "/example/"
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']:
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            shutil.copyfile(script_path + '/template/' + item, apps_folder + '/' + item)
66
67        # mark set_port.sh as executable
68        os.chmod(apps_folder + '/set_port.sh', 0o755)
69
70        # create Makefile file
71        with open(apps_folder + "Makefile", "wt") as fout:
72            fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c")))
73
74        # create main folder
75        main_folder = apps_folder + "main/"
76        if not os.path.exists(main_folder):
77            os.makedirs(main_folder)
78
79        # copy example file
80        shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c")
81
82        # add sco_demo_util.c for audio examples
83        if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hf_demo']:
84            shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/')
85            shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/')
86
87        # add component.mk file to main folder
88        main_component_mk = apps_folder + "/main/component.mk"
89        shutil.copyfile(script_path + '/template/main/component.mk', main_component_mk)
90
91        # add rules to compile gatt db if .gatt file is present
92        gatt_path = examples_embedded + example + ".gatt"
93        if os.path.exists(gatt_path):
94            shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt")
95            with open(main_component_mk, "a") as fout:
96                fout.write(component_mk_gatt_add_on.replace("EXAMPLE", example))
97            print("- %s including GATT DB compilation rules" % example)
98        else:
99            print("- %s" % example)
100
101if __name__ == '__main__':
102    # get script path
103    script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
104    create_examples(script_path)
105
106
107