xref: /btstack/port/esp32/create_examples.py (revision d6c00b86e82930dc05634eaa5701714ef0f08ec3)
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
34# get script path
35script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
36
37# path to examples
38examples_embedded = script_path + "/../../example/"
39
40# path to samples
41example_folder = script_path + "/example/"
42
43print("Creating examples folder")
44if not os.path.exists(example_folder):
45    os.makedirs(example_folder)
46
47print("Creating examples in examples folder")
48
49# iterate over btstack examples
50for file in os.listdir(examples_embedded):
51    if not file.endswith(".c"):
52        continue
53    if file in ['panu_demo.c', 'sco_demo_util.c']:
54        continue
55
56    example = file[:-2]
57    gatt_path = examples_embedded + example + ".gatt"
58
59    # create folder
60    apps_folder = example_folder + example + "/"
61    if os.path.exists(apps_folder):
62        shutil.rmtree(apps_folder)
63    os.makedirs(apps_folder)
64
65    # copy files
66    for item in ['sdkconfig', 'set_port.sh']:
67        shutil.copyfile(script_path + '/template/' + item, apps_folder + '/' + item)
68
69    # mark set_port.sh as executable
70    os.chmod(apps_folder + '/set_port.sh', 0o755)
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    # create main folder
77    main_folder = apps_folder + "main/"
78    if not os.path.exists(main_folder):
79        os.makedirs(main_folder)
80
81    # copy example file
82    shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c")
83
84    # add sco_demo_util.c for audio examples
85    if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hf_demo']:
86        shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/')
87        shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/')
88
89    # add component.mk file to main folder
90    main_component_mk = apps_folder + "/main/component.mk"
91    shutil.copyfile(script_path + '/template/main/component.mk', main_component_mk)
92
93    # add rules to compile gatt db 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        with open(main_component_mk, "a") as fout:
98            fout.write(component_mk_gatt_add_on.replace("EXAMPLE", example))
99        print("- %s including GATT DB compilation rules" % example)
100    else:
101        print("- %s" % example)
102