android按键长按短按功能的实现策略
定时器轮休的方式,根据采样次数来区分长按还是短按
1.当按下键1的时候,就不断的计数,直到你放手电压值变化,再根据计数的次数判断上报长按还是短按的功能
没有按键的时候采样率为HZ/4,采样是HZ/40
static void lradc1_data_function(unsigned long data)
248 {
249 volatile unsigned int reg_val;
250 static int key_pressed = -1, count = 0;
251 int delay = HZ/40;
252 reg_val = readl(KEY_BASSADDRESS+LRADC_DATA1);
253 // printk("[lkj] second lradc data1 =0x%x, s_headset_plug=%d \n", reg_val, s_headset_plug);
254 //because recorder app close this, so ....
255 writel( readl(baseaddr+0x28) | (1<<27),(baseaddr+(0x28)));
256 writel( readl(baseaddr+0x28) | (1<<29),(baseaddr+(0x28)));
257
258 if(s_first_headset_plug != 1 || s_mic_type != 1) {
259 mod_timer(&mic_data->timer, jiffies + HZ/4);
260 printk("[lkj] input key hook key return \n");
261 return ;
262 }
263 if (reg_val >=0 && reg_val < 0x5 )
264 {
265 // hook key
266 if (key_pressed == 0)
267 count++;
268 else {
269 key_pressed = 0;
270 count = 0;
271 }
272
273 if (count > 5)
274 {
275 sw_pressed[0] = 1;
276 input_report_key(sun4ikbd_dev, HOOK_KEY, 1);
277 input_sync(sun4ikbd_dev);
278 printk("[lkj] input key sw1 down \n");
279 count = 0;
280 }
281
282
283
284 }
285 else if( reg_val >= 0x6 && reg_val <= 0xb ) //根据电压值采取不同按键
286 {
287 // sw2
288 if (key_pressed == 1) //计数为了减小误触以及实现短按长按的功能
289 count++;
290 else {
291 key_pressed = 1;
292 count = 0;
293 }
294
295 if (count > 5) //计数到大于5时,有2种情况出现
296 {
297 sw_pressed[1] = 1;
298 // printk("[lkj] input key sw2 down \n");
299 }
300
301 }
302 else if ( reg_val >= 0xf && reg_val < 0x1f )
303 {
304 // sw3
305 if (key_pressed == 2)
306 count++;
307 else {
308 key_pressed = 2;
309 count = 0;
310 }
311
312 if (count > 5)
313 {
314 sw_pressed[2] = 1;
315 // printk("[lkj] input key sw3 down \n");
316 }
317
318 }
319 else if ( reg_val >= 0x34) {//大于0x34时,根据count的次数判断长按还是短按,并上报不同的功能
320 key_pressed = -1;
321 if (sw_pressed[0])
322 {
323 sw_pressed[0] = 0;
324 input_report_key(sun4ikbd_dev, HOOK_KEY, 0);
325 input_sync(sun4ikbd_dev);
326 // printk("[lkj] input key sw1 up \n");
327 }
328 if (sw_pressed[1])
329 {
330 sw_pressed[1] = 0;
331 if (count > 20){
332 input_report_key(sun4ikbd_dev,KEY_NEXTSONG,1);
333 input_sync(sun4ikbd_dev);
334 input_report_key(sun4ikbd_dev, KEY_NEXTSONG, 0);
335 input_sync(sun4ikbd_dev);
336
337 }//long
338 else{
339
340 input_report_key(sun4ikbd_dev, KEY_VOLUMEUP, 1);
341 input_sync(sun4ikbd_dev);
342 input_report_key(sun4ikbd_dev, KEY_VOLUMEUP, 0);
343 input_sync(sun4ikbd_dev);
344
345 }// shot
346
347 }
348 if (sw_pressed[2])
349 {
350 sw_pressed[2] = 0;
351 if (count > 20){
352 input_report_key(sun4ikbd_dev,KEY_PREVIOUSSONG,1);
353 input_sync(sun4ikbd_dev);
354 input_report_key(sun4ikbd_dev, KEY_PREVIOUSSONG, 0);
355 input_sync(sun4ikbd_dev);
356
357 }//long
358 else{
359
360 input_report_key(sun4ikbd_dev, KEY_VOLUMEDOWN, 1);
361 input_sync(sun4ikbd_dev);
362 input_report_key(sun4ikbd_dev, KEY_VOLUMEDOWN, 0);
363 input_sync(sun4ikbd_dev);
364
365 }// shot
366 }
367 count = 0;
368 delay = HZ/4;
369 }