]> git.donarmstrong.com Git - tmk_firmware.git/blobdiff - common/keyboard.c
Replace layer_stack with layer_switch
[tmk_firmware.git] / common / keyboard.c
index 25f32eb02f52a615adf4c44e016afb03a488ff4f..e4bc3dc8c26fadf01fff978c633c2dfe3c163743 100644 (file)
@@ -1,5 +1,5 @@
 /*
-Copyright 2011 Jun Wako <wakojun@gmail.com>
+Copyright 2011,2012,2013 Jun Wako <wakojun@gmail.com>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -14,179 +14,101 @@ GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
+#include <stdint.h>
+#include <util/delay.h>
 #include "keyboard.h"
-#include "host.h"
-#include "layer.h"
 #include "matrix.h"
+#include "keymap.h"
+#include "host.h"
 #include "led.h"
-#include "usb_keycodes.h"
+#include "keycode.h"
 #include "timer.h"
 #include "print.h"
 #include "debug.h"
 #include "command.h"
+#include "util.h"
+#include "sendchar.h"
+#include "bootloader.h"
 #ifdef MOUSEKEY_ENABLE
 #include "mousekey.h"
 #endif
-#ifdef EXTRAKEY_ENABLE
-#include <util/delay.h>
-#endif
-
-
-static uint8_t last_leds = 0;
 
 
 void keyboard_init(void)
 {
+    // TODO: configuration of sendchar impl
+    print_sendchar_func = sendchar;
+
     timer_init();
     matrix_init();
+
+    /* matrix scan for boot magic keys */
+#ifdef DEBOUNCE
+    uint8_t scan = DEBOUNCE * 2;
+    while (scan--) { matrix_scan(); _delay_ms(1); }
+#else
+    matrix_scan();
+#endif
+
+    /* boot magic keys */
+#ifdef IS_BOOTMAGIC_BOOTLOADER
+    /* kick up bootloader */
+    if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump();
+#endif
+#ifdef IS_BOOTMAGIC_DEBUG
+    if (IS_BOOTMAGIC_DEBUG()) debug_enable = true;
+#endif
+
 #ifdef PS2_MOUSE_ENABLE
     ps2_mouse_init();
 #endif
 }
 
