forked from RT-Thread/rt-thread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey.c
164 lines (139 loc) · 3.39 KB
/
key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* 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:
* Date Author Notes
* 2011-03-03 lgnq
*/
#include <rtthread.h>
#include "key.h"
#include "lcd.h"
#ifdef RT_USING_RTGUI
#include <rtgui/event.h>
#include <rtgui/rtgui_server.h>
#endif
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)
{
#ifdef RT_USING_RTGUI
rt_time_t next_delay;
rt_uint8_t i;
struct rtgui_event_kbd kbd_event;
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;
if (KEY_ENTER_GETVALUE() == 0 )
{
for(i=0; ; i++)
{
rt_thread_delay( next_delay );
if (KEY_ENTER_GETVALUE() == 0)
{
if (i>=4)
{
/* HOME key */
kbd_event.key = RTGUIK_HOME;
next_delay = RT_TICK_PER_SECOND/5;
break;
}
}
else
{
kbd_event.key = RTGUIK_RETURN;
break;
}
}
}
if (KEY_DOWN_GETVALUE() == 0)
{
kbd_event.key = RTGUIK_DOWN;
}
if (KEY_UP_GETVALUE() == 0)
{
kbd_event.key = RTGUIK_UP;
}
if (KEY_RIGHT_GETVALUE() == 0)
{
kbd_event.key = RTGUIK_RIGHT;
}
if (KEY_LEFT_GETVALUE() == 0)
{
kbd_event.key = RTGUIK_LEFT;
}
if (kbd_event.key != RTGUIK_UNKNOWN)
{
/* post down event */
rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
}
else
{
kbd_event.type = RTGUI_KEYUP;
rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
}
/* wait next key press */
rt_thread_delay(next_delay);
}
#else
extern struct rt_messagequeue mq;
rt_time_t next_delay;
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
}
static rt_thread_t key_thread;
void rt_hw_key_init(void)
{
key_thread = rt_thread_create("key", key_thread_entry, RT_NULL, 384, 28, 5);
if (key_thread != RT_NULL)
rt_thread_startup(key_thread);
}