]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - tmk_core/common/keyboard.c
a bit smaller
[qmk_firmware.git] / tmk_core / common / keyboard.c
index 371d93f3e59b755130b5afdb7fc8428e879d8bf9..d1794c8878f82d414c429a876ecde06fe3d9cd42 100644 (file)
@@ -14,6 +14,7 @@ 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 "keyboard.h"
 #include "matrix.h"
@@ -50,6 +51,9 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #ifdef RGBLIGHT_ENABLE
 #   include "rgblight.h"
 #endif
+#ifdef FAUXCLICKY_ENABLE
+#   include "fauxclicky.h"
+#endif
 #ifdef SERIAL_LINK_ENABLE
 #   include "serial_link/system/serial_link.h"
 #endif
@@ -58,24 +62,55 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #endif
 
 
-
 #ifdef MATRIX_HAS_GHOST
-static bool has_ghost_in_row(uint8_t row)
+static uint16_t matrix_ghost_check[MATRIX_ROWS];
+
+static inline bool countones(uint16_t data)
 {
-    matrix_row_t matrix_row = matrix_get_row(row);
-    // No ghost exists when less than 2 keys are down on the row
-    if (((matrix_row - 1) & matrix_row) == 0)
+    int count = 0;
+    for (int col = 0; col < MATRIX_COLS; col++) {
+        if (data & (1<<col))
+            count++;
+    }
+    if (count > 1){
+        return true;
+    }
+    return false;
+}
+static inline bool has_ghost_in_row(uint8_t row, uint16_t rowdata)
+{
+    rowdata &= matrix_ghost_check[row];
+    if (((rowdata - 1) & rowdata) == 0){
         return false;
-
-    // Ghost occurs when the row shares column line with other row
+    }
+    /* No ghost exists when less than 2 keys are down on the row.
+    If there are "active" blanks in the matrix, the key can't be pressed by the user,
+    there is no doubt as to which keys are really being pressed.
+    The ghosts will be ignored, they are KC_NO.   */
+    // Ghost occurs when the row shares column line with other row, blanks in the matrix don't matter
+    // If there are more than two real keys pressed and they match another row's real keys, the row will be ignored.
     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
-        if (i != row && (matrix_get_row(i) & matrix_row))
+        if (i != row && countones((matrix_get_row(i) & matrix_ghost_check[i]) & rowdata)){
             return true;
+        }
     }
     return false;
 }
+
+extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
+// bit map of true keys and empty spots in matrix, each row is reversed
+static inline void make_ghost_check_array(void){
+    for (int row = 0; row < MATRIX_ROWS; row++) {
+        for (int col = 0; col < MATRIX_COLS; col++) {
+            if (pgm_read_byte(&keymaps[0][row][col]) != 0)
+                matrix_ghost_check[row] |= 1<<col;
+        }
+    }
+}
+
 #endif
 
+
 __attribute__ ((weak))
 void matrix_setup(void) {
 }
@@ -107,9 +142,15 @@ void keyboard_init(void) {
 #ifdef RGBLIGHT_ENABLE
     rgblight_init();
 #endif
+#ifdef FAUXCLICKY_ENABLE
+    fauxclicky_init();
+#endif
 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
     keymap_config.nkro = 1;
 #endif
+#ifdef MATRIX_HAS_GHOST
+    make_ghost_check_array();
+#endif
 }
 
 /*
@@ -132,7 +173,7 @@ void keyboard_task(void)
         matrix_change = matrix_row ^ matrix_prev[r];
         if (matrix_change) {
 #ifdef MATRIX_HAS_GHOST
-            if (has_ghost_in_row(r)) {
+            if (has_ghost_in_row(r, matrix_row)) {
                 /* Keep track of whether ghosted status has changed for
                  * debugging. But don't update matrix_prev until un-ghosted, or
                  * the last key would be lost.
@@ -188,7 +229,7 @@ MATRIX_LOOP_END:
 #endif
 
 #ifdef VISUALIZER_ENABLE
-    visualizer_update(default_layer_state, layer_state, host_keyboard_leds());
+    visualizer_update(default_layer_state, layer_state, visualizer_get_mods(), host_keyboard_leds());
 #endif
 
     // update LED