-void keyboard_proc(void)
+/*
+ * Do keyboard routine jobs: scan mantrix, light LEDs, ...
+ * This is repeatedly called as fast as possible.
+ */
+void keyboard_task(void)
 {
-    uint8_t fn_bits = 0;
-#ifdef EXTRAKEY_ENABLE
-    uint16_t consumer_code = 0;
-    uint16_t system_code = 0;
-#endif
+    static matrix_row_t matrix_prev[MATRIX_ROWS];
+    static uint8_t led_status = 0;
+    matrix_row_t matrix_row = 0;
+    matrix_row_t matrix_change = 0;
 
     matrix_scan();
-
-    if (matrix_is_modified()) {
-        if (debug_matrix) matrix_print();
-#ifdef DEBUG_LED
-        // LED flash for debug
-        DEBUG_LED_CONFIG;
-        DEBUG_LED_ON;
-#endif
-    }
-
-    if (matrix_has_ghost()) {
-        // should send error?
-        debug("matrix has ghost!!\n");
-        return;
-    }
-
-    host_swap_keyboard_report();
-    host_clear_keyboard_report();
-    for (int row = 0; row < matrix_rows(); row++) {
-        for (int col = 0; col < matrix_cols(); col++) {
-            if (!matrix_is_on(row, col)) continue;
-
-            uint8_t code = layer_get_keycode(row, col);
-            if (code == KB_NO) {
-                // do nothing
-            } else if (IS_MOD(code)) {
-                host_add_mod_bit(MOD_BIT(code));
-            } else if (IS_FN(code)) {
-                fn_bits |= FN_BIT(code);
-            }
-// TODO: use table or something
-#ifdef EXTRAKEY_ENABLE
-            // System Control
-            else if (code == KB_SYSTEM_POWER) {
-#ifdef HOST_PJRC
-                if (suspend && remote_wakeup) {
-                    usb_remote_wakeup();
+    for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
+        matrix_row = matrix_get_row(r);
+        matrix_change = matrix_row ^ matrix_prev[r];
+        if (matrix_change) {
+            if (debug_matrix) matrix_print();
+
+            for (uint8_t c = 0; c < MATRIX_COLS; c++) {
+                if (matrix_change & ((matrix_row_t)1<<c)) {
+                    action_exec((keyevent_t){
+                        .key = (key_t){ .row = r, .col = c },
+                        .pressed = (matrix_row & (1<<c)),
+                        .time = (timer_read() | 1) /* time should not be 0 */
+                    });
+                    // record a processed key
+                    matrix_prev[r] ^= ((matrix_row_t)1<<c);
+                    // process a key per task call
+                    goto MATRIX_LOOP_END;
                 }
-#endif
-                system_code = SYSTEM_POWER_DOWN;
-            } else if (code == KB_SYSTEM_SLEEP) {
-                system_code = SYSTEM_SLEEP;
-            } else if (code == KB_SYSTEM_WAKE) {
-                system_code = SYSTEM_WAKE_UP;
-            }
-            // Consumer Page
-            else if (code == KB_AUDIO_MUTE) {
-                consumer_code = AUDIO_MUTE;
-            } else if (code == KB_AUDIO_VOL_UP) {
-                consumer_code = AUDIO_VOL_UP;
-            } else if (code == KB_AUDIO_VOL_DOWN) {
-                consumer_code = AUDIO_VOL_DOWN;
-            }
-            else if (code == KB_MEDIA_NEXT_TRACK) {
-                consumer_code = TRANSPORT_NEXT_TRACK;
-            } else if (code == KB_MEDIA_PREV_TRACK) {
-                consumer_code = TRANSPORT_PREV_TRACK;
-            } else if (code == KB_MEDIA_STOP) {
-                consumer_code = TRANSPORT_STOP;
-            } else if (code == KB_MEDIA_PLAY_PAUSE) {
-                consumer_code = TRANSPORT_PLAY_PAUSE;
-            } else if (code == KB_MEDIA_SELECT) {
-                consumer_code = AL_CC_CONFIG;
-            }
-            else if (code == KB_MAIL) {
-                consumer_code = AL_EMAIL;
-            } else if (code == KB_CALCULATOR) {
-                consumer_code = AL_CALCULATOR;
-            } else if (code == KB_MY_COMPUTER) {
-                consumer_code = AL_LOCAL_BROWSER;
-            }
-            else if (code == KB_WWW_SEARCH) {
-                consumer_code = AC_SEARCH;
-            } else if (code == KB_WWW_HOME) {
-                consumer_code = AC_HOME;
-            } else if (code == KB_WWW_BACK) {
-                consumer_code = AC_BACK;
-            } else if (code == KB_WWW_FORWARD) {
-                consumer_code = AC_FORWARD;
-            } else if (code == KB_WWW_STOP) {
-                consumer_code = AC_STOP;
-            } else if (code == KB_WWW_REFRESH) {
-                consumer_code = AC_REFRESH;
-            } else if (code == KB_WWW_FAVORITES) {
-                consumer_code = AC_BOOKMARKS;
-            }
-#endif
-            else if (IS_KEY(code)) {
-                host_add_key(code);
-            }
-#ifdef MOUSEKEY_ENABLE
-            else if (IS_MOUSEKEY(code)) {
-                mousekey_decode(code);
-            }
-#endif
-            else {
-                debug("ignore keycode: "); debug_hex(code); debug("\n");
             }
         }
     }
+    // call with pseudo tick event when no real key event.
+    action_exec(TICK);
 
-    layer_switching(fn_bits);
-
-    if (command_proc()) {
-        return;
-    }
-
-    // TODO: should send only when changed from last report
-    if (matrix_is_modified()) {
-        host_send_keyboard_report();
-#ifdef EXTRAKEY_ENABLE
-        host_consumer_send(consumer_code);
-        host_system_send(system_code);
-#endif
-#ifdef DEBUG_LED
-        // LED flash for debug
-        DEBUG_LED_CONFIG;
-        DEBUG_LED_OFF;
-#endif
-    }
-
+MATRIX_LOOP_END:
 #ifdef MOUSEKEY_ENABLE
-    mousekey_send();
+    // mousekey repeat & acceleration
+    mousekey_task();
 #endif
-
-#ifdef PS2_MOUSE_ENABLE
-    // TODO: should comform new API
-    if (ps2_mouse_read() == 0)
-        ps2_mouse_usb_send();
-#endif
-
-    if (last_leds != host_keyboard_leds()) {
-        keyboard_set_leds(host_keyboard_leds());
-        last_leds = host_keyboard_leds();
+    // update LED
+    if (led_status != host_keyboard_leds()) {
+        led_status = host_keyboard_leds();
+        keyboard_set_leds(led_status);
     }
 }