Linux和qtopia下的矩阵键盘驱动程序(二)

2014-11-24 09:12:45 · 作者: · 浏览: 1
------------------------
* func: 扫描键盘,判断哪一个键被按下
* param:
* x: 得到按键的行号
* y: 得到按键的列号
* return:
* KEY_DOWN: 键盘上有键被按下
* NO_KEY_DOWN: 键盘上没有键被按下
------------------------------------------------------*/

static inline int scan_keyboard(int* x,int* y)
{
int matrix_row,matrix_col,matrix_col_status;

output_giop(0xF); //往所有行输出1


//判断按键在哪一行
matrix_row=matrix_col=-1;

output_giop(0xE);//在第1行上输出1,其余行输出0
matrix_col_status = get_eint_value(kbdev.irq);
if(matrix_col_status != NO_EINT_DOWN)
{
matrix_row = 0;
matrix_col = matrix_col_status;
goto scanend;

}

output_giop(0xD);//在第2行上输出1,其余行输出0

matrix_col_status = get_eint_value(kbdev.irq);
if(matrix_col_status != NO_EINT_DOWN)
{
matrix_row=1;
matrix_col = matrix_col_status;
goto scanend;


}

output_giop(0xB);//在第3行上输出1,其余行输出0
matrix_col_status =get_eint_value(kbdev.irq);
if(matrix_col_status != NO_EINT_DOWN)
{
matrix_row=2;
matrix_col = matrix_col_status;
goto scanend;


}

output_giop(0x7);//在第4行上输出1,其余行输出0
matrix_col_status =get_eint_value(kbdev.irq);
if(matrix_col_status != NO_EINT_DOWN)
{
matrix_row=3;
matrix_col = matrix_col_status;
goto scanend;


}
scanend:
output_giop(0);
if(matrix_row >=0 )
{
snap_keyboard_code[matrix_row][matrix_col_status]= snap_keyboard_code[matrix_row][matrix_col_status] + 1;
if(snap_keyboard_code[matrix_row][matrix_col]>=DETECTION_THROLD)
{
*x=matrix_row;
*y=matrix_col;
curr_keyboard_code = keyboard_code_map[matrix_row][matrix_col];
return KEY_DOWN;
}


}
return NO_KEY_DOWN;

}

/*----------------------------------------------------
* func: 判断本次按键是否与上次按键相同
* param:
*
* return:
* 0: 相同
* 1: 不同
------------------------------------------------------*/
static inline int key_changed()
{

return (pre_keyboard_code == curr_keyboard_code) 0 : 1;
}

/*----------------------------------------------------
* func: 将按键对应的键盘码保存到循环队列中
* param:
* keyValue: 按键的对应的键盘码

* return:
*
------------------------------------------------------*/

static inline void save_key_to_queue(u_short keyValue)
{
if (kbdev.keyStatus == KEY_DOWN)
{
BUF_HEAD = keyValue;
INCBUF(kbdev.head);
//wake_up_interruptible(&(kbdev.wq));
}

}

/*----------------------------------------------------
* func: 重复按键定时器处理程序,如果一直按住某键,
则将该键的键盘码定时存到循环队列中
* param:
* data: 无参数

* return:
*
------------------------------------------------------*/
static inline void repeat_timer_handler(unsigned long data)
{
spin_lock_irq(&(repeat_lock));

if(kbdev.keyStatus ==KEY_DOWN)
{
repeat_timer.expires = jiffies + REPEAT_DELAY;//设置自动重复延时
add_timer(&repeat_timer);//将定时器加入队列
if(pre_keyboard_code != 0)
{
//printk("repeat save keyvalue\n %d",pre_keyboard_code);
save_key_to_queue(pre_keyboard_code);//将按键值存入循环队列
}
}
else//如果按键弹起
{
//del_timer(&repeat_timer);
// printk("del repeat timer\n");
}

spin_unlock_irq(&(repeat_lock));


}

/*----------------------------------------------------
* func: 使能中断
* param:
* return:
*
------------------------------------------------------*/
//使能中断
static inline void enableIrq()
{
//