2020-02-10 13:16:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
#include "wait.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "matrix.h"
|
|
|
|
|
|
|
|
// Encoder things
|
|
|
|
#define SWITCH_1 F7
|
|
|
|
#define SWITCH_2 D7
|
2020-07-17 12:35:44 +00:00
|
|
|
static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row);
|
2020-02-10 13:16:27 +00:00
|
|
|
|
|
|
|
static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
|
|
|
static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
|
|
|
|
|
|
|
/* matrix state(1:on, 0:off) */
|
2021-02-28 05:03:49 +00:00
|
|
|
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
|
|
|
extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
2020-02-10 13:16:27 +00:00
|
|
|
|
|
|
|
static void select_row(uint8_t row) {
|
2024-05-03 05:21:29 +00:00
|
|
|
gpio_set_pin_output(row_pins[row]);
|
|
|
|
gpio_write_pin_low(row_pins[row]);
|
2020-02-10 13:16:27 +00:00
|
|
|
}
|
|
|
|
|
2024-05-03 05:21:29 +00:00
|
|
|
static void unselect_row(uint8_t row) { gpio_set_pin_input_high(row_pins[row]); }
|
2020-02-10 13:16:27 +00:00
|
|
|
|
|
|
|
static void unselect_rows(void) {
|
|
|
|
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
2024-05-03 05:21:29 +00:00
|
|
|
gpio_set_pin_input_high(row_pins[x]);
|
2020-02-10 13:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_pins(void) {
|
|
|
|
unselect_rows();
|
|
|
|
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
2024-05-03 05:21:29 +00:00
|
|
|
gpio_set_pin_input_high(col_pins[x]);
|
2020-02-10 13:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
|
|
|
|
// Store last value of row prior to reading
|
|
|
|
matrix_row_t last_row_value = current_matrix[current_row];
|
|
|
|
|
|
|
|
// Clear data in matrix row
|
|
|
|
current_matrix[current_row] = 0;
|
|
|
|
|
|
|
|
// Select row and wait for row selecton to stabilize
|
|
|
|
select_row(current_row);
|
|
|
|
wait_us(30);
|
|
|
|
|
|
|
|
// For each col...
|
|
|
|
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
|
|
|
// Select the col pin to read (active low)
|
2024-05-03 05:21:29 +00:00
|
|
|
uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
|
2020-02-10 13:16:27 +00:00
|
|
|
|
|
|
|
// Populate the matrix row with the state of the col pin
|
|
|
|
current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unselect row
|
2020-07-17 12:35:44 +00:00
|
|
|
unselect_row(current_row);
|
2020-02-10 13:16:27 +00:00
|
|
|
|
|
|
|
return (last_row_value != current_matrix[current_row]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-28 05:03:49 +00:00
|
|
|
void matrix_init_custom(void) {
|
2020-02-10 13:16:27 +00:00
|
|
|
// initialize key pins
|
2024-05-03 05:21:29 +00:00
|
|
|
gpio_set_pin_input(SWITCH_1);
|
|
|
|
gpio_set_pin_input(SWITCH_2);
|
2020-02-10 13:16:27 +00:00
|
|
|
init_pins();
|
|
|
|
}
|
|
|
|
|
2021-02-28 05:03:49 +00:00
|
|
|
bool matrix_scan_custom(void) {
|
2020-02-10 13:16:27 +00:00
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
// Set row, read cols
|
|
|
|
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
|
|
|
|
changed |= read_cols_on_row(raw_matrix, current_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read encoder switches, already debounced
|
2020-07-17 12:35:44 +00:00
|
|
|
changed |= read_encoder_switches(matrix, 4);
|
2020-02-10 13:16:27 +00:00
|
|
|
|
2021-02-28 05:03:49 +00:00
|
|
|
return changed;
|
2020-02-10 13:16:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 12:35:44 +00:00
|
|
|
static bool read_encoder_switches(matrix_row_t current_matrix[], uint8_t current_row) {
|
2020-02-10 13:16:27 +00:00
|
|
|
// Store last value of row prior to reading
|
|
|
|
matrix_row_t last_row_value = current_matrix[current_row];
|
|
|
|
|
|
|
|
// Clear data in matrix row
|
|
|
|
current_matrix[current_row] = 0;
|
|
|
|
|
|
|
|
// Debounce the encoder buttons using a shift register
|
|
|
|
static uint8_t btn_1_array;
|
|
|
|
static uint8_t btn_2_array;
|
2021-02-28 05:03:49 +00:00
|
|
|
bool btn_1_rise = 0;
|
|
|
|
bool btn_2_rise = 0;
|
2020-02-10 13:16:27 +00:00
|
|
|
btn_1_array <<= 1;
|
|
|
|
btn_2_array <<= 1;
|
2024-05-03 05:21:29 +00:00
|
|
|
btn_1_array |= gpio_read_pin(SWITCH_1);
|
|
|
|
btn_2_array |= gpio_read_pin(SWITCH_2);
|
2021-02-28 05:03:49 +00:00
|
|
|
(btn_1_array == 0b01111111) ? (btn_1_rise = 1) : (btn_1_rise = 0);
|
|
|
|
(btn_2_array == 0b01111111) ? (btn_2_rise = 1) : (btn_2_rise = 0);
|
2020-02-10 13:16:27 +00:00
|
|
|
|
|
|
|
// Populate the matrix row with the state of the encoder
|
2021-02-28 05:03:49 +00:00
|
|
|
current_matrix[current_row] |= btn_1_rise ? (1 << 0) : 0;
|
|
|
|
current_matrix[current_row] |= btn_2_rise ? (1 << 1) : 0;
|
2020-02-10 13:16:27 +00:00
|
|
|
|
|
|
|
return (last_row_value != current_matrix[current_row]);
|
2021-02-28 05:03:49 +00:00
|
|
|
}
|