Sistema l'errore da me commesso in fase di conversione...
[bertos.git] / drv / kbd.c
old mode 100755 (executable)
new mode 100644 (file)
index f85f897..83483f4
--- a/drv/kbd.c
+++ b/drv/kbd.c
@@ -1,4 +1,4 @@
-/*!
+/**
  * \file
  * <!--
  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
 
 /*#*
  *#* $Log$
+ *#* Revision 1.9  2006/09/13 18:25:45  bernie
+ *#* Merge CONFIG_KBD_LONGPRESS.
+ *#*
+ *#* Revision 1.8  2006/07/19 12:56:25  bernie
+ *#* Convert to new Doxygen style.
+ *#*
+ *#* Revision 1.7  2006/06/03 13:57:36  bernie
+ *#* Make keyboard repeat mask run-time configurable.
+ *#*
+ *#* Revision 1.6  2006/03/20 17:50:17  bernie
+ *#* Add FreeRTOS and Observers support.
+ *#*
+ *#* Revision 1.5  2006/02/27 22:39:45  bernie
+ *#* Misc build and doc fixes from project_grl.
+ *#*
  *#* Revision 1.4  2006/02/24 00:27:14  bernie
  *#* Use new naming convention for list macros.
  *#*
 
 #include <drv/timer.h>
 #include <drv/kbd.h>
-#include <drv/buzzer.h>
 
 #include <cfg/debug.h>
 #include <cfg/module.h>
+#include <appconfig.h>
 
 /* Configuration sanity checks */
 #if !defined(CONFIG_KBD_POLL) || (CONFIG_KBD_POLL != KBD_POLL_SOFTINT && CONFIG_KBD_POLL != CONFIG_POLL_FREERTOS)
        #error CONFIG_KBD_POLL must be defined to either KBD_POLL_SOFTINT or CONFIG_POLL_FREERTOS
 #endif
+#if !defined(CONFIG_KBD_BEEP) || (CONFIG_KBD_BEEP != 0 && CONFIG_KBD_BEEP != 1)
+       #error CONFIG_KBD_BEEP must be defined to either 0 or 1
+#endif
+#if !defined(CONFIG_KBD_OBSERVER) || (CONFIG_KBD_OBSERVER != 0 && CONFIG_KBD_OBSERVER != 1)
+       #error CONFIG_KBD_OBSERVER must be defined to either 0 or 1
+#endif
+#if !defined(CONFIG_KBD_LONGPRESS) || (CONFIG_KBD_LONGPRESS != 0 && CONFIG_KBD_LONGPRESS != 1)
+       #error CONFIG_KBD_LONGPRESS must be defined to either 0 or 1
+#endif
 
+#if CONFIG_KBD_BEEP
+       #include <drv/buzzer.h>
+#endif
 
-#define KBD_CHECK_INTERVAL  10  /*!< (ms) Timing for kbd softint */
-#define KBD_DEBOUNCE_TIME   30  /*!< (ms) Debounce time */
-#define KBD_BEEP_TIME        5  /*!< (ms) Duration of keybeep */
+#define KBD_CHECK_INTERVAL  10  /**< (ms) Timing for kbd softint */
+#define KBD_DEBOUNCE_TIME   30  /**< (ms) Debounce time */
+#define KBD_BEEP_TIME        5  /**< (ms) Duration of keybeep */
 
-#define KBD_REPEAT_DELAY   400  /*!< (ms) Keyboard repeat delay for first character */
-#define KBD_REPEAT_RATE    100  /*!< (ms) Initial interchar delay for keyboard repeat */
-#define KBD_REPEAT_MAXRATE  20  /*!< (ms) Minimum delay for keyboard repeat */
-#define KBD_REPEAT_ACCEL     5  /*!< (ms) Keyboard repeat speed increase */
+#define KBD_REPEAT_DELAY   400  /**< (ms) Keyboard repeat delay for first character */
+#define KBD_REPEAT_RATE    100  /**< (ms) Initial interchar delay for keyboard repeat */
+#define KBD_REPEAT_MAXRATE  20  /**< (ms) Minimum delay for keyboard repeat */
+#define KBD_REPEAT_ACCEL     5  /**< (ms) Keyboard repeat speed increase */
 
