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 18 19include $(IDF_PATH)/make/project.mk 20''' 21 22component_mk_gatt_add_on = ''' 23# app depends on compiled gatt db 24EXAMPLE.o: EXAMPLE.h 25 26# rule to compile gatt db 27EXAMPLE.h: $(COMPONENT_PATH)/EXAMPLE.gatt 28\t$(IDF_PATH)/components/btstack/tool/compile_gatt.py $^ $@ 29 30# remove compiled gatt db on clean 31COMPONENT_EXTRA_CLEAN = EXAMPLE.h 32''' 33 34example_cmake_template = ''' 35# BTstack example 'EXAMPLE' for ESP32 port 36# 37# Generated by TOOL 38# On DATE 39 40# The following lines of boilerplate have to be in your project's 41# CMakeLists in this exact order for cmake to work correctly 42cmake_minimum_required(VERSION 3.5) 43 44include($ENV{IDF_PATH}/tools/cmake/project.cmake) 45project(EXAMPLE) 46''' 47 48main_cmake_template = ''' 49set(COMPONENT_SRCS "EXAMPLE.c") 50set(COMPONENT_ADD_INCLUDEDIRS "") 51register_component() 52''' 53 54 55def create_examples(script_path, suffix): 56 # path to examples 57 examples_embedded = script_path + "/../../example/" 58 59 # path to samples 60 example_folder = script_path + "/example" + suffix + "/" 61 62 print("Creating examples folder") 63 if not os.path.exists(example_folder): 64 os.makedirs(example_folder) 65 66 print("Creating examples in examples folder") 67 68 # iterate over btstack examples 69 for file in os.listdir(examples_embedded): 70 if not file.endswith(".c"): 71 continue 72 if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c']: 73 continue 74 75 example = file[:-2] 76 gatt_path = examples_embedded + example + ".gatt" 77 78 # create folder 79 apps_folder = example_folder + example + "/" 80 if os.path.exists(apps_folder): 81 shutil.rmtree(apps_folder) 82 os.makedirs(apps_folder) 83 84 # copy files 85 for item in ['sdkconfig', 'set_port.sh']: 86 src = script_path + '/template/' + item 87 if item == 'sdkconfig': 88 src = src + suffix 89 dst = apps_folder + '/' + item 90 shutil.copyfile(src, dst) 91 92 # mark set_port.sh as executable 93 os.chmod(apps_folder + '/set_port.sh', 0o755) 94 95 # create Makefile file 96 with open(apps_folder + "Makefile", "wt") as fout: 97 fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 98 99 # create CMakeLists.txt file 100 with open(apps_folder + "CMakeLists.txt", "wt") as fout: 101 fout.write(example_cmake_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 102 103 # create main folder 104 main_folder = apps_folder + "main/" 105 if not os.path.exists(main_folder): 106 os.makedirs(main_folder) 107 108 # copy example file 109 shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c") 110 111 # add sco_demo_util.c for audio examples 112 if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']: 113 shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/') 114 shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/') 115 116 # add component.mk file to main folder 117 main_component_mk = apps_folder + "/main/component.mk" 118 shutil.copyfile(script_path + '/template/main/component.mk', main_component_mk) 119 120 # create CMakeLists.txt file 121 with open(apps_folder + "/main/CMakeLists.txt", "wt") as fout: 122 fout.write(main_cmake_template.replace("EXAMPLE", example)) 123 124 # add rules to compile gatt db if .gatt file is present 125 gatt_path = examples_embedded + example + ".gatt" 126 if os.path.exists(gatt_path): 127 shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt") 128 with open(main_component_mk, "a") as fout: 129 fout.write(component_mk_gatt_add_on.replace("EXAMPLE", example)) 130 print("- %s including GATT DB compilation rules" % example) 131 else: 132 print("- %s" % example) 133 134if __name__ == '__main__': 135 # get script path 136 script_path = os.path.abspath(os.path.dirname(sys.argv[0])) 137 suffix = '' 138 if len(sys.argv) > 1: 139 suffix = sys.argv[1] 140 create_examples(script_path, suffix) 141 142 143