xref: /btstack/port/esp32/create_examples.py (revision adaba9f34040c696f6e8e6f1096f218bf6ceb78e)
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']:
59        shutil.copyfile(script_path + '/template/' + item, apps_folder + '/' + item)
60
61    # create Makefile file
62    with open(apps_folder + "Makefile", "wt") as fout:
63        fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c")))
64
65    # copy components folder
66    shutil.copytree(script_path + '/template/components', apps_folder + '/components')
67
68    # create main folder
69    main_folder = apps_folder + "main/"
70    if not os.path.exists(main_folder):
71        os.makedirs(main_folder)
72
73    # copy example file
74    shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c")
75
76    # add sco_demo_util.c for audio examples
77    if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hf_demo']:
78        shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/')
79        shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/')
80
81    # add component.mk file to main folder
82    shutil.copyfile(script_path + '/template/main/component.mk', apps_folder + "/main/component.mk")
83
84    # create update_gatt.sh if .gatt file is present
85    gatt_path = examples_embedded + example + ".gatt"
86    if os.path.exists(gatt_path):
87        update_gatt_script = apps_folder + "update_gatt_db.sh"
88        with open(update_gatt_script, "wt") as fout:
89            fout.write(gatt_update_template.replace("EXAMPLE", example))
90        os.chmod(update_gatt_script, 0o755)
91        subprocess.call(update_gatt_script + "> /dev/null", shell=True)
92        print("- %s including compiled GATT DB" % example)
93    else:
94        print("- %s" % example)
95
96