-#define KBD_LNG_DELAY     1000  /*!< (ms) Keyboard long pression keys delay */
+#define KBD_LNG_DELAY     1000  /**< (ms) Keyboard long pression keys delay */
 
 
-/*! Status for keyboard repeat state machine */
+/** Status for keyboard repeat state machine */
 static enum { KS_IDLE, KS_REPDELAY, KS_REPEAT } kbd_rptStatus;
 
 
-static volatile keymask_t kbd_buf; /*!< Single entry keyboard buffer */
-static volatile keymask_t kbd_cnt; /*!< Number of keypress events in \c kbd_buf */
+static volatile keymask_t kbd_buf; /**< Single entry keyboard buffer */
+static volatile keymask_t kbd_cnt; /**< Number of keypress events in \c kbd_buf */
+static keymask_t kbd_rpt_mask;     /**< Mask of repeatable keys. */
+
+#if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
+static Timer kbd_timer;            /**< Keyboard softtimer */
+#endif
 
-static Timer kbd_timer;            /*!< Keyboard softtimer */
+static List kbd_rawHandlers;       /**< Raw keyboard handlers */
+static List kbd_handlers;          /**< Cooked keyboard handlers */
 
-static List kbd_rawHandlers;       /*!< Raw keyboard handlers */
-static List kbd_handlers;          /*!< Cooked keyboard handlers */
+static KbdHandler kbd_defHandler;  /**< The default keyboard handler */
+static KbdHandler kbd_debHandler;  /**< The debounce keyboard handler */
+static KbdHandler kbd_rptHandler;  /**< Auto-repeat keyboard handler */
 
-static KbdHandler kbd_defHandler;  /*!< The default keyboard handler */
-static KbdHandler kbd_debHandler;  /*!< The debounce keyboard handler */
-static KbdHandler kbd_rptHandler;  /*!< Auto-repeat keyboard handler */
-#ifdef  K_LNG_MASK
-static KbdHandler kbd_lngHandler;  /*!< Long pression keys handler */
+#if CONFIG_KBD_LONGPRESS
+static KbdHandler kbd_lngHandler;  /**< Long pression keys handler */
 #endif
 
+#if CONFIG_KBD_OBSERVER
+       #include <mware/observer.h>
+       Subject kbd_subject;
+#endif
 
 
-/*!
+/**
  * Poll keyboard and dispatch keys to handlers.
  *
  * Read the key states and invoke all keyboard
@@ -90,7 +125,7 @@ static KbdHandler kbd_lngHandler;  /*!< Long pression keys handler */
  */
 static void kbd_poll(void)
 {
-       /*! Currently depressed key */
+       /** Currently depressed key */
        static keymask_t current_key;
 
        struct KbdHandler *handler;
@@ -114,7 +149,7 @@ static void kbd_poll(void)
 
 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
 
-/*!
+/**
  * Keyboard soft-irq handler.
  */
 static void kbd_softint(UNUSED_ARG(iptr_t, arg))
@@ -128,24 +163,26 @@ static void kbd_softint(UNUSED_ARG(iptr_t, arg))
 #include "FreeRTOS.h"
 #include "task.h"
 
-static portTASK_FUNCTION(kbd_poll, arg)
+static portTASK_FUNCTION(kbd_task, arg)
 {
        for (;;)
        {
-               kbd_poll(0);
+               kbd_poll();
                timer_delay(KBD_CHECK_INTERVAL);
        }
 }
 
 #endif /* CONFIG_KBD_POLL */
 
-/*!
+/**
  * \brief Read a key from the keyboard buffer.
  *
  * When a key is kept depressed between calls of this function a value
  * is returned only after the time specified with KBD_REPAT_DELAY to
  * avoid too fast keyboard repeat.
  *
+ * \note Calls \c schedule() internally.
+ *
  * \note This function is \b not interrupt safe!
  *
  * \return The mask of depressed keys or 0 if no keys are depressed.
@@ -155,7 +192,8 @@ keymask_t kbd_peek(void)
 {
        keymask_t key = 0;
 
-// FIXME
+// FIXME: make it optional
+       /* Let other tasks run for a while */
        extern void schedule(void);
        schedule();
 
@@ -171,8 +209,7 @@ keymask_t kbd_peek(void)
        return key;
 }
 
