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