]> 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 93a066e574a3cd763212b1149fee278065610f72..d1794c8878f82d414c429a876ecde06fe3d9cd42 100644 (file)
@@ -62,45 +62,55 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #endif
 
 
-
 #ifdef MATRIX_HAS_GHOST
 static uint16_t matrix_ghost_check[MATRIX_ROWS];
-static bool has_ghost_in_row(uint8_t row)
+
+static inline bool countones(uint16_t data)
+{
+    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)
 {
-    matrix_row_t matrix_row = (matrix_get_row(row) & matrix_ghost_check[row]);
+    rowdata &= matrix_ghost_check[row];
+    if (((rowdata - 1) & rowdata) == 0){
+        return false;
+    }
     /* 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.   */
-    if (((matrix_row - 1) & matrix_row) == 0){
-        return false;
-    }
     // 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 && __builtin_popcount((matrix_get_row(i) & matrix_ghost_check[i]) & matrix_row) > 1){
+        if (i != row && countones((matrix_get_row(i) & matrix_ghost_check[i]) & rowdata)){
             return true;
         }
     }
     return false;
-    return false;
 }
 
-extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
+extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
 // bit map of true keys and empty spots in matrix, each row is reversed
-void make_ghost_check_array(){
+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 (keymaps[0][row][col] & 0xFF)
+            if (pgm_read_byte(&keymaps[0][row][col]) != 0)
                 matrix_ghost_check[row] |= 1<<col;
-            else
-                matrix_ghost_check[row] |= 0<<col;
         }
     }
 }
 
 #endif
 
+
 __attribute__ ((weak))
 void matrix_setup(void) {
 }
@@ -163,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.