Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/src/key.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations) (download) (as text)
Fri Jun 8 06:26:43 2012 UTC (11 years, 11 months ago) by tekken_boss
File MIME type: text/x-csrc
File size: 6159 byte(s)
Import
1
2 /* --------------------------------------------- */
3 /* H8-3069F Switch control function */
4 /* */
5 /* CPU : Renesus H8/3069F 25MHz */
6 /* Memory : ROM 512KB, RAM 16KB E-RAM 2MB */
7 /* (c) KAZ.Imamura */
8 /* --------------------------------------------- */
9
10 #include "key.h"
11 #include "sc1602.h"
12 #include "ui.h"
13
14 #define _KEY0 P5DR.BIT.B0
15 #define _KEY1 P5DR.BIT.B1
16 #define _KEY2 P5DR.BIT.B2
17 #define _KEY3 P5DR.BIT.B3
18 #define CH_TIMEUP 10
19
20 typedef union {
21 unsigned char BYTE;
22
23 struct {
24 unsigned char up:1;
25 unsigned char down:1;
26 unsigned char back:1;
27 unsigned char ok:1;
28 unsigned char reserve:4;
29 } BIT;
30 } KEY_HW_INFO;
31
32 // -------------------------------------------
33 // Proto type definitions
34 // -------------------------------------------
35 void key_1ms_handler(void);
36 int key_initialize(void);
37 int key_process(void);
38
39
40 // Locals
41 static void key_event_up (unsigned char edge);
42 static void key_event_down(unsigned char edge);
43 static void key_event_back(unsigned char edge);
44 static void key_event_ok (unsigned char edge);
45
46 // -------------------------------------------
47 // Variables
48 // -------------------------------------------
49
50 // Locals
51 unsigned short key_timer;
52 unsigned short key_wait_timer;
53 unsigned short key_proc;
54 int ch_timer_up; // Chattering wait timer for UP key
55 int ch_timer_down; // Chattering wait timer for DOWN key
56 int ch_timer_back; // Chattering wait timer for BACK key
57 int ch_timer_ok; // Chattering wait timer for OK key
58
59 KEY_HW_INFO current_key_status;
60 KEY_HW_INFO previous_key_status;
61 KEY_HW_INFO status_key_status;
62
63 enum key_process_mode {
64 KEY_00_INIT,
65 KEY_01_STARTUP,
66 KEY_02_IDLE,
67 KEY_03_READ,
68 };
69
70 // -------------------------------------------
71 // Interrupt handlers
72 // -------------------------------------------
73 void key_1ms_handler(void) {
74 if( key_wait_timer ) key_wait_timer--;
75
76 // 4ms interval, kick off key read process
77 if( !( ++key_timer & 0x03 ) ) {
78 if( key_proc == KEY_02_IDLE ) key_proc = KEY_03_READ;
79 }
80 }
81
82
83 // -------------------------------------------
84 // Initialize
85 // -------------------------------------------
86 int key_initialize(void) {
87 key_wait_timer = 0;
88 key_proc = KEY_00_INIT;
89
90 // Chattering timer initialize
91 ch_timer_up = 0;
92 ch_timer_down = 0;
93 ch_timer_back = 0;
94 ch_timer_ok = 0;
95
96 }
97
98 // -------------------------------------------
99 // Main process
100 // -------------------------------------------
101 int key_process(void) {
102 switch( key_proc ) {
103 case KEY_00_INIT:
104 key_wait_timer = 500; // Start up wait
105 key_proc++;
106 break;
107
108 case KEY_01_STARTUP:
109 if( !key_wait_timer ) {
110 // read conditions
111 current_key_status.BIT.up = _KEY0;
112 current_key_status.BIT.down = _KEY1;
113 current_key_status.BIT.back = _KEY2;
114 current_key_status.BIT.ok = _KEY3;
115
116 status_key_status.BYTE = current_key_status.BYTE;
117 previous_key_status.BYTE = current_key_status.BYTE;
118
119 key_proc++;
120 }
121 break;
122
123 case KEY_02_IDLE:
124 break;
125
126 case KEY_03_READ:
127 // read conditions
128 current_key_status.BIT.up = _KEY0;
129 current_key_status.BIT.down = _KEY1;
130 current_key_status.BIT.back = _KEY2;
131 current_key_status.BIT.ok = _KEY3;
132
133 // UP key
134 if( current_key_status.BIT.up != previous_key_status.BIT.up ) {
135 ch_timer_up = CH_TIMEUP;
136 } else if ( ch_timer_up ) {
137 if( !(--ch_timer_up) ) {
138 status_key_status.BIT.up = !current_key_status.BIT.up;
139 key_event_up(status_key_status.BIT.up);
140 }
141 }
142 // DOWN key
143 if( current_key_status.BIT.down != previous_key_status.BIT.down ) {
144 ch_timer_down = CH_TIMEUP;
145 } else if ( ch_timer_down ) {
146 if( !(--ch_timer_down) ) {
147 status_key_status.BIT.down = !current_key_status.BIT.down;
148 key_event_down(status_key_status.BIT.down);
149 }
150 }
151 // BACK_KEY
152 if( current_key_status.BIT.back != previous_key_status.BIT.back ) {
153 ch_timer_back = CH_TIMEUP;
154 } else if ( ch_timer_back ) {
155 if( !(--ch_timer_back) ) {
156 status_key_status.BIT.back = !current_key_status.BIT.back;
157 key_event_back(status_key_status.BIT.back);
158 }
159 }
160 // OK key
161 if( current_key_status.BIT.ok != previous_key_status.BIT.ok ) {
162 ch_timer_ok = CH_TIMEUP;
163 } else if ( ch_timer_ok ) {
164 if( !(--ch_timer_ok) ) {
165 status_key_status.BIT.ok = !current_key_status.BIT.ok;
166 key_event_ok(status_key_status.BIT.ok);
167 }
168 }
169 previous_key_status.BYTE = current_key_status.BYTE;
170 key_proc = KEY_02_IDLE;
171 }
172
173 return 0;
174 }
175
176 // -------------------------------------------
177 // Key events
178 // -------------------------------------------
179 static void key_event_up (unsigned char edge) {
180 // unsigned char da;
181 UI_COMMAND uicmd;
182
183 // UI Command set
184 uicmd.cmd = UI_CMD_KEY_PRESS_UP;
185 uicmd.param = edge;
186 ui_cmdset(uicmd);
187
188 if( edge == ON_EDGE ) {
189 // da = DA.DADR0;
190 // da += 0x04;
191 // DA.DADR0 = da;
192 } else {
193 }
194 }
195
196 static void key_event_down(unsigned char edge) {
197 unsigned char da;
198 UI_COMMAND uicmd;
199
200 // UI Command set
201 uicmd.cmd = UI_CMD_KEY_PRESS_DOWN;
202 uicmd.param = edge;
203 ui_cmdset(uicmd);
204
205 if( edge == ON_EDGE ) {
206 // da = DA.DADR0;
207 // da -= 0x04;
208 // DA.DADR0 = da;
209 } else {
210 }
211 }
212
213 static void key_event_back(unsigned char edge) {
214 UI_COMMAND uicmd;
215
216 // UI Command set
217 uicmd.cmd = UI_CMD_KEY_PRESS_BACK;
218 uicmd.param = edge;
219 ui_cmdset(uicmd);
220
221 if( edge == ON_EDGE ) {
222 // strcpy( sc1602_buffer[0], "BACK KEY ON ");
223 // sc1602_buffer_updates[0]=1;
224 } else {
225 // strcpy( sc1602_buffer[0], "BACK KEY OFF ");
226 // sc1602_buffer_updates[0]=1;
227 }
228 }
229
230 static void key_event_ok (unsigned char edge){
231 UI_COMMAND uicmd;
232
233 // UI Command set
234 uicmd.cmd = UI_CMD_KEY_PRESS_OK;
235 uicmd.param = edge;
236 ui_cmdset(uicmd);
237
238 if( edge == ON_EDGE ) {
239 // strcpy( sc1602_buffer[1], "OK KEY ON ");
240 // sc1602_buffer_updates[1]=1;
241 } else {
242 // strcpy( sc1602_buffer[1], "OK KEY OFF ");
243 // sc1602_buffer_updates[1]=1;
244 }
245 }
246

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26