2021-09-19 05:10:32 +02:00
|
|
|
/*
|
2023-06-09 03:46:09 +02:00
|
|
|
* Copyright 2018-2023 Jack Humbert <jack.humb@gmail.com>
|
2021-09-19 05:10:32 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2024-03-30 15:07:19 +01:00
|
|
|
#include "matrix.h"
|
|
|
|
#include "wait.h"
|
2021-09-19 05:10:32 +02:00
|
|
|
|
|
|
|
/* matrix state(1:on, 0:off) */
|
2023-06-09 03:46:09 +02:00
|
|
|
static pin_t matrix_row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
|
|
|
static pin_t matrix_col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
2021-09-19 05:10:32 +02:00
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
static matrix_row_t matrix_inverted[MATRIX_COLS];
|
2021-09-19 05:10:32 +02:00
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
void matrix_init_custom(void) {
|
|
|
|
// actual matrix setup - cols
|
|
|
|
for (int i = 0; i < MATRIX_COLS; i++) {
|
2024-05-03 07:21:29 +02:00
|
|
|
gpio_set_pin_output(matrix_col_pins[i]);
|
|
|
|
gpio_write_pin_low(matrix_col_pins[i]);
|
2023-06-09 03:46:09 +02:00
|
|
|
}
|
2021-09-19 05:10:32 +02:00
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
// rows
|
|
|
|
for (int i = 0; i < MATRIX_ROWS; i++) {
|
2024-05-03 07:21:29 +02:00
|
|
|
gpio_set_pin_input_low(matrix_row_pins[i]);
|
2023-06-09 03:46:09 +02:00
|
|
|
}
|
2022-07-11 15:17:05 +02:00
|
|
|
}
|
2021-09-19 05:10:32 +02:00
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
bool matrix_scan_custom(matrix_row_t current_matrix[]) {
|
|
|
|
bool changed = false;
|
2021-09-19 05:10:32 +02:00
|
|
|
|
|
|
|
// actual matrix
|
|
|
|
for (int col = 0; col < MATRIX_COLS; col++) {
|
|
|
|
matrix_row_t data = 0;
|
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
// strobe col
|
2024-05-03 07:21:29 +02:00
|
|
|
gpio_write_pin_high(matrix_col_pins[col]);
|
2021-09-19 05:10:32 +02:00
|
|
|
|
|
|
|
// need wait to settle pin state
|
|
|
|
wait_us(20);
|
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
// read row data
|
|
|
|
for (int row = 0; row < MATRIX_ROWS; row++) {
|
2024-05-03 07:21:29 +02:00
|
|
|
data |= (gpio_read_pin(matrix_row_pins[row]) << row);
|
2021-09-19 05:10:32 +02:00
|
|
|
}
|
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
// unstrobe col
|
2024-05-03 07:21:29 +02:00
|
|
|
gpio_write_pin_low(matrix_col_pins[col]);
|
2021-09-19 05:10:32 +02:00
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
if (matrix_inverted[col] != data) {
|
|
|
|
matrix_inverted[col] = data;
|
2021-09-19 05:10:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-09 03:46:09 +02:00
|
|
|
for (int row = 0; row < MATRIX_ROWS; row++) {
|
|
|
|
matrix_row_t old = current_matrix[row];
|
|
|
|
current_matrix[row] = 0;
|
2021-09-19 05:10:32 +02:00
|
|
|
for (int col = 0; col < MATRIX_COLS; col++) {
|
2023-06-09 03:46:09 +02:00
|
|
|
current_matrix[row] |= ((matrix_inverted[col] & (1 << row) ? 1 : 0) << col);
|
2021-09-19 05:10:32 +02:00
|
|
|
}
|
2023-06-09 03:46:09 +02:00
|
|
|
changed |= old != current_matrix[row];
|
2021-09-19 05:10:32 +02:00
|
|
|
}
|
2023-06-09 03:46:09 +02:00
|
|
|
|
|
|
|
return changed;
|
2021-09-19 05:10:32 +02:00
|
|
|
}
|