原创 基于相互关联的数据结构的技巧定义

2010-9-4 17:39 1849 10 10 分类: MCU/ 嵌入式

 最近在开发项目中,涉及到多维相关联常量表。基于传统的直接的方法就是多维常量表。


比如我们定义多维的向量表:


/* (name, mask, code, persistence, sympt, enlamp, fault_type */


XX_TAB[]={


   "A1_NUMB", 0X0001, 0X9567,DTC_FAIL, SYSTEMFAIL, LAMP_ON,SYS_TYPE,


  "A2_NUMB",0X00020,0X9568,DTC_SYSFAIL,SYSTEMSENSORFAIL,LAMP_ON,SENSOR_TYPE,


  "A3_NUMB",0X0040,0X9578,DTC_LRSENSOR,SYSSENSORFAIL,LAMP_ON,SENSOR_TYPE


 };


 


这里笔者发现一种简洁的构造表达的方法:


我们分三步来实现表达:



1。先建立 一个列表文件:FL_list.h


/****************************************************************************


 * File        : FL_list.h


 * Project     : 


 * Description  :   Fault Enum, code and mask defined.


 * Copyright   :


 ***************************************************************************/ 


 


 #ifdef FAULT_ENUM


  #define FAULT(name, mask, code, persistence, sympt, enlamp, fault_type)   \


                                                                                                                       name##_ENUM,


 #endif 


 #ifdef FAULT_MASK


  #define FAULT(name, mask, code, persistence, sympt, enlamp, fault_type)    \


                                                                                                                       (unsigned long)mask,


 #endif


 #ifdef FAULT_CODE


  #define FAULT(name, mask, code, persistence, sympt, enlamp, fault_type)    \


                                                                                                                       (unsigned short)code,


 #endif


 #ifdef FAULT_RETAIN


  #define FAULT(name, mask, code, persistence, sympt, enlamp, fault_type)     \


                                                                                                                       (unsigned char)persistence,


 #endif


 #ifdef FAULT_SYMPT


  #define FAULT(name, mask, code, persistence, sympt, enlamp, fault_type)     \


                                                                                                                       (unsigned char)sympt,


 #endif


  #ifdef FAULT_ENLAMP


  #define FAULT(name, mask, code, persistence, sympt, enlamp, fault_type)      \


                                                                                                                       (unsigned char)enlamp,


 #endif


 #ifdef FAULT_TYPE


  #define FAULT(name, mask, code, persistence, sympt, enlamp, fault_type)      \


                                                                                                                       (unsigned char)fault_type,


 #endif


 


 


 


 


 /*1*/FAULT( BATTERY_LOW,  0x00000002, 0x5406, DTC_NOT_PERSISTENT, DTC_BELOW_MIN_THRESHOLD,     DTC_TPMS_LAMP_ON, FLT_SYS                                  )


 /*2*/FAULT( BATTERY_HIGH,     0x00000004, 0x5407, DTC_NOT_PERSISTENT, DTC_ABOVE_MAX_THRESHOLD, DTC_TPMS_LAMP_ON, FLT_SYS                                  )


 /*3*/FAULT( SELF_CHECK,  0x00000008, 0x5800, DTC_NOT_PERSISTENT, DTC_INVALID_SIGNAL, DTC_TPMS_LAMP_ON, FLT_SYS                                )


 /*4*/FAULT( SENSOR_NOT_CALIBRATED,    0x00000010, 0x5802, DTC_NOT_PERSISTENT, DTC_INVALID_SIGNAL, DTC_TPMS_LAMP_ON, FLT_SYS      )


 /*5*/FAULT( EEPROM_CHECKSUM_FAILURE, 0x00000010, 0x5801, DTC_NOT_PERSISTENT, DTC_INVALID_SIGNAL, DTC_TPMS_LAMP_ON, FLT_SYS                                  )


 


 /*6*/FAULT( SENSOR_FL_BATTERY_LOW,  0x00010000, 0x5807, DTC_PERSISTENT, DTC_BELOW_MIN_THRESHOLD, DTC_TPMS_LAMP_ON, FLT_FL_SENSOR         )


 /*7*/FAULT( SENSOR_FR_BATTERY_LOW,  0x00010000, 0x5808, DTC_PERSISTENT, DTC_BELOW_MIN_THRESHOLD, DTC_TPMS_LAMP_ON, FLT_FR_SENSOR         )


 /*8*/FAULT( SENSOR_RL_BATTERY_LOW,  0x00010000, 0x5809, DTC_PERSISTENT, DTC_BELOW_MIN_THRESHOLD, DTC_TPMS_LAMP_ON, FLT_RL_SENSOR         )


 /*9*/FAULT( SENSOR_RR_BATTERY_LOW,  0x00010000, 0x5810, DTC_PERSISTENT, DTC_BELOW_MIN_THRESHOLD, DTC_TPMS_LAMP_ON, FLT_RR_SENSOR        )


 


 /*10*/FAULT( SENSOR_FL_NOT_TRANSMITTING, 0x00020000, 0x5803, DTC_PERSISTENT, DTC_NOT_TRANSMITTING, DTC_TPMS_LAMP_ON, FLT_FL_SENSOR         )


 /*11*/FAULT( SENSOR_FR_NOT_TRANSMITTING, 0x00020000, 0x5804, DTC_PERSISTENT, DTC_NOT_TRANSMITTING, DTC_TPMS_LAMP_ON, FLT_FR_SENSOR         )


 /*12*/FAULT( SENSOR_RL_NOT_TRANSMITTING, 0x00020000, 0x5805, DTC_PERSISTENT, DTC_NOT_TRANSMITTING, DTC_TPMS_LAMP_ON, FLT_RL_SENSOR         )


 /*13*/FAULT( SENSOR_RR_NOT_TRANSMITTING, 0x00020000, 0x5806, DTC_PERSISTENT, DTC_NOT_TRANSMITTING, DTC_TPMS_LAMP_ON, FLT_RR_SENSOR        )


 


 


 


 #undef FAULT /*(name, mask, code, persistence, sympt, enlamp, fault_type) 


 #undef FAULT_ENUM


 #undef FAULT_MASK


 #undef FAULT_CODE


 #undef FAULT_RETAIN


 #undef FAULT_SYMPT


 #undef FAULT_ENLAMP


 #undef FAULT_TYPE


 


