2011-03-11 17:44:54 +08:00
|
|
|
/*
|
|
|
|
* File : key.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2011, RT-Thread Develop Team
|
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
|
|
|
* http://www.rt-thread.org/license/LICENSE
|
|
|
|
*
|
|
|
|
* Change Logs:
|
2011-04-15 18:13:35 +08:00
|
|
|
* Date Author Notes
|
2011-03-11 17:44:54 +08:00
|
|
|
* 2011-03-03 lgnq
|
|
|
|
*/
|
|
|
|
|
2011-03-08 14:04:00 +08:00
|
|
|
#include <rtthread.h>
|
|
|
|
#include "key.h"
|
2011-04-15 18:13:35 +08:00
|
|
|
#include "lcd.h"
|
2011-03-08 14:04:00 +08:00
|
|
|
|
2011-04-15 18:13:35 +08:00
|
|
|
#ifdef RT_USING_RTGUI
|
2011-03-08 14:04:00 +08:00
|
|
|
#include <rtgui/event.h>
|
|
|
|
#include <rtgui/rtgui_server.h>
|
2011-04-15 18:13:35 +08:00
|
|
|
#endif
|
2011-03-08 14:04:00 +08:00
|
|
|
|
|
|
|
static void key_io_init(void)
|
|
|
|
{
|
|
|
|
/*Select CPIO function*/
|
|
|
|
KEY_PFR &= ~KEY_MASK;
|
|
|
|
/*Set CPIO Pull-Up function*/
|
|
|
|
KEY_PCR |= KEY_MASK;
|
|
|
|
/*Make button pins inputs*/
|
|
|
|
KEY_DDR &= ~KEY_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void key_thread_entry(void *parameter)
|
|
|
|
{
|
2011-04-15 18:13:35 +08:00
|
|
|
#ifdef RT_USING_RTGUI
|
2011-03-08 14:04:00 +08:00
|
|
|
rt_time_t next_delay;
|
|
|
|
rt_uint8_t i;
|
2011-04-15 18:13:35 +08:00
|
|
|
|
|
|
|
struct rtgui_event_kbd kbd_event;
|
2011-03-08 14:04:00 +08:00
|
|
|
|
|
|
|
key_io_init();
|
|
|
|
|
|
|
|
/* init keyboard event */
|
|
|
|
RTGUI_EVENT_KBD_INIT(&kbd_event);
|
|
|
|
kbd_event.mod = RTGUI_KMOD_NONE;
|
|
|
|
kbd_event.unicode = 0;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
next_delay = RT_TICK_PER_SECOND/10;
|
|
|
|
kbd_event.key = RTGUIK_UNKNOWN;
|
|
|
|
kbd_event.type = RTGUI_KEYDOWN;
|
|
|
|
|
2011-03-11 17:44:54 +08:00
|
|
|
if (KEY_ENTER_GETVALUE() == 0 )
|
2011-03-08 14:04:00 +08:00
|
|
|
{
|
|
|
|
for(i=0; ; i++)
|
|
|
|
{
|
|
|
|
rt_thread_delay( next_delay );
|
|
|
|
if (KEY_ENTER_GETVALUE() == 0)
|
|
|
|
{
|
2011-03-11 17:44:54 +08:00
|
|
|
if (i>=4)
|
2011-03-08 14:04:00 +08:00
|
|
|
{
|
|
|
|
/* HOME key */
|
|
|
|
kbd_event.key = RTGUIK_HOME;
|
|
|
|
next_delay = RT_TICK_PER_SECOND/5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
kbd_event.key = RTGUIK_RETURN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-11 17:44:54 +08:00
|
|
|
if (KEY_DOWN_GETVALUE() == 0)
|
2011-03-08 14:04:00 +08:00
|
|
|
{
|
|
|
|
kbd_event.key = RTGUIK_DOWN;
|
|
|
|
}
|
|
|
|
|
2011-03-11 17:44:54 +08:00
|
|
|
if (KEY_UP_GETVALUE() == 0)
|
2011-03-08 14:04:00 +08:00
|
|
|
{
|
|
|
|
kbd_event.key = RTGUIK_UP;
|
|
|
|
}
|
|
|
|
|
2011-03-11 17:44:54 +08:00
|
|
|
if (KEY_RIGHT_GETVALUE() == 0)
|
2011-03-08 14:04:00 +08:00
|
|
|
{
|
|
|
|
kbd_event.key = RTGUIK_RIGHT;
|
|
|
|
}
|
|
|
|
|
2011-03-11 17:44:54 +08:00
|
|
|
if (KEY_LEFT_GETVALUE() == 0)
|
2011-03-08 14:04:00 +08:00
|
|
|
{
|
|
|
|
kbd_event.key = RTGUIK_LEFT;
|
|
|
|
}
|
|
|
|
|
2011-03-11 17:44:54 +08:00
|
|
|
if (kbd_event.key != RTGUIK_UNKNOWN)
|
2011-03-08 14:04:00 +08:00
|
|
|
{
|
|
|
|
/* post down event */
|
|
|
|
rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
|
2011-03-11 17:44:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-03-08 14:04:00 +08:00
|
|
|
kbd_event.type = RTGUI_KEYUP;
|
|
|
|
rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
|
|
|
|
}
|
|
|
|
/* wait next key press */
|
|
|
|
rt_thread_delay(next_delay);
|
|
|
|
}
|
2011-04-15 18:13:35 +08:00
|
|
|
#else
|
|
|
|
extern struct rt_messagequeue mq;
|
|
|
|
rt_time_t next_delay;
|
|
|
|
rt_uint8_t i;
|
|
|
|
struct lcd_msg msg;
|
|
|
|
msg.type = KEY_MSG;
|
|
|
|
|
|
|
|
key_io_init();
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
msg.key = NO_KEY;
|
|
|
|
|
|
|
|
next_delay = RT_TICK_PER_SECOND/10;
|
|
|
|
|
|
|
|
if (KEY_ENTER_GETVALUE() == 0 )
|
|
|
|
{
|
|
|
|
msg.key = KEY_ENTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KEY_DOWN_GETVALUE() == 0)
|
|
|
|
{
|
|
|
|
msg.key = KEY_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KEY_UP_GETVALUE() == 0)
|
|
|
|
{
|
|
|
|
msg.key = KEY_UP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KEY_RIGHT_GETVALUE() == 0)
|
|
|
|
{
|
|
|
|
msg.key = KEY_RIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KEY_LEFT_GETVALUE() == 0)
|
|
|
|
{
|
|
|
|
msg.key = KEY_LEFT;
|
|
|
|
}
|
|
|
|
|
|
|
|
rt_mq_send(&mq, &msg, sizeof(msg));
|
|
|
|
|
|
|
|
/* wait next key press */
|
|
|
|
rt_thread_delay(next_delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2011-03-08 14:04:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_thread_t key_thread;
|
|
|
|
void rt_hw_key_init(void)
|
|
|
|
{
|
2011-03-11 17:44:54 +08:00
|
|
|
key_thread = rt_thread_create("key", key_thread_entry, RT_NULL, 384, 28, 5);
|
|
|
|
if (key_thread != RT_NULL)
|
2011-03-08 14:04:00 +08:00
|
|
|
rt_thread_startup(key_thread);
|
|
|
|
}
|