mirror of
https://github.com/goldenfishs/MRobot.git
synced 2026-03-22 08:23:57 +08:00
重构User_code目录结构:将文件组织到子文件夹中
主要更改: - 将所有BSP外设文件移动到独立子文件夹(can/, fdcan/, uart/等) - 将所有Component文件移动到独立子文件夹(pid/, filter/, cmd/等) - 将所有Device文件移动到独立子文件夹(dr16/, bmi088/等) - 更新代码生成器以支持新的文件夹结构 - 保持向后兼容性,支持从子文件夹或根目录加载模板 - 添加STRUCTURE.md文档说明新的目录结构 优势: ✅ 更好的代码组织和管理 ✅ 便于添加、删除、修改模板 ✅ 清晰的模块划分 ✅ 向后兼容现有结构
This commit is contained in:
parent
daf0a28517
commit
eeb02a2de3
@ -61,9 +61,17 @@ class BspSimplePeripheral(QWidget):
|
|||||||
return "skipped" # 返回特殊值表示跳过
|
return "skipped" # 返回特殊值表示跳过
|
||||||
return "not_needed" # 返回特殊值表示不需要生成
|
return "not_needed" # 返回特殊值表示不需要生成
|
||||||
|
|
||||||
template_dir = CodeGenerator.get_template_dir()
|
# 使用外设名称作为子文件夹名(小写)
|
||||||
|
periph_folder = self.peripheral_name.lower()
|
||||||
|
template_base_dir = CodeGenerator.get_assets_dir("User_code/bsp")
|
||||||
|
|
||||||
for key, filename in self.template_names.items():
|
for key, filename in self.template_names.items():
|
||||||
template_path = os.path.join(template_dir, filename)
|
# 先尝试从子文件夹加载
|
||||||
|
template_path = os.path.join(template_base_dir, periph_folder, filename)
|
||||||
|
if not os.path.exists(template_path):
|
||||||
|
# 如果子文件夹不存在,尝试从根目录加载(向后兼容)
|
||||||
|
template_path = os.path.join(template_base_dir, filename)
|
||||||
|
|
||||||
template_content = CodeGenerator.load_template(template_path)
|
template_content = CodeGenerator.load_template(template_path)
|
||||||
if not template_content:
|
if not template_content:
|
||||||
return False
|
return False
|
||||||
@ -206,7 +214,15 @@ class BspPeripheralBase(QWidget):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def _generate_header_file(self, configs, template_dir):
|
def _generate_header_file(self, configs, template_dir):
|
||||||
template_path = os.path.join(template_dir, self.template_names['header'])
|
# 构建模板路径,优先从子文件夹读取
|
||||||
|
periph_folder = self.peripheral_name.lower()
|
||||||
|
template_base_dir = CodeGenerator.get_assets_dir("User_code/bsp")
|
||||||
|
template_path = os.path.join(template_base_dir, periph_folder, self.template_names['header'])
|
||||||
|
|
||||||
|
if not os.path.exists(template_path):
|
||||||
|
# 向后兼容:尝试从根目录读取
|
||||||
|
template_path = os.path.join(template_base_dir, self.template_names['header'])
|
||||||
|
|
||||||
template_content = CodeGenerator.load_template(template_path)
|
template_content = CodeGenerator.load_template(template_path)
|
||||||
if not template_content:
|
if not template_content:
|
||||||
return False
|
return False
|
||||||
|
|||||||
@ -107,9 +107,17 @@ class ComponentSimple(QWidget):
|
|||||||
return "skipped" # 返回特殊值表示跳过
|
return "skipped" # 返回特殊值表示跳过
|
||||||
return "not_needed" # 返回特殊值表示不需要生成
|
return "not_needed" # 返回特殊值表示不需要生成
|
||||||
|
|
||||||
template_dir = self._get_component_template_dir()
|
# 使用组件名称作为子文件夹名(小写)
|
||||||
|
comp_folder = self.component_name.lower()
|
||||||
|
template_base_dir = CodeGenerator.get_assets_dir("User_code/component")
|
||||||
|
|
||||||
for key, filename in self.template_names.items():
|
for key, filename in self.template_names.items():
|
||||||
template_path = os.path.join(template_dir, filename)
|
# 先尝试从子文件夹加载
|
||||||
|
template_path = os.path.join(template_base_dir, comp_folder, filename)
|
||||||
|
if not os.path.exists(template_path):
|
||||||
|
# 如果子文件夹不存在,尝试从根目录加载(向后兼容)
|
||||||
|
template_path = os.path.join(template_base_dir, filename)
|
||||||
|
|
||||||
template_content = CodeGenerator.load_template(template_path)
|
template_content = CodeGenerator.load_template(template_path)
|
||||||
if not template_content:
|
if not template_content:
|
||||||
print(f"模板文件不存在或为空: {template_path}")
|
print(f"模板文件不存在或为空: {template_path}")
|
||||||
|
|||||||
@ -240,12 +240,18 @@ class DeviceSimple(QWidget):
|
|||||||
# 获取BSP配置
|
# 获取BSP配置
|
||||||
bsp_config = self.get_bsp_config()
|
bsp_config = self.get_bsp_config()
|
||||||
|
|
||||||
# 复制并修改文件
|
# 使用设备名称作为子文件夹名(小写)
|
||||||
template_dir = self._get_device_template_dir()
|
device_folder = self.device_name.lower()
|
||||||
|
template_base_dir = CodeGenerator.get_assets_dir("User_code/device")
|
||||||
files = self.device_config.get('files', {})
|
files = self.device_config.get('files', {})
|
||||||
|
|
||||||
for file_type, filename in files.items():
|
for file_type, filename in files.items():
|
||||||
src_path = os.path.join(template_dir, filename)
|
# 先尝试从子文件夹加载
|
||||||
|
src_path = os.path.join(template_base_dir, device_folder, filename)
|
||||||
|
if not os.path.exists(src_path):
|
||||||
|
# 如果子文件夹不存在,尝试从根目录加载(向后兼容)
|
||||||
|
src_path = os.path.join(template_base_dir, filename)
|
||||||
|
|
||||||
dst_path = os.path.join(self.project_path, f"User/device/{filename}")
|
dst_path = os.path.join(self.project_path, f"User/device/{filename}")
|
||||||
|
|
||||||
if os.path.exists(src_path):
|
if os.path.exists(src_path):
|
||||||
|
|||||||
113
assets/User_code/STRUCTURE.md
Normal file
113
assets/User_code/STRUCTURE.md
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
# User_code 目录结构说明
|
||||||
|
|
||||||
|
## 新的文件夹结构
|
||||||
|
|
||||||
|
所有外设、组件和设备的文件现在都存放在独立的子文件夹中,便于管理和维护。
|
||||||
|
|
||||||
|
### BSP (板级支持包)
|
||||||
|
```
|
||||||
|
bsp/
|
||||||
|
├── bsp.h # BSP 总头文件
|
||||||
|
├── describe.csv # 外设描述文件
|
||||||
|
├── .gitkeep
|
||||||
|
├── can/
|
||||||
|
│ ├── can.c
|
||||||
|
│ └── can.h
|
||||||
|
├── fdcan/
|
||||||
|
│ ├── fdcan.c
|
||||||
|
│ └── fdcan.h
|
||||||
|
├── uart/
|
||||||
|
│ ├── uart.c
|
||||||
|
│ └── uart.h
|
||||||
|
├── spi/
|
||||||
|
│ ├── spi.c
|
||||||
|
│ └── spi.h
|
||||||
|
├── i2c/
|
||||||
|
│ ├── i2c.c
|
||||||
|
│ └── i2c.h
|
||||||
|
├── gpio/
|
||||||
|
│ ├── gpio.c
|
||||||
|
│ └── gpio.h
|
||||||
|
├── pwm/
|
||||||
|
│ ├── pwm.c
|
||||||
|
│ └── pwm.h
|
||||||
|
├── time/
|
||||||
|
│ ├── time.c
|
||||||
|
│ └── time.h
|
||||||
|
├── dwt/
|
||||||
|
│ ├── dwt.c
|
||||||
|
│ └── dwt.h
|
||||||
|
└── mm/
|
||||||
|
├── mm.c
|
||||||
|
└── mm.h
|
||||||
|
```
|
||||||
|
|
||||||
|
### Component (组件)
|
||||||
|
```
|
||||||
|
component/
|
||||||
|
├── describe.csv # 组件描述文件
|
||||||
|
├── dependencies.csv # 组件依赖关系
|
||||||
|
├── .gitkeep
|
||||||
|
├── ahrs/
|
||||||
|
├── capacity/
|
||||||
|
├── cmd/
|
||||||
|
├── crc16/
|
||||||
|
├── crc8/
|
||||||
|
├── error_detect/
|
||||||
|
├── filter/
|
||||||
|
├── freertos_cli/
|
||||||
|
├── limiter/
|
||||||
|
├── mixer/
|
||||||
|
├── pid/
|
||||||
|
├── ui/
|
||||||
|
└── user_math/
|
||||||
|
```
|
||||||
|
|
||||||
|
### Device (设备)
|
||||||
|
```
|
||||||
|
device/
|
||||||
|
├── device.h # Device 总头文件
|
||||||
|
├── config.yaml # 设备配置文件
|
||||||
|
├── .gitkeep
|
||||||
|
├── bmi088/
|
||||||
|
├── buzzer/
|
||||||
|
├── dm_imu/
|
||||||
|
├── dr16/
|
||||||
|
├── ist8310/
|
||||||
|
├── led/
|
||||||
|
├── motor/
|
||||||
|
├── motor_dm/
|
||||||
|
├── motor_lk/
|
||||||
|
├── motor_lz/
|
||||||
|
├── motor_odrive/
|
||||||
|
├── motor_rm/
|
||||||
|
├── motor_vesc/
|
||||||
|
├── oid/
|
||||||
|
├── ops9/
|
||||||
|
├── rc_can/
|
||||||
|
├── servo/
|
||||||
|
├── vofa/
|
||||||
|
├── ws2812/
|
||||||
|
└── lcd_driver/ # LCD 驱动(原有结构)
|
||||||
|
```
|
||||||
|
|
||||||
|
## 代码生成逻辑
|
||||||
|
|
||||||
|
代码生成器会:
|
||||||
|
1. 首先尝试从子文件夹加载模板(如 `bsp/can/can.c`)
|
||||||
|
2. 如果子文件夹不存在,回退到根目录加载(向后兼容)
|
||||||
|
3. 生成时将文件展开到项目的扁平目录结构中(如 `User/bsp/can.c`)
|
||||||
|
|
||||||
|
## 优势
|
||||||
|
|
||||||
|
✅ **更好的组织**: 每个外设/组件的文件都在独立文件夹中
|
||||||
|
✅ **便于管理**: 添加、删除、修改模板更加方便
|
||||||
|
✅ **向后兼容**: 现有的扁平结构仍然可以正常工作
|
||||||
|
✅ **清晰的结构**: 一目了然地看到所有可用的外设/组件
|
||||||
|
|
||||||
|
## 迁移说明
|
||||||
|
|
||||||
|
如果你添加新的外设/组件/设备:
|
||||||
|
1. 在对应目录下创建新的子文件夹(小写命名)
|
||||||
|
2. 将 .c 和 .h 文件放入子文件夹
|
||||||
|
3. 代码生成器会自动识别并使用
|
||||||
Loading…
Reference in New Issue
Block a user