static int lastPicture = 0;static void SendToPanel_Location_Menu(void) { uint8_t abBuff_menu[] = {0xEE,0xB1,0x00,0x00,0x04,0xFF,0xFC,0xFF,0xFF}; uint8_t abBuff_home[] = {0xEE,0xB1,0x00,0x00,0x01,0xFF,0xFC,0xFF,0xFF}; //----- transmit ----- if (!lastPicture) { UART_SendString(COM_PANEL, abBuff_menu, sizeof(abBuff_menu), 0); lastPicture = 1; } else if (lastPicture == 1)//kk { UART_SendString(COM_PANEL, abBuff_home, sizeof(abBuff_home), 0); lastPicture = 0; } }上面这段代码优化
时间: 2024-03-29 14:40:34 浏览: 55
static int lastPicture = 0;
static const uint8_t abBuff_menu[] = {0xEE,0xB1,0x00,0x00,0x04,0xFF,0xFC,0xFF,0xFF};
static const uint8_t abBuff_home[] = {0xEE,0xB1,0x00,0x00,0x01,0xFF,0xFC,0xFF,0xFF};
static void SendToPanel_Location_Menu(void) {
//----- transmit -----
if (!lastPicture) {
UART_SendString(COM_PANEL, abBuff_menu, sizeof(abBuff_menu), 0);
lastPicture = 1;
} else {
UART_SendString(COM_PANEL, abBuff_home, sizeof(abBuff_home), 0);
lastPicture = 0;
}
}
主要优化:
1. 将常量数组设置为 const,防止误修改;
2. 将常量数组声明放到函数外部,避免每次函数调用时重新创建数组;
3. 函数内部使用 if-else 代替 if-if,提高代码可读性。
阅读全文