宏定义和宏编译
// 以下是宏定义的一些用法
11.4.2 #if defined… #else …#endif 此编译指令为#ifdef … #else …#endif的等价编译指令。其语法格式如下: #if defined 常量表达式 代码段1 #else 代码段2 #endif 或 #if defined (常量表达式) 代码段1 #else 代码段2 #endif 此编译指令的简单形式为单分支的条件编译指令,其语法形式如下: #if defined常量表达式 代码段1 #endif
因为对于一个大程序而言,我们可能要定义很多常量( 不管是放在源文件还是头文件 ),那么我们有时考虑定义某个常量时,我们就必须返回检查原来此常量是否定义,但这样做很麻烦.if defined宏正是为这种情况提供了解决方案.举个例子,如下: #define .... #define .... ........ ........ #define a 100 ....... 此时,我们要检查a是否定义(假设我们已经记不着这点了),或者我们要给a一个不同的值,就加入如下句子 #if defined a #undef a #define a 200 #endif 上述语句检验a是否被定义,如果被定义,则用#undef语句解除定义,并重新定义a为200 同样,检验a是否定义: #ifndef a //如果a没有被定义 #define a 100 #endif 以上所用的宏中:#undef为解除定义,#ifndef是if not defined的缩写,即如果没有定义。 这就是#if defined 的唯一作用!
#include <lcm_drv.h> extern LCM_DRIVER nt35510_lcm_drv; extern LCM_DRIVER hx8369_lcm_drv; extern LCM_DRIVER bm8578_lcm_drv; extern LCM_DRIVER nt35582_mcu_lcm_drv; extern LCM_DRIVER hx8357b_lcm_drv; extern LCM_DRIVER hx8369_dsi_lcm_drv; extern LCM_DRIVER hx8369_hvga_lcm_drv; extern LCM_DRIVER ili9481_lcm_drv; extern LCM_DRIVER nt35582_lcm_drv; extern LCM_DRIVER s6d0170_lcm_drv; extern LCM_DRIVER spfd5461a_lcm_drv; extern LCM_DRIVER ta7601_lcm_drv; extern LCM_DRIVER tft1p3037_lcm_drv; extern LCM_DRIVER zte6516_lcm_drv; extern LCM_DRIVER r61408_lcm_drv; extern LCM_DRIVER lg4572b_lcm_drv; LCM_DRIVER* lcm_driver_list[] = { #if defined(LCM_DRIVER_CUST) &nt35510_lcm_drv,// 0 &r61408_lcm_drv, //1 &lg4572b_lcm_drv,// 2 &hx8369_lcm_drv,//3 #endif #if defined(NT35510) &nt35510_lcm_drv, #endif #if defined(NT35510) &nt35510_lcm_drv, #endif #if defined(HX8369) &hx8369_lcm_drv, #endif #if defined(BM8578) &bm8578_lcm_drv, #endif #if defined(NT35582_MCU) &nt35582_mcu_lcm_drv, #endif #if defined(HX8357B) &hx8357b_lcm_drv, #endif #if defined(HX8369_DSI) &hx8369_dsi_lcm_drv, #endif #if defined(HX8369_HVGA) &hx8369_hvga_lcm_drv, #endif #if defined(ILI9481) &ili9481_lcm_drv, #endif #if defined(NT35582) &nt35582_lcm_drv, #endif #if defined(S6D0170) &s6d0170_lcm_drv, #endif #if defined(SPFD5461A) &spfd5461a_lcm_drv, #endif #if defined(TA7601) &ta7601_lcm_drv, #endif #if defined(TFT1P3037) &tft1p3037_lcm_drv, #endif #if defined(ZTE6516) &zte6516_lcm_drv, #endif #if defined(HX8357B_LENOVO) &hx8357b_lcm_drv, #endif #if defined(R61408) &r61408_lcm_drv, #endif #if defined(LG4572B) &lg4572b_lcm_drv #endif }; #define LCM_COMPILE_ASSERT(condition) LCM_COMPILE_ASSERT_X(condition, __LINE__) #define LCM_COMPILE_ASSERT_X(condition, line) LCM_COMPILE_ASSERT_XX(condition, line) #define LCM_COMPILE_ASSERT_XX(condition, line) char assertion_failed_at_line_##line[(condition)?1:-1] unsigned int lcm_count = sizeof(lcm_driver_list)/sizeof(LCM_DRIVER*); LCM_COMPILE_ASSERT(0 != sizeof(lcm_driver_list)/sizeof(LCM_DRIVER*));
extern LCM_DRIVER* lcm_driver_list[]; extern unsigned int lcm_count; const char str[]="JBLCM#1#AAAA"; LCM_DRIVER *disp_drv_get_lcm_driver(const char* lcm_name) { LCM_DRIVER *lcm = NULL; printf("[LCM Auto Detect], we have %d lcm drivers built in\n", lcm_count); int g_lcm_number; g_lcm_number = (int)(str[6])-'0'; #if defined(LCM_DRIVER_CUST) { if (g_lcm_number>-1) { lcm = lcm_driver_list[g_lcm_number];// 0 lcm->set_util_funcs(&lcm_utils); lcm->get_params(&s_lcm_params); lcm_params = &s_lcm_params; lcm_drv = lcm; isLCMFound = TRUE; printf("[LCM Specified]\t[%s]\n", (lcm->name==NULL)?"unknown":lcm->name); goto done; } else { LCM_PARAMS s_lcm_params_new = {0}; LCM_PARAMS *lcm_params_new = &s_lcm_params_new; lcm = lcm_driver_list[0]; memset((void*)lcm_params, 0, sizeof(LCM_PARAMS)); lcm->get_params(lcm_params); int i; for(i = 1;i < lcm_count;i++) { memset((void*)lcm_params_new, 0, sizeof(LCM_PARAMS)); lcm_driver_list[i]->get_params(lcm_params_new); if((lcm_params->width * lcm_params->height) > (lcm_params_new->width * lcm_params_new->height)) { lcm = lcm_driver_list[i]; memset((void*)lcm_params, 0, sizeof(LCM_PARAMS)); lcm->get_params(lcm_params); } } } printf("[LCM Auto Detect] no lcm device found\n"); printf("[LCM Auto Detect] we will using the lcm driver with minimum resolution\n"); // if we are here, it means: // 1.there is no correct driver for the lcm used; // 2.there is no lcm device connected. // we will find the lcm with minimum resolution to use, // in case physical memory is not enough // goto done; } #else { if(lcm_count ==1) { // we need to verify whether the lcm is connected // even there is only one lcm type defined lcm = lcm_driver_list[0]; lcm->set_util_funcs(&lcm_utils); lcm->get_params(&s_lcm_params); lcm_params = &s_lcm_params; lcm_drv = lcm; isLCMFound = TRUE; printf("[LCM Specified]\t[%s]\n", (lcm->name==NULL)?"unknown":lcm->name); goto done; } else { int i; for(i = 0;i < lcm_count;i++) { lcm_params = &s_lcm_params; lcm = lcm_driver_list[i]; printf("[LCM Auto Detect] [%d] - [%s]\t", i, (lcm->name==NULL)?"unknown":lcm->name); lcm->set_util_funcs(&lcm_utils); memset((void*)lcm_params, 0, sizeof(LCM_PARAMS)); lcm->get_params(lcm_params); disp_drv_init_ctrl_if(); disp_drv_set_driving_current(lcm_params); disp_drv_init_io_pad(lcm_params); if(lcm_name != NULL) { if(!strcmp(lcm_name,lcm->name)) { printf("\t\t[success]\n"); isLCMFound = TRUE; lcm_drv = lcm; goto done; } else { printf("\t\t[fail]\n"); } } else { if(lcm->compare_id != NULL && lcm->compare_id()) { printf("\t\t[success]\n"); isLCMFound = TRUE; lcm_drv = lcm; goto done; } else { printf("\t\t[fail]\n"); } } } printf("[LCM Auto Detect] no lcm device found\n"); printf("[LCM Auto Detect] we will using the lcm driver with minimum resolution\n"); // if we are here, it means: // 1.there is no correct driver for the lcm used; // 2.there is no lcm device connected. // we will find the lcm with minimum resolution to use, // in case physical memory is not enough { LCM_PARAMS s_lcm_params_new = {0}; LCM_PARAMS *lcm_params_new = &s_lcm_params_new; lcm = lcm_driver_list[0]; memset((void*)lcm_params, 0, sizeof(LCM_PARAMS)); lcm->get_params(lcm_params); for(i = 1;i < lcm_count;i++) { memset((void*)lcm_params_new, 0, sizeof(LCM_PARAMS)); lcm_driver_list[i]->get_params(lcm_params_new); if((lcm_params->width * lcm_params->height) > (lcm_params_new->width * lcm_params_new->height)) { lcm = lcm_driver_list[i]; memset((void*)lcm_params, 0, sizeof(LCM_PARAMS)); lcm->get_params(lcm_params); } } } } }