mirror of
https://github.com/goldenfishs/MRobot.git
synced 2026-03-31 21:07:14 +08:00
bsp写的差不多了
This commit is contained in:
@@ -1,7 +1,146 @@
|
||||
|
||||
|
||||
class analyzing_ioc:
|
||||
def __init__(self, ioc_data): # 初始化方法,接收IOC数据
|
||||
self.ioc_data = ioc_data # 存储IOC数据
|
||||
@staticmethod
|
||||
def is_freertos_enabled_from_ioc(ioc_path):
|
||||
"""
|
||||
检查指定 .ioc 文件是否开启了 FreeRTOS
|
||||
"""
|
||||
config = {}
|
||||
with open(ioc_path, encoding='utf-8') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
if '=' in line:
|
||||
key, value = line.split('=', 1)
|
||||
config[key.strip()] = value.strip()
|
||||
ip_keys = [k for k in config if k.startswith('Mcu.IP')]
|
||||
for k in ip_keys:
|
||||
if config[k] == 'FREERTOS':
|
||||
return True
|
||||
for k in config:
|
||||
if k.startswith('FREERTOS.'):
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_enabled_i2c_from_ioc(ioc_path):
|
||||
"""
|
||||
从.ioc文件中获取已启用的I2C列表
|
||||
返回格式: ['I2C1', 'I2C3'] 等
|
||||
"""
|
||||
enabled_i2c = []
|
||||
with open(ioc_path, encoding='utf-8', errors='ignore') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
if '=' in line:
|
||||
key, value = line.split('=', 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
# 检查是否启用了I2C
|
||||
if key.startswith('Mcu.IP') and value.startswith('I2C'):
|
||||
# 提取I2C编号,如I2C1, I2C2等
|
||||
i2c_name = value.split('.')[0] if '.' in value else value
|
||||
if i2c_name not in enabled_i2c:
|
||||
enabled_i2c.append(i2c_name)
|
||||
return sorted(enabled_i2c)
|
||||
|
||||
@staticmethod
|
||||
def get_enabled_spi_from_ioc(ioc_path):
|
||||
"""
|
||||
获取已启用的SPI列表
|
||||
返回格式: ['SPI1', 'SPI2'] 等
|
||||
"""
|
||||
enabled_spi = []
|
||||
with open(ioc_path, encoding='utf-8', errors='ignore') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
if '=' in line:
|
||||
key, value = line.split('=', 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
if key.startswith('Mcu.IP') and value.startswith('SPI'):
|
||||
spi_name = value.split('.')[0] if '.' in value else value
|
||||
if spi_name not in enabled_spi:
|
||||
enabled_spi.append(spi_name)
|
||||
return sorted(enabled_spi)
|
||||
|
||||
@staticmethod
|
||||
def get_enabled_can_from_ioc(ioc_path):
|
||||
"""
|
||||
获取已启用的CAN列表
|
||||
返回格式: ['CAN1', 'CAN2'] 等
|
||||
"""
|
||||
enabled_can = []
|
||||
with open(ioc_path, encoding='utf-8', errors='ignore') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
if '=' in line:
|
||||
key, value = line.split('=', 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
if key.startswith('Mcu.IP') and value.startswith('CAN'):
|
||||
can_name = value.split('.')[0] if '.' in value else value
|
||||
if can_name not in enabled_can:
|
||||
enabled_can.append(can_name)
|
||||
return sorted(enabled_can)
|
||||
|
||||
@staticmethod
|
||||
def get_enabled_uart_from_ioc(ioc_path):
|
||||
"""
|
||||
获取已启用的UART/USART列表
|
||||
返回格式: ['USART1', 'USART2', 'UART4'] 等
|
||||
"""
|
||||
enabled_uart = []
|
||||
with open(ioc_path, encoding='utf-8', errors='ignore') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
if '=' in line:
|
||||
key, value = line.split('=', 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
# 检查是否启用了UART或USART
|
||||
if key.startswith('Mcu.IP') and (value.startswith('USART') or value.startswith('UART')):
|
||||
uart_name = value.split('.')[0] if '.' in value else value
|
||||
if uart_name not in enabled_uart:
|
||||
enabled_uart.append(uart_name)
|
||||
return sorted(enabled_uart)
|
||||
|
||||
@staticmethod
|
||||
def get_enabled_gpio_from_ioc(ioc_path):
|
||||
"""
|
||||
获取所有带 EXTI 且有 Label 的 GPIO,返回 [{'pin': 'PC4', 'label': 'ACCL_INT'}, ...]
|
||||
"""
|
||||
gpio_list = []
|
||||
gpio_params = {}
|
||||
with open(ioc_path, encoding='utf-8', errors='ignore') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
if '=' in line:
|
||||
key, value = line.split('=', 1)
|
||||
key = key.strip()
|
||||
value = value.strip()
|
||||
if '.GPIOParameters' in key:
|
||||
gpio_params[key.split('.')[0]] = value
|
||||
elif '.GPIO_Label' in key:
|
||||
pin = key.split('.')[0]
|
||||
gpio_params[f"{pin}_label"] = value
|
||||
elif '.GPIO_ModeDefaultEXTI' in key:
|
||||
pin = key.split('.')[0]
|
||||
gpio_params[f"{pin}_exti"] = value
|
||||
for pin in gpio_params:
|
||||
if not pin.endswith('_label') and not pin.endswith('_exti'):
|
||||
label = gpio_params.get(f"{pin}_label", None)
|
||||
exti = gpio_params.get(f"{pin}_exti", None)
|
||||
if label and exti:
|
||||
gpio_list.append({'pin': pin, 'label': label})
|
||||
return gpio_list
|
||||
75
app/tools/code_generator.py
Normal file
75
app/tools/code_generator.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import os
|
||||
import yaml
|
||||
import shutil
|
||||
from typing import Dict, List, Tuple
|
||||
|
||||
class CodeGenerator:
|
||||
"""通用代码生成器"""
|
||||
|
||||
@staticmethod
|
||||
def load_template(template_path: str) -> str:
|
||||
"""加载代码模板"""
|
||||
try:
|
||||
with open(template_path, 'r', encoding='utf-8') as f:
|
||||
return f.read()
|
||||
except Exception as e:
|
||||
print(f"加载模板失败: {template_path}, 错误: {e}")
|
||||
return ""
|
||||
|
||||
@staticmethod
|
||||
def replace_auto_generated(content: str, marker: str, replacement: str) -> str:
|
||||
"""替换自动生成的代码标记"""
|
||||
marker_line = f"/* {marker} */"
|
||||
if marker_line in content:
|
||||
return content.replace(marker_line, replacement)
|
||||
return content
|
||||
|
||||
@staticmethod
|
||||
def save_file(content: str, file_path: str) -> bool:
|
||||
"""保存文件"""
|
||||
try:
|
||||
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(content)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"保存文件失败: {file_path}, 错误: {e}")
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def load_config(config_path: str) -> Dict:
|
||||
"""加载配置文件"""
|
||||
if os.path.exists(config_path):
|
||||
try:
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
return yaml.safe_load(f) or {}
|
||||
except Exception as e:
|
||||
print(f"加载配置失败: {config_path}, 错误: {e}")
|
||||
return {}
|
||||
|
||||
@staticmethod
|
||||
def save_config(config: Dict, config_path: str) -> bool:
|
||||
"""保存配置文件"""
|
||||
try:
|
||||
os.makedirs(os.path.dirname(config_path), exist_ok=True)
|
||||
with open(config_path, 'w', encoding='utf-8') as f:
|
||||
yaml.safe_dump(config, f, allow_unicode=True, default_flow_style=False)
|
||||
return True
|
||||
except Exception as e:
|
||||
print(f"保存配置失败: {config_path}, 错误: {e}")
|
||||
return False
|
||||
|
||||
@staticmethod
|
||||
def get_template_dir():
|
||||
"""获取模板文件目录"""
|
||||
# 从当前文件向上找到 MRobot 目录,然后定位到模板目录
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
# 向上找到 MRobot 根目录
|
||||
while os.path.basename(current_dir) != 'MRobot' and current_dir != '/':
|
||||
current_dir = os.path.dirname(current_dir)
|
||||
|
||||
if os.path.basename(current_dir) == 'MRobot':
|
||||
return os.path.join(current_dir, "assets/User_code/bsp")
|
||||
else:
|
||||
# 如果找不到,使用相对路径作为备选
|
||||
return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "assets/User_code/bsp")
|
||||
Reference in New Issue
Block a user