/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file screen.c * @brief 串口屏驱动实现(JC系列),使用 USART1 ****************************************************************************** */ /* USER CODE END Header */ #include "screen.h" #include "main.h" #include "usart.h" #include "cmsis_os.h" #include #include /* 模块初始化:不要使用 MP3 的二进制初始化指令,改为屏幕清屏作为默认初始化 */ /* 发送缓冲 */ static char txbuf[256]; /** * @brief 发送字符串到串口屏(阻塞直到发送完成) * @param cmd: 要发送的C字符串(不强制包含结尾CR/LF) * @retval 0=OK, <0=错误 */ int Screen_SendCmd(const char *cmd) { if (cmd == NULL) return -1; size_t len = strlen(cmd); if (len == 0) return -1; HAL_StatusTypeDef st = HAL_UART_Transmit(&huart1, (uint8_t *)cmd, (uint16_t)len, HAL_MAX_DELAY); return (st == HAL_OK) ? 0 : -2; } /** * @brief 发送命令并等待模块返回 "OK\r\n",超时返回非0 */ int Screen_SendCmdWaitOK(const char *cmd, uint32_t timeout_ms) { (void)timeout_ms; /* 保留接口:当前不等待屏幕回包,直接发送后返回发送状态 */ return Screen_SendCmd(cmd); } void Screen_Init(void) { /* * 屏幕初始化:简单实现为上电等待并清除屏幕 * 若需要按照厂商例程发送更多配置指令,可在此扩展 */ osDelay(1000); Screen_Clear(0); osDelay(200); } /* 简单封装接口示例 */ void Screen_Clear(uint8_t layer) { sprintf(txbuf, "DIR(1);CLR(%u);\r\n", (unsigned)layer); Screen_SendCmd(txbuf); } void Screen_DisplayImage(uint32_t addr) { sprintf(txbuf, "DIR(1);FSIMG(%lu,0,0,128,128,0);\r\n", (unsigned long)addr); Screen_SendCmd(txbuf); } void Screen_SetBrightness(uint8_t val) { sprintf(txbuf, "BL(%u);\r\n", (unsigned)val); Screen_SendCmd(txbuf); } void Screen_DrawText16(uint16_t x, uint16_t y, const char *text, uint8_t color) { sprintf(txbuf, "SBC(%u);DC16(%u,%u,'%s',%u);\r\n", (unsigned)color, (unsigned)x, (unsigned)y, text, (unsigned)color); Screen_SendCmd(txbuf); } /* 额外封装实现 */ /* 以下为屏幕命令封装函数实现,均以字符串形式发送到串口屏 */ /* 函数内部不等待屏幕回包,调用者若需确认执行结果请自行实现回包处理 */ void Screen_SetRotation(uint8_t dir) { sprintf(txbuf, "DIR(%u);\r\n", dir); Screen_SendCmd(txbuf); } void Screen_SetBaud(uint32_t baud) { sprintf(txbuf, "BPS(%lu);\r\n", (unsigned long)baud); Screen_SendCmd(txbuf); } void Screen_DisplayImageAtAddr(uint32_t addr, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t mode) { sprintf(txbuf, "FSIMG(%lu,%u,%u,%u,%u,%u);\r\n", (unsigned long)addr, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, (unsigned)mode); Screen_SendCmd(txbuf); } void Screen_DisplayImageByIndex(uint32_t index, uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t mode) { sprintf(txbuf, "FSIMG(%u,%u,%u,%u,%u,%u);\r\n", (unsigned)index, (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, (unsigned)mode); Screen_SendCmd(txbuf); } void Screen_Pixel(uint16_t x, uint16_t y, uint8_t color) { sprintf(txbuf, "PS(%u,%u,%u);\r\n", (unsigned)x, (unsigned)y, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_Line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t color) { sprintf(txbuf, "PL(%u,%u,%u,%u,%u);\r\n", (unsigned)x1, (unsigned)y1, (unsigned)x2, (unsigned)y2, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_Box(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t color) { sprintf(txbuf, "BOX(%u,%u,%u,%u,%u);\r\n", (unsigned)x1, (unsigned)y1, (unsigned)x2, (unsigned)y2, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_BoxFill(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint8_t color) { sprintf(txbuf, "BOXF(%u,%u,%u,%u,%u);\r\n", (unsigned)x1, (unsigned)y1, (unsigned)x2, (unsigned)y2, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_Circle(uint16_t x, uint16_t y, uint16_t r, uint8_t color) { sprintf(txbuf, "CIR(%u,%u,%u,%u);\r\n", (unsigned)x, (unsigned)y, (unsigned)r, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_CircleFill(uint16_t x, uint16_t y, uint16_t r, uint8_t color) { sprintf(txbuf, "CIRF(%u,%u,%u,%u);\r\n", (unsigned)x, (unsigned)y, (unsigned)r, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_SetBGColor(uint8_t color) { sprintf(txbuf, "SBC(%u);\r\n", (unsigned)color); Screen_SendCmd(txbuf); } void Screen_DrawText16V(uint16_t x, uint16_t y, const char *text, uint8_t color) { sprintf(txbuf, "DCV16(%u,%u,'%s',%u);\r\n", (unsigned)x, (unsigned)y, text, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_DrawText24(uint16_t x, uint16_t y, const char *text, uint8_t color) { sprintf(txbuf, "DC24(%u,%u,'%s',%u);\r\n", (unsigned)x, (unsigned)y, text, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_DrawText24V(uint16_t x, uint16_t y, const char *text, uint8_t color) { sprintf(txbuf, "DCV24(%u,%u,'%s',%u);\r\n", (unsigned)x, (unsigned)y, text, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_DrawText32(uint16_t x, uint16_t y, const char *text, uint8_t color) { sprintf(txbuf, "DC32(%u,%u,'%s',%u);\r\n", (unsigned)x, (unsigned)y, text, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_DrawText32V(uint16_t x, uint16_t y, const char *text, uint8_t color) { sprintf(txbuf, "DCV32(%u,%u,'%s',%u);\r\n", (unsigned)x, (unsigned)y, text, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_DrawText48(uint16_t x, uint16_t y, const char *text, uint8_t color, uint8_t transparent) { sprintf(txbuf, "DC48(%u,%u,'%s',%u,%u);\r\n", (unsigned)x, (unsigned)y, text, (unsigned)color, (unsigned)transparent); Screen_SendCmd(txbuf); } void Screen_Button(uint16_t x, uint16_t y, uint16_t w, uint16_t h, const char *label, uint8_t fontColor, uint8_t bgColor, uint8_t pressFlag, uint8_t style) { sprintf(txbuf, "BTN(%u,%u,%u,%u,'%s',%u,%u,%u,%u);\r\n", (unsigned)x, (unsigned)y, (unsigned)w, (unsigned)h, label, (unsigned)fontColor, (unsigned)bgColor, (unsigned)pressFlag, (unsigned)style); Screen_SendCmd(txbuf); } void Screen_QRCode(uint16_t x, uint16_t y, const char *url) { sprintf(txbuf, "QRCODE(%u,%u,%s);\r\n", (unsigned)x, (unsigned)y, url); Screen_SendCmd(txbuf); } void Screen_QRCodeEx(uint16_t x, uint16_t y, const char *url, uint16_t size, uint8_t color) { sprintf(txbuf, "QRCODE(%u,%u,%s,%u,%u);\r\n", (unsigned)x, (unsigned)y, url, (unsigned)size, (unsigned)color); Screen_SendCmd(txbuf); } void Screen_Ver(void) { sprintf(txbuf, "VER();\r\n"); Screen_SendCmd(txbuf); } /* USER CODE BEGIN AdditionalFunctions */ /* 可以在这里扩展更多绘图/控件函数供应用层调用 */ /* USER CODE END AdditionalFunctions */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/