修一下

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

1
.gitignore vendored
View File

@ -10,6 +10,7 @@
*.lst
*.ini
*.iex
*.pyc
*.sct
*.scvd
*.uvguix

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,22 +710,15 @@ 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}")

View File

@ -124,6 +124,26 @@ class ComponentSimple(QWidget):
continue
output_path = os.path.join(self.project_path, f"User/component/{filename}")
CodeGenerator.save_with_preserve(output_path, template_content)
# 复制组件文件夹下的其他额外文件
comp_template_dir = os.path.join(template_base_dir, comp_folder)
if os.path.exists(comp_template_dir) and os.path.isdir(comp_template_dir):
import shutil
files_to_process = list(self.template_names.values())
for item in os.listdir(comp_template_dir):
# 跳过已处理的主要文件
if item in files_to_process:
continue
src_file = os.path.join(comp_template_dir, item)
dst_file = os.path.join(self.project_path, f"User/component/{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"复制组件额外文件: {item}")
self._save_config()
return True
@ -271,75 +291,27 @@ class component(QWidget):
if not dep_name.endswith(('.h', '.c', '.hpp', '.cpp')):
components_to_generate.add(dep_name)
# 为没有对应页面但需要生成的依赖组件创建临时页面
# 检查缺失的依赖组件
user_code_dir = CodeGenerator.get_assets_dir("User_code")
missing_dependencies = []
for comp_name in components_to_generate:
if comp_name not in component_pages:
# 创建临时组件页面
template_names = {'header': f'{comp_name}.h', 'source': f'{comp_name}.c'}
temp_page = ComponentSimple(project_path, comp_name.upper(), template_names)
# temp_page.set_forced_enabled(True) # 自动启用依赖组件
component_pages[comp_name] = temp_page
missing_dependencies.append(comp_name)
# 如果没有组件需要生成,返回提示信息
if not components_to_generate:
return "没有启用的组件需要生成代码。"
# 生成代码和依赖文件
# 如果有缺失的依赖组件,提示用户
if missing_dependencies:
missing_msg = f"警告:以下依赖组件未启用,可能导致编译错误:{', '.join(missing_dependencies)}\n请在组件配置中启用这些依赖组件。\n\n"
else:
missing_msg = ""
# 生成代码
success_count = 0
fail_count = 0
fail_list = []
# 处理依赖文件的复制
all_deps = set()
for page in pages:
if hasattr(page, "component_name") and hasattr(page, "is_need_generate"):
if page.is_need_generate():
deps = page.get_enabled_dependencies()
all_deps.update(deps)
# 复制依赖文件
for dep_path in all_deps:
try:
# 检查是否是 bsp 层依赖
if dep_path.startswith('bsp/'):
# 对于 bsp 层依赖,跳过复制,因为这些由 BSP 代码生成负责
print(f"跳过 BSP 层依赖: {dep_path} (由 BSP 代码生成负责)")
continue
# dep_path 格式如 "component/filter" 或 "component/user_math.h"
src_path = os.path.join(user_code_dir, dep_path)
dst_path = os.path.join(project_path, "User", dep_path)
if os.path.isdir(src_path):
# 如果是目录,复制整个目录
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
if os.path.exists(dst_path):
shutil.rmtree(dst_path)
shutil.copytree(src_path, dst_path)
elif os.path.isfile(src_path):
# 如果是文件,复制单个文件并保留用户区域
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
with open(src_path, 'r', encoding='utf-8') as f:
new_content = f.read()
CodeGenerator.save_with_preserve(dst_path, new_content)
else:
# 如果既不是文件也不是目录,跳过
print(f"跳过不存在的依赖: {dep_path}")
continue
success_count += 1
print(f"成功复制依赖: {dep_path}")
except Exception as e:
# 对于 bsp 层依赖的错误,只记录但不计入失败
if dep_path.startswith('bsp/'):
print(f"BSP 层依赖 {dep_path} 复制失败,但忽略此错误: {e}")
else:
fail_count += 1
fail_list.append(f"{dep_path} (依赖复制异常: {e})")
print(f"复制依赖失败: {dep_path}, 错误: {e}")
# 生成组件代码
skipped_count = 0
skipped_list = []
@ -370,8 +342,8 @@ class component(QWidget):
fail_list.append(f"{comp_name} (生成异常: {e})")
print(f"生成组件异常: {comp_name}, 错误: {e}")
total_items = len(all_deps) + len(components_to_generate)
msg = f"组件代码生成完成:总共处理 {total_items} 项,成功生成 {success_count} 项,跳过 {skipped_count} 项,失败 {fail_count} 项。"
total_items = len(components_to_generate)
msg = missing_msg + f"组件代码生成完成:总共处理 {total_items} 项,成功生成 {success_count} 项,跳过 {skipped_count} 项,失败 {fail_count} 项。"
if skipped_list:
msg += f"\n跳过项(文件已存在且未勾选):\n" + "\n".join(skipped_list)
if fail_list:

View File

@ -278,11 +278,11 @@ class DeviceSimple(QWidget):
with open(dst_path, 'w', encoding='utf-8') as f:
f.write(content)
# 复制设备文件夹下的其他文件(如 lcd_lib.h
# 复制设备文件夹下的其他额外文件(如 lcd_lib.h
if os.path.exists(device_template_dir):
import shutil
for item in os.listdir(device_template_dir):
# 跳过已处理的文件
# 跳过已处理的主要文件
if item in files_to_process:
continue
@ -291,11 +291,9 @@ class DeviceSimple(QWidget):
# 只复制文件,不复制子目录
if os.path.isfile(src_file):
# 检查文件是否已存在,避免覆盖
if not os.path.exists(dst_file):
os.makedirs(os.path.dirname(dst_file), exist_ok=True)
shutil.copy2(src_file, dst_file)
print(f"复制额外文件: {dst_file}")
print(f"复制设备额外文件: {item}")
self._save_config()
return True