xref: /btstack/port/renesas-tb-s1ja-cc256x/create_examples.py (revision a8d51f092f1b660d0f6921369ad2bc3f9368296c)
1#!/usr/bin/env python3
2#
3# Create project files for all BTstack embedded examples in local port/renesas-tb-s1ja-cc256x folder
4
5import os
6import shutil
7import sys
8import time
9import subprocess
10
11# use for CC2564B
12# init_script = 'bluetooth_init_cc2564B_1.8_BT_Spec_4.1.c'
13
14# use for CC2564C
15init_script = 'bluetooth_init_cc2564C_1.4.c'
16
17gatt_update_bat_template = '''python.exe BTSTACK_ROOT\\tool\\compile_gatt.py BTSTACK_ROOT\\example\\EXAMPLE.gatt PROJECT_SRC\\EXAMPLE.h
18'''
19
20gatt_update_sh_template = '''#!/bin/sh
21DIR=`dirname $0`
22BTSTACK_ROOT=$DIR/../../../..
23echo "Creating EXAMPLE.h from EXAMPLE.gatt"
24$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/src/EXAMPLE.h
25'''
26
27def fetch_init_script():
28    # print("Fetch CC256x initscript: " + init_script)
29    # subprocess.call("make -f ../../chipset/cc256x/Makefile.inc BTSTACK_ROOT=../.. " + init_script, shell=True)
30    print("Using local CC256x initscript: " + init_script)
31
32def create_folder(path):
33    if os.path.exists(path):
34        shutil.rmtree(path)
35    os.makedirs(path)
36
37def create_examples(port_folder, suffix):
38
39	# btstack root
40    btstack_root = os.path.abspath(port_folder + "/../..")
41
42    # path to examples
43    examples_embedded = btstack_root + "/example/"
44
45    # path to project template
46    project_template = port_folder + "/template/btstack_example/"
47
48    # path to example projects
49    example_folder = port_folder + "/example" + suffix + "/"
50
51
52    print("Creating example folder")
53    create_folder(example_folder)
54
55    print("Creating example projects in example folder")
56
57    # iterate over btstack examples
58    example_files = os.listdir(examples_embedded)
59    example_files.sort()
60    for file in example_files:
61        if not file.endswith(".c"):
62            continue
63        if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c', 'audio_duplex.c', 'mod_player.c']:
64            continue
65        if file in ['a2dp_sink_demo.c', 'a2dp_source_demo.c', 'hfp_hf_demo.c', 'hfp_ag_demo.c', 'hsp_hs_demo.c', 'hsp_ag_demo.c']:
66            continue
67
68        example = file[:-2]
69        gatt_path = examples_embedded + example + ".gatt"
70
71        # create project folder
72        project_folder = example_folder + example + "/"
73        create_folder(project_folder)
74
75        # copy some folders
76        for folder in ['.settings', 'script', 'synergy', 'synergy_cfg']:
77            src = project_template + folder
78            dst = project_folder   + folder
79            shutil.copytree(src, dst)
80
81        # copy some files
82        for file in ['configuration.xml', 'synergy_cfg.txt', 'TB_S1JA.pincfg']:
83            shutil.copy(project_template + file, project_folder)
84
85        # create src folder
86        src_folder = project_folder + 'src/'
87        create_folder(src_folder)
88
89        # copy files skipping example.c and gatt_streamer_server.h
90        for file in ['btstack_config.h', 'hal_entry.c', 'hal_flash_bank_synergy.c',  'hal_flash_bank_synergy.h']:
91            shutil.copy(project_template + "src/" + file, src_folder)
92
93        # copy synergy_gen
94        src = project_template + 'src/synergy_gen'
95        dst = project_folder   + 'src/synergy_gen'
96        shutil.copytree(src, dst)
97
98        # copy example file
99        shutil.copyfile(examples_embedded + example + '.c', src_folder + example + ".c")
100
101        # copy init script
102        shutil.copy(port_folder + "/" + init_script, src_folder)
103
104        # add sco_demo_util.c for audio examples
105        # if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']:
106        #    shutil.copy(examples_embedded + 'sco_demo_util.c', src_folder)
107        #    shutil.copy(examples_embedded + 'sco_demo_util.h', src_folder)
108
109        # update project files
110        for file in ['.project', '.cproject','btstack_example.jdebug']:
111            with open(project_template + file, 'r') as fin:
112                template = fin.read()
113            file = file.replace('btstack_example',example)
114            with open(project_folder + file, 'wt') as fout:
115                template = template.replace("btstack_example", example)
116                fout.write(template)
117
118        # copy jlink and launch files
119        shutil.copy(project_template + 'btstack_example Debug.jlink',  project_folder + example + ' Debug.jlink' )
120        shutil.copy(project_template + 'btstack_example Debug.launch', project_folder + example + ' Debug.launch')
121
122        # generate .h from .gatt
123        gatt_path = examples_embedded + example + ".gatt"
124        if os.path.exists(gatt_path):
125            update_gatt_script_sh  = project_folder + "update_gatt_db.sh"
126            update_gatt_script_bat = project_folder + "update_gatt_db.bat"
127            with open(update_gatt_script_sh, "wt") as fout:
128                fout.write(gatt_update_sh_template.replace("EXAMPLE", example))
129            os.chmod(update_gatt_script_sh, 0o755)
130            with open(update_gatt_script_bat, "wt") as fout:
131                fout.write(gatt_update_bat_template.replace("EXAMPLE", example).replace("BTSTACK_ROOT", btstack_root).replace("PROJECT_SRC", src_folder))
132            if os.name == "nt":
133                subprocess.run(["python.exe", port_folder + "/../../tool/compile_gatt.py", gatt_path, src_folder + example + ".h" ], shell=True, capture_output=True)
134            else:
135	            subprocess.run(update_gatt_script_sh, shell=True, capture_output=True)
136            print("- %s - converting GATT DB" % example)
137        else:
138            print("- %s" % example)
139
140if __name__ == '__main__':
141    # get script path
142    port_folder = os.path.abspath(os.path.dirname(sys.argv[0]))
143    suffix = ''
144    if len(sys.argv) > 1:
145        suffix = sys.argv[1]
146    fetch_init_script()
147    create_examples(port_folder, suffix)
148
149
150