Boen_Shi 8be2866433 feat(base): 初始化 MSPM0 开发环境
- 添加头文件和配置文件支持
- 更新.gitignore忽略编译和IDE相关文件
- 添加基础的bsp代码
2026-07-16 14:45:26 +08:00

275 lines
7.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "../2025_include_user/Host.h"
#include <string.h>
#include <stdio.h>
extern "C" void HOST_INST_IRQHandler(void) {
const char _buffer = DL_UART_Main_receiveData(HOST_INST);
switch (DL_UART_getPendingInterrupt(HOST_INST)) {
case DL_UART_IIDX_RX:
Host::inject(_buffer);
break;
default:
break;
}
NVIC_ClearPendingIRQ(HOST_INST_INT_IRQN);
DL_UART_enableInterrupt(HOST_INST, DL_UART_INTERRUPT_RX); // 启用接收中断
}
extern "C" void PRINTF_INST_IRQHandler(void) {
const char _buffer = DL_UART_Main_receiveData(PRINTF_INST);
switch (DL_UART_getPendingInterrupt(PRINTF_INST)) {
case DL_UART_IIDX_RX:
Host::inject(_buffer);
break;
default:
break;
}
NVIC_ClearPendingIRQ(PRINTF_INST_INT_IRQN);
DL_UART_enableInterrupt(PRINTF_INST, DL_UART_INTERRUPT_RX); // 启用接收中断
}
KeyValuePair Host::data[MAX_KEYS] = {0}; // 初始化为0
int Host::count = 0;
void Host::init() {
NVIC_ClearPendingIRQ(HOST_INST_INT_IRQN);
NVIC_EnableIRQ(HOST_INST_INT_IRQN);
DL_UART_enable(HOST_INST); // 启用 UART 模块
DL_UART_enableInterrupt(HOST_INST, DL_UART_INTERRUPT_RX); // 启用接收中断
NVIC_ClearPendingIRQ(PRINTF_INST_INT_IRQN);
NVIC_EnableIRQ(PRINTF_INST_INT_IRQN);
DL_UART_enable(PRINTF_INST); // 启用 UART 模块
DL_UART_enableInterrupt(PRINTF_INST, DL_UART_INTERRUPT_RX); // 启用接收中断
}
double Host::posX = 0.0;
double Host::posY = 0.0;
int Host::x_count = 0;
int Host::y_count = 0;
void Host::inject(const char ch) {
static char buffer[MAX_KEY_LENGTH];
static int bufferIndex = 0;
static bool isKey = false;
static bool isValue = false;
static char key[MAX_KEY_LENGTH];
static char value[MAX_KEY_LENGTH];
if (ch == '$') {
bufferIndex = 0;
isKey = true;
isValue = false;
} else if (ch == '(') {
bufferIndex = 0;
} else if (ch == ')') {
strncpy(key, buffer, bufferIndex);
key[bufferIndex] = '\0';
bufferIndex = 0;
isKey = false;
isValue = true;
} else if (ch == '#') {
strncpy(value, buffer, bufferIndex);
value[bufferIndex] = '\0';
bufferIndex = 0;
isValue = false;
double num;
if (sscanf(value, "%lf", &num) == 1) {
if (strcmp(key, "x") == 0) {
posX = num;
return;
} else if (strcmp(key, "y") == 0) {
posY = num;
return;
}
for (int i = 0; i < count; ++i) {
if (strcmp(data[i].key, key) == 0) {
data[i].value = num;
return;
}
}
if (count < MAX_KEYS) {
strncpy(data[count].key, key, MAX_KEY_LENGTH);
data[count].value = num;
count++;
}
}
} else if (isKey || isValue) {
if (bufferIndex < MAX_KEY_LENGTH - 1) {
buffer[bufferIndex++] = ch;
}
}
}
// void Host::inject(const char ch) {
// static char buffer[MAX_KEY_LENGTH];
// static int bufferIndex = 0;
// static bool isKey = false;
// static bool isValue = false;
// static char key[MAX_KEY_LENGTH];
// static char value[MAX_KEY_LENGTH];
//
// if (ch == '$') {
// bufferIndex = 0;
// isKey = true;
// isValue = false;
// } else if (ch == '(') {
// bufferIndex = 0;
// } else if (ch == ')') {
// strncpy(key, buffer, bufferIndex);
// key[bufferIndex] = '\0';
// bufferIndex = 0;
// isKey = false;
// isValue = true;
// } else if (ch == '#') {
// strncpy(value, buffer, bufferIndex);
// value[bufferIndex] = '\0';
// bufferIndex = 0;
// isValue = false;
//
// double num;
// if (sscanf(value, "%lf", &num) == 1) {
// if (strcmp(key, "x") == 0) {
// posX = num;
// x_count = 2;
// // print("x: %d\n", num);
// } else if (strcmp(key, "y") == 0) {
// posY = num;
// y_count = 2;
// // print("y: %d\n", num);
// }
// // 其他key直接丢弃不处理
// }
// } else if (isKey || isValue) {
// if (bufferIndex < MAX_KEY_LENGTH - 1) {
// buffer[bufferIndex++] = ch;
//
// }
// }
// }
// void Host::inject(const char ch) {
// static char buffer[MAX_KEY_LENGTH];
// static int bufferIndex = 0;
// static bool isKey = false;
// static bool isValue = false;
// static char key[MAX_KEY_LENGTH];
// static char value[MAX_KEY_LENGTH];
//
// if (ch == '$') {
// bufferIndex = 0;
// isKey = true;
// isValue = false;
// } else if (ch == '(') {
// bufferIndex = 0;
// } else if (ch == ')') {
// strncpy(key, buffer, bufferIndex);
// key[bufferIndex] = '\0';
// bufferIndex = 0;
// isKey = false;
// isValue = true;
// } else if (ch == '#') {
// strncpy(value, buffer, bufferIndex);
// value[bufferIndex] = '\0';
// bufferIndex = 0;
// isValue = false;
//
// double num;
// if (sscanf(value, "%lf", &num) == 1) {
// for (int i = 0; i < count; ++i) {
// if (strcmp(data[i].key, key) == 0) {
// data[i].value = num;
// return;
// }
// }
// if (count < MAX_KEYS) {
// strncpy(data[count].key, key, MAX_KEY_LENGTH);
// data[count].value = num;
// count++;
// }
// }
// } else if (isKey || isValue) {
// if (bufferIndex < MAX_KEY_LENGTH - 1) {
// buffer[bufferIndex++] = ch;
// }
// }
// }
double Host::get(const char* key, double defaultValue) {
for (int i = 0; i < count; i++) {
if (strcmp(data[i].key, key) == 0) {
double value = data[i].value;
data[i].value = defaultValue;
return value;
}
}
return defaultValue; // 默认值
}
double Host::getWithoutClean(const char* key, double defaultValue) {
for (int i = 0; i < count; i++) {
if (strcmp(data[i].key, key) == 0) {
return data[i].value;
}
}
return defaultValue; // 默认值
}
void Host::log(const char* level, const char* format, ...) {
char buffer[256];
va_list args;
// 初始化可变参数列表
va_start(args, format);
// 格式化字符串到缓冲区
vsnprintf(buffer, sizeof(buffer), format, args);
// 结束可变参数列表
va_end(args);
// 创建完整的INFO字符串
char fullInfo[256];
snprintf(fullInfo, sizeof(fullInfo), "%s%s%s", level, buffer, "#");
// 调用printf输出
print("%s\n", fullInfo);
}
void Host::info(const char* format, ...) {
va_list args;
va_start(args, format);
log("$0100", format, args);
va_end(args);
}
void Host::warn(const char* format, ...) {
va_list args;
va_start(args, format);
log("$0200", format, args);
va_end(args);
}
void Host::error(const char* format, ...) {
va_list args;
va_start(args, format);
log("$0300", format, args);
va_end(args);
}
void Host::plot(const char* key, double value) {
char buffer[256];
char fullInfo[256];
// 格式化键值对
snprintf(buffer, sizeof(buffer), "(%s)%f", key, value);
// 创建完整的PLOT字符串
snprintf(fullInfo, sizeof(fullInfo), "$0400%s#", buffer);
// 调用printf输出
print("%s\n", fullInfo);
}