xref: /btstack/port/samv71-xplained-atwilc3000/scripts/create_examples.py (revision 6ccd8248590f666db07dd7add13fecb4f5664fb5)
1#!/usr/bin/env python3
2#
3# Create project files for all BTstack embedded examples in WICED/apps/btstack
4
5import os
6import re
7import shutil
8import subprocess
9import sys
10
11# build all template
12build_all = '''
13SUBDIRS =  \\
14%s
15
16all:
17\techo Building all examples
18\tfor dir in $(SUBDIRS); do \\
19\t$(MAKE) -C $$dir || exit 1; \\
20\tdone
21
22clean:
23\techo Cleaning all ports
24\tfor dir in $(SUBDIRS); do \\
25\t$(MAKE) -C $$dir clean; \\
26\tdone
27'''
28
29#
30le_examples = [
31'ancs_client_demo.c',
32'gap_le_advertisements.c',
33'gatt_battery_query.c',
34'gatt_browser.c',
35'hog_keyboard_demo.c',
36'hog_mouse_demo.c',
37'gatt_counter.c',
38'gatt_streamer.c',
39'le_streamer_client.c',
40'led_counter.c',
41'sm_pairing_central.c',
42'sm_pairing_peripheral.c',
43]
44
45# get script path
46script_path = os.path.abspath(os.path.dirname(sys.argv[0])) + '/../'
47
48# get btstack root
49btstack_root = script_path + '../../'
50
51# path to examples
52examples_embedded = btstack_root + 'example/'
53
54# path to generated example projects
55projects_path = script_path + "example/"
56
57# path to template
58template_path = script_path + 'example/template/'
59
60print("Creating example projects:")
61
62# iterate over btstack examples
63example_files = os.listdir(examples_embedded)
64
65examples = []
66
67for file in example_files:
68    if not file.endswith(".c"):
69        continue
70    if not file in le_examples:
71        continue
72    example = file[:-2]
73    examples.append(example)
74
75    # create folder
76    project_folder = projects_path + example + "/"
77    if not os.path.exists(project_folder):
78        os.makedirs(project_folder)
79
80    # check if .gatt file is present
81    gatt_path = examples_embedded + example + ".gatt"
82    gatt_h = ""
83    if os.path.exists(gatt_path):
84        gatt_h = example+'.h'
85
86    # create Makefile
87    shutil.copyfile(template_path + 'Makefile', project_folder + 'Makefile')
88
89    # create upload.cfg
90    with open(project_folder + 'upload.cfg', 'wt') as fout:
91        with open(template_path + 'upload.cfg', 'rt') as fin:
92            for line in fin:
93                if 'flash write_image erase le_counter_flash.elf' in line:
94                    fout.write('flash write_image erase %s_flash.elf\n' % example)
95                    continue
96                fout.write(line)
97
98    # create Makefile
99    with open(project_folder + 'Makefile', 'wt') as fout:
100        with open(template_path + 'Makefile', 'rt') as fin:
101            for line in fin:
102                if 'le_counter.h: ${BTSTACK_ROOT}/example/le_counter.gatt' in line:
103                    fout.write('%s.h: ${BTSTACK_ROOT}/example/%s.gatt\n' % (example,example))
104                    continue
105                if 'all: le_counter.h wilc3000_ble_firmware.h' in line:
106                    if len(gatt_h):
107                        fout.write("all: %s.h wilc3000_ble_firmware.h\n" % example)
108                    else:
109                        fout.write("all: wilc3000_ble_firmware.h\n")
110                    continue
111                fout.write(line)
112
113    # create config.mk
114    with open(project_folder + 'config.mk', 'wt') as fout:
115        with open(template_path + 'config.mk', 'rt') as fin:
116            for line in fin:
117                if 'TARGET_FLASH=le_counter_flash.elf' in line:
118                    fout.write('TARGET_FLASH=%s_flash.elf\n' % example)
119                    continue
120                if 'TARGET_SRAM=le_counter_sram.elf' in line:
121                    fout.write('TARGET_SRAM=%s_sram.elf\n' % example)
122                    continue
123                if 'CSRCS+=${BTSTACK_ROOT_CONFIG}/example/le_counter.c' in line:
124                    fout.write('CSRCS+=${BTSTACK_ROOT_CONFIG}/example/%s.c\n' % example)
125                    continue
126                if 'INC_PATH += ${BTSTACK_ROOT_CONFIG}/port/samv71-xplained-atwilc3000/example/template' in line:
127                    fout.write('INC_PATH += ${BTSTACK_ROOT_CONFIG}/port/samv71-xplained-atwilc3000/example/%s\n' % example)
128                    continue
129                fout.write(line)
130
131    print("- %s" % example)
132
133with open(projects_path+'Makefile', 'wt') as fout:
134    fout.write(build_all % ' \\\n'.join(examples))
135
136print("Projects are ready for compile in example folder. See README for details.")
137