-
-/*!
+/**
  * Wait for a keypress and return the mask of depressed keys.
  *
  * \note This function is \b not interrupt safe!
@@ -182,11 +219,12 @@ keymask_t kbd_get(void)
        keymask_t key;
 
        while (!(key = kbd_peek())) {}
+
        return key;
 }
 
 
-/*!
+/**
  * Wait up to \c timeout ms for a keypress
  * and return the mask of depressed keys, or K_TIMEOUT
  * if the timeout was reacked.
@@ -242,7 +280,7 @@ void kbd_remHandler(struct KbdHandler *handler)
 }
 
 
-/*!
+/**
  * This is the default key handler, called after
  * all other handlers have had their chance to
  * do their special processing. This handler
@@ -256,28 +294,32 @@ static keymask_t kbd_defHandlerFunc(keymask_t key)
                kbd_buf = key;
                kbd_cnt = 1;
 
-#if KBD_BEEP_TIME
-               if (!(key & K_REPEAT))
-                       buz_beep(KBD_BEEP_TIME);
-#endif
+               #if CONFIG_KBD_OBSERVER
+                       observer_notify(&kbd_subject, KBD_EVENT_KEY, &key);
+               #endif
+
+               #if CONFIG_KBD_BEEP
+                       if (!(key & K_REPEAT))
+                               buz_beep(KBD_BEEP_TIME);
+               #endif
        }
 
        /* Eat all input */
        return 0;
 }
 
-/*!
+/**
  * Handle keyboard debounce
  */
 static keymask_t kbd_debHandlerFunc(keymask_t key)
 {
-       /*! Buffer for debounce */
+       /** Buffer for debounce */
        static keymask_t debounce_key;
 
-       /*! Timer for keyboard debounce */
+       /** Timer for keyboard debounce */
        static ticks_t debounce_time;
 
-       /*! Key aquired after debounce */
+       /** Key aquired after debounce */
        static keymask_t new_key;
 
 
@@ -299,44 +341,53 @@ static keymask_t kbd_debHandlerFunc(keymask_t key)
        return new_key;
 }
 
-#ifdef  K_LNG_MASK
-/*!
- * Handle long pression keys
+#if CONFIG_KBD_LONGPRESS
+/**
+ * Handle long pression keys.
  */
 static keymask_t kbd_lngHandlerFunc(keymask_t key)
 {
-       static ticks_t stop;
+       static ticks_t start;
        ticks_t now = timer_clock();
 
        if (key & K_LNG_MASK)
        {
-               if (now - stop > 0)
-                       key &= K_LNG_MASK;
-               else
-                       key &= ~K_LNG_MASK;
+               if (now - start > ms_to_ticks(KBD_LNG_DELAY))
+                       key |= K_LONG;
        }
        else
-               stop = now + ms_to_ticks(KBD_LNG_DELAY);
+               start = now;
        return key;
 }
 #endif
 
