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 18:40:34 浏览: 9
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,提高代码可读性。
相关问题
TIM_HandleTypeDef Tim3Handle; uint32_t uwPrescalerValue = 0; static bool s_bTimer3Trigger = false; static uint8_t s_Timer3Count = 0; static bool s_Timer3Trigger_1ms = false;这些变量有什么用
这些变量看起来是在使用STM32的HAL库来控制定时器(TIM3)。具体来说:
- `TIM_HandleTypeDef Tim3Handle;`是一个结构体变量,用于存储TIM3的配置和状态信息。
- `uint32_t uwPrescalerValue = 0;`是一个变量,用于存储TIM3的预分频值。
- `static bool s_bTimer3Trigger = false;`是一个标志位变量,用于表示TIM3是否已经触发。
- `static uint8_t s_Timer3Count = 0;`是一个计数器变量,用于记录TIM3触发的次数。
- `static bool s_Timer3Trigger_1ms = false;`是另一个标志位变量,用于表示TIM3是否已经触发了1ms。
这些变量的具体用途需要结合代码来分析。
static void FM1701_Write_Reg(uint8_t Address, uint8_t Dat)
这是一个函数定义,用于向FM1701芯片写入寄存器的值。
函数名:`FM1701_Write_Reg`
参数:
- `Address`:要写入的寄存器地址,类型为 `uint8_t`。
- `Dat`:要写入的数据值,类型为 `uint8_t`。
函数体内的具体实现需要根据FM1701芯片的通信协议和写入操作的要求进行编写。一般来说,这个函数会根据协议将 `Address` 和 `Dat` 值发送给FM1701芯片,实现写入操作。
以下是一个示例函数的框架:
```c
static void FM1701_Write_Reg(uint8_t Address, uint8_t Dat) {
// 根据FM1701的通信协议,实现写入操作
// 将Address和Dat发送给FM1701芯片
// ...
}
```
具体实现中需要根据FM1701的技术文档和数据手册,了解相关的通信协议和寄存器写入操作的细节,并使用适当的接口或通信方式与FM1701芯片进行通信。