Convert to new Doxygen style.
[bertos.git] / drv / kbd.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999, 2003 Bernardo Innocenti
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  * \author Francesco Sacchi <batt@develer.com>
14  *
15  * \brief Keyboard driver (implementation)
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.8  2006/07/19 12:56:25  bernie
21  *#* Convert to new Doxygen style.
22  *#*
23  *#* Revision 1.7  2006/06/03 13:57:36  bernie
24  *#* Make keyboard repeat mask run-time configurable.
25  *#*
26  *#* Revision 1.6  2006/03/20 17:50:17  bernie
27  *#* Add FreeRTOS and Observers support.
28  *#*
29  *#* Revision 1.5  2006/02/27 22:39:45  bernie
30  *#* Misc build and doc fixes from project_grl.
31  *#*
32  *#* Revision 1.4  2006/02/24 00:27:14  bernie
33  *#* Use new naming convention for list macros.
34  *#*
35  *#* Revision 1.3  2006/02/17 21:15:42  bernie
36  *#* Add MOD_CHECK() checks.
37  *#*
38  *#* Revision 1.2  2006/02/10 12:36:20  bernie
39  *#* Add preliminary FreeRTOS support; Enforce CONFIG_* definitions.
40  *#*
41  *#* Revision 1.1  2005/06/27 21:28:45  bernie
42  *#* Import generic keyboard driver.
43  *#*
44  *#*/
45
46 #include <hw_kbd.h>
47
48 #include <drv/timer.h>
49 #include <drv/kbd.h>
50
51 #include <cfg/debug.h>
52 #include <cfg/module.h>
53 #include <appconfig.h>
54
55 /* Configuration sanity checks */
56 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT && CONFIG_KBD_POLL != CONFIG_POLL_FREERTOS)
57         #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT or CONFIG_POLL_FREERTOS
58 #endif
59 #if !defined(CONFIG_KBD_BEEP) || (CONFIG_KBD_BEEP != 0 && CONFIG_KBD_BEEP != 1)
60         #error CONFIG_KBD_BEEP must be defined to either 0 or 1
61 #endif
62 #if !defined(CONFIG_KBD_OBSERVER) || (CONFIG_KBD_OBSERVER != 0 && CONFIG_KBD_OBSERVER != 1)
63         #error CONFIG_KBD_OBSERVER must be defined to either 0 or 1
64 #endif
65
66 #if CONFIG_KBD_BEEP
67         #include <drv/buzzer.h>
68 #endif
69
70 #define KBD_CHECK_INTERVAL  10  /**< (ms) Timing for kbd softint */
71 #define KBD_DEBOUNCE_TIME   30  /**< (ms) Debounce time */
72 #define KBD_BEEP_TIME        5  /**< (ms) Duration of keybeep */
73
74 #define KBD_REPEAT_DELAY   400  /**< (ms) Keyboard repeat delay for first character */
75 #define KBD_REPEAT_RATE    100  /**< (ms) Initial interchar delay for keyboard repeat */
76 #define KBD_REPEAT_MAXRATE  20  /**< (ms) Minimum delay for keyboard repeat */
77 #define KBD_REPEAT_ACCEL     5  /**< (ms) Keyboard repeat speed increase */
78
79 #define KBD_LNG_DELAY     1000  /**< (ms) Keyboard long pression keys delay */
80
81
82 /** Status for keyboard repeat state machine */
83 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
84
85
86 static volatile keymask_t kbd_buf; /**< Single entry keyboard buffer */
87 static volatile keymask_t kbd_cnt; /**< Number of keypress events in \c kbd_buf */
88 static keymask_t kbd_rpt_mask;     /**< Mask of repeatable keys. */
89
90 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
91 static Timer kbd_timer;            /**< Keyboard softtimer */
92 #endif
93
94 static List kbd_rawHandlers;       /**< Raw keyboard handlers */
95 static List kbd_handlers;          /**< Cooked keyboard handlers */
96
97 static KbdHandler kbd_defHandler;  /**< The default keyboard handler */
98 static KbdHandler kbd_debHandler;  /**< The debounce keyboard handler */
99 static KbdHandler kbd_rptHandler;  /**< Auto-repeat keyboard handler */
100 #ifdef  K_LNG_MASK
101 static KbdHandler kbd_lngHandler;  /**< Long pression keys handler */
102 #endif
103
104 #if CONFIG_KBD_OBSERVER
105         #include <mware/observer.h>
106         Subject kbd_subject;
107 #endif
108
109
110 /**
111  * Poll keyboard and dispatch keys to handlers.
112  *
113  * Read the key states and invoke all keyboard
114  * handlers to process the new state.
115  *
116  * Call this function periodically using a software
117  * timer, an interrupt or a process.
118  */
119 static void kbd_poll(void)
120 {
121         /** Currently depressed key */
122         static keymask_t current_key;
123
124         struct KbdHandler *handler;
125         keymask_t key = kbd_readkeys();
126
127         /* Call raw input handlers */
128         FOREACH_NODE(handler, &kbd_rawHandlers)
129                 key = handler->hook(key);
130
131         /* If this key was not previously pressed */
132         if (key != current_key)
133         {
134                 /* Remember last key */
135                 current_key = key;
136
137                 /* Call cooked input handlers */
138                 FOREACH_NODE(handler, &kbd_handlers)
139                         key = handler->hook(key);
140         }
141 }
142
143 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
144
145 /**
146  * Keyboard soft-irq handler.
147  */
148 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
149 {
150         kbd_poll();
151         timer_add(&kbd_timer);
152 }
153
154 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
155
156 #include "FreeRTOS.h"
157 #include "task.h"
158
159 static portTASK_FUNCTION(kbd_task, arg)
160 {
161         for (;;)
162         {
163                 kbd_poll();
164                 timer_delay(KBD_CHECK_INTERVAL);
165         }
166 }
167
168 #endif /* CONFIG_KBD_POLL */
169
170 /**
171  * \brief Read a key from the keyboard buffer.
172  *
173  * When a key is kept depressed between calls of this function a value
174  * is returned only after the time specified with KBD_REPAT_DELAY to
175  * avoid too fast keyboard repeat.
176  *
177  * \note This function is \b not interrupt safe!
178  *
179  * \return The mask of depressed keys or 0 if no keys are depressed.
180  *
181  */
182 keymask_t kbd_peek(void)
183 {
184         keymask_t key = 0;
185
186 // FIXME
187         /* Let other tasks run for a while */
188         extern void schedule(void);
189         schedule();
190
191         /* Extract an event from the keyboard buffer */
192         IRQ_DISABLE;
193         if (kbd_cnt)
194         {
195                 --kbd_cnt;
196                 key = kbd_buf;
197         }
198         IRQ_ENABLE;
199
200         return key;
201 }
202
203 /**
204  * Wait for a keypress and return the mask of depressed keys.
205  *
206  * \note This function is \b not interrupt safe!
207  */
208 keymask_t kbd_get(void)
209 {
210         keymask_t key;
211
212         while (!(key = kbd_peek())) {}
213
214         return key;
215 }
216
217
218 /**
219  * Wait up to \c timeout ms for a keypress
220  * and return the mask of depressed keys, or K_TIMEOUT
221  * if the timeout was reacked.
222  */
223 keymask_t kbd_get_timeout(mtime_t timeout)
224 {
225         keymask_t key;
226
227         ticks_t start = timer_clock();
228         ticks_t stop  = ms_to_ticks(timeout);
229         do
230         {
231                 if ((key = kbd_peek()))
232                         return key;
233         }
234         while (timer_clock() - start < stop);
235
236         return K_TIMEOUT;
237 }
238
239
240 void kbd_addHandler(struct KbdHandler *handler)
241 {
242         KbdHandler *node;
243         List *list;
244
245         cpuflags_t flags;
246         IRQ_SAVE_DISABLE(flags);
247
248         /* Choose between raw and coocked handlers list */
249         list = (handler->flags & KHF_RAWKEYS) ?
250                 &kbd_rawHandlers : &kbd_handlers;
251
252         /*
253          * Search for the first node whose priority
254          * is lower than the timer we want to add.
255          */
256         FOREACH_NODE(node,list)
257                 if (node->pri < handler->pri)
258                         break;
259
260         /* Enqueue handler in the handlers chain */
261         INSERT_BEFORE(&handler->link, &node->link);
262
263         IRQ_RESTORE(flags);
264 }
265
266
267 void kbd_remHandler(struct KbdHandler *handler)
268 {
269         /* Remove the handler */
270         ATOMIC(REMOVE(&handler->link));
271 }
272
273
274 /**
275  * This is the default key handler, called after
276  * all other handlers have had their chance to
277  * do their special processing. This handler
278  * pushes all input in the keyboard FIFO buffer.
279  */
280 static keymask_t kbd_defHandlerFunc(keymask_t key)
281 {
282         if (key)
283         {
284                 /* Force a single event in kbd buffer */
285                 kbd_buf = key;
286                 kbd_cnt = 1;
287
288                 #if CONFIG_KBD_OBSERVER
289                         observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
290                 #endif
291
292                 #if CONFIG_KBD_BEEP
293                         if (!(key & K_REPEAT))
294                                 buz_beep(KBD_BEEP_TIME);
295                 #endif
296         }
297
298         /* Eat all input */
299         return 0;
300 }
301
302 /**
303  * Handle keyboard debounce
304  */
305 static keymask_t kbd_debHandlerFunc(keymask_t key)
306 {
307         /** Buffer for debounce */
308         static keymask_t debounce_key;
309
310         /** Timer for keyboard debounce */
311         static ticks_t debounce_time;
312
313         /** Key aquired after debounce */
314         static keymask_t new_key;
315
316
317         ticks_t now = timer_clock();
318
319         if (key != debounce_key)
320         {
321                 /* Reset debounce timer */
322                 debounce_key = key;
323                 debounce_time = now;
324         }
325         else if ((new_key != debounce_key)
326                 && (now - debounce_time > ms_to_ticks(KBD_DEBOUNCE_TIME)))
327         {
328                 new_key = debounce_key;
329                 debounce_time = now;
330         }
331
332         return new_key;
333 }
334
335 #ifdef  K_LNG_MASK
336 /**
337  * Handle long pression keys.
338  */
339 static keymask_t kbd_lngHandlerFunc(keymask_t key)
340 {
341         static ticks_t start;
342         ticks_t now = timer_clock();
343
344         if (key & K_LNG_MASK)
345         {
346                 if (now - start > ms_to_ticks(KBD_LNG_DELAY))
347                         key |= K_LONG;
348         }
349         else
350                 start = now;
351         return key;
352 }
353 #endif
354
355 /**
356  * Set current mask of repeatable keys.
357  */
358 keymask_t kbd_setRepeatMask(keymask_t mask)
359 {
360         keymask_t oldmask = kbd_rpt_mask;
361         ATOMIC(kbd_rpt_mask = mask);
362         return oldmask;
363 }
364
365 /**
366  * Handle keyboard repeat
367  */
368 static keymask_t kbd_rptHandlerFunc(keymask_t key)
369 {
370         /* Timer for keyboard repeat events. */
371         static ticks_t repeat_time;
372
373         /* Current repeat rate (for acceleration). */
374         static ticks_t repeat_rate; /** Current repeat rate (for acceleration) */
375
376         ticks_t now = timer_clock();
377
378         switch (kbd_rptStatus)
379         {
380                 case KS_IDLE:
381                         if (key & kbd_rpt_mask)
382                         {
383                                 repeat_time = now;
384                                 kbd_rptStatus = KS_REPDELAY;
385                         }
386                         break;
387
388                 case KS_REPDELAY:
389                         if (key & kbd_rpt_mask)
390                         {
391                                 if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
392                                 {
393                                         key = (key & kbd_rpt_mask) | K_REPEAT;
394                                         repeat_time = now;
395                                         repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
396                                         kbd_rptStatus = KS_REPEAT;
397                                 }
398                                 else
399                                         key = 0;
400                         }
401                         else
402                                 kbd_rptStatus = KS_IDLE;
403                         break;
404
405                 case KS_REPEAT:
406                         if (key & kbd_rpt_mask)
407                         {
408                                 if (now - repeat_time > repeat_rate)
409                                 {
410                                         /* Enqueue a new event in the buffer */
411                                         key = (key & kbd_rpt_mask) | K_REPEAT;
412                                         repeat_time = now;
413
414                                         /* Repeat rate acceleration */
415                                         if (repeat_rate > ms_to_ticks(KBD_REPEAT_MAXRATE))
416                                                 repeat_rate -= ms_to_ticks(KBD_REPEAT_ACCEL);
417                                 }
418                                 else
419                                         key = 0;
420                         }
421                         else
422                                 kbd_rptStatus = KS_IDLE;
423
424                         break;
425         }
426
427         return key;
428 }
429
430
431 MOD_DEFINE(kbd)
432
433 /**
434  * Initialize keyboard ports and softtimer
435  */
436 void kbd_init(void)
437 {
438 #if CONFIG_KBD_BEEP
439         MOD_CHECK(buzzer);
440 #endif
441
442         KBD_HW_INIT;
443
444         /* Init handlers lists */
445         LIST_INIT(&kbd_handlers);
446         LIST_INIT(&kbd_rawHandlers);
447
448         /* Add debounce keyboard handler */
449         kbd_debHandler.hook = kbd_debHandlerFunc;
450         kbd_debHandler.pri = 100; /* high priority */
451         kbd_debHandler.flags = KHF_RAWKEYS;
452         kbd_addHandler(&kbd_debHandler);
453
454         #ifdef  K_LNG_MASK
455         /* Add long pression keyboard handler */
456         kbd_lngHandler.hook = kbd_lngHandlerFunc;
457         kbd_lngHandler.pri = 90; /* high priority */
458         kbd_lngHandler.flags = KHF_RAWKEYS;
459         kbd_addHandler(&kbd_lngHandler);
460         #endif
461
462         /* Add repeat keyboard handler */
463         kbd_rptHandler.hook = kbd_rptHandlerFunc;
464         kbd_rptHandler.pri = 80; /* high priority */
465         kbd_rptHandler.flags = KHF_RAWKEYS;
466         kbd_addHandler(&kbd_rptHandler);
467
468         /* Add default keyboard handler */
469         kbd_defHandler.hook = kbd_defHandlerFunc;
470         kbd_defHandler.pri = -128; /* lowest priority */
471         kbd_addHandler(&kbd_defHandler);
472
473 #if CONFIG_KBD_OBSERVER
474         observer_InitSubject(&kbd_subject);
475 #endif
476
477 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
478
479         MOD_CHECK(timer);
480
481         /* Add kbd handler to soft timers list */
482         event_initSoftInt(&kbd_timer.expire, kbd_softint, 0);
483         timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
484         timer_add(&kbd_timer);
485
486 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
487
488         /* Create a timer specific thread */
489         xTaskCreate(kbd_task, "kbd", CONFIG_STACK_KBD,
490                         NULL, CONFIG_PRI_KBD, NULL);
491
492 #else
493         #error "Define keyboard poll method"
494 #endif
495
496         MOD_INIT(kbd);
497 }