From d71ec9b0bdda4c1f380f3e8cad12a408d23bf596 Mon Sep 17 00:00:00 2001 From: wuyangyong Date: Tue, 2 Feb 2010 07:10:13 +0000 Subject: [PATCH] update remote for stm32netradio git-svn-id: https://rt-thread.googlecode.com/svn/trunk@365 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- bsp/stm32_radio/key.c | 26 ++++--- bsp/stm32_radio/remote.c | 155 ++++++++++++++++++++++----------------- 2 files changed, 103 insertions(+), 78 deletions(-) diff --git a/bsp/stm32_radio/key.c b/bsp/stm32_radio/key.c index 2200bc9457..8fcfc58a2b 100644 --- a/bsp/stm32_radio/key.c +++ b/bsp/stm32_radio/key.c @@ -5,11 +5,11 @@ #include /* -key_enter PA0 -key_down PA1 -key_up PA2 -key_right PC2 -key_left PC3 +key_enter PF11 +key_down PG15 +key_up PG11 +key_right PG14 +key_left PG13 */ #define key_enter_GETVALUE() GPIO_ReadInputDataBit(GPIOF,GPIO_Pin_11) #define key_down_GETVALUE() GPIO_ReadInputDataBit(GPIOG,GPIO_Pin_15) @@ -23,10 +23,8 @@ extern void rem_encoder(struct rtgui_event_kbd * p); extern unsigned int rem_mode; /* from remote.c */ -static void key_thread_entry(void *parameter) +static void GPIO_Configuration(void) { - rt_time_t next_delay; - struct rtgui_event_kbd kbd_event; GPIO_InitTypeDef GPIO_InitStructure; /* init gpio configuration */ @@ -45,7 +43,15 @@ static void key_thread_entry(void *parameter) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOE,&GPIO_InitStructure); GPIO_SetBits(GPIOE,GPIO_Pin_2); +} + +static void key_thread_entry(void *parameter) +{ + rt_time_t next_delay; + struct rtgui_event_kbd kbd_event; + GPIO_Configuration(); + /* start remote */ rem_start(); /* init keyboard event */ @@ -123,11 +129,11 @@ static void key_thread_entry(void *parameter) } } +static rt_thread_t key_tid; void rt_hw_key_init(void) { - rt_thread_t key_tid; key_tid = rt_thread_create("key", key_thread_entry, RT_NULL, - 512, 30, 5); + 768, 30, 5); if (key_tid != RT_NULL) rt_thread_startup(key_tid); } diff --git a/bsp/stm32_radio/remote.c b/bsp/stm32_radio/remote.c index 0afb94e215..a08c007cd3 100644 --- a/bsp/stm32_radio/remote.c +++ b/bsp/stm32_radio/remote.c @@ -13,16 +13,22 @@ #include #include -unsigned int rem_mode = 0;//红外模式 0:没启动,1:自学习,2:正常解码 +/* 设置允许偏差,单位0.01ms */ +#define remote_deviation 15 +#define remote_code_len_max 100 + + +/* 红外模式 0:没启动,1:自学习,2:正常解码 */ +unsigned int rem_mode = 0; static unsigned int first_tick = 0; static unsigned int rx_count = 0; -static unsigned short rm_code[100]; +static unsigned short rm_code[remote_code_len_max]; struct rem_codes_typedef { unsigned int len; - unsigned short rem_code[100]; + unsigned short rem_code[remote_code_len_max]; }; struct rem_codes_typedef * p_rem_code_src = RT_NULL; @@ -33,7 +39,6 @@ static const char str4[]="KEY_RIGHT"; /* static const char str5[]="KEY_ENTER"; /* 确认 */ static const char str6[]="KEY_RETURN"; /* 返回 */ static const char * desc_key[6]= {str1,str2,str3,str4,str5,str6}; -#define wucha 15 /* tim5 configure */ @@ -132,12 +137,13 @@ void rem_start(void) NVIC_Configuration(); TIM5_Configuration(); - p_rem_code_src = rt_malloc( 1500 ); + p_rem_code_src = rt_malloc( sizeof(struct rem_codes_typedef)*6 ); + rt_memset(p_rem_code_src,0, sizeof(struct rem_codes_typedef)*6 ); /* 解读红外信息 */ { int fd,size; - char buf[6];//文件读取临时缓存 + char buf[7];/* 文件读取临时缓存 #####\r\n */ unsigned int i; unsigned short tmp; unsigned int read_index = 0; @@ -150,29 +156,32 @@ void rem_start(void) rt_kprintf("\r/resource/remote.txt打开成功"); while( EOF_flag ) { - //读取长度 - size = read(fd,buf,6); - if( (size == 6) && (buf[4]=='\r') && buf[5]=='\n' ) + /* 读取长度 */ + size = read(fd,buf,7); + if( (size == 7) && (buf[5]=='\r') && buf[6]=='\n' ) { - //转换得到样本数据长度 - tmp = (buf[0]-'0')*1000 - + (buf[1]-'0')*100 - + (buf[2]-'0')*10 - + (buf[3]-'0'); + /* 转换得到样本数据长度 */ + tmp = (buf[0]-'0')*10000 + + (buf[1]-'0')*1000 + + (buf[2]-'0')*100 + + (buf[3]-'0')*10 + + (buf[4]-'0'); if( tmp<100 ) { unsigned int code_len = tmp; p_rem_code_src[read_index].len = code_len; - //如果样本长度符合 + /* 如果样本长度符合 就开始从文件读取编码数据 */ for(i=0; i 0 && p_rem_code_src[0].len < remote_code_len_max ) + { + /* 设置工作模式为正常识别模式 */ + rem_mode = 2; + rt_kprintf("\r红外遥控编码文件解读完成,已打开红外遥控功能\r\n"); + } + else + { + /* 设置工作模式为正常识别模式 */ + rem_mode = 0; + rt_kprintf("\r\n红外遥控编码文件解读失败,已关闭红外遥控功能\r\n"); + } } else { @@ -200,29 +220,21 @@ void rem_start(void) void rem_encoder(struct rtgui_event_kbd * p) { struct rtgui_event_kbd * p_kbd_event = p; - /* 红外遥控匹配 */ + + /* 检查是否有数据被捕获 */ if( (rem_mode==2) && (rt_tick_get()>first_tick+10) && (rx_count > 0) ) { /* 手动清零第一个捕获结果 */ rm_code[0] = 0; rx_count = 0; -#if 0 - { - unsigned int iii; - for(iii=0; iii<100; iii++) - { - rt_kprintf("\r\n%d",rm_code[iii]); - } - } -#endif -#if 1 + /* 匹配捕获的数据 */ { unsigned int tmp; - unsigned int fflag = 0; + unsigned int err_flag = 0; unsigned int rem_cmp_n = 6; - //循环匹配所有KEY + /* 循环匹配所有KEY */ while( rem_cmp_n ) { unsigned int tmp2 = p_rem_code_src[ 6-rem_cmp_n ].len; @@ -232,22 +244,23 @@ void rem_encoder(struct rtgui_event_kbd * p) for(tmp=0; tmp p_rem_code_src[6-rem_cmp_n].rem_code[tmp]-wucha)) ) + /* 判断捕获结果是否在偏差允许范围内 */ + if( !( (rm_code[tmp] < p_rem_code_src[6-rem_cmp_n].rem_code[tmp]+remote_deviation) + && (rm_code[tmp] > p_rem_code_src[6-rem_cmp_n].rem_code[tmp]-remote_deviation)) ) { - fflag = 1; - //rt_kprintf("\r\nerr %d: rm_code[%d] p_rem_code_src[%d].rem_code[%d]",tmp,rm_code[tmp],6-rem_cmp_n,p_rem_code_src[ 6-rem_cmp_n].rem_code[tmp]); + err_flag = 1; } } } else { - fflag = 1; + err_flag = 1; rt_kprintf("\r\n解码失败"); } - if(fflag==0) + if( err_flag==0 ) { - //成功 + /* 对比全部数据符合 */ rt_kprintf("\r\n识别到遥控按键 %s",desc_key[6-rem_cmp_n]); switch( rem_cmp_n ) { @@ -276,14 +289,13 @@ void rem_encoder(struct rtgui_event_kbd * p) } else { - //不成功 - fflag = 0; + /* 对比不符合,清零错误标致,以进行下一次对比 */ + err_flag = 0; rem_cmp_n --; } } } -#endif }//红外遥控匹配 } @@ -307,7 +319,7 @@ void remote_isr(void) rx_count = 0; clr_flag = 1; } - if( rx_count<100 ) + if( rx_count < remote_code_len_max ) { rm_code[rx_count++] = TIM_GetCapture3(TIM5); } @@ -318,7 +330,7 @@ void remote_isr(void) rx_count = 0; clr_flag = 1; } - if(rx_count<100 ) + if(rx_count < remote_code_len_max ) { rm_code[rx_count++] = TIM_GetCapture3(TIM5); } @@ -338,13 +350,13 @@ void remote_isr(void) case 0://未启动 break; case 1://自学习 - if( rx_count<100 ) + if( rx_count < remote_code_len_max ) { rm_code[rx_count++] = TIM_GetCapture4(TIM5); } break; case 2://正常解码 - if( rx_count<100 ) + if( rx_count < remote_code_len_max ) { rm_code[rx_count++] = TIM_GetCapture4(TIM5); } @@ -374,7 +386,7 @@ int rem_study(void) unsigned int i; int fd,size; - unsigned char tmp_buf[606]; + unsigned char tmp_buf[ (remote_code_len_max+1)*7 ]; rem_mode = 1; rx_count = 0; @@ -390,7 +402,7 @@ int rem_study(void) return -1; } - //学习6个键盘 + /* 学习6个键盘 */ for( i=0; i<6; i++) { unsigned int is_ok = 1; @@ -413,36 +425,43 @@ int rem_study(void) TIM_ITConfig(TIM5, TIM_IT_CC3, DISABLE); TIM_ITConfig(TIM5, TIM_IT_CC4, DISABLE); - p[0] = rx_count / 1000 +'0'; + p[0] = rx_count / 10000 +'0'; + rx_count = rx_count % 10000; + p[1] = rx_count / 1000 +'0'; rx_count = rx_count % 1000; - p[1] = rx_count / 100 +'0'; + p[2] = rx_count / 100 +'0'; rx_count = rx_count % 100; - p[2] = rx_count / 10 +'0'; + p[3] = rx_count / 10 +'0'; rx_count = rx_count % 10; - p[3] = rx_count +'0'; + p[4] = rx_count +'0'; rx_count = 0; - p[4] = '\r'; - p[5] = '\n'; - p += 6; + p[5] = '\r'; + p[6] = '\n'; + p += 7; rm_code[0] = 0; for( a=0; a