正好昨天论坛有童鞋问了一个问题,这是C语音的一个小细节。如下:ok case1:用的比较少 直接用的 struct{ }ringbuf;
- #define Buffer_MAX_SIZE 10
- struct{
- unsigned int headPosition;
- unsigned int tailPosition;
- unsigned int *Dat_Buf[Buffer_MAX_SIZE];
- }ringbuf;
- int main()
- {
- ringbuf.headPosition=2;
- }
复制代码 ok case2:墙裂推荐用法 typedef struct { }ringbuf; ringbuf Ringbuf;
- #define Buffer_MAX_SIZE 10
- typedef struct {
- unsigned int headPosition;
- unsigned int tailPosition;
- unsigned int *Dat_Buf[Buffer_MAX_SIZE];
- }ringbuf;
- ringbuf Ringbuf;
- int main()
- {
- Ringbuf.headPosition=2;
- }
复制代码 ok case3:类似于上面 也就是typedef struct _ringbuf{}ringbuf;的 _ringbuf可有可无
- <blockquote>#define Buffer_MAX_SIZE 10
复制代码 好了 开始机器风格的第三章,跟第二章一样,但是他更加简洁。第二章用的是case3 本章我们用case2
第一部分:框架
- typedef struct
- {
- KeyParmaType KeyParma;
- DeviceFixedPwsType DevicePwd;
- PwdSavePosType PwdSavePos;
- PwdSaveVarType PwdSaveVar;
- }_TouchKeyVarType;
- typedef struct
- {
- void (*AddOpenLog)( void *pL , BYTE *UsePwd);
- void (*AddTempPwd)( void *pL , BYTE *ucPwd , DWORD dTimeStamp);
- void (*TouchI2CInit)( void);
- void (*TouchLightInit)( void);
- void (*LightOn)(void);
- void (*LightOff)(void);
- void (*ParmaClear)(void *pL);
- BYTE (*InspectPwd)(void *pL , BYTE *UserPwd , BYTE *InputPwd , BYTE len);
- WORD (*ReadTouchIc)(void);//Read value form touch ic
- TouchKeyValeType (*ReadKeyVaule )( void *pL); //Read key value
- }_TouchKeyConstType;
- typedef struct
- {
- _TouchKeyConstType *TKConst;
- _TouchKeyVarType *TKVar;
- }_TouchKeyHandleType , *TouchKeyHandleType;
复制代码 可以看到 它的风格_TouchKeyVarType这种,_ 开头的就直接是名字了。最后封在一起
_TouchKeyHandleType , *TouchKeyHandleType;----------用_ 和 名字区分
前面第二章是 BeeperType, *PBeeper;--------------------用Type和*p名字区分
第二部分:实例化
- _TouchKeyVarType TouchKeyVar;
复制代码 一样的 数据成员暂时不管
方法成员则实例化 比如下面的实现
void SetTouchLightOpen ( void )
{
GPIO_SetBits ( TOUCH_LIGHT_PORT, TOUCH_LIGHT_PIN );
}
第三部:使用
现在gTKHandle就是一个全局变量了,它是一个句柄,(不是指针它是实体),它包含password的全部属性,拿去用吧。
如果喜欢指针怎么办?
- void vKeyInputTask ( void * pvParameters )
- {
- TouchKeyHandleType pHandle = &gTKHandle;//指针出场
复制代码
最后详细说一个案例:注意ReadKeyVaule
1
void vKeyInputTask ( void * pvParameters )
{
TouchKeyHandleType pHandle = &gTKHandle;//指针出场
KeyParmaType *KeyData = &pHandle->TKVar->KeyParma;
BYTE ic_door = 0;
pwd_log ( "Into touch key task ,password is init...\r\n" );
PasswordParmaInit ( pHandle );
while ( 1 )
{
if ( xSemaphoreTake ( xBinarySemaphoreTouchPwd, portMAX_DELAY ) == pdTRUE )
{
if ( KeyData->ucInputKeyFlag == INPUT_KEY )
{
TouchKeyValeType KeyValue = KEY_INIT;
KeyData->ucInputKeyFlag = NO_KEY;
KeyValue = pHandle->TKConst->ReadKeyVaule ( pHandle );---完成死循环 定时轮训这个函数。
。。。}
2
前面已经初始化了
_TouchKeyConstType TouchKeyConst =
{
.AddTempPwd = GetTempPwd,
.ReadKeyVaule = ReadTouchKeyVaule,
。。。};
3
我们看ReadTouchKeyVaule的实现(你无法找到它的调用的啦!!)
- TouchKeyValeType ReadTouchKeyVaule ( void *pL )
- {
- TouchKeyHandleType pHandle = ( TouchKeyHandleType ) pL;
- WORD ReadVal = 0;
- TouchKeyValeType ucKey = KEY_INIT;
- ReadVal = pHandle->TKConst->ReadTouchIc();
- switch ( ReadVal )
- {
- case 0x0001:
- ucKey = KEY_ENTER;
- pwd_log ( "Input # key \r\n" );
- break;
- case 0x0002:
- ucKey = KEY_0;
- pwd_log ( "Input 0 key \r\n" );
- break;
- case 0x0004:
- ucKey = KEY_BACK;
- pwd_log ( " Input * key \r\n" );
- break;
- case 0x0008:
- ucKey = KEY_9;
- pwd_log ( " Input 9 key \r\n" );
- break;
- case 0x0010:
- ucKey = KEY_8;
- pwd_log ( " Input 8 key \r\n" );
- break;
- case 0x0020:
- ucKey = KEY_7;
- pwd_log ( " Input 7 key \r\n" );
- break;
- case 0x0040:
- ucKey = KEY_6;
- pwd_log ( " Input 6 key \r\n" );
- break;
- case 0x0080:
- ucKey = KEY_5;
- pwd_log ( " Input 5 key \r\n" );
- break;
- case 0x0100:
- ucKey = KEY_4;
- pwd_log ( " Input 4 key \r\n" );
- break;
- case 0x0200:
- ucKey = KEY_3;
- pwd_log ( " Input 3 key \r\n" );
- break;
- case 0x0400:
- ucKey = KEY_2;
- pwd_log ( " Input 2 key \r\n" );
- break;
- case 0x0800:
- ucKey = KEY_1;
- pwd_log ( " Input 1 key \r\n" );
- break;
- default :
- pwd_log ( "Read error value = %x.\r\n", ReadVal );
- break;
- }
- BeepRunCycle ( gpBeeper, 1, 50, 10 );
- return ucKey;
- }
复制代码 隔一段时间就执行一下,知道你按密码了。类似于刷卡板实时轮训,知道你刷卡了。
是的 上面的函数嵌套了按键板的IIC读函数,同上
- #define DEVICE_ID 0xA0
- WORD ReadTouchKey(void)
- {
- BYTE uD[4]={0x00,0x00,0x00,0x00};
- uint16_t touchDataTemp = 0;
- //LED_Backlight_Out(1);
- //iic_read_all(DEVICE_ID,8,4,uD);
- TouchKeyReadValue(&gTouchKeyI2c,0xA0,8 ,uD , 4);
- touchDataTemp = uD[1];
- touchDataTemp <<= 8;
- touchDataTemp |= uD[0];
- return touchDataTemp;
- }
复制代码
|
老大让我给产品提提建议
我。。。。。
我就说我们看到的吧
编码风格