优化这段代码 sprintf(szVal, "%0.3f" , get_ActionDZInfo_val(UNIT_GAP_RT1064,gapid,DBData[i])); LCD_DisString((i%9)+1, 19, szVal); if (DBData[i] >= RT1064_YS_GL1 && DBData[i] <= RT1064_YS_FBS_JY) LCD_DisString((i%9)+1, 25, (char *)"S"); else if ((DBData[i] >= RT1064_DZ_GL1 && DBData[i] <= RT1064_DZ_I02) || ((DBData[i] >= RT1064_DZ_PHASE_I && DBData[i] <= RT1064_DZ_I0DLT) && DBData[i] != RT1064_DZ_YL_HAR && DBData[i] != RT1064_DZ_LMJ) || DBData[i] == RT1064_DZ_SD || DBData[i] == RT1064_DZ_I0HJS || DBData[i] == RT1064_DZ_FC_CHZ || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)|| DBData[i] == RT1064_DZ_FBS_OL) { LCD_DisString((i%9)+1, 25, (char *)"A"); } else if (DBData[i] == RT1064_DZ_LMJ || DBData[i] == RT1064_DZ_JC || DBData[i] == RT1064_DZ_GYJC) { LCD_DisString((i%9)+1, 25, (char *)"°"); } else if (DBData[i] == RT1064_DZ_YL_HAR) LCD_DisString((i%9)+1, 25, (char *)"%"); else if (DBData[i] == RT1064_DZ_IDS ||DBData[i] == RT1064_DZ_OPENCS || (DBData[i] >= RT1064_DZ_I03 && DBData[i] <= RT1064_DZ_FBS_I0)) { LCD_DisString((i%9)+1, 24, (char *)"次"); } else if (DBData[i] == RT1064_DZ_DP || DBData[i] == RT1064_DZ_GP) { LCD_DisString((i%9)+1, 25, (char *)"HZ"); } else LCD_DisString((i%9)+1, 25, (char *)"V"); if (not == 2) { if(draw == (i+1)) len = LCD_BitNot(UNIT_PROTECT,UNIT_GAP_RT1064,gapid,DBData[i],i,bit); }
时间: 2024-02-01 09:16:57 浏览: 79
可以考虑对这段代码进行如下优化:
1. 将一些常量定义为宏或枚举类型,增加代码可读性。
2. 将重复的代码抽象成函数,提高代码复用性。
3. 使用 switch-case 语句替换一系列的 if-else 语句,使代码更加简洁易懂。
4. 使用数组存储一些字符串,避免重复创建字符串,提高效率。
优化后的代码如下:
```
#define LCD_ROW_COUNT 9
#define LCD_COL_COUNT 25
enum DBDataType {
RT1064_YS_GL1,
RT1064_YS_FBS_JY,
RT1064_DZ_GL1,
RT1064_DZ_I02,
RT1064_DZ_PHASE_I,
RT1064_DZ_I0DLT,
RT1064_DZ_YL_HAR,
RT1064_DZ_LMJ,
RT1064_DZ_SD,
RT1064_DZ_I0HJS,
RT1064_DZ_FC_CHZ,
RT1064_DZ_I03,
RT1064_DZ_FBS_I0,
RT1064_DZ_FBS_OL,
RT1064_DZ_IDS,
RT1064_DZ_OPENCS,
RT1064_DZ_DP,
RT1064_DZ_GP,
DBDataCount
};
const char* suffixes[] = {"V", "S", "A", "°", "%", "次", "HZ"};
void displayDBData(int index, float val, int dbData) {
char szVal[8];
sprintf(szVal, "%0.3f", val);
LCD_DisString(index % LCD_ROW_COUNT + 1, 19, szVal);
switch (dbData) {
case RT1064_YS_GL1 ... RT1064_YS_FBS_JY:
LCD_DisString(index % LCD_ROW_COUNT + 1, 25, suffixes[1]);
break;
case RT1064_DZ_GL1 ... RT1064_DZ_I02:
case RT1064_DZ_PHASE_I ... RT1064_DZ_I0DLT:
case RT1064_DZ_SD:
case RT1064_DZ_I0HJS:
case RT1064_DZ_FC_CHZ:
case RT1064_DZ_I03 ... RT1064_DZ_FBS_I0:
case RT1064_DZ_FBS_OL:
LCD_DisString(index % LCD_ROW_COUNT + 1, 25, suffixes[2]);
break;
case RT1064_DZ_LMJ:
case RT1064_DZ_JC:
case RT1064_DZ_GYJC:
LCD_DisString(index % LCD_ROW_COUNT + 1, 25, suffixes[3]);
break;
case RT1064_DZ_YL_HAR:
LCD_DisString(index % LCD_ROW_COUNT + 1, 25, suffixes[4]);
break;
case RT1064_DZ_IDS:
case RT1064_DZ_OPENCS:
case RT1064_DZ_I03 ... RT1064_DZ_FBS_I0:
LCD_DisString(index % LCD_ROW_COUNT + 1, 24, suffixes[5]);
break;
case RT1064_DZ_DP:
case RT1064_DZ_GP:
LCD_DisString(index % LCD_ROW_COUNT + 1, 25, suffixes[6]);
break;
default:
LCD_DisString(index % LCD_ROW_COUNT + 1, 25, suffixes[0]);
}
if (not == 2 && draw == (index + 1)) {
len = LCD_BitNot(UNIT_PROTECT, UNIT_GAP_RT1064, gapid, dbData[i], i, bit);
}
}
```
阅读全文