+
+ // Matrix ghosting check and elimination
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
+#ifdef GHOSTING_MATRIX
+ // strobe = column, sense = row
+
+ // Count (rows) use for columns
+ //print("C ");
+ for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
+ {
+ uint8_t used = 0;
+ for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
+ {
+ uint8_t key = Matrix_colsNum * row + col;
+ KeyState *state = &Matrix_scanArray[ key ];
+ if ( keyOn(state->curState) )
+ used++;
+ }
+ //printInt8(used);
+ col_use[col] = used;
+ col_ghost[col] = 0; // clear
+ }
+
+ // Count (columns) use for rows
+ //print(" R ");
+ for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
+ {
+ uint8_t used = 0;
+ for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
+ {
+ uint8_t key = Matrix_colsNum * row + col;
+ KeyState *state = &Matrix_scanArray[ key ];
+ if ( keyOn(state->curState) )
+ used++;
+ }
+ //printInt8(used);
+ row_use[row] = used;
+ row_ghost[row] = 0; // clear
+ }
+
+ // Check if matrix has ghost
+ // Happens when key is pressed and some other key is pressed in same row and another in same column
+ //print(" G ");
+ for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
+ {
+ for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
+ {
+ uint8_t key = Matrix_colsNum * row + col;
+ KeyState *state = &Matrix_scanArray[ key ];
+ if ( keyOn(state->curState) && col_use[col] >= 2 && row_use[row] >= 2 )
+ {
+ // mark col and row as having ghost
+ col_ghost[col] = 1;
+ row_ghost[row] = 1;
+ //print(" "); printInt8(col); print(","); printInt8(row);
+ }
+ }
+ }
+ //print( NL );
+
+ // Send keys
+ for ( uint8_t col = 0; col < Matrix_colsNum; col++ )
+ {
+ for ( uint8_t row = 0; row < Matrix_rowsNum; row++ )
+ {
+ uint8_t key = Matrix_colsNum * row + col;
+ KeyState *state = &Matrix_scanArray[ key ];
+ KeyGhost *st = &Matrix_ghostArray[ key ];
+
+ // col or row is ghosting (crossed)
+ uint8_t ghost = (col_ghost[col] > 0 || row_ghost[row] > 0) ? 1 : 0;
+
+ st->prev = st->cur; // previous
+ // save state if no ghost or outside ghosted area
+ if ( ghost == 0 )
+ st->saved = state->curState; // save state if no ghost
+ // final
+ // use saved state if ghosting, or current if not
+ st->cur = ghost > 0 ? st->saved : state->curState;
+
+ // Send keystate to macro module
+ KeyPosition k = !st->cur
+ ? (!st->prev ? KeyState_Off : KeyState_Release)
+ : ( st->prev ? KeyState_Hold : KeyState_Press);
+ Macro_keyState( key, k );
+ }
+ }
+#endif
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
+
+