xref: /btstack/port/samv71-xplained-atwilc3000/scripts/create_examples.py (revision 360243be41f47158adff357b9fead2686419a2df)
1#!/usr/bin/env python
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# get script path
30script_path = os.path.abspath(os.path.dirname(sys.argv[0])) + '/../'
31
32# get btstack root
33btstack_root = script_path + '../../'
34
35## pick correct init script based on your hardware
36# - init script for ATWILC300 SHIELD
37
38subprocess.call("make -C ../../chipset/atwilc3000", shell=True)
39
40# path to examples
41examples_embedded = btstack_root + 'example/'
42
43# path to generated example projects
44projects_path = script_path + "example/"
45
46# path to template
47template_path = script_path + 'example/template/'
48
49print("Creating example projects:")
50
51# iterate over btstack examples
52example_files = os.listdir(examples_embedded)
53
54examples = []
55
56for file in example_files:
57    if not file.endswith(".c"):
58        continue
59    if file in ['panu_demo.c', 'sco_demo_util.c']:
60        continue
61    example = file[:-2]
62    examples.append(example)
63
64    # create folder
65    project_folder = projects_path + example + "/"
66    if not os.path.exists(project_folder):
67        os.makedirs(project_folder)
68
69    # check if .gatt file is present
70    gatt_path = examples_embedded + example + ".gatt"
71    gatt_h = ""
72    if os.path.exists(gatt_path):
73        gatt_h = example+'.h'
74
75    # create Makefile
76    shutil.copyfile(template_path + 'Makefile', project_folder + 'Makefile')
77
78    # create upload.cfg
79    with open(project_folder + 'upload.cfg', 'wt') as fout:
80        with open(template_path + 'upload.cfg', 'rt') as fin:
81            for line in fin:
82                if 'flash write_image erase le_counter_flash.elf' in line:
83                    fout.write('flash write_image erase %s_flash.elf\n' % example)
84                    continue
85                fout.write(line)
86
87    # create Makefile
88    with open(project_folder + 'Makefile', 'wt') as fout:
89        with open(template_path + 'Makefile', 'rt') as fin:
90            for line in fin:
91                if 'le_counter.h: ${BTSTACK_ROOT}/example/le_counter.gatt' in line:
92                    fout.write('%s.h: ${BTSTACK_ROOT}/example/%s.gatt\n' % (example,example))
93                    continue
94                if 'all: le_counter.h wilc3000_bt_firmware.c' in line:
95                    if len(gatt_h):
96                        fout.write("all: %s.h wilc3000_bt_firmware.c\n" % example)
97                    else:
98                        fout.write("all: wilc3000_bt_firmware.c\n")
99                    continue
100                fout.write(line)
101
102    # create config.mk
103    with open(project_folder + 'config.mk', 'wt') as fout:
104        with open(template_path + 'config.mk', 'rt') as fin:
105            for line in fin:
106                if 'TARGET_FLASH=le_counter_flash.elf' in line:
107                    fout.write('TARGET_FLASH=%s_flash.elf\n' % example)
108                    continue
109                if 'TARGET_SRAM=le_counter_sram.elf' in line:
110                    fout.write('TARGET_SRAM=%s_sram.elf\n' % example)
111                    continue
112                if 'CSRCS+=${BTSTACK_ROOT_CONFIG}/example/le_counter.c' in line:
113                    fout.write('CSRCS+=${BTSTACK_ROOT_CONFIG}/example/%s.c\n' % example)
114                    continue
115                if 'CSRCS+=${BTSTACK_ROOT_CONFIG}/port/samv71-xplained-atwilc3000/example/template/wilc3000_bt_firmware.c' in line:
116                    fout.write('CSRCS+=${BTSTACK_ROOT_CONFIG}/port/samv71-xplained-atwilc3000/example/%s/wilc3000_bt_firmware.c \\\n' % example)
117                    continue
118                if 'INC_PATH += ${BTSTACK_ROOT_CONFIG}/port/samv71-xplained-atwilc3000/example/template' in line:
119                    fout.write('INC_PATH += ${BTSTACK_ROOT_CONFIG}/port/samv71-xplained-atwilc3000/example/%s\n' % example)
120                    continue
121                fout.write(line)
122
123    print("- %s" % example)
124
125with open(projects_path+'Makefile', 'wt') as fout:
126    fout.write(build_all % ' \\\n'.join(examples))
127
128print("Projects are ready for compile in example folder. See README for details.")
129