修一下

This commit is contained in:
2026-01-07 15:30:58 +08:00
parent 296118b408
commit 7d8ae97c25
4 changed files with 79 additions and 81 deletions

View File

@@ -77,8 +77,29 @@ class BspSimplePeripheral(QWidget):
return False
output_path = os.path.join(self.project_path, f"User/bsp/{filename}")
CodeGenerator.save_with_preserve(output_path, template_content) # 使用保留用户区域的写入
# 复制BSP文件夹下的其他额外文件如 README.md, CHANGELOG.md
periph_template_dir = os.path.join(template_base_dir, periph_folder)
if os.path.exists(periph_template_dir) and os.path.isdir(periph_template_dir):
import shutil
files_to_process = list(self.template_names.values())
for item in os.listdir(periph_template_dir):
# 跳过已处理的主要文件
if item in files_to_process:
continue
src_file = os.path.join(periph_template_dir, item)
dst_file = os.path.join(self.project_path, f"User/bsp/{item}")
# 只复制文件,不复制子目录
if os.path.isfile(src_file):
os.makedirs(os.path.dirname(dst_file), exist_ok=True)
shutil.copy2(src_file, dst_file)
print(f"复制BSP额外文件: {item}")
self._save_config()
return True
return True
def _save_config(self):
@@ -405,6 +426,19 @@ class bsp_can(BspPeripheralBase):
get_available_can
)
def _generate_bsp_code_internal(self):
"""重写生成方法直接复制can文件夹中的文件"""
# 检查是否需要生成
if not self.is_need_generate():
for filename in self.template_names.values():
output_path = os.path.join(self.project_path, f"User/bsp/{filename}")
if os.path.exists(output_path):
return "skipped"
return "not_needed"
# 调用父类方法生成配置化的代码(头文件和源文件)
return super()._generate_bsp_code_internal()
def _generate_source_file(self, configs, template_dir):
# 从子文件夹加载模板与_generate_header_file保持一致
periph_folder = self.peripheral_name.lower()
@@ -658,16 +692,16 @@ class bsp_fdcan(BspPeripheralBase):
)
def _generate_bsp_code_internal(self):
"""重写以在生成FDCAN代码后自动复制CAN兼容层"""
"""重写生成方法,调用父类生成FDCAN代码后复制CAN兼容层"""
# 先调用父类方法生成FDCAN代码
result = super()._generate_bsp_code_internal()
if result:
if result and result not in ["skipped", "not_needed"]:
# 成功后复制CAN兼容层文件
self._copy_can_wrapper()
return result
def _copy_can_wrapper(self):
"""复制CAN兼容层文件(can.h和can.c)到项目"""
"""复制CAN兼容层文件(can.h)到项目"""
try:
template_base_dir = CodeGenerator.get_assets_dir("User_code/bsp")
fdcan_folder = os.path.join(template_base_dir, "fdcan")
@@ -676,21 +710,14 @@ class bsp_fdcan(BspPeripheralBase):
# 确保输出目录存在
os.makedirs(bsp_output_dir, exist_ok=True)
# 复制can.h
# 复制can.hCAN兼容层头文件
can_h_src = os.path.join(fdcan_folder, "can.h")
can_h_dst = os.path.join(bsp_output_dir, "can.h")
if os.path.exists(can_h_src):
import shutil
shutil.copy2(can_h_src, can_h_dst)
with open(can_h_src, 'r', encoding='utf-8') as f:
content = f.read()
CodeGenerator.save_with_preserve(can_h_dst, content)
print(f"✓ 已复制CAN兼容层: can.h")
# 复制can.c (如果存在)
can_c_src = os.path.join(fdcan_folder, "can.c")
can_c_dst = os.path.join(bsp_output_dir, "can.c")
if os.path.exists(can_c_src):
import shutil
shutil.copy2(can_c_src, can_c_dst)
print(f"✓ 已复制CAN兼容层: can.c")
except Exception as e:
print(f"复制CAN兼容层文件时出错: {e}")