/******************************End of File**********************************/


 


2。 头文件定义引用的常量


  在另外的头文件内,这样定义引用


使用一个采用符号的枚举变量来定位索引,便于后面的数据结构的定位:


typedef enum
{
   #define   FAULT_ENUM
   #include "FL_list.h"
} T_FAULT;


这里通过采用枚举变量类型定义,多维数组通过常量变量实现了数组的偏移定位,同时达到名字易于理解的目的。


 


3。 原代码内定义常量数组


#define NUMBER_OF_DTC      13


 


 static const uint32 FLT_u32MaskArray [NUMBER_OF_DTC] =
{
   #define   FAULT_MASK
   #include "fault_list.h"
};


static const uint8 FLT_u8FaultType [NUMBER_OF_DTC] =
{
   #define   FAULT_TYPE
   #include "fault_list.h"
};


 


const uint8 FLT_u8Persistence [NUMBER_OF_DTC] =


{


   #define   FAULT_RETAIN


   #include "fault_list.h"


};


 


const uint8 FLT_u8Symptom [NUMBER_OF_DTC] =


{


   #define   FAULT_SYMPT


   #include "fault_list.h"


};


 


const uint8 FLT_u8EnLamp [NUMBER_OF_DTC] =


{


   #define   FAULT_ENLAMP


   #include "fault_list.h"


};


 


这里,可以看到我们通过T_FAULT 枚举常量,可以定位后面的 FLT_u32MaskArray ,FLT_u8FaultType 等常量。


程序的引用采用与实际表示的对象对应。简单,明了。



 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
10
关闭 站长推荐上一条 /3 下一条