-/*!
+/**
+ * Set current mask of repeatable keys.
+ */
+keymask_t kbd_setRepeatMask(keymask_t mask)
+{
+       keymask_t oldmask = kbd_rpt_mask;
+       ATOMIC(kbd_rpt_mask = mask);
+       return oldmask;
+}
+
+/**
  * Handle keyboard repeat
  */
 static keymask_t kbd_rptHandlerFunc(keymask_t key)
 {
-       /*! Timer for keyboard repeat events */
+       /* Timer for keyboard repeat events. */
        static ticks_t repeat_time;
 
-       static ticks_t repeat_rate; /*! Current repeat rate (for acceleration) */
+       /* Current repeat rate (for acceleration). */
+       static ticks_t repeat_rate; /** Current repeat rate (for acceleration) */
 
        ticks_t now = timer_clock();
 
        switch (kbd_rptStatus)
        {
                case KS_IDLE:
-                       if (key & K_RPT_MASK)
+                       if (key & kbd_rpt_mask)
                        {
                                repeat_time = now;
                                kbd_rptStatus = KS_REPDELAY;
@@ -344,11 +395,11 @@ static keymask_t kbd_rptHandlerFunc(keymask_t key)
                        break;
 
                case KS_REPDELAY:
-                       if (key & K_RPT_MASK)
+                       if (key & kbd_rpt_mask)
                        {
                                if (now - repeat_time > ms_to_ticks(KBD_REPEAT_DELAY))
                                {
-                                       key = (key & K_RPT_MASK) | K_REPEAT;
+                                       key = (key & kbd_rpt_mask) | K_REPEAT;
                                        repeat_time = now;
                                        repeat_rate = ms_to_ticks(KBD_REPEAT_RATE);
                                        kbd_rptStatus = KS_REPEAT;
@@ -361,12 +412,12 @@ static keymask_t kbd_rptHandlerFunc(keymask_t key)
                        break;
 
                case KS_REPEAT:
-                       if (key & K_RPT_MASK)
+                       if (key & kbd_rpt_mask)
                        {
                                if (now - repeat_time > repeat_rate)
                                {
                                        /* Enqueue a new event in the buffer */
-                                       key = (key & K_RPT_MASK) | K_REPEAT;
+                                       key = (key & kbd_rpt_mask) | K_REPEAT;
                                        repeat_time = now;
 
                                        /* Repeat rate acceleration */
@@ -388,12 +439,14 @@ static keymask_t kbd_rptHandlerFunc(keymask_t key)
 
 MOD_DEFINE(kbd)
 
-/*!
+/**
  * Initialize keyboard ports and softtimer
  */
 void kbd_init(void)
 {
+#if CONFIG_KBD_BEEP
        MOD_CHECK(buzzer);
+#endif
 
        KBD_HW_INIT;
 
@@ -407,7 +460,7 @@ void kbd_init(void)
        kbd_debHandler.flags = KHF_RAWKEYS;
        kbd_addHandler(&kbd_debHandler);
 
-       #ifdef  K_LNG_MASK
+       #if CONFIG_KBD_LONGPRESS
        /* Add long pression keyboard handler */
        kbd_lngHandler.hook = kbd_lngHandlerFunc;
        kbd_lngHandler.pri = 90; /* high priority */
@@ -426,19 +479,23 @@ void kbd_init(void)
        kbd_defHandler.pri = -128; /* lowest priority */
        kbd_addHandler(&kbd_defHandler);
 
+#if CONFIG_KBD_OBSERVER
+       observer_InitSubject(&kbd_subject);
+#endif
+
 #if CONFIG_KBD_POLL == KBD_POLL_SOFTINT
 
        MOD_CHECK(timer);
 
        /* Add kbd handler to soft timers list */
-       event_initSoftInt(&kbd_timer.expire, kbd_softint, 0);
+       event_initSoftInt(&kbd_timer.expire, kbd_softint, NULL);
        timer_setDelay(&kbd_timer, ms_to_ticks(KBD_CHECK_INTERVAL));
        timer_add(&kbd_timer);
 
 #elif CONFIG_KBD_POLL == CONFIG_POLL_FREERTOS
 
        /* Create a timer specific thread */
-       xTaskCreate(kbd_poll, "kbd_poll", CONFIG_STACK_KBD,
+       xTaskCreate(kbd_task, "kbd", CONFIG_STACK_KBD,
                        NULL, CONFIG_PRI_KBD, NULL);
 
 #else