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