mirror of
https://github.com/qmk/qmk_firmware.git
synced 2025-09-10 17:15:43 +00:00
Merge 9ad57ba38f into c113250c4e
This commit is contained in:
4
docs/ChangeLog/20251130/PR25632.md
Normal file
4
docs/ChangeLog/20251130/PR25632.md
Normal file
@@ -0,0 +1,4 @@
|
||||
# Changes Requiring User Action
|
||||
## Debounce: Deprecate init and remove num_rows parameter [#25632](https://github.com/qmk/qmk_firmware/pull/25632)
|
||||
|
||||
With dynamic memory allocation removed from all debounce implementations ([#25515](https://github.com/qmk/qmk_firmware/pull/25515)), the `debounce_init()` function is now deprecated, and the `num_rows` parameter has been removed from the `debounce()` function. The `MATRIX_ROWS_PER_HAND` is now used by default in every implementation.
|
||||
@@ -73,9 +73,6 @@ void matrix_print(void) {
|
||||
void matrix_init(void) {
|
||||
// TODO: initialize hardware and global matrix state here
|
||||
|
||||
// Unless hardware debouncing - Init the configured debounce routine
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
// This *must* be called for correct keyboard behavior
|
||||
matrix_init_kb();
|
||||
}
|
||||
@@ -86,7 +83,7 @@ uint8_t matrix_scan(void) {
|
||||
// TODO: add matrix scanning routine here
|
||||
|
||||
// Unless hardware debouncing - use the configured debounce routine
|
||||
changed = debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
changed = debounce(raw_matrix, matrix, changed);
|
||||
|
||||
// This *must* be called for correct keyboard behavior
|
||||
matrix_scan_kb();
|
||||
|
||||
@@ -31,13 +31,17 @@ static matrix_row_t matrix[MATRIX_ROWS];
|
||||
static matrix_row_t matrix_debouncing[MATRIX_ROWS];
|
||||
|
||||
static matrix_row_t read_cols(void);
|
||||
static void select_row(uint8_t col);
|
||||
static void select_row(uint8_t col);
|
||||
|
||||
// user-defined overridable functions
|
||||
|
||||
__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
@@ -45,105 +49,125 @@ __attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
// helper functions
|
||||
|
||||
inline uint8_t matrix_rows(void)
|
||||
{
|
||||
return MATRIX_ROWS;
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline uint8_t matrix_cols(void)
|
||||
{
|
||||
return MATRIX_COLS;
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
void matrix_init(void)
|
||||
{
|
||||
/* Column output pins */
|
||||
DDRD |= 0b01111011;
|
||||
/* Row input pins */
|
||||
DDRC &= ~0b10000000;
|
||||
DDRB &= ~0b01111111;
|
||||
PORTC |= 0b10000000;
|
||||
PORTB |= 0b01111111;
|
||||
void matrix_init(void) {
|
||||
/* Column output pins */
|
||||
DDRD |= 0b01111011;
|
||||
/* Row input pins */
|
||||
DDRC &= ~0b10000000;
|
||||
DDRB &= ~0b01111111;
|
||||
PORTC |= 0b10000000;
|
||||
PORTB |= 0b01111111;
|
||||
|
||||
for (uint8_t i=0; i < matrix_rows(); i++) {
|
||||
matrix[i] = 0;
|
||||
matrix_debouncing[i] = 0;
|
||||
}
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
bool changed = false;
|
||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||
select_row(col);
|
||||
wait_us(30);
|
||||
matrix_row_t rows = read_cols();
|
||||
for (uint8_t row = 0; row < matrix_rows(); row++) {
|
||||
bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
|
||||
bool curr_bit = rows & (1<<row);
|
||||
if ((changed |= prev_bit != curr_bit)) {
|
||||
matrix_debouncing[row] ^= (matrix_row_t) 1 << col;
|
||||
}
|
||||
for (uint8_t i = 0; i < matrix_rows(); i++) {
|
||||
matrix[i] = 0;
|
||||
matrix_debouncing[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
debounce(matrix_debouncing, matrix, matrix_rows(), changed);
|
||||
matrix_scan_kb();
|
||||
|
||||
return (uint8_t)changed;
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
return matrix[row];
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||
select_row(col);
|
||||
wait_us(30);
|
||||
matrix_row_t rows = read_cols();
|
||||
for (uint8_t row = 0; row < matrix_rows(); row++) {
|
||||
bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1 << col);
|
||||
bool curr_bit = rows & (1 << row);
|
||||
if ((changed |= prev_bit != curr_bit)) {
|
||||
matrix_debouncing[row] ^= (matrix_row_t)1 << col;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debounce(matrix_debouncing, matrix, changed);
|
||||
matrix_scan_kb();
|
||||
|
||||
return (uint8_t)changed;
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
print("\nr/c 0123456789ABCDEF\n");
|
||||
for (uint8_t row = 0; row < matrix_rows(); row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_bin_reverse16(matrix_get_row(row));
|
||||
print("\n");
|
||||
}
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
return matrix[row];
|
||||
}
|
||||
|
||||
static matrix_row_t read_cols(void)
|
||||
{
|
||||
return
|
||||
(PINB&(1<<5) ? 0 : ((matrix_row_t)1<<0)) |
|
||||
(PINC&(1<<7) ? 0 : ((matrix_row_t)1<<1)) |
|
||||
(PINB&(1<<4) ? 0 : ((matrix_row_t)1<<2)) |
|
||||
(PINB&(1<<6) ? 0 : ((matrix_row_t)1<<3)) |
|
||||
(PINB&(1<<1) ? 0 : ((matrix_row_t)1<<4)) |
|
||||
(PINB&(1<<2) ? 0 : ((matrix_row_t)1<<5)) |
|
||||
(PINB&(1<<3) ? 0 : ((matrix_row_t)1<<6)) |
|
||||
(PINB&(1<<0) ? 0 : ((matrix_row_t)1<<7));
|
||||
void matrix_print(void) {
|
||||
print("\nr/c 0123456789ABCDEF\n");
|
||||
for (uint8_t row = 0; row < matrix_rows(); row++) {
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_bin_reverse16(matrix_get_row(row));
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void select_row(uint8_t col)
|
||||
{
|
||||
switch (col) {
|
||||
case 0: PORTD = (PORTD & ~0b01111011) | 0b00011011; break;
|
||||
case 1: PORTD = (PORTD & ~0b01111011) | 0b01000011; break;
|
||||
case 2: PORTD = (PORTD & ~0b01111011) | 0b01100000; break;
|
||||
case 3: PORTD = (PORTD & ~0b01111011) | 0b01111001; break;
|
||||
case 4: PORTD = (PORTD & ~0b01111011) | 0b01100010; break;
|
||||
case 5: PORTD = (PORTD & ~0b01111011) | 0b01101010; break;
|
||||
case 6: PORTD = (PORTD & ~0b01111011) | 0b01110001; break;
|
||||
case 7: PORTD = (PORTD & ~0b01111011) | 0b01101001; break;
|
||||
case 8: PORTD = (PORTD & ~0b01111011) | 0b01100001; break;
|
||||
case 9: PORTD = (PORTD & ~0b01111011) | 0b01111000; break;
|
||||
case 10: PORTD = (PORTD & ~0b01111011) | 0b00100011; break;
|
||||
case 11: PORTD = (PORTD & ~0b01111011) | 0b00101011; break;
|
||||
case 12: PORTD = (PORTD & ~0b01111011) | 0b00110011; break;
|
||||
case 13: PORTD = (PORTD & ~0b01111011) | 0b01110000; break;
|
||||
case 14: PORTD = (PORTD & ~0b01111011) | 0b00010011; break;
|
||||
case 15: PORTD = (PORTD & ~0b01111011) | 0b01101000; break;
|
||||
case 16: PORTD = (PORTD & ~0b01111011) | 0b00001011; break;
|
||||
case 17: PORTD = (PORTD & ~0b01111011) | 0b00111011; break;
|
||||
}
|
||||
static matrix_row_t read_cols(void) {
|
||||
return (PINB & (1 << 5) ? 0 : ((matrix_row_t)1 << 0)) | (PINC & (1 << 7) ? 0 : ((matrix_row_t)1 << 1)) | (PINB & (1 << 4) ? 0 : ((matrix_row_t)1 << 2)) | (PINB & (1 << 6) ? 0 : ((matrix_row_t)1 << 3)) | (PINB & (1 << 1) ? 0 : ((matrix_row_t)1 << 4)) | (PINB & (1 << 2) ? 0 : ((matrix_row_t)1 << 5)) | (PINB & (1 << 3) ? 0 : ((matrix_row_t)1 << 6)) | (PINB & (1 << 0) ? 0 : ((matrix_row_t)1 << 7));
|
||||
}
|
||||
|
||||
static void select_row(uint8_t col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b00011011;
|
||||
break;
|
||||
case 1:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01000011;
|
||||
break;
|
||||
case 2:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01100000;
|
||||
break;
|
||||
case 3:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01111001;
|
||||
break;
|
||||
case 4:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01100010;
|
||||
break;
|
||||
case 5:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01101010;
|
||||
break;
|
||||
case 6:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01110001;
|
||||
break;
|
||||
case 7:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01101001;
|
||||
break;
|
||||
case 8:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01100001;
|
||||
break;
|
||||
case 9:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01111000;
|
||||
break;
|
||||
case 10:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b00100011;
|
||||
break;
|
||||
case 11:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b00101011;
|
||||
break;
|
||||
case 12:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b00110011;
|
||||
break;
|
||||
case 13:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01110000;
|
||||
break;
|
||||
case 14:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b00010011;
|
||||
break;
|
||||
case 15:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b01101000;
|
||||
break;
|
||||
case 16:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b00001011;
|
||||
break;
|
||||
case 17:
|
||||
PORTD = (PORTD & ~0b01111011) | 0b00111011;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Jane Bernhardt (https://github.com/germ)
|
||||
// SPDX-License-Identifier: GPL-2.0+
|
||||
|
||||
#include "gergo.h"
|
||||
|
||||
bool i2c_initialized = 0;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Jane Bernhardt (https://github.com/germ)
|
||||
// SPDX-License-Identifier: GPL-2.0+
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "quantum.h"
|
||||
|
||||
@@ -24,76 +24,75 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "gergo.h"
|
||||
|
||||
#ifdef BALLER
|
||||
#include <avr/interrupt.h>
|
||||
#include "pointing_device.h"
|
||||
# include <avr/interrupt.h>
|
||||
# include "pointing_device.h"
|
||||
#endif
|
||||
|
||||
#ifndef DEBOUNCE
|
||||
# define DEBOUNCE 5
|
||||
# define DEBOUNCE 5
|
||||
#endif
|
||||
|
||||
// MCP Pin Defs
|
||||
#define RROW1 (1u<<3)
|
||||
#define RROW2 (1u<<2)
|
||||
#define RROW3 (1u<<1)
|
||||
#define RROW4 (1u<<0)
|
||||
#define COL0 (1u<<0)
|
||||
#define COL1 (1u<<1)
|
||||
#define COL2 (1u<<2)
|
||||
#define COL3 (1u<<3)
|
||||
#define COL4 (1u<<4)
|
||||
#define COL5 (1u<<5)
|
||||
#define COL6 (1u<<6)
|
||||
#define RROW1 (1u << 3)
|
||||
#define RROW2 (1u << 2)
|
||||
#define RROW3 (1u << 1)
|
||||
#define RROW4 (1u << 0)
|
||||
#define COL0 (1u << 0)
|
||||
#define COL1 (1u << 1)
|
||||
#define COL2 (1u << 2)
|
||||
#define COL3 (1u << 3)
|
||||
#define COL4 (1u << 4)
|
||||
#define COL5 (1u << 5)
|
||||
#define COL6 (1u << 6)
|
||||
|
||||
// ATmega pin defs
|
||||
#define ROW1 (1u<<6)
|
||||
#define ROW2 (1u<<5)
|
||||
#define ROW3 (1u<<4)
|
||||
#define ROW4 (1u<<1)
|
||||
#define COL7 (1u<<0)
|
||||
#define COL8 (1u<<1)
|
||||
#define COL9 (1u<<2)
|
||||
#define COL10 (1u<<3)
|
||||
#define COL11 (1u<<2)
|
||||
#define COL12 (1u<<3)
|
||||
#define COL13 (1u<<6)
|
||||
|
||||
//Trackball pin defs
|
||||
#define TRKUP (1u<<4)
|
||||
#define TRKDN (1u<<5)
|
||||
#define TRKLT (1u<<6)
|
||||
#define TRKRT (1u<<7)
|
||||
#define TRKBTN (1u<<6)
|
||||
#define ROW1 (1u << 6)
|
||||
#define ROW2 (1u << 5)
|
||||
#define ROW3 (1u << 4)
|
||||
#define ROW4 (1u << 1)
|
||||
#define COL7 (1u << 0)
|
||||
#define COL8 (1u << 1)
|
||||
#define COL9 (1u << 2)
|
||||
#define COL10 (1u << 3)
|
||||
#define COL11 (1u << 2)
|
||||
#define COL12 (1u << 3)
|
||||
#define COL13 (1u << 6)
|
||||
|
||||
// Trackball pin defs
|
||||
#define TRKUP (1u << 4)
|
||||
#define TRKDN (1u << 5)
|
||||
#define TRKLT (1u << 6)
|
||||
#define TRKRT (1u << 7)
|
||||
#define TRKBTN (1u << 6)
|
||||
|
||||
// Multiple for mouse moves
|
||||
#ifndef TRKSTEP
|
||||
#define TRKSTEP 20
|
||||
# define TRKSTEP 20
|
||||
#endif
|
||||
|
||||
// multiple for mouse scroll
|
||||
#ifndef SCROLLSTEP
|
||||
#define SCROLLSTEP 5
|
||||
# define SCROLLSTEP 5
|
||||
#endif
|
||||
|
||||
// bit masks
|
||||
#define BMASK (COL7 | COL8 | COL9 | COL10)
|
||||
#define CMASK (COL13)
|
||||
#define DMASK (COL11 | COL12)
|
||||
#define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
|
||||
#define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
|
||||
#define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
|
||||
#define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT)
|
||||
#define BMASK (COL7 | COL8 | COL9 | COL10)
|
||||
#define CMASK (COL13)
|
||||
#define DMASK (COL11 | COL12)
|
||||
#define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
|
||||
#define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
|
||||
#define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
|
||||
#define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT)
|
||||
|
||||
// Trackball interrupts accumulate over here. Processed on scan
|
||||
// Stores prev state of mouse, high bits store direction
|
||||
uint8_t trkState = 0;
|
||||
uint8_t trkBtnState = 0;
|
||||
|
||||
volatile uint8_t tbUpCnt = 0;
|
||||
volatile uint8_t tbDnCnt = 0;
|
||||
volatile uint8_t tbLtCnt = 0;
|
||||
volatile uint8_t tbRtCnt = 0;
|
||||
volatile uint8_t tbUpCnt = 0;
|
||||
volatile uint8_t tbDnCnt = 0;
|
||||
volatile uint8_t tbLtCnt = 0;
|
||||
volatile uint8_t tbRtCnt = 0;
|
||||
|
||||
/* matrix state(1:on, 0:off) */
|
||||
static matrix_row_t matrix[MATRIX_ROWS];
|
||||
@@ -111,29 +110,30 @@ static matrix_row_t read_cols(uint8_t row);
|
||||
static void init_cols(void);
|
||||
static void unselect_rows(void);
|
||||
static void select_row(uint8_t row);
|
||||
static void enableInterrupts(void);
|
||||
static void enableInterrupts(void);
|
||||
|
||||
static uint8_t mcp23018_reset_loop;
|
||||
// static uint16_t mcp23018_reset_loop;
|
||||
|
||||
__attribute__ ((weak)) void matrix_init_user(void) {}
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak)) void matrix_scan_user(void) {}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
|
||||
|
||||
inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
void matrix_init(void) {
|
||||
// initialize row and col
|
||||
@@ -141,13 +141,12 @@ void matrix_init(void) {
|
||||
unselect_rows();
|
||||
init_cols();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
matrix[i] = 0;
|
||||
raw_matrix[i] = 0;
|
||||
}
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
matrix[i] = 0;
|
||||
raw_matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
@@ -158,7 +157,7 @@ void matrix_power_up(void) {
|
||||
init_cols();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
matrix[i] = 0;
|
||||
}
|
||||
}
|
||||
@@ -166,63 +165,66 @@ void matrix_power_up(void) {
|
||||
// Reads and stores a row, returning
|
||||
// whether a change occurred.
|
||||
static inline bool store_raw_matrix_row(uint8_t index) {
|
||||
matrix_row_t temp = read_cols(index);
|
||||
if (raw_matrix[index] != temp) {
|
||||
raw_matrix[index] = temp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
matrix_row_t temp = read_cols(index);
|
||||
if (raw_matrix[index] != temp) {
|
||||
raw_matrix[index] = temp;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t matrix_scan(void) {
|
||||
// TODO: Find what is trashing interrupts
|
||||
enableInterrupts();
|
||||
|
||||
// First we handle the mouse inputs
|
||||
#ifdef BALLER
|
||||
uint8_t pBtn = PINE & TRKBTN;
|
||||
uint8_t pBtn = PINE & TRKBTN;
|
||||
|
||||
#ifdef DEBUG_BALLER
|
||||
# ifdef DEBUG_BALLER
|
||||
// Compare to previous, mod report
|
||||
if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
|
||||
xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
|
||||
#endif
|
||||
if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0) xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
|
||||
# endif
|
||||
|
||||
// Modify the report
|
||||
report_mouse_t pRprt = pointing_device_get_report();
|
||||
|
||||
// Scroll by default, move on layer
|
||||
if (layer_state == 0) {
|
||||
pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
|
||||
pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
|
||||
pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
|
||||
pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
|
||||
pRprt.h += tbLtCnt * SCROLLSTEP;
|
||||
tbLtCnt = 0;
|
||||
pRprt.h -= tbRtCnt * SCROLLSTEP;
|
||||
tbRtCnt = 0;
|
||||
pRprt.v -= tbUpCnt * SCROLLSTEP;
|
||||
tbUpCnt = 0;
|
||||
pRprt.v += tbDnCnt * SCROLLSTEP;
|
||||
tbDnCnt = 0;
|
||||
} else {
|
||||
pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
|
||||
pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
|
||||
pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
|
||||
pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
|
||||
pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1);
|
||||
tbLtCnt = 0;
|
||||
pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1);
|
||||
tbRtCnt = 0;
|
||||
pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1);
|
||||
tbUpCnt = 0;
|
||||
pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1);
|
||||
tbDnCnt = 0;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_BALLER
|
||||
if (pRprt.x != 0 || pRprt.y != 0)
|
||||
xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
|
||||
#endif
|
||||
# ifdef DEBUG_BALLER
|
||||
if (pRprt.x != 0 || pRprt.y != 0) xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
|
||||
# endif
|
||||
|
||||
if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
|
||||
if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
|
||||
if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
|
||||
if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
|
||||
|
||||
// Save state, push update
|
||||
if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
|
||||
pointing_device_set_report(pRprt);
|
||||
if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn)) pointing_device_set_report(pRprt);
|
||||
|
||||
trkBtnState = pBtn;
|
||||
#endif
|
||||
|
||||
// Then the keyboard
|
||||
if (mcp23018_status) { // if there was an error
|
||||
if (mcp23018_status) { // if there was an error
|
||||
if (++mcp23018_reset_loop == 0) {
|
||||
// if (++mcp23018_reset_loop >= 1300) {
|
||||
// since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
|
||||
@@ -240,7 +242,7 @@ uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
|
||||
// select rows from left and right hands
|
||||
uint8_t left_index = i;
|
||||
uint8_t left_index = i;
|
||||
uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
|
||||
select_row(left_index);
|
||||
select_row(right_index);
|
||||
@@ -254,41 +256,46 @@ uint8_t matrix_scan(void) {
|
||||
unselect_rows();
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
matrix_scan_kb();
|
||||
|
||||
enableInterrupts();
|
||||
|
||||
#ifdef DEBUG_MATRIX
|
||||
for (uint8_t c = 0; c < MATRIX_COLS; c++)
|
||||
for (uint8_t r = 0; r < MATRIX_ROWS; r++)
|
||||
if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
|
||||
for (uint8_t r = 0; r < MATRIX_ROWS; r++)
|
||||
if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
return matrix[row];
|
||||
}
|
||||
|
||||
void matrix_print(void) {
|
||||
print("\nr/c 0123456789ABCDEF\n");
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_bin_reverse16(matrix_get_row(row));
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Remember this means ROWS
|
||||
static void init_cols(void) {
|
||||
static void init_cols(void) {
|
||||
// init on mcp23018
|
||||
// not needed, already done as part of init_mcp23018()
|
||||
|
||||
// Input with pull-up(DDR:0, PORT:1)
|
||||
DDRF &= ~FMASK;
|
||||
PORTF |= FMASK;
|
||||
DDRF &= ~FMASK;
|
||||
PORTF |= FMASK;
|
||||
}
|
||||
|
||||
static matrix_row_t read_cols(uint8_t row) {
|
||||
@@ -296,7 +303,7 @@ static matrix_row_t read_cols(uint8_t row) {
|
||||
if (mcp23018_status) { // if there was an error
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t data = 0;
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_read_register(I2C_ADDR, GPIOB, &data, 1, I2C_TIMEOUT);
|
||||
|
||||
#ifdef DEBUG_MATRIX
|
||||
@@ -305,113 +312,106 @@ static matrix_row_t read_cols(uint8_t row) {
|
||||
return ~data;
|
||||
}
|
||||
} else {
|
||||
/* read from teensy
|
||||
* bitmask is 0b0111001, but we want the lower four
|
||||
* we'll return 1s for the top two, but that's harmless.
|
||||
*/
|
||||
/* read from teensy
|
||||
* bitmask is 0b0111001, but we want the lower four
|
||||
* we'll return 1s for the top two, but that's harmless.
|
||||
*/
|
||||
// So I need to confuckulate all this
|
||||
//return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
|
||||
//return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
|
||||
return ~(
|
||||
(((PINF & ROW4) >> 1)
|
||||
| ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
|
||||
& 0xF);
|
||||
// return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
|
||||
// return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
|
||||
return ~((((PINF & ROW4) >> 1) | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3)) & 0xF);
|
||||
}
|
||||
}
|
||||
|
||||
// Row pin configuration
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
static void unselect_rows(void) {
|
||||
// no need to unselect on mcp23018, because the select step sets all
|
||||
// the other row bits high, and it's not changing to a different
|
||||
// direction
|
||||
// Hi-Z(DDR:0, PORT:0) to unselect
|
||||
DDRB &= ~(BMASK | TRKMASK);
|
||||
DDRB &= ~(BMASK | TRKMASK);
|
||||
PORTB &= ~(BMASK);
|
||||
DDRC &= ~CMASK;
|
||||
DDRC &= ~CMASK;
|
||||
PORTC &= ~CMASK;
|
||||
DDRD &= ~DMASK;
|
||||
DDRD &= ~DMASK;
|
||||
PORTD &= ~DMASK;
|
||||
|
||||
// Fix trashing of DDRB for TB
|
||||
// Fix trashing of DDRB for TB
|
||||
PORTB |= TRKMASK;
|
||||
}
|
||||
|
||||
static void select_row(uint8_t row)
|
||||
{
|
||||
static void select_row(uint8_t row) {
|
||||
if (row < 7) {
|
||||
// select on mcp23018
|
||||
if (mcp23018_status) { // do nothing on error
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
uint8_t data = 0xFF & ~(1<<row);
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
uint8_t data = 0xFF & ~(1 << row);
|
||||
mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
|
||||
}
|
||||
} else {
|
||||
// Output low(DDR:1, PORT:0) to select
|
||||
switch (row) {
|
||||
case 7:
|
||||
DDRB |= COL7;
|
||||
DDRB |= COL7;
|
||||
PORTB &= ~COL7;
|
||||
break;
|
||||
case 8:
|
||||
DDRB |= COL8;
|
||||
DDRB |= COL8;
|
||||
PORTB &= ~COL8;
|
||||
break;
|
||||
case 9:
|
||||
DDRB |= COL9;
|
||||
DDRB |= COL9;
|
||||
PORTB &= ~COL9;
|
||||
break;
|
||||
case 10:
|
||||
DDRB |= COL10;
|
||||
DDRB |= COL10;
|
||||
PORTB &= ~COL10;
|
||||
break;
|
||||
case 11:
|
||||
DDRD |= COL11;
|
||||
DDRD |= COL11;
|
||||
PORTD &= ~COL11;
|
||||
break;
|
||||
case 12:
|
||||
DDRD |= COL12;
|
||||
DDRD |= COL12;
|
||||
PORTD &= ~COL12;
|
||||
break;
|
||||
case 13:
|
||||
DDRC |= COL13;
|
||||
DDRC |= COL13;
|
||||
PORTC &= ~COL13;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Trackball Interrupts
|
||||
static void enableInterrupts(void) {
|
||||
#ifdef BALLER
|
||||
// Set interrupt mask
|
||||
// Set port defs
|
||||
DDRB &= ~TRKMASK;
|
||||
PORTB |= TRKMASK;
|
||||
DDRE &= ~TRKBTN;
|
||||
PORTE |= TRKBTN;
|
||||
#ifdef BALLER
|
||||
// Set interrupt mask
|
||||
// Set port defs
|
||||
DDRB &= ~TRKMASK;
|
||||
PORTB |= TRKMASK;
|
||||
DDRE &= ~TRKBTN;
|
||||
PORTE |= TRKBTN;
|
||||
|
||||
// Interrupt shenanigans
|
||||
//EIMSK |= (1 << PCIE0);
|
||||
PCMSK0 |= TRKMASK;
|
||||
PCICR |= (1 << PCIE0);
|
||||
sei();
|
||||
#endif
|
||||
// Interrupt shenanigans
|
||||
// EIMSK |= (1 << PCIE0);
|
||||
PCMSK0 |= TRKMASK;
|
||||
PCICR |= (1 << PCIE0);
|
||||
sei();
|
||||
#endif
|
||||
|
||||
return;
|
||||
}
|
||||
#ifdef BALLER
|
||||
ISR (PCINT0_vect) {
|
||||
// Don't get fancy, we're in a interrupt here
|
||||
// PCINT reports a interrupt for a change on the bus
|
||||
// We hand the button at scantime for debounce
|
||||
volatile uint8_t pState = PINB & TRKMASK;
|
||||
if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
|
||||
if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
|
||||
if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
|
||||
if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
|
||||
trkState = pState;
|
||||
|
||||
ISR(PCINT0_vect) {
|
||||
// Don't get fancy, we're in a interrupt here
|
||||
// PCINT reports a interrupt for a change on the bus
|
||||
// We hand the button at scantime for debounce
|
||||
volatile uint8_t pState = PINB & TRKMASK;
|
||||
if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
|
||||
if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
|
||||
if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
|
||||
if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
|
||||
trkState = pState;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Jane Bernhardt (https://github.com/germ)
|
||||
// SPDX-License-Identifier: GPL-2.0+
|
||||
|
||||
#pragma once
|
||||
|
||||
#define COMBO_ALLOW_ACTION_KEYS
|
||||
|
||||
@@ -68,7 +68,9 @@ static uint8_t mcp23018_reset_loop;
|
||||
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
void matrix_init(void) {
|
||||
// initialize row and col
|
||||
@@ -82,7 +84,6 @@ void matrix_init(void) {
|
||||
raw_matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
matrix_init_kb();
|
||||
}
|
||||
void matrix_power_up(void) {
|
||||
@@ -108,7 +109,7 @@ static inline bool store_raw_matrix_row(uint8_t index) {
|
||||
return false;
|
||||
}
|
||||
uint8_t matrix_scan(void) {
|
||||
if (mcp23018_status) { // if there was an error
|
||||
if (mcp23018_status) { // if there was an error
|
||||
if (++mcp23018_reset_loop == 0) {
|
||||
// if (++mcp23018_reset_loop >= 1300) {
|
||||
// since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
|
||||
@@ -140,7 +141,7 @@ uint8_t matrix_scan(void) {
|
||||
unselect_rows();
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
matrix_scan_kb();
|
||||
|
||||
#ifdef DEBUG_MATRIX
|
||||
@@ -152,8 +153,12 @@ uint8_t matrix_scan(void) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
return matrix[row];
|
||||
}
|
||||
|
||||
void matrix_print(void) {
|
||||
print("\nr/c 0123456789ABCDEF\n");
|
||||
@@ -168,16 +173,16 @@ void matrix_print(void) {
|
||||
// Remember this means ROWS
|
||||
static void init_cols(void) {
|
||||
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
}
|
||||
|
||||
static matrix_row_t read_cols(uint8_t row) {
|
||||
if (row < 5) {
|
||||
if (mcp23018_status) { // if there was an error
|
||||
if (mcp23018_status) { // if there was an error
|
||||
return 0;
|
||||
} else {
|
||||
uint8_t data = 0;
|
||||
uint8_t data = 0;
|
||||
mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, I2C_TIMEOUT);
|
||||
#ifdef DEBUG_MATRIX
|
||||
if (~data != 0x00) xprintf("I2C: %d\n", ~data);
|
||||
@@ -195,20 +200,19 @@ static void unselect_rows(void) {
|
||||
// the other row bits high, and it's not changing to a different direction
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS_PER_SIDE; row++) {
|
||||
gpio_set_pin_input(row_pins[row]);
|
||||
gpio_write_pin_low(row_pins[row]);
|
||||
gpio_set_pin_input(row_pins[row]);
|
||||
gpio_write_pin_low(row_pins[row]);
|
||||
}
|
||||
}
|
||||
|
||||
static void select_row(uint8_t row) {
|
||||
if (row < 5) {
|
||||
// select on mcp23018
|
||||
if (mcp23018_status) { // do nothing on error
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
if (mcp23018_status) { // do nothing on error
|
||||
} else { // set active row low : 0 // set other rows hi-Z : 1
|
||||
uint8_t data;
|
||||
data = 0xFF & ~(1 << (row + 1));
|
||||
data = 0xFF & ~(1 << (row + 1));
|
||||
mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
|
||||
|
||||
}
|
||||
} else {
|
||||
gpio_set_pin_output(row_pins[row - MATRIX_ROWS_PER_SIDE]);
|
||||
|
||||
@@ -29,16 +29,20 @@ static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
||||
static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
||||
|
||||
/* matrix state(1:on, 0:off) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
// row offsets for each hand
|
||||
uint8_t thisHand, thatHand;
|
||||
|
||||
// user-defined overridable functions
|
||||
__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
@@ -46,7 +50,9 @@ __attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
__attribute__((weak)) void matrix_slave_scan_user(void) {}
|
||||
|
||||
matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
|
||||
matrix_row_t matrix_get_row(uint8_t row) {
|
||||
return matrix[row];
|
||||
}
|
||||
|
||||
void matrix_print(void) {}
|
||||
|
||||
@@ -58,13 +64,19 @@ static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
|
||||
}
|
||||
|
||||
static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
|
||||
ATOMIC_BLOCK_FORCEON { gpio_set_pin_input_high(pin); }
|
||||
ATOMIC_BLOCK_FORCEON {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
}
|
||||
|
||||
// matrix code
|
||||
static void select_row(uint8_t row) { gpio_atomic_set_pin_output_low(row_pins[row]); }
|
||||
static void select_row(uint8_t row) {
|
||||
gpio_atomic_set_pin_output_low(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t row) { gpio_atomic_set_pin_input_high(row_pins[row]); }
|
||||
static void unselect_row(uint8_t row) {
|
||||
gpio_atomic_set_pin_input_high(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_rows(void) {
|
||||
for (uint8_t x = 0; x < ROWS_PER_HAND; x++) {
|
||||
@@ -92,7 +104,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
// Unselect row
|
||||
unselect_row(current_row);
|
||||
if (current_row + 1 < MATRIX_ROWS) {
|
||||
wait_us(30); // wait for row signal to go HIGH
|
||||
wait_us(30); // wait for row signal to go HIGH
|
||||
}
|
||||
|
||||
// If the row has changed, store the row and return the changed flag.
|
||||
@@ -103,9 +115,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
return false;
|
||||
}
|
||||
|
||||
static void select_col(uint8_t col) { gpio_atomic_set_pin_output_low(col_pins[col]); }
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_atomic_set_pin_output_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col) { gpio_atomic_set_pin_input_high(col_pins[col]); }
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_atomic_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
@@ -156,7 +172,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
// Unselect col
|
||||
unselect_col(current_col);
|
||||
if (current_col + 1 < MATRIX_COLS) {
|
||||
wait_us(30); // wait for col signal to go HIGH
|
||||
wait_us(30); // wait for col signal to go HIGH
|
||||
}
|
||||
|
||||
return matrix_changed;
|
||||
@@ -201,8 +217,6 @@ void matrix_init(void) {
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(ROWS_PER_HAND);
|
||||
|
||||
matrix_init_kb();
|
||||
|
||||
split_post_init();
|
||||
@@ -248,26 +262,26 @@ bool matrix_post_scan(void) {
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void) {
|
||||
bool local_changed = false;
|
||||
static matrix_row_t temp_raw_matrix[MATRIX_ROWS]; // temp raw values
|
||||
bool local_changed = false;
|
||||
static matrix_row_t temp_raw_matrix[MATRIX_ROWS]; // temp raw values
|
||||
|
||||
// Set row, read cols
|
||||
for (uint8_t current_row = 0; current_row < ROWS_PER_HAND/2; current_row++) {
|
||||
for (uint8_t current_row = 0; current_row < ROWS_PER_HAND / 2; current_row++) {
|
||||
local_changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
local_changed |= read_rows_on_col(temp_raw_matrix, current_col);
|
||||
//Updated key matrix on lines 6-10 (or lines 16-20)
|
||||
if(local_changed) {
|
||||
for (uint8_t i = ROWS_PER_HAND/2; i < ROWS_PER_HAND; i++) {
|
||||
// Updated key matrix on lines 6-10 (or lines 16-20)
|
||||
if (local_changed) {
|
||||
for (uint8_t i = ROWS_PER_HAND / 2; i < ROWS_PER_HAND; i++) {
|
||||
raw_matrix[i] = temp_raw_matrix[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed);
|
||||
debounce(raw_matrix, matrix + thisHand, local_changed);
|
||||
|
||||
bool remote_changed = matrix_post_scan();
|
||||
return (uint8_t)(local_changed || remote_changed);
|
||||
|
||||
@@ -29,82 +29,61 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "timer.h"
|
||||
|
||||
#if (MATRIX_COLS <= 8)
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint8_t)1)
|
||||
#elif (MATRIX_COLS <= 16)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint16_t)1)
|
||||
#elif (MATRIX_COLS <= 32)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
#endif
|
||||
|
||||
static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
||||
static const uint8_t col_select_pins[3] = MATRIX_COL_SELECT_PINS;
|
||||
static const uint8_t dat_pin = MATRIX_COL_DATA_PIN;
|
||||
static const uint8_t col_select_pins[3] = MATRIX_COL_SELECT_PINS;
|
||||
static const uint8_t dat_pin = MATRIX_COL_DATA_PIN;
|
||||
|
||||
/* matrix state(1:on, 0:off) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // raw values
|
||||
|
||||
/* 2d array containing binary representation of its index */
|
||||
static const uint8_t num_in_binary[8][3] = {
|
||||
{0, 0, 0},
|
||||
{0, 0, 1},
|
||||
{0, 1, 0},
|
||||
{0, 1, 1},
|
||||
{1, 0, 0},
|
||||
{1, 0, 1},
|
||||
{1, 1, 0},
|
||||
{1, 1, 1},
|
||||
{0, 0, 0}, {0, 0, 1}, {0, 1, 0}, {0, 1, 1}, {1, 0, 0}, {1, 0, 1}, {1, 1, 0}, {1, 1, 1},
|
||||
};
|
||||
|
||||
static void select_col_analog(uint8_t col);
|
||||
static void mux_pin_control(const uint8_t binary[]);
|
||||
void debounce_init(uint8_t num_rows);
|
||||
void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||
void debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed);
|
||||
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_user(void) {}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_rows(void)
|
||||
{
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_cols(void)
|
||||
{
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline
|
||||
bool matrix_is_on(uint8_t row, uint8_t col)
|
||||
{
|
||||
return (matrix[row] & ((matrix_row_t)1<<col));
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
// Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
|
||||
// switch blocker installed and the switch is always pressed.
|
||||
#ifdef MATRIX_MASKED
|
||||
@@ -114,48 +93,44 @@ matrix_row_t matrix_get_row(uint8_t row)
|
||||
#endif
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_matrix_row(row);
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// uses standard row code
|
||||
static void select_row(uint8_t row)
|
||||
{
|
||||
static void select_row(uint8_t row) {
|
||||
gpio_set_pin_output(row_pins[row]);
|
||||
gpio_write_pin_low(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t row)
|
||||
{
|
||||
static void unselect_row(uint8_t row) {
|
||||
gpio_set_pin_input_high(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
static void unselect_rows(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_pins(void) { // still need some fixing, this might not work
|
||||
unselect_rows(); // with the loop
|
||||
/*
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
*/
|
||||
gpio_set_pin_input_high(dat_pin);
|
||||
static void init_pins(void) { // still need some fixing, this might not work
|
||||
unselect_rows(); // with the loop
|
||||
/*
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
*/
|
||||
gpio_set_pin_input_high(dat_pin);
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
{
|
||||
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];
|
||||
|
||||
@@ -167,15 +142,14 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
wait_us(30);
|
||||
|
||||
// For each col...
|
||||
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
// Select the col pin to read (active low)
|
||||
select_col_analog(col_index);
|
||||
wait_us(30);
|
||||
uint8_t pin_state = gpio_read_pin(dat_pin);
|
||||
|
||||
// Populate the matrix row with the state of the col pin
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
|
||||
// Unselect row
|
||||
@@ -184,20 +158,16 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
}
|
||||
|
||||
|
||||
void matrix_init(void) {
|
||||
|
||||
// initialize key pins
|
||||
init_pins();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
raw_matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
|
||||
gpio_set_pin_input(D5);
|
||||
@@ -205,15 +175,14 @@ void matrix_init(void) {
|
||||
}
|
||||
|
||||
// modified for per col read matrix scan
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
|
||||
changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
@@ -231,7 +200,7 @@ uint8_t matrix_scan(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
@@ -239,8 +208,7 @@ uint8_t matrix_scan(void)
|
||||
*/
|
||||
|
||||
static void select_col_analog(uint8_t col) {
|
||||
switch(col) {
|
||||
|
||||
switch (col) {
|
||||
case 0:
|
||||
mux_pin_control(num_in_binary[0]);
|
||||
break;
|
||||
@@ -273,26 +241,23 @@ static void select_col_analog(uint8_t col) {
|
||||
static void mux_pin_control(const uint8_t binary[]) {
|
||||
// set pin0
|
||||
gpio_set_pin_output(col_select_pins[0]);
|
||||
if(binary[2] == 0) {
|
||||
if (binary[2] == 0) {
|
||||
gpio_write_pin_low(col_select_pins[0]);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
gpio_write_pin_high(col_select_pins[0]);
|
||||
}
|
||||
// set pin1
|
||||
gpio_set_pin_output(col_select_pins[1]);
|
||||
if(binary[1] == 0) {
|
||||
if (binary[1] == 0) {
|
||||
gpio_write_pin_low(col_select_pins[1]);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
gpio_write_pin_high(col_select_pins[1]);
|
||||
}
|
||||
// set pin2
|
||||
gpio_set_pin_output(col_select_pins[2]);
|
||||
if(binary[0] == 0) {
|
||||
if (binary[0] == 0) {
|
||||
gpio_write_pin_low(col_select_pins[2]);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
gpio_write_pin_high(col_select_pins[2]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
|
||||
static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
|
||||
static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
||||
# ifdef MATRIX_MUL_SELECT
|
||||
static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL;
|
||||
static const pin_t col_sel[MATRIX_COLS] = MATRIX_MUL_SEL;
|
||||
# endif
|
||||
#endif
|
||||
|
||||
@@ -57,8 +57,8 @@ static const uint8_t delay_sel[] = {MATRIX_IO_DELAY_MULSEL};
|
||||
#endif
|
||||
|
||||
/* matrix state(1:on, 0:off) */
|
||||
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
|
||||
ATOMIC_BLOCK_FORCEON {
|
||||
@@ -68,7 +68,9 @@ static inline void gpio_atomic_set_pin_output_low(pin_t pin) {
|
||||
}
|
||||
|
||||
static inline void gpio_atomic_set_pin_input_high(pin_t pin) {
|
||||
ATOMIC_BLOCK_FORCEON { gpio_set_pin_input_high(pin); }
|
||||
ATOMIC_BLOCK_FORCEON {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
}
|
||||
|
||||
// matrix code
|
||||
@@ -108,9 +110,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
#elif defined(DIODE_DIRECTION)
|
||||
# if (DIODE_DIRECTION == COL2ROW)
|
||||
|
||||
static void select_row(uint8_t row) { gpio_atomic_set_pin_output_low(row_pins[row]); }
|
||||
static void select_row(uint8_t row) {
|
||||
gpio_atomic_set_pin_output_low(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t row) { gpio_atomic_set_pin_input_high(row_pins[row]); }
|
||||
static void unselect_row(uint8_t row) {
|
||||
gpio_atomic_set_pin_input_high(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_rows(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
@@ -153,7 +159,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
// Unselect row
|
||||
unselect_row(current_row);
|
||||
# ifdef MATRIX_IO_DELAY_PORTS
|
||||
if (current_row_value) { // wait for col signal to go HIGH
|
||||
if (current_row_value) { // wait for col signal to go HIGH
|
||||
bool is_pressed;
|
||||
do {
|
||||
MATRIX_DEBUG_DELAY_START();
|
||||
@@ -170,7 +176,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
}
|
||||
# endif
|
||||
# ifdef MATRIX_IO_DELAY_ADAPTIVE
|
||||
if (current_row_value) { // wait for col signal to go HIGH
|
||||
if (current_row_value) { // wait for col signal to go HIGH
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
MATRIX_DEBUG_DELAY_START();
|
||||
# ifdef MATRIX_MUL_SELECT
|
||||
@@ -184,7 +190,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
}
|
||||
# endif
|
||||
# ifdef MATRIX_IO_DELAY_ADAPTIVE2
|
||||
if (current_row_value) { // wait for col signal to go HIGH
|
||||
if (current_row_value) { // wait for col signal to go HIGH
|
||||
pin_t state;
|
||||
do {
|
||||
MATRIX_DEBUG_DELAY_START();
|
||||
@@ -204,7 +210,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
# endif
|
||||
if (MATRIX_IO_DELAY_ALWAYS || current_row + 1 < MATRIX_ROWS) {
|
||||
MATRIX_DEBUG_DELAY_START();
|
||||
matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for col signal to go HIGH
|
||||
matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for col signal to go HIGH
|
||||
MATRIX_DEBUG_DELAY_END();
|
||||
}
|
||||
|
||||
@@ -218,9 +224,13 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
|
||||
# elif (DIODE_DIRECTION == ROW2COL)
|
||||
|
||||
static void select_col(uint8_t col) { gpio_atomic_set_pin_output_low(col_pins[col]); }
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_atomic_set_pin_output_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col) { gpio_atomic_set_pin_input_high(col_pins[col]); }
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_atomic_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
@@ -237,7 +247,7 @@ static void init_pins(void) {
|
||||
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
|
||||
bool matrix_changed = false;
|
||||
bool key_pressed = false;
|
||||
bool key_pressed = false;
|
||||
|
||||
// Select col
|
||||
select_col(current_col);
|
||||
@@ -269,7 +279,7 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
// Unselect col
|
||||
unselect_col(current_col);
|
||||
if (MATRIX_IO_DELAY_ALWAYS || current_col + 1 < MATRIX_COLS) {
|
||||
matrix_output_unselect_delay(current_col, key_pressed); // wait for col signal to go HIGH
|
||||
matrix_output_unselect_delay(current_col, key_pressed); // wait for col signal to go HIGH
|
||||
}
|
||||
|
||||
return matrix_changed;
|
||||
@@ -292,8 +302,6 @@ void matrix_init(void) {
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
@@ -317,7 +325,7 @@ uint8_t matrix_scan(void) {
|
||||
MATRIX_DEBUG_GAP();
|
||||
|
||||
MATRIX_DEBUG_SCAN_START();
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
MATRIX_DEBUG_SCAN_END();
|
||||
MATRIX_DEBUG_GAP();
|
||||
|
||||
|
||||
@@ -166,8 +166,6 @@ void matrix_init(void) {
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
@@ -221,7 +219,7 @@ uint8_t matrix_scan(void) {
|
||||
MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP(); MATRIX_DEBUG_SCAN_START();
|
||||
|
||||
// debounce raw_matrix[] to matrix[]
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
MATRIX_DEBUG_SCAN_END(); MATRIX_DEBUG_GAP();
|
||||
|
||||
MATRIX_DEBUG_SCAN_START();
|
||||
|
||||
@@ -22,67 +22,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "debounce.h"
|
||||
|
||||
#if (MATRIX_COLS <= 8)
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint8_t)1)
|
||||
#elif (MATRIX_COLS <= 16)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint16_t)1)
|
||||
#elif (MATRIX_COLS <= 32)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
#endif
|
||||
|
||||
#ifdef MATRIX_MASKED
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
#endif
|
||||
|
||||
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) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
inline
|
||||
uint8_t matrix_rows(void) {
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_cols(void) {
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline
|
||||
bool matrix_is_on(uint8_t row, uint8_t col)
|
||||
{
|
||||
return (matrix[row] & ((matrix_row_t)1<<col));
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
// Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
|
||||
// switch blocker installed and the switch is always pressed.
|
||||
#ifdef MATRIX_MASKED
|
||||
@@ -92,66 +80,59 @@ matrix_row_t matrix_get_row(uint8_t row)
|
||||
#endif
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_matrix_row(row);
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void select_row(uint8_t row)
|
||||
{
|
||||
static void select_row(uint8_t row) {
|
||||
gpio_set_pin_output(row_pins[row]);
|
||||
gpio_write_pin_low(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t row)
|
||||
{
|
||||
static void unselect_row(uint8_t row) {
|
||||
gpio_set_pin_input_high(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
static void unselect_rows(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void select_col(uint8_t col)
|
||||
{
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_set_pin_output(col_pins[col]);
|
||||
gpio_write_pin_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col)
|
||||
{
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_rows();
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
unselect_rows();
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
{
|
||||
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];
|
||||
|
||||
@@ -163,13 +144,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
wait_us(30);
|
||||
|
||||
// For each col...
|
||||
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
// Select the col pin to read (active low)
|
||||
uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
|
||||
|
||||
// Populate the matrix row with the state of the col pin
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
|
||||
// Unselect row
|
||||
@@ -178,8 +158,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
}
|
||||
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
{
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
|
||||
bool matrix_changed = false;
|
||||
|
||||
// Select col and wait for col selecton to stabilize
|
||||
@@ -187,27 +166,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
wait_us(30);
|
||||
|
||||
// For each row...
|
||||
for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
|
||||
{
|
||||
uint8_t tmp = row_index + MATRIX_ROWS/2;
|
||||
for (uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
|
||||
uint8_t tmp = row_index + MATRIX_ROWS / 2;
|
||||
// Store last value of row prior to reading
|
||||
matrix_row_t last_row_value = current_matrix[tmp];
|
||||
|
||||
// Check row pin state
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0)
|
||||
{
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0) {
|
||||
// Pin LO, set col bit
|
||||
current_matrix[tmp] |= (ROW_SHIFTER << current_col);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Pin HI, clear col bit
|
||||
current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
|
||||
}
|
||||
|
||||
// Determine if the matrix changed state
|
||||
if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
|
||||
{
|
||||
if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
|
||||
matrix_changed = true;
|
||||
}
|
||||
}
|
||||
@@ -219,37 +193,33 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
}
|
||||
|
||||
void matrix_init(void) {
|
||||
|
||||
// initialize key pins
|
||||
init_pins();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
raw_matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
bool changed = false;
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
|
||||
// Set row, read cols
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
|
||||
changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
//else
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
// Set row, read cols
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
|
||||
changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
// else
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
}
|
||||
|
||||
@@ -22,67 +22,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "debounce.h"
|
||||
|
||||
#if (MATRIX_COLS <= 8)
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint8_t)1)
|
||||
#elif (MATRIX_COLS <= 16)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint16_t)1)
|
||||
#elif (MATRIX_COLS <= 32)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
#endif
|
||||
|
||||
#ifdef MATRIX_MASKED
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
#endif
|
||||
|
||||
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) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
inline
|
||||
uint8_t matrix_rows(void) {
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_cols(void) {
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline
|
||||
bool matrix_is_on(uint8_t row, uint8_t col)
|
||||
{
|
||||
return (matrix[row] & ((matrix_row_t)1<<col));
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
// Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
|
||||
// switch blocker installed and the switch is always pressed.
|
||||
#ifdef MATRIX_MASKED
|
||||
@@ -92,66 +80,59 @@ matrix_row_t matrix_get_row(uint8_t row)
|
||||
#endif
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_matrix_row(row);
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void select_row(uint8_t row)
|
||||
{
|
||||
static void select_row(uint8_t row) {
|
||||
gpio_set_pin_output(row_pins[row]);
|
||||
gpio_write_pin_low(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t row)
|
||||
{
|
||||
static void unselect_row(uint8_t row) {
|
||||
gpio_set_pin_input_high(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
static void unselect_rows(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void select_col(uint8_t col)
|
||||
{
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_set_pin_output(col_pins[col]);
|
||||
gpio_write_pin_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col)
|
||||
{
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_rows();
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
unselect_rows();
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
{
|
||||
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];
|
||||
|
||||
@@ -163,13 +144,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
wait_us(30);
|
||||
|
||||
// For each col...
|
||||
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
// Select the col pin to read (active low)
|
||||
uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
|
||||
|
||||
// Populate the matrix row with the state of the col pin
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
|
||||
// Unselect row
|
||||
@@ -178,8 +158,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
}
|
||||
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
{
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
|
||||
bool matrix_changed = false;
|
||||
|
||||
// Select col and wait for col selecton to stabilize
|
||||
@@ -187,27 +166,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
wait_us(30);
|
||||
|
||||
// For each row...
|
||||
for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
|
||||
{
|
||||
uint8_t tmp = row_index + MATRIX_ROWS/2;
|
||||
for (uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
|
||||
uint8_t tmp = row_index + MATRIX_ROWS / 2;
|
||||
// Store last value of row prior to reading
|
||||
matrix_row_t last_row_value = current_matrix[tmp];
|
||||
|
||||
// Check row pin state
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0)
|
||||
{
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0) {
|
||||
// Pin LO, set col bit
|
||||
current_matrix[tmp] |= (ROW_SHIFTER << current_col);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Pin HI, clear col bit
|
||||
current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
|
||||
}
|
||||
|
||||
// Determine if the matrix changed state
|
||||
if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
|
||||
{
|
||||
if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
|
||||
matrix_changed = true;
|
||||
}
|
||||
}
|
||||
@@ -219,37 +193,33 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
}
|
||||
|
||||
void matrix_init(void) {
|
||||
|
||||
// initialize key pins
|
||||
init_pins();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
raw_matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
bool changed = false;
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
|
||||
// Set row, read cols
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
|
||||
changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
//else
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
// Set row, read cols
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
|
||||
changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
// else
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
}
|
||||
|
||||
@@ -22,67 +22,55 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "debounce.h"
|
||||
|
||||
#if (MATRIX_COLS <= 8)
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint8_t)1)
|
||||
#elif (MATRIX_COLS <= 16)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint16_t)1)
|
||||
#elif (MATRIX_COLS <= 32)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
#endif
|
||||
|
||||
#ifdef MATRIX_MASKED
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
#endif
|
||||
|
||||
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) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
inline
|
||||
uint8_t matrix_rows(void) {
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_cols(void) {
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline
|
||||
bool matrix_is_on(uint8_t row, uint8_t col)
|
||||
{
|
||||
return (matrix[row] & ((matrix_row_t)1<<col));
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
// Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
|
||||
// switch blocker installed and the switch is always pressed.
|
||||
#ifdef MATRIX_MASKED
|
||||
@@ -92,66 +80,59 @@ matrix_row_t matrix_get_row(uint8_t row)
|
||||
#endif
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_matrix_row(row);
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void select_row(uint8_t row)
|
||||
{
|
||||
static void select_row(uint8_t row) {
|
||||
gpio_set_pin_output(row_pins[row]);
|
||||
gpio_write_pin_low(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t row)
|
||||
{
|
||||
static void unselect_row(uint8_t row) {
|
||||
gpio_set_pin_input_high(row_pins[row]);
|
||||
}
|
||||
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
static void unselect_rows(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void select_col(uint8_t col)
|
||||
{
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_set_pin_output(col_pins[col]);
|
||||
gpio_write_pin_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col)
|
||||
{
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_rows();
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
unselect_rows();
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
{
|
||||
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];
|
||||
|
||||
@@ -163,13 +144,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
wait_us(30);
|
||||
|
||||
// For each col...
|
||||
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
// Select the col pin to read (active low)
|
||||
uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
|
||||
|
||||
// Populate the matrix row with the state of the col pin
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
|
||||
// Unselect row
|
||||
@@ -178,8 +158,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
}
|
||||
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
{
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
|
||||
bool matrix_changed = false;
|
||||
|
||||
// Select col and wait for col selecton to stabilize
|
||||
@@ -187,27 +166,22 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
wait_us(30);
|
||||
|
||||
// For each row...
|
||||
for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
|
||||
{
|
||||
uint8_t tmp = row_index + MATRIX_ROWS/2;
|
||||
for (uint8_t row_index = 0; row_index < MATRIX_ROWS / 2; row_index++) {
|
||||
uint8_t tmp = row_index + MATRIX_ROWS / 2;
|
||||
// Store last value of row prior to reading
|
||||
matrix_row_t last_row_value = current_matrix[tmp];
|
||||
|
||||
// Check row pin state
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0)
|
||||
{
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0) {
|
||||
// Pin LO, set col bit
|
||||
current_matrix[tmp] |= (ROW_SHIFTER << current_col);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Pin HI, clear col bit
|
||||
current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
|
||||
}
|
||||
|
||||
// Determine if the matrix changed state
|
||||
if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
|
||||
{
|
||||
if ((last_row_value != current_matrix[tmp]) && !(matrix_changed)) {
|
||||
matrix_changed = true;
|
||||
}
|
||||
}
|
||||
@@ -219,37 +193,33 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
}
|
||||
|
||||
void matrix_init(void) {
|
||||
|
||||
// initialize key pins
|
||||
init_pins();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
raw_matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
bool changed = false;
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
|
||||
// Set row, read cols
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
|
||||
changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
//else
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
// Set row, read cols
|
||||
for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
|
||||
changed |= read_cols_on_row(raw_matrix, current_row);
|
||||
}
|
||||
// else
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
matrix_scan_kb();
|
||||
return (uint8_t)changed;
|
||||
}
|
||||
|
||||
@@ -39,24 +39,36 @@ 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) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
return matrix[row];
|
||||
}
|
||||
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
@@ -182,8 +194,6 @@ void matrix_init(void) {
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
@@ -194,7 +204,7 @@ uint8_t matrix_scan(void) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
|
||||
|
||||
183
keyboards/redscarf_iiplus/verb/matrix.c
Executable file → Normal file
183
keyboards/redscarf_iiplus/verb/matrix.c
Executable file → Normal file
@@ -22,21 +22,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "debounce.h"
|
||||
|
||||
#if (MATRIX_COLS <= 8)
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint8_t)1)
|
||||
#elif (MATRIX_COLS <= 16)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint16_t)1)
|
||||
#elif (MATRIX_COLS <= 32)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
#endif
|
||||
|
||||
#ifdef MATRIX_MASKED
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
#endif
|
||||
|
||||
#ifdef DIRECT_PINS
|
||||
@@ -47,46 +47,34 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
||||
#endif
|
||||
|
||||
/* matrix state(1:on, 0:off) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
inline
|
||||
uint8_t matrix_rows(void) {
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_cols(void) {
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline
|
||||
bool matrix_is_on(uint8_t row, uint8_t col)
|
||||
{
|
||||
return (matrix[row] & ((matrix_row_t)1<<col));
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
// Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
|
||||
// switch blocker installed and the switch is always pressed.
|
||||
#ifdef MATRIX_MASKED
|
||||
@@ -96,12 +84,12 @@ matrix_row_t matrix_get_row(uint8_t row)
|
||||
#endif
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_matrix_row(row);
|
||||
print("\n");
|
||||
}
|
||||
@@ -110,28 +98,28 @@ void matrix_print(void)
|
||||
#ifdef DIRECT_PINS
|
||||
|
||||
static void init_pins(void) {
|
||||
for (int row = 0; row < MATRIX_ROWS; row++) {
|
||||
for (int col = 0; col < MATRIX_COLS; col++) {
|
||||
pin_t pin = direct_pins[row][col];
|
||||
if (pin != NO_PIN) {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
for (int row = 0; row < MATRIX_ROWS; row++) {
|
||||
for (int col = 0; col < MATRIX_COLS; col++) {
|
||||
pin_t pin = direct_pins[row][col];
|
||||
if (pin != NO_PIN) {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
|
||||
matrix_row_t last_row_value = current_matrix[current_row];
|
||||
current_matrix[current_row] = 0;
|
||||
matrix_row_t last_row_value = current_matrix[current_row];
|
||||
current_matrix[current_row] = 0;
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
pin_t pin = direct_pins[current_row][col_index];
|
||||
if (pin != NO_PIN) {
|
||||
current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
pin_t pin = direct_pins[current_row][col_index];
|
||||
if (pin != NO_PIN) {
|
||||
current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
}
|
||||
|
||||
#elif (DIODE_DIRECTION == COL2ROW)
|
||||
@@ -146,8 +134,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
* 4: 1 0 0
|
||||
* 5: 1 0 1
|
||||
*/
|
||||
static void select_row(uint8_t col)
|
||||
{
|
||||
static void select_row(uint8_t col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
gpio_write_pin_low(B0);
|
||||
@@ -175,8 +162,7 @@ static void select_row(uint8_t col)
|
||||
}
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t col)
|
||||
{
|
||||
static void unselect_row(uint8_t col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
gpio_write_pin_high(B0);
|
||||
@@ -204,26 +190,24 @@ static void unselect_row(uint8_t col)
|
||||
}
|
||||
}
|
||||
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
static void unselect_rows(void) {
|
||||
gpio_set_pin_output(B0);
|
||||
gpio_set_pin_output(B1);
|
||||
gpio_set_pin_output(B2);
|
||||
// make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
|
||||
// make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
|
||||
gpio_write_pin_high(B0);
|
||||
gpio_write_pin_high(B1);
|
||||
gpio_write_pin_high(B2);
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_rows();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
unselect_rows();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
{
|
||||
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];
|
||||
|
||||
@@ -235,13 +219,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
wait_us(30);
|
||||
|
||||
// For each col...
|
||||
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
// Select the col pin to read (active low)
|
||||
uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
|
||||
|
||||
// Populate the matrix row with the state of the col pin
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
|
||||
// Unselect row
|
||||
@@ -252,33 +235,29 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
|
||||
#elif (DIODE_DIRECTION == ROW2COL)
|
||||
|
||||
static void select_col(uint8_t col)
|
||||
{
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_set_pin_output(col_pins[col]);
|
||||
gpio_write_pin_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col)
|
||||
{
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
{
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
|
||||
bool matrix_changed = false;
|
||||
|
||||
// Select col and wait for col selecton to stabilize
|
||||
@@ -286,27 +265,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
wait_us(30);
|
||||
|
||||
// For each row...
|
||||
for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
|
||||
{
|
||||
|
||||
for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
|
||||
// Store last value of row prior to reading
|
||||
matrix_row_t last_row_value = current_matrix[row_index];
|
||||
|
||||
// Check row pin state
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0)
|
||||
{
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0) {
|
||||
// Pin LO, set col bit
|
||||
current_matrix[row_index] |= (ROW_SHIFTER << current_col);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Pin HI, clear col bit
|
||||
current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
|
||||
}
|
||||
|
||||
// Determine if the matrix changed state
|
||||
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
|
||||
{
|
||||
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
|
||||
matrix_changed = true;
|
||||
}
|
||||
}
|
||||
@@ -320,39 +293,35 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
#endif
|
||||
|
||||
void matrix_init(void) {
|
||||
|
||||
// initialize key pins
|
||||
init_pins();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
raw_matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
bool changed = false;
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
|
||||
#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
|
||||
// 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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
#elif (DIODE_DIRECTION == ROW2COL)
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
#endif
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return 1;
|
||||
matrix_scan_kb();
|
||||
return 1;
|
||||
}
|
||||
|
||||
183
keyboards/redscarf_iiplus/verc/matrix.c
Executable file → Normal file
183
keyboards/redscarf_iiplus/verc/matrix.c
Executable file → Normal file
@@ -22,21 +22,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "debounce.h"
|
||||
|
||||
#if (MATRIX_COLS <= 8)
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint8_t)1)
|
||||
#elif (MATRIX_COLS <= 16)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint16_t)1)
|
||||
#elif (MATRIX_COLS <= 32)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
#endif
|
||||
|
||||
#ifdef MATRIX_MASKED
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
#endif
|
||||
|
||||
#ifdef DIRECT_PINS
|
||||
@@ -47,46 +47,34 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
||||
#endif
|
||||
|
||||
/* matrix state(1:on, 0:off) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
inline
|
||||
uint8_t matrix_rows(void) {
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_cols(void) {
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline
|
||||
bool matrix_is_on(uint8_t row, uint8_t col)
|
||||
{
|
||||
return (matrix[row] & ((matrix_row_t)1<<col));
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
// Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
|
||||
// switch blocker installed and the switch is always pressed.
|
||||
#ifdef MATRIX_MASKED
|
||||
@@ -96,12 +84,12 @@ matrix_row_t matrix_get_row(uint8_t row)
|
||||
#endif
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_matrix_row(row);
|
||||
print("\n");
|
||||
}
|
||||
@@ -110,28 +98,28 @@ void matrix_print(void)
|
||||
#ifdef DIRECT_PINS
|
||||
|
||||
static void init_pins(void) {
|
||||
for (int row = 0; row < MATRIX_ROWS; row++) {
|
||||
for (int col = 0; col < MATRIX_COLS; col++) {
|
||||
pin_t pin = direct_pins[row][col];
|
||||
if (pin != NO_PIN) {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
for (int row = 0; row < MATRIX_ROWS; row++) {
|
||||
for (int col = 0; col < MATRIX_COLS; col++) {
|
||||
pin_t pin = direct_pins[row][col];
|
||||
if (pin != NO_PIN) {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
|
||||
matrix_row_t last_row_value = current_matrix[current_row];
|
||||
current_matrix[current_row] = 0;
|
||||
matrix_row_t last_row_value = current_matrix[current_row];
|
||||
current_matrix[current_row] = 0;
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
pin_t pin = direct_pins[current_row][col_index];
|
||||
if (pin != NO_PIN) {
|
||||
current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
pin_t pin = direct_pins[current_row][col_index];
|
||||
if (pin != NO_PIN) {
|
||||
current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
}
|
||||
|
||||
#elif (DIODE_DIRECTION == COL2ROW)
|
||||
@@ -146,8 +134,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
* 4: 1 0 0
|
||||
* 5: 1 0 1
|
||||
*/
|
||||
static void select_row(uint8_t col)
|
||||
{
|
||||
static void select_row(uint8_t col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
gpio_write_pin_low(B0);
|
||||
@@ -175,8 +162,7 @@ static void select_row(uint8_t col)
|
||||
}
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t col)
|
||||
{
|
||||
static void unselect_row(uint8_t col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
gpio_write_pin_high(B0);
|
||||
@@ -204,26 +190,24 @@ static void unselect_row(uint8_t col)
|
||||
}
|
||||
}
|
||||
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
static void unselect_rows(void) {
|
||||
gpio_set_pin_output(B0);
|
||||
gpio_set_pin_output(B1);
|
||||
gpio_set_pin_output(B2);
|
||||
// make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
|
||||
// make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
|
||||
gpio_write_pin_high(B0);
|
||||
gpio_write_pin_high(B1);
|
||||
gpio_write_pin_high(B2);
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_rows();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
unselect_rows();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
{
|
||||
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];
|
||||
|
||||
@@ -235,13 +219,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
wait_us(30);
|
||||
|
||||
// For each col...
|
||||
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
// Select the col pin to read (active low)
|
||||
uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
|
||||
|
||||
// Populate the matrix row with the state of the col pin
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
|
||||
// Unselect row
|
||||
@@ -252,33 +235,29 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
|
||||
#elif (DIODE_DIRECTION == ROW2COL)
|
||||
|
||||
static void select_col(uint8_t col)
|
||||
{
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_set_pin_output(col_pins[col]);
|
||||
gpio_write_pin_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col)
|
||||
{
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
{
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
|
||||
bool matrix_changed = false;
|
||||
|
||||
// Select col and wait for col selecton to stabilize
|
||||
@@ -286,27 +265,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
wait_us(30);
|
||||
|
||||
// For each row...
|
||||
for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
|
||||
{
|
||||
|
||||
for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
|
||||
// Store last value of row prior to reading
|
||||
matrix_row_t last_row_value = current_matrix[row_index];
|
||||
|
||||
// Check row pin state
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0)
|
||||
{
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0) {
|
||||
// Pin LO, set col bit
|
||||
current_matrix[row_index] |= (ROW_SHIFTER << current_col);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Pin HI, clear col bit
|
||||
current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
|
||||
}
|
||||
|
||||
// Determine if the matrix changed state
|
||||
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
|
||||
{
|
||||
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
|
||||
matrix_changed = true;
|
||||
}
|
||||
}
|
||||
@@ -320,39 +293,35 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
#endif
|
||||
|
||||
void matrix_init(void) {
|
||||
|
||||
// initialize key pins
|
||||
init_pins();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
raw_matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
bool changed = false;
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
|
||||
#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
|
||||
// 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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
#elif (DIODE_DIRECTION == ROW2COL)
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
#endif
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return 1;
|
||||
matrix_scan_kb();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -22,21 +22,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#include "debounce.h"
|
||||
|
||||
#if (MATRIX_COLS <= 8)
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 01234567\n")
|
||||
# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint8_t)1)
|
||||
#elif (MATRIX_COLS <= 16)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint16_t)1)
|
||||
#elif (MATRIX_COLS <= 32)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
# define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
|
||||
# define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
|
||||
# define ROW_SHIFTER ((uint32_t)1)
|
||||
#endif
|
||||
|
||||
#ifdef MATRIX_MASKED
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
extern const matrix_row_t matrix_mask[];
|
||||
#endif
|
||||
|
||||
#ifdef DIRECT_PINS
|
||||
@@ -47,46 +47,34 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
|
||||
#endif
|
||||
|
||||
/* matrix state(1:on, 0:off) */
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
|
||||
static matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
|
||||
static matrix_row_t matrix[MATRIX_ROWS]; // debounced values
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_kb(void) {
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_kb(void) {
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_init_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__ ((weak))
|
||||
void matrix_scan_user(void) {
|
||||
}
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
inline
|
||||
uint8_t matrix_rows(void) {
|
||||
inline uint8_t matrix_rows(void) {
|
||||
return MATRIX_ROWS;
|
||||
}
|
||||
|
||||
inline
|
||||
uint8_t matrix_cols(void) {
|
||||
inline uint8_t matrix_cols(void) {
|
||||
return MATRIX_COLS;
|
||||
}
|
||||
|
||||
inline
|
||||
bool matrix_is_on(uint8_t row, uint8_t col)
|
||||
{
|
||||
return (matrix[row] & ((matrix_row_t)1<<col));
|
||||
inline bool matrix_is_on(uint8_t row, uint8_t col) {
|
||||
return (matrix[row] & ((matrix_row_t)1 << col));
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
// Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
|
||||
// switch blocker installed and the switch is always pressed.
|
||||
#ifdef MATRIX_MASKED
|
||||
@@ -96,12 +84,12 @@ matrix_row_t matrix_get_row(uint8_t row)
|
||||
#endif
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
void matrix_print(void) {
|
||||
print_matrix_header();
|
||||
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_matrix_row(row);
|
||||
print("\n");
|
||||
}
|
||||
@@ -110,28 +98,28 @@ void matrix_print(void)
|
||||
#ifdef DIRECT_PINS
|
||||
|
||||
static void init_pins(void) {
|
||||
for (int row = 0; row < MATRIX_ROWS; row++) {
|
||||
for (int col = 0; col < MATRIX_COLS; col++) {
|
||||
pin_t pin = direct_pins[row][col];
|
||||
if (pin != NO_PIN) {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
for (int row = 0; row < MATRIX_ROWS; row++) {
|
||||
for (int col = 0; col < MATRIX_COLS; col++) {
|
||||
pin_t pin = direct_pins[row][col];
|
||||
if (pin != NO_PIN) {
|
||||
gpio_set_pin_input_high(pin);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
|
||||
matrix_row_t last_row_value = current_matrix[current_row];
|
||||
current_matrix[current_row] = 0;
|
||||
matrix_row_t last_row_value = current_matrix[current_row];
|
||||
current_matrix[current_row] = 0;
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
pin_t pin = direct_pins[current_row][col_index];
|
||||
if (pin != NO_PIN) {
|
||||
current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
pin_t pin = direct_pins[current_row][col_index];
|
||||
if (pin != NO_PIN) {
|
||||
current_matrix[current_row] |= gpio_read_pin(pin) ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
return (last_row_value != current_matrix[current_row]);
|
||||
}
|
||||
|
||||
#elif (DIODE_DIRECTION == COL2ROW)
|
||||
@@ -146,8 +134,7 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
* 4: 1 0 0
|
||||
* 5: 1 0 1
|
||||
*/
|
||||
static void select_row(uint8_t col)
|
||||
{
|
||||
static void select_row(uint8_t col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
gpio_write_pin_low(B0);
|
||||
@@ -175,8 +162,7 @@ static void select_row(uint8_t col)
|
||||
}
|
||||
}
|
||||
|
||||
static void unselect_row(uint8_t col)
|
||||
{
|
||||
static void unselect_row(uint8_t col) {
|
||||
switch (col) {
|
||||
case 0:
|
||||
gpio_write_pin_high(B0);
|
||||
@@ -204,26 +190,24 @@ static void unselect_row(uint8_t col)
|
||||
}
|
||||
}
|
||||
|
||||
static void unselect_rows(void)
|
||||
{
|
||||
static void unselect_rows(void) {
|
||||
gpio_set_pin_output(B0);
|
||||
gpio_set_pin_output(B1);
|
||||
gpio_set_pin_output(B2);
|
||||
// make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
|
||||
// make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
|
||||
gpio_write_pin_high(B0);
|
||||
gpio_write_pin_high(B1);
|
||||
gpio_write_pin_high(B2);
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_rows();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
unselect_rows();
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
{
|
||||
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];
|
||||
|
||||
@@ -235,13 +219,12 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
wait_us(30);
|
||||
|
||||
// For each col...
|
||||
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
|
||||
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
|
||||
// Select the col pin to read (active low)
|
||||
uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
|
||||
|
||||
// Populate the matrix row with the state of the col pin
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
|
||||
}
|
||||
|
||||
// Unselect row
|
||||
@@ -252,33 +235,29 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
|
||||
|
||||
#elif (DIODE_DIRECTION == ROW2COL)
|
||||
|
||||
static void select_col(uint8_t col)
|
||||
{
|
||||
static void select_col(uint8_t col) {
|
||||
gpio_set_pin_output(col_pins[col]);
|
||||
gpio_write_pin_low(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_col(uint8_t col)
|
||||
{
|
||||
static void unselect_col(uint8_t col) {
|
||||
gpio_set_pin_input_high(col_pins[col]);
|
||||
}
|
||||
|
||||
static void unselect_cols(void)
|
||||
{
|
||||
for(uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
static void unselect_cols(void) {
|
||||
for (uint8_t x = 0; x < MATRIX_COLS; x++) {
|
||||
gpio_set_pin_input_high(col_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static void init_pins(void) {
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
unselect_cols();
|
||||
for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
|
||||
gpio_set_pin_input_high(row_pins[x]);
|
||||
}
|
||||
}
|
||||
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
{
|
||||
static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
|
||||
bool matrix_changed = false;
|
||||
|
||||
// Select col and wait for col selecton to stabilize
|
||||
@@ -286,27 +265,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
wait_us(30);
|
||||
|
||||
// For each row...
|
||||
for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
|
||||
{
|
||||
|
||||
for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
|
||||
// Store last value of row prior to reading
|
||||
matrix_row_t last_row_value = current_matrix[row_index];
|
||||
|
||||
// Check row pin state
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0)
|
||||
{
|
||||
if (gpio_read_pin(row_pins[row_index]) == 0) {
|
||||
// Pin LO, set col bit
|
||||
current_matrix[row_index] |= (ROW_SHIFTER << current_col);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Pin HI, clear col bit
|
||||
current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
|
||||
}
|
||||
|
||||
// Determine if the matrix changed state
|
||||
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
|
||||
{
|
||||
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
|
||||
matrix_changed = true;
|
||||
}
|
||||
}
|
||||
@@ -320,39 +293,35 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
|
||||
#endif
|
||||
|
||||
void matrix_init(void) {
|
||||
|
||||
// initialize key pins
|
||||
init_pins();
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) {
|
||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
|
||||
raw_matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
bool changed = false;
|
||||
uint8_t matrix_scan(void) {
|
||||
bool changed = false;
|
||||
|
||||
#if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
|
||||
// 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);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
#elif (DIODE_DIRECTION == ROW2COL)
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
// Set col, read rows
|
||||
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
|
||||
changed |= read_rows_on_col(raw_matrix, current_col);
|
||||
}
|
||||
#endif
|
||||
|
||||
debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
|
||||
debounce(raw_matrix, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
return 1;
|
||||
matrix_scan_kb();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
// Copyright 2019 Neil Kettle
|
||||
// SPDX-License-Identifier: GPL-2.0+
|
||||
|
||||
#include QMK_KEYBOARD_H
|
||||
|
||||
enum layer_names {
|
||||
|
||||
@@ -40,132 +40,128 @@ static uint8_t mcp23018_reset_loop = 0;
|
||||
|
||||
// user-defined overridable functions
|
||||
|
||||
__attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); }
|
||||
__attribute__((weak)) void matrix_init_kb(void) {
|
||||
matrix_init_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); }
|
||||
__attribute__((weak)) void matrix_scan_kb(void) {
|
||||
matrix_scan_user();
|
||||
}
|
||||
|
||||
__attribute__((weak)) void matrix_init_user(void) {}
|
||||
|
||||
__attribute__((weak)) void matrix_scan_user(void) {}
|
||||
|
||||
// helper functions
|
||||
void matrix_init(void)
|
||||
{
|
||||
// all outputs for rows high
|
||||
DDRB = 0xFF;
|
||||
PORTB = 0xFF;
|
||||
// all inputs for columns
|
||||
DDRA = 0x00;
|
||||
DDRC &= ~(0x111111<<2);
|
||||
DDRD &= ~(1<<PIND7);
|
||||
// all columns are pulled-up
|
||||
PORTA = 0xFF;
|
||||
PORTC |= (0b111111<<2);
|
||||
PORTD |= (1<<PIND7);
|
||||
void matrix_init(void) {
|
||||
// all outputs for rows high
|
||||
DDRB = 0xFF;
|
||||
PORTB = 0xFF;
|
||||
// all inputs for columns
|
||||
DDRA = 0x00;
|
||||
DDRC &= ~(0x111111 << 2);
|
||||
DDRD &= ~(1 << PIND7);
|
||||
// all columns are pulled-up
|
||||
PORTA = 0xFF;
|
||||
PORTC |= (0b111111 << 2);
|
||||
PORTD |= (1 << PIND7);
|
||||
|
||||
#ifdef RIGHT_HALF
|
||||
// initialize row and col
|
||||
mcp23018_status = init_mcp23018();
|
||||
// initialize row and col
|
||||
mcp23018_status = init_mcp23018();
|
||||
#endif
|
||||
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
matrix[row] = 0;
|
||||
matrix_debouncing[row] = 0;
|
||||
}
|
||||
debounce_init(MATRIX_ROWS);
|
||||
matrix_init_kb();
|
||||
// initialize matrix state: all keys off
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
matrix[row] = 0;
|
||||
matrix_debouncing[row] = 0;
|
||||
}
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
uint8_t matrix_scan(void)
|
||||
{
|
||||
uint8_t matrix_scan(void) {
|
||||
#ifdef RIGHT_HALF
|
||||
// Then the keyboard
|
||||
if (mcp23018_status != I2C_STATUS_SUCCESS) {
|
||||
if (++mcp23018_reset_loop == 0) {
|
||||
// if (++mcp23018_reset_loop >= 1300) {
|
||||
// since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
|
||||
// this will be approx bit more frequent than once per second
|
||||
print("trying to reset mcp23018\n");
|
||||
mcp23018_status = init_mcp23018();
|
||||
if (mcp23018_status) {
|
||||
print("left side not responding\n");
|
||||
} else {
|
||||
print("left side attached\n");
|
||||
}
|
||||
// Then the keyboard
|
||||
if (mcp23018_status != I2C_STATUS_SUCCESS) {
|
||||
if (++mcp23018_reset_loop == 0) {
|
||||
// if (++mcp23018_reset_loop >= 1300) {
|
||||
// since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
|
||||
// this will be approx bit more frequent than once per second
|
||||
print("trying to reset mcp23018\n");
|
||||
mcp23018_status = init_mcp23018();
|
||||
if (mcp23018_status) {
|
||||
print("left side not responding\n");
|
||||
} else {
|
||||
print("left side attached\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
bool changed = false;
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++)
|
||||
{
|
||||
matrix_row_t cols;
|
||||
bool changed = false;
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
matrix_row_t cols;
|
||||
|
||||
matrix_select_row(row);
|
||||
matrix_select_row(row);
|
||||
#ifndef RIGHT_HALF
|
||||
_delay_us(5);
|
||||
_delay_us(5);
|
||||
#endif
|
||||
|
||||
cols = (
|
||||
// cols 0..7, PORTA 0 -> 7
|
||||
(~PINA) & 0xFF
|
||||
);
|
||||
cols = (
|
||||
// cols 0..7, PORTA 0 -> 7
|
||||
(~PINA) & 0xFF);
|
||||
|
||||
#ifdef RIGHT_HALF
|
||||
uint8_t data = 0x7F;
|
||||
// Receive the columns from right half
|
||||
i2c_receive(I2C_ADDR, &data, 1, MCP23018_I2C_TIMEOUT);
|
||||
cols |= ((~(data) & 0x7F) << 7);
|
||||
uint8_t data = 0x7F;
|
||||
// Receive the columns from right half
|
||||
i2c_receive(I2C_ADDR, &data, 1, MCP23018_I2C_TIMEOUT);
|
||||
cols |= ((~(data) & 0x7F) << 7);
|
||||
#endif
|
||||
|
||||
if (matrix_debouncing[row] != cols) {
|
||||
matrix_debouncing[row] = cols;
|
||||
//debouncing = DEBOUNCE;
|
||||
changed = true;
|
||||
if (matrix_debouncing[row] != cols) {
|
||||
matrix_debouncing[row] = cols;
|
||||
// debouncing = DEBOUNCE;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debounce(matrix_debouncing, matrix, MATRIX_ROWS, changed);
|
||||
debounce(matrix_debouncing, matrix, changed);
|
||||
|
||||
matrix_scan_kb();
|
||||
matrix_scan_kb();
|
||||
|
||||
#ifdef DEBUG_MATRIX
|
||||
for (uint8_t c = 0; c < MATRIX_COLS; c++)
|
||||
for (uint8_t r = 0; r < MATRIX_ROWS; r++)
|
||||
if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
|
||||
for (uint8_t c = 0; c < MATRIX_COLS; c++)
|
||||
for (uint8_t r = 0; r < MATRIX_ROWS; r++)
|
||||
if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
|
||||
#endif
|
||||
|
||||
return (uint8_t)changed;
|
||||
return (uint8_t)changed;
|
||||
}
|
||||
|
||||
inline
|
||||
matrix_row_t matrix_get_row(uint8_t row)
|
||||
{
|
||||
inline matrix_row_t matrix_get_row(uint8_t row) {
|
||||
return matrix[row];
|
||||
}
|
||||
|
||||
void matrix_print(void)
|
||||
{
|
||||
print("\nr/c 0123456789ABCDEF\n");
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row); print(": ");
|
||||
print_bin_reverse16(matrix_get_row(row));
|
||||
print("\n");
|
||||
}
|
||||
void matrix_print(void) {
|
||||
print("\nr/c 0123456789ABCDEF\n");
|
||||
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
|
||||
print_hex8(row);
|
||||
print(": ");
|
||||
print_bin_reverse16(matrix_get_row(row));
|
||||
print("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void matrix_select_row(uint8_t row)
|
||||
{
|
||||
static void matrix_select_row(uint8_t row) {
|
||||
#ifdef RIGHT_HALF
|
||||
uint8_t txdata[3];
|
||||
uint8_t txdata[3];
|
||||
|
||||
//Set the remote row on port A
|
||||
txdata[0] = GPIOA;
|
||||
txdata[1] = 0xFF & ~(1<<row);
|
||||
mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)txdata, 2, MCP23018_I2C_TIMEOUT);
|
||||
// Set the remote row on port A
|
||||
txdata[0] = GPIOA;
|
||||
txdata[1] = 0xFF & ~(1 << row);
|
||||
mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)txdata, 2, MCP23018_I2C_TIMEOUT);
|
||||
#endif
|
||||
|
||||
// select other half
|
||||
DDRB = (1 << row);
|
||||
PORTB = ~(1 << row);
|
||||
// select other half
|
||||
DDRB = (1 << row);
|
||||
PORTB = ~(1 << row);
|
||||
}
|
||||
|
||||
@@ -9,11 +9,8 @@
|
||||
*
|
||||
* @param raw The current key state
|
||||
* @param cooked The debounced key state
|
||||
* @param num_rows Number of rows to debounce
|
||||
* @param changed True if raw has changed since the last call
|
||||
* @return true Cooked has new keychanges after debouncing
|
||||
* @return false Cooked is the same as before
|
||||
*/
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed);
|
||||
|
||||
void debounce_init(uint8_t num_rows);
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed);
|
||||
|
||||
@@ -38,9 +38,7 @@ static bool cooked_changed;
|
||||
static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
|
||||
static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||
|
||||
void debounce_init(uint8_t num_rows) {}
|
||||
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
|
||||
static fast_timer_t last_time;
|
||||
bool updated_last = false;
|
||||
cooked_changed = false;
|
||||
|
||||
@@ -17,13 +17,11 @@
|
||||
#include "debounce.h"
|
||||
#include <string.h>
|
||||
|
||||
void debounce_init(uint8_t num_rows) {}
|
||||
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
|
||||
bool cooked_changed = false;
|
||||
|
||||
if (changed) {
|
||||
size_t matrix_size = num_rows * sizeof(matrix_row_t);
|
||||
size_t matrix_size = MATRIX_ROWS_PER_HAND * sizeof(matrix_row_t);
|
||||
if (memcmp(cooked, raw, matrix_size) != 0) {
|
||||
memcpy(cooked, raw, matrix_size);
|
||||
cooked_changed = true;
|
||||
|
||||
@@ -20,9 +20,7 @@
|
||||
|
||||
#if DEBOUNCE > 0
|
||||
|
||||
void debounce_init(uint8_t num_rows) {}
|
||||
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
|
||||
static fast_timer_t debouncing_time;
|
||||
static bool debouncing = false;
|
||||
bool cooked_changed = false;
|
||||
@@ -31,7 +29,7 @@ bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool
|
||||
debouncing = true;
|
||||
debouncing_time = timer_read_fast();
|
||||
} else if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) {
|
||||
size_t matrix_size = num_rows * sizeof(matrix_row_t);
|
||||
size_t matrix_size = MATRIX_ROWS_PER_HAND * sizeof(matrix_row_t);
|
||||
if (memcmp(cooked, raw, matrix_size) != 0) {
|
||||
memcpy(cooked, raw, matrix_size);
|
||||
cooked_changed = true;
|
||||
|
||||
@@ -32,9 +32,7 @@ static bool cooked_changed;
|
||||
static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
|
||||
static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||
|
||||
void debounce_init(uint8_t num_rows) {}
|
||||
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
|
||||
static fast_timer_t last_time;
|
||||
bool updated_last = false;
|
||||
cooked_changed = false;
|
||||
|
||||
@@ -33,9 +33,7 @@ static bool cooked_changed;
|
||||
static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
|
||||
static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||
|
||||
void debounce_init(uint8_t num_rows) {}
|
||||
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
|
||||
static fast_timer_t last_time;
|
||||
bool updated_last = false;
|
||||
cooked_changed = false;
|
||||
|
||||
@@ -46,9 +46,7 @@ static bool cooked_changed;
|
||||
static inline void update_debounce_counters(uint8_t elapsed_time);
|
||||
static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||
|
||||
void debounce_init(uint8_t num_rows) {}
|
||||
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
|
||||
static fast_timer_t last_time;
|
||||
bool updated_last = false;
|
||||
cooked_changed = false;
|
||||
|
||||
@@ -33,9 +33,7 @@ static bool cooked_changed;
|
||||
static inline void update_debounce_counters(uint8_t elapsed_time);
|
||||
static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
|
||||
|
||||
void debounce_init(uint8_t num_rows) {}
|
||||
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
|
||||
bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
|
||||
static fast_timer_t last_time;
|
||||
bool updated_last = false;
|
||||
cooked_changed = false;
|
||||
|
||||
@@ -60,7 +60,6 @@ void DebounceTest::runEventsInternal() {
|
||||
bool first = true;
|
||||
|
||||
/* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */
|
||||
debounce_init(MATRIX_ROWS);
|
||||
set_time(time_offset_);
|
||||
simulate_async_tick(async_time_jumps_);
|
||||
std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0);
|
||||
@@ -129,7 +128,7 @@ void DebounceTest::runDebounce(bool changed) {
|
||||
|
||||
reset_access_counter();
|
||||
|
||||
bool cooked_changed = debounce(raw_matrix_, cooked_matrix_, MATRIX_ROWS, changed);
|
||||
bool cooked_changed = debounce(raw_matrix_, cooked_matrix_, changed);
|
||||
|
||||
if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) {
|
||||
FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime() << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) << "\nraw_matrix:\n" << strMatrix(raw_matrix_);
|
||||
|
||||
@@ -303,8 +303,6 @@ void matrix_init(void) {
|
||||
memset(matrix, 0, sizeof(matrix));
|
||||
memset(raw_matrix, 0, sizeof(raw_matrix));
|
||||
|
||||
debounce_init(MATRIX_ROWS_PER_HAND);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
@@ -336,9 +334,9 @@ uint8_t matrix_scan(void) {
|
||||
if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix));
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
changed = debounce(raw_matrix, matrix + thisHand, MATRIX_ROWS_PER_HAND, changed) | matrix_post_scan();
|
||||
changed = debounce(raw_matrix, matrix + thisHand, changed) | matrix_post_scan();
|
||||
#else
|
||||
changed = debounce(raw_matrix, matrix, MATRIX_ROWS_PER_HAND, changed);
|
||||
changed = debounce(raw_matrix, matrix, changed);
|
||||
matrix_scan_kb();
|
||||
#endif
|
||||
return (uint8_t)changed;
|
||||
|
||||
@@ -156,8 +156,6 @@ __attribute__((weak)) void matrix_init(void) {
|
||||
matrix[i] = 0;
|
||||
}
|
||||
|
||||
debounce_init(MATRIX_ROWS_PER_HAND);
|
||||
|
||||
matrix_init_kb();
|
||||
}
|
||||
|
||||
@@ -165,9 +163,9 @@ __attribute__((weak)) uint8_t matrix_scan(void) {
|
||||
bool changed = matrix_scan_custom(raw_matrix);
|
||||
|
||||
#ifdef SPLIT_KEYBOARD
|
||||
changed = debounce(raw_matrix, matrix + thisHand, MATRIX_ROWS_PER_HAND, changed) | matrix_post_scan();
|
||||
changed = debounce(raw_matrix, matrix + thisHand, changed) | matrix_post_scan();
|
||||
#else
|
||||
changed = debounce(raw_matrix, matrix, MATRIX_ROWS_PER_HAND, changed);
|
||||
changed = debounce(raw_matrix, matrix, changed);
|
||||
matrix_scan_kb();
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user