代码风格更新以及加入超电的对接

This commit is contained in:
lyf committed 2026-11-25 17:24:46 +08:00
1 parent 957c535bda
commit 9de485e4ef
117 files changed
+9046 -6457

No files matched your search

+163 -75
View File
@@ -1,72 +1,89 @@
/* =============================================================================
* referee_ui.c - 裁判系统自定义UI组包与发送(版式见 referee_ui.h / hud_mock5.png)
*
* 组包: 每包 = ext_student_interactive_header_data_t{data_cmd_id, sender_ID,
* receiver_ID} + N×15 字节图形数据,N 由 data_cmd_id 决定(1/2/5/7)。
* 图形名用 3 字符自增计数器(base36)保证唯一;首轮 ADD,之后 MODIFY。
* 发送: referee_send_frame(0x0301, payload, 6 + N*15) -> USART6 DMA
* ==========================================================================*/
/*
*文件名称:referee_ui.c
*功能描述:裁判系统自定义UI组包与发送模块
*备注:每包包含6字节交互头与1/2/5/7个15字节图形数据,首轮ADD之后一律MODIFY
*/
#include "referee_ui.h"
#include "referee.h"
#include "robot_param.h"
#include <string.h>
/* 协议要求:图形数据恰好 15 字节(头 11 + 尾 4)。若 pack(1) 失效或位域被重排,
* 这里直接编译失败,而不是把错位的图形发给选手端。 */
_Static_assert(sizeof(ui_head_t) == 11, "ui_head_t 必须是 11 字节");
_Static_assert(sizeof(ui_draw_t) == 15, "ui_draw_t 必须是 15 字节");
_Static_assert(sizeof(ui_num_t) == 15, "ui_num_t 必须是 15 字节");
_Static_assert(sizeof(ui_graphic_t) == 15, "ui_graphic_t 必须是 15 字节");
/* ---- 图形名生成:base26 三字符(17576 个组合,足够) ---- */
static uint16_t ui_name_seq = 0;
static uint16_t ui_name_seq = 0U;//图形名称自增序号(base26)
#define UI_PAYLOAD_MAX (6U + UI_MAX_PER_PACKET * sizeof(ui_graphic_t))
static uint8_t ui_pkt[UI_PAYLOAD_MAX];//单包发送拼装缓冲
static uint8_t ui_pkt_n = 0U;//当前包内已装填图形数
static uint8_t ui_op = UI_OP_ADD;//当前图形操作码(首轮ADD,之后MOD)
static uint8_t ui_ready = 0U;//首轮ADD发送完成标志
/*
*函数简介:生成唯一三字符图形名称(base26编码)
*参数说明:name:3字节名称输出缓冲区
*返回类型:无
*备注:内部函数
*/
static void ui_next_name(uint8_t name[3])
{
uint16_t v = ui_name_seq++;
name[0] = (uint8_t)('A' + (v / 676) % 26);
name[1] = (uint8_t)('A' + (v / 26) % 26);
name[2] = (uint8_t)('A' + v % 26);
name[0] = (uint8_t)('A' + (v / 676U) % 26U);
name[1] = (uint8_t)('A' + (v / 26U) % 26U);
name[2] = (uint8_t)('A' + v % 26U);
}
/* ---- 打包缓冲 ---- */
#define UI_PAYLOAD_MAX (6 + UI_MAX_PER_PACKET * sizeof(ui_graphic_t))
static uint8_t ui_pkt[UI_PAYLOAD_MAX];
static uint8_t ui_pkt_n; /* 当前包里的图形个数 */
static uint8_t ui_op = UI_OP_ADD; /* 首轮 ADD,之后 MOD */
static uint8_t ui_ready = 0; /* 首轮是否已发完 */
/*
*函数简介:将当前缓冲内的图形打包通过裁判系统0x0301发出
*参数说明:无
*返回类型:无
*备注:协议仅支持1/2/5/7个图形每包
*/
static void ui_flush(void)
{
if (ui_pkt_n == 0)
if (ui_pkt_n == 0U)
return;
uint16_t cmd;
uint16_t cmd = 0U;
switch (ui_pkt_n)
{
case 1: cmd = UI_CMD_1; break;
case 2: cmd = UI_CMD_2; break;
case 5: cmd = UI_CMD_5; break;
case 7: cmd = UI_CMD_7; break;
default: return; /* 协议只支持 1/2/5/7,不该发生 */
case 1U: cmd = UI_CMD_1; break;
case 2U: cmd = UI_CMD_2; break;
case 5U: cmd = UI_CMD_5; break;
case 7U: cmd = UI_CMD_7; break;
default: return;
}
/* header: data_cmd_id / sender_ID / receiver_ID,均小端 */
ui_pkt[0] = (uint8_t)(cmd & 0xFF);
ui_pkt[0] = (uint8_t)(cmd & 0xFFU);
ui_pkt[1] = (uint8_t)(cmd >> 8);
uint16_t sid = (uint16_t)referee.robot_id;
ui_pkt[2] = (uint8_t)(sid & 0xFF);
ui_pkt[2] = (uint8_t)(sid & 0xFFU);
ui_pkt[3] = (uint8_t)(sid >> 8);
ui_pkt[4] = (uint8_t)(UI_RECEIVER_ID & 0xFF);
ui_pkt[4] = (uint8_t)(UI_RECEIVER_ID & 0xFFU);
ui_pkt[5] = (uint8_t)(UI_RECEIVER_ID >> 8);
referee_send_frame(UI_CMD_BASE, ui_pkt, (uint16_t)(6 + ui_pkt_n * sizeof(ui_graphic_t)));
ui_pkt_n = 0;
referee_send_frame(UI_CMD_BASE, ui_pkt, (uint16_t)(6U + ui_pkt_n * sizeof(ui_graphic_t)));
ui_pkt_n = 0U;
}
/*
*函数简介:在发送缓冲中分配并初始化一个新的图形槽位
*参数说明:type:图形类型(UI_T_*)
*参数说明:layer:图层编号(0~9)
*参数说明:color:颜色编号(UI_C_*)
*返回类型:ui_graphic_t *:新图形联合体指针
*备注:缓冲满7个时自动触发ui_flush发送
*/
static ui_graphic_t * ui_new(uint8_t type, uint8_t layer, uint8_t color)
{
if (ui_pkt_n >= UI_MAX_PER_PACKET)
ui_flush();
ui_graphic_t * g = (ui_graphic_t *)&ui_pkt[6 + ui_pkt_n * sizeof(ui_graphic_t)];
ui_graphic_t * g = (ui_graphic_t *)&ui_pkt[6U + ui_pkt_n * sizeof(ui_graphic_t)];
memset(g, 0, sizeof(*g));
ui_next_name(g->d.h.name); /* d.h 与 n.h 同址 */
ui_next_name(g->d.h.name);
g->d.h.type = type;
g->d.h.layer = layer;
g->d.h.color = color;
@@ -75,43 +92,107 @@ static ui_graphic_t * ui_new(uint8_t type, uint8_t layer, uint8_t color)
return g;
}
/* ---- 图元封装(绘制类写 d.*,数值类写 n.val) ---- */
/*
*函数简介:绘制直线图元
*参数说明:layer:图层,color:颜色,x0/y0:起点,x1/y1:终点,w:线宽
*返回类型:无
*备注:内部图元封装
*/
static void ui_line(uint8_t layer, uint8_t color, int x0, int y0, int x1, int y1, int w)
{
ui_graphic_t * g = ui_new(UI_T_LINE, layer, color);
g->d.h.x0 = (uint32_t)x0; g->d.h.y0 = (uint32_t)y0;
g->d.x1 = (uint32_t)x1; g->d.y1 = (uint32_t)y1;
g->d.h.x0 = (uint32_t)x0;
g->d.h.y0 = (uint32_t)y0;
g->d.x1 = (uint32_t)x1;
g->d.y1 = (uint32_t)y1;
g->d.h.width = (uint32_t)w;
}
/*
*函数简介:绘制矩形图元
*参数说明:layer:图层,color:颜色,x0/y0:左上角,x1/y1:右下角
*返回类型:无
*备注:内部图元封装
*/
static void ui_rect(uint8_t layer, uint8_t color, int x0, int y0, int x1, int y1)
{
ui_graphic_t * g = ui_new(UI_T_RECT, layer, color);
g->d.h.x0 = (uint32_t)x0; g->d.h.y0 = (uint32_t)y0;
g->d.x1 = (uint32_t)x1; g->d.y1 = (uint32_t)y1;
g->d.h.x0 = (uint32_t)x0;
g->d.h.y0 = (uint32_t)y0;
g->d.x1 = (uint32_t)x1;
g->d.y1 = (uint32_t)y1;
}
/*
*函数简介:绘制浮点数值图元
*参数说明:layer:图层,color:颜色,x/y:起点坐标,v:浮点值,digits:小数位数
*返回类型:无
*备注:内部图元封装
*/
static void ui_fval(uint8_t layer, uint8_t color, int x, int y, float v, int digits)
{
ui_graphic_t * g = ui_new(UI_T_FLOAT, layer, color);
g->n.h.x0 = (uint32_t)x; g->n.h.y0 = (uint32_t)y;
g->n.h.width = (uint32_t)digits; /* 浮点:小数位数 */
g->n.h.x0 = (uint32_t)x;
g->n.h.y0 = (uint32_t)y;
g->n.h.width = (uint32_t)digits;
g->n.val.f = v;
}
/*
*函数简介:绘制整型数值图元
*参数说明:layer:图层,color:颜色,x/y:起点坐标,v:整型值
*返回类型:无
*备注:内部图元封装
*/
static void ui_ival(uint8_t layer, uint8_t color, int x, int y, int32_t v)
{
ui_graphic_t * g = ui_new(UI_T_INT, layer, color);
g->n.h.x0 = (uint32_t)x; g->n.h.y0 = (uint32_t)y;
g->n.h.x0 = (uint32_t)x;
g->n.h.y0 = (uint32_t)y;
g->n.val.i = v;
}
/*
*函数简介:绘制短字符图元(最多4字节ASCII)
*参数说明:layer:图层,color:颜色,x/y:起点坐标,s:字符串指针
*返回类型:无
*备注:内部图元封装,遇到'\0'停止拷贝避免短字符串越界读取
*/
static void ui_char(uint8_t layer, uint8_t color, int x, int y, const char * s)
{
if (s == NULL)
return;
ui_graphic_t * g = ui_new(UI_T_CHAR, layer, color);
g->n.h.x0 = (uint32_t)x; g->n.h.y0 = (uint32_t)y;
g->n.h.x0 = (uint32_t)x;
g->n.h.y0 = (uint32_t)y;
g->n.h.width = (uint32_t)strlen(s);
for (int i = 0; i < 4; i++) /* 最多 4 字节 ASCII */
for (int i = 0; i < 4 && s[i] != '\0'; i++)
g->n.val.s[i] = s[i];
}
/* 角度归一到 [0,360) */
/*
*函数简介:将像素长度限制在[0, max_val]区间内
*参数说明:val:原始像素值
*参数说明:max_val:最大允许像素值
*返回类型:int:限幅后的像素值
*备注:内部函数
*/
static int ui_clamp_px(int val, int max_val)
{
if (val < 0)
return 0;
if (val > max_val)
return max_val;
return val;
}
/*
*函数简介:将角度归一化到[0, 360)区间
*参数说明:a:输入角度(°)
*返回类型:float:归一化角度(°)
*备注:内部函数
*/
static float ui_norm360(float a)
{
while (a < 0.0f) a += 360.0f;
@@ -119,44 +200,52 @@ static float ui_norm360(float a)
return a;
}
/*
*函数简介:组包并发送选手端自定义UI图形
*参数说明:d:UI显示数据结构体指针
*返回类型:无
*备注:裁判系统离线时发送一轮删除图层并重置状态为ADD
*/
void referee_ui_update(const ui_data_t * d)
{
if (d == NULL)
return;
/* 裁判系统离线:发一轮删除图层,然后停发(避免选手端残留最后一帧) */
if (!d->ref_online)
{
if (ui_ready)
{
ui_pkt_n = 0;
ui_pkt_n = 0U;
ui_graphic_t * g = ui_new(UI_T_RECT, UI_L_FRAME, UI_C_BLACK);
g->d.h.operate = UI_OP_DEL;
g->d.h.x0 = 0; g->d.h.y0 = 0; g->d.x1 = 0; g->d.y1 = 0;
g->d.h.x0 = 0U;
g->d.h.y0 = 0U;
g->d.x1 = 0U;
g->d.y1 = 0U;
ui_flush();
ui_ready = 0;
ui_name_seq = 0;
ui_ready = 0U;
ui_name_seq = 0U;
ui_op = UI_OP_ADD;
}
return;
}
/* ---------------- 左上:预估底盘功率 ---------------- */
/* 左上:预估底盘功率 */
uint8_t pw_col = (d->pwr_w > d->pwr_limit_w) ? UI_C_RED
: (d->pwr_w > d->pwr_limit_w * 0.8f) ? UI_C_ORANGE : UI_C_GREEN;
ui_rect(UI_L_FRAME, UI_C_CYAN, UI_PWR_X0, UI_PWR_Y0, UI_PWR_X1, UI_PWR_Y0 + 4);
ui_char(UI_L_TEXT, UI_C_CYAN, UI_PWR_X0 + 20, UI_PWR_Y0 + 30, "PWR");
ui_fval(UI_L_TEXT, UI_C_WHITE, UI_PWR_X0 + 20, UI_PWR_Y0 + 64, d->pwr_w, 1);
ui_char(UI_L_TEXT, UI_C_WHITE, UI_PWR_X0 + 210, UI_PWR_Y0 + 84, "W");
int pw_w_px = (int)((UI_PWR_X1 - UI_PWR_X0 - 40) * (d->pwr_w / (d->pwr_limit_w > 1.0f ? d->pwr_limit_w : 1.0f)));
if (pw_w_px > UI_PWR_X1 - UI_PWR_X0 - 40) pw_w_px = UI_PWR_X1 - UI_PWR_X0 - 40;
if (pw_w_px < 0) pw_w_px = 0;
const int pw_bar_max = UI_PWR_X1 - UI_PWR_X0 - 40;
int pw_w_px = ui_clamp_px((int)(pw_bar_max * (d->pwr_w / (d->pwr_limit_w > 1.0f ? d->pwr_limit_w : 1.0f))),
pw_bar_max);
ui_rect(UI_L_GAUGE, pw_col, UI_PWR_X0 + 20, UI_PWR_Y0 + 120, UI_PWR_X0 + 20 + pw_w_px, UI_PWR_Y0 + 142);
ui_line(UI_L_GAUGE, UI_C_WHITE, UI_PWR_X0 + 20 + (UI_PWR_X1 - UI_PWR_X0 - 40) - 3, UI_PWR_Y0 + 116,
UI_PWR_X0 + 20 + (UI_PWR_X1 - UI_PWR_X0 - 40) - 3, UI_PWR_Y0 + 146, 2);
ui_line(UI_L_GAUGE, UI_C_WHITE, UI_PWR_X0 + 20 + pw_bar_max - 3, UI_PWR_Y0 + 116,
UI_PWR_X0 + 20 + pw_bar_max - 3, UI_PWR_Y0 + 146, 2);
/* ---------------- 左中:超级电容 ---------------- */
uint8_t cap_col = (d->cap_pct < 15) ? UI_C_RED : (d->cap_pct < 40) ? UI_C_ORANGE : UI_C_CYAN;
/* 左中:超级电容 */
uint8_t cap_col = (d->cap_pct < 15U) ? UI_C_RED : (d->cap_pct < 40U) ? UI_C_ORANGE : UI_C_CYAN;
ui_rect(UI_L_FRAME, cap_col, UI_CAP_X0, UI_CAP_Y0, UI_CAP_X1, UI_CAP_Y0 + 4);
ui_char(UI_L_TEXT, cap_col, UI_CAP_X0 + 20, UI_CAP_Y0 + 30, "CAP");
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_CAP_X0 + 20, UI_CAP_Y0 + 64, (int32_t)d->cap_pct);
@@ -164,16 +253,16 @@ void referee_ui_update(const ui_data_t * d)
int cap_px = (int)((UI_CAP_X1 - UI_CAP_X0 - 40) * (d->cap_pct / 100.0f));
ui_rect(UI_L_GAUGE, cap_col, UI_CAP_X0 + 20, UI_CAP_Y0 + 118, UI_CAP_X0 + 20 + cap_px, UI_CAP_Y0 + 140);
/* ---------------- 顶部:罗盘刻度带(底盘正方向朝向) ---------------- */
/* 顶部:罗盘刻度带(底盘正方向朝向) */
{
float h = ui_norm360(d->hdg_deg);
ui_rect(UI_L_FRAME, UI_C_CYAN, UI_HDG_X0, UI_HDG_Y0, UI_HDG_X1, UI_HDG_Y0 + 4);
ui_line(UI_L_FRAME, UI_C_CYAN, UI_HDG_X0 + 20, UI_HDG_Y0 + 62, UI_HDG_X1 - 20, UI_HDG_Y0 + 62, 2);
int cx = (UI_HDG_X0 + UI_HDG_X1) / 2;
for (int k = -1; k <= 1; k++) /* 3 条刻度 */
for (int k = -1; k <= 1; k++)
{
int x = cx + k * (UI_HDG_TAPE_HALF / 2);
ui_line(UI_L_FRAME, UI_C_CYAN, x, UI_HDG_Y0 + 40, x, UI_HDG_Y0 + 62, k == 0 ? 3 : 2);
ui_line(UI_L_FRAME, UI_C_CYAN, x, UI_HDG_Y0 + 40, x, UI_HDG_Y0 + 62, (k == 0) ? 3 : 2);
}
ui_ival(UI_L_TEXT, UI_C_CYAN, cx - UI_HDG_TAPE_HALF / 2 - 24, UI_HDG_Y0 + 18,
(int32_t)ui_norm360(h - 30.0f));
@@ -187,12 +276,13 @@ void referee_ui_update(const ui_data_t * d)
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_HDG_X1 - 104, UI_HDG_Y0 + 66, (int32_t)ui_norm360(d->yaw_deg));
}
/* ---------------- 中央:俯仰梯 ---------------- */
/* 中央:俯仰梯 */
ui_line(UI_L_FRAME, UI_C_WHITE, UI_LAD_CX - 44, UI_LAD_CY, UI_LAD_CX - 14, UI_LAD_CY, 4);
ui_line(UI_L_FRAME, UI_C_WHITE, UI_LAD_CX + 14, UI_LAD_CY, UI_LAD_CX + 44, UI_LAD_CY, 4);
for (int i = -2; i <= 2; i++) /* ±10/±5/0 五条梯线 */
for (int i = -2; i <= 2; i++)
{
if (i == 0) continue;
if (i == 0)
continue;
int v = i * 5;
int yy = UI_LAD_CY - v * UI_LAD_PX_PER_DEG;
int half = (i % 2 == 0) ? UI_LAD_HALF_LONG : UI_LAD_HALF_SHORT;
@@ -208,7 +298,7 @@ void referee_ui_update(const ui_data_t * d)
ui_fval(UI_L_TEXT, UI_C_WHITE, UI_LAD_CX - UI_LAD_HALF_LONG - 76, py - 14, d->pitch_deg, 1);
}
/* ---------------- 右上:摩擦轮速度 ---------------- */
/* 右上:摩擦轮速度 */
{
uint8_t f_col = (d->fric_l_rpm > 1000.0f && d->fric_r_rpm > 1000.0f) ? UI_C_GREEN : UI_C_ORANGE;
ui_rect(UI_L_FRAME, f_col, UI_FRIC_X0, UI_FRIC_Y0, UI_FRIC_X1, UI_FRIC_Y0 + 4);
@@ -217,15 +307,14 @@ void referee_ui_update(const ui_data_t * d)
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_FRIC_X0 + 250, UI_FRIC_Y0 + 74, (int32_t)d->fric_r_rpm);
ui_char(UI_L_TEXT, f_col, UI_FRIC_X0 + 24, UI_FRIC_Y1 - 60,
(d->fric_l_rpm > 1000.0f && d->fric_r_rpm > 1000.0f) ? "ARMED" : "IDLE");
int bl = (int)((UI_FRIC_Y1 - UI_FRIC_Y0 - 140) * (d->fric_l_rpm / 7000.0f));
int br = (int)((UI_FRIC_Y1 - UI_FRIC_Y0 - 140) * (d->fric_r_rpm / 7000.0f));
if (bl < 0) bl = 0; if (bl > UI_FRIC_Y1 - UI_FRIC_Y0 - 140) bl = UI_FRIC_Y1 - UI_FRIC_Y0 - 140;
if (br < 0) br = 0; if (br > UI_FRIC_Y1 - UI_FRIC_Y0 - 140) br = UI_FRIC_Y1 - UI_FRIC_Y0 - 140;
const int fric_bar_max = UI_FRIC_Y1 - UI_FRIC_Y0 - 140;
int bl = ui_clamp_px((int)(fric_bar_max * (d->fric_l_rpm / 7000.0f)), fric_bar_max);
int br = ui_clamp_px((int)(fric_bar_max * (d->fric_r_rpm / 7000.0f)), fric_bar_max);
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_FRIC_X0 + 30, UI_FRIC_Y1 - 40 - bl, UI_FRIC_X0 + 74, UI_FRIC_Y1 - 40);
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_FRIC_X0 + 256, UI_FRIC_Y1 - 40 - br, UI_FRIC_X0 + 300, UI_FRIC_Y1 - 40);
}
/* ---------------- 底部:小陀螺状态 ---------------- */
/* 底部:小陀螺状态 */
ui_rect(UI_L_FRAME, d->spin_on ? UI_C_GREEN : UI_C_CYAN, UI_SPIN_X0, UI_SPIN_Y0, UI_SPIN_X1, UI_SPIN_Y0 + 4);
ui_char(UI_L_TEXT, UI_C_CYAN, UI_SPIN_X0 + 24, UI_SPIN_Y0 + 34, "SPIN");
if (d->spin_on)
@@ -233,11 +322,10 @@ void referee_ui_update(const ui_data_t * d)
ui_char(UI_L_TEXT, d->spin_on ? UI_C_BLACK : UI_C_WHITE, UI_SPIN_X0 + 184, UI_SPIN_Y0 + 36,
d->spin_on ? "ON" : "OFF");
/* 收尾:最后一包;首轮发完切换为 MODIFY */
ui_flush();
if (!ui_ready)
{
ui_ready = 1;
ui_ready = 1U;
ui_op = UI_OP_MOD;
}
}