Add matrix scan code for reactive ligthing modes

anne_pro
Michiel Visser 2019-06-26 22:21:46 +02:00
parent 16866059a8
commit 81f496379a
1 changed files with 42 additions and 0 deletions

View File

@ -91,3 +91,45 @@ void keyboard_post_init_kb(void) {
matrix_init_user();
}
/* Current state of the matrix */
static matrix_row_t matrix_status[MATRIX_ROWS];
/* Buffer for the keystate packet */
static uint8_t keystate[12] = {9, 10, 7, 0};
/* Update the reactive lighting on every keymatrix scan when lighting is enabled */
void matrix_scan_kb(void) {
matrix_row_t matrix_row = 0;
matrix_row_t matrix_change = 0;
/* Determine if the matrix state has changed, and update the state */
for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
matrix_row = matrix_get_row(r);
matrix_change |= matrix_row ^ matrix_status[r];
matrix_status[r] = matrix_row;
}
/* If the leds are enabled and the state changed */
if (leds_enabled && matrix_change) {
/* Go through every scan position */
for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
for (uint8_t c = 0; c < MATRIX_COLS; c++) {
int position = r * MATRIX_COLS + c;
int index = position / 8;
int bit = position % 8;
/* Update the keystate based on the state of the matrix */
if (matrix_status[r] & (1 << c)) {
keystate[3 + index] |= (1 << bit);
} else {
keystate[3 + index] &= ~(1 << bit);
}
}
}
/* Send the keystate to the LED controller */
uartStartSend(&UARTD3, 12, keystate);
}
/* Run matrix_scan_user code */
matrix_scan_user();
}