/* *文件名称:referee_ui.c *功能描述:裁判系统自定义UI组包与发送模块 *备注:每包包含6字节交互头与1/2/5/7个15字节图形数据,首轮ADD之后一律MODIFY */ #include "referee_ui.h" #include "referee.h" #include "robot_param.h" #include _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 字节"); 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 / 676U) % 26U); name[1] = (uint8_t)('A' + (v / 26U) % 26U); name[2] = (uint8_t)('A' + v % 26U); } /* *函数简介:将当前缓冲内的图形打包通过裁判系统0x0301发出 *参数说明:无 *返回类型:无 *备注:协议仅支持1/2/5/7个图形每包 */ static void ui_flush(void) { if (ui_pkt_n == 0U) return; uint16_t cmd = 0U; switch (ui_pkt_n) { 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; } 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 & 0xFFU); ui_pkt[3] = (uint8_t)(sid >> 8); 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)(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[6U + ui_pkt_n * sizeof(ui_graphic_t)]; memset(g, 0, sizeof(*g)); ui_next_name(g->d.h.name); g->d.h.type = type; g->d.h.layer = layer; g->d.h.color = color; g->d.h.operate = ui_op; ui_pkt_n++; return g; } /* *函数简介:绘制直线图元 *参数说明: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.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; } /* *函数简介:绘制浮点数值图元 *参数说明: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.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.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.width = (uint32_t)strlen(s); for (int i = 0; i < 4 && s[i] != '\0'; i++) g->n.val.s[i] = s[i]; } /* *函数简介:将像素长度限制在[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; while (a >= 360.0f) a -= 360.0f; 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 = 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 = 0U; g->d.h.y0 = 0U; g->d.x1 = 0U; g->d.y1 = 0U; ui_flush(); 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"); 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 + 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 < 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); ui_char(UI_L_TEXT, UI_C_WHITE, UI_CAP_X0 + 130, UI_CAP_Y0 + 84, "%"); 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++) { 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_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)); 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)); ui_line(UI_L_FRAME, UI_C_WHITE, cx, UI_HDG_Y0 + 62, cx - 8, UI_HDG_Y0 + 50, 2); ui_line(UI_L_FRAME, UI_C_WHITE, cx, UI_HDG_Y0 + 62, cx + 8, UI_HDG_Y0 + 50, 2); ui_char(UI_L_TEXT, UI_C_CYAN, UI_HDG_X0 + 24, UI_HDG_Y0 + 70, "HDG"); ui_ival(UI_L_TEXT, UI_C_WHITE, UI_HDG_X0 + 90, UI_HDG_Y0 + 66, (int32_t)h); ui_char(UI_L_TEXT, UI_C_CYAN, UI_HDG_X1 - 170, UI_HDG_Y0 + 70, "YAW"); 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++) { 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; ui_line(UI_L_GAUGE, UI_C_CYAN, UI_LAD_CX - half, yy, UI_LAD_CX - 18, yy, 2); ui_line(UI_L_GAUGE, UI_C_CYAN, UI_LAD_CX + 18, yy, UI_LAD_CX + half, yy, 2); } ui_ival(UI_L_TEXT, UI_C_CYAN, UI_LAD_CX - UI_LAD_HALF_SHORT - 40, UI_LAD_CY - 10 * UI_LAD_PX_PER_DEG - 10, 10); ui_ival(UI_L_TEXT, UI_C_CYAN, UI_LAD_CX + UI_LAD_HALF_SHORT + 12, UI_LAD_CY - 10 * UI_LAD_PX_PER_DEG - 10, 10); { int py = UI_LAD_CY - (int)(d->pitch_deg * UI_LAD_PX_PER_DEG); ui_line(UI_L_GAUGE, UI_C_ORANGE, UI_LAD_CX - UI_LAD_HALF_LONG - 20, py, UI_LAD_CX - UI_LAD_HALF_LONG - 4, py, 4); ui_char(UI_L_TEXT, UI_C_ORANGE, UI_LAD_CX - UI_LAD_HALF_LONG - 132, py - 10, "PIT"); 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); ui_char(UI_L_TEXT, UI_C_CYAN, UI_FRIC_X0 + 24, UI_FRIC_Y0 + 34, "FRIC"); ui_ival(UI_L_TEXT, UI_C_WHITE, UI_FRIC_X0 + 24, UI_FRIC_Y0 + 74, (int32_t)d->fric_l_rpm); 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"); 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) ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_SPIN_X0 + 160, UI_SPIN_Y0 + 26, UI_SPIN_X0 + 268, UI_SPIN_Y0 + 66); 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"); ui_flush(); if (!ui_ready) { ui_ready = 1U; ui_op = UI_OP_MOD; } }