/*
-Copyright 2012 Jun Wako
-Copyright 2014 Jack Humbert
+Copyright 2012-2017 Jun Wako, Jack Humbert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
*/
#include <stdint.h>
#include <stdbool.h>
+#if defined(__AVR__)
#include <avr/io.h>
+#endif
#include "wait.h"
#include "print.h"
#include "debug.h"
#include "util.h"
#include "matrix.h"
+#include "timer.h"
-#ifdef MATRIX_HAS_GHOST
-# error "The universal matrix.c file cannot be used for this keyboard."
-#endif
+
+/* Set 0 if debouncing isn't needed */
#ifndef DEBOUNCING_DELAY
# define DEBOUNCING_DELAY 5
#endif
-static const io_pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
-static const io_pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
-/* matrix state */
-#if DIODE_DIRECTION == COL2ROW
-static matrix_row_t matrix[MATRIX_ROWS];
-#else
-static matrix_col_t matrix[MATRIX_COLS];
+#if (DEBOUNCING_DELAY > 0)
+ static uint16_t debouncing_time;
+ static bool debouncing = false;
#endif
-static int8_t debouncing_delay = -1;
-#if DIODE_DIRECTION == COL2ROW
-static void toggle_row(uint8_t row);
-static matrix_row_t read_cols(void);
-#else
-static void toggle_col(uint8_t col);
-static matrix_col_t read_rows(void);
+#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 matrix_bitpop(i) bitpop(matrix[i])
+# 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 matrix_bitpop(i) bitpop16(matrix[i])
+# 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 matrix_bitpop(i) bitpop32(matrix[i])
+# define ROW_SHIFTER ((uint32_t)1)
+#endif
+
+#ifdef MATRIX_MASKED
+ extern const matrix_row_t matrix_mask[];
+#endif
+
+#if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
+static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
+static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
+#endif
+
+/* matrix state(1:on, 0:off) */
+static matrix_row_t matrix[MATRIX_ROWS];
+
+static matrix_row_t matrix_debouncing[MATRIX_ROWS];
+
+
+#if (DIODE_DIRECTION == COL2ROW)
+ static void init_cols(void);
+ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
+ static void unselect_rows(void);
+ static void select_row(uint8_t row);
+ static void unselect_row(uint8_t row);
+#elif (DIODE_DIRECTION == ROW2COL)
+ static void init_rows(void);
+ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
+ static void unselect_cols(void);
+ static void unselect_col(uint8_t col);
+ static void select_col(uint8_t col);
#endif
__attribute__ ((weak))
void matrix_init_quantum(void) {
+ matrix_init_kb();
}
__attribute__ ((weak))
void matrix_scan_quantum(void) {
+ matrix_scan_kb();
+}
+
+__attribute__ ((weak))
+void matrix_init_kb(void) {
+ matrix_init_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_cols(void) {
return MATRIX_COLS;
}
-void matrix_power_up(void) {
-#if DIODE_DIRECTION == COL2ROW
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- /* DDRxn */
- _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
- toggle_row(r);
- }
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- /* PORTxn */
- _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
- }
-#else
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- /* DDRxn */
- _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
- toggle_col(c);
- }
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- /* PORTxn */
- _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
- }
-#endif
-}
+// void matrix_power_up(void) {
+// #if (DIODE_DIRECTION == COL2ROW)
+// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
+// /* DDRxn */
+// _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
+// toggle_row(r);
+// }
+// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
+// /* PORTxn */
+// _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
+// }
+// #elif (DIODE_DIRECTION == ROW2COL)
+// for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
+// /* DDRxn */
+// _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
+// toggle_col(c);
+// }
+// for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
+// /* PORTxn */
+// _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
+// }
+// #endif
+// }
void matrix_init(void) {
- /* frees PORTF by setting the JTD bit twice within four cycles */
- #ifdef __AVR_ATmega32U4__
+
+ // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
+ #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
MCUCR |= _BV(JTD);
MCUCR |= _BV(JTD);
#endif
- /* initializes the I/O pins */
-#if DIODE_DIRECTION == COL2ROW
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- /* DDRxn */
- _SFR_IO8(row_pins[r].input_addr + 1) |= _BV(row_pins[r].bit);
- toggle_row(r);
- }
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- /* PORTxn */
- _SFR_IO8(col_pins[c].input_addr + 2) |= _BV(col_pins[c].bit);
- }
-#else
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- /* DDRxn */
- _SFR_IO8(col_pins[c].input_addr + 1) |= _BV(col_pins[c].bit);
- toggle_col(c);
- }
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- /* PORTxn */
- _SFR_IO8(row_pins[r].input_addr + 2) |= _BV(row_pins[r].bit);
- }
+
+ // initialize row and col
+#if (DIODE_DIRECTION == COL2ROW)
+ unselect_rows();
+ init_cols();
+#elif (DIODE_DIRECTION == ROW2COL)
+ unselect_cols();
+ init_rows();
#endif
+
+ // initialize matrix state: all keys off
+ for (uint8_t i=0; i < MATRIX_ROWS; i++) {
+ matrix[i] = 0;
+ matrix_debouncing[i] = 0;
+ }
+
matrix_init_quantum();
}
-#if DIODE_DIRECTION == COL2ROW
-uint8_t matrix_scan(void) {
- static matrix_row_t debouncing_matrix[MATRIX_ROWS];
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- toggle_row(r);
- matrix_row_t state = read_cols();
- if (debouncing_matrix[r] != state) {
- debouncing_matrix[r] = state;
- debouncing_delay = DEBOUNCING_DELAY;
- }
- toggle_row(r);
+uint8_t matrix_scan(void)
+{
+
+#if (DIODE_DIRECTION == COL2ROW)
+
+ // Set row, read cols
+ for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
+# if (DEBOUNCING_DELAY > 0)
+ bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
+
+ if (matrix_changed) {
+ debouncing = true;
+ debouncing_time = timer_read();
+ }
+
+# else
+ read_cols_on_row(matrix, current_row);
+# endif
+
}
- if (debouncing_delay >= 0) {
- dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
- --debouncing_delay;
- if (debouncing_delay >= 0) {
- wait_ms(1);
- }
- else {
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- matrix[r] = debouncing_matrix[r];
+
+#elif (DIODE_DIRECTION == ROW2COL)
+
+ // Set col, read rows
+ for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
+# if (DEBOUNCING_DELAY > 0)
+ bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
+ if (matrix_changed) {
+ debouncing = true;
+ debouncing_time = timer_read();
}
- }
+# else
+ read_rows_on_col(matrix, current_col);
+# endif
+
}
+
+#endif
+
+# if (DEBOUNCING_DELAY > 0)
+ if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+ matrix[i] = matrix_debouncing[i];
+ }
+ debouncing = false;
+ }
+# endif
+
matrix_scan_quantum();
return 1;
}
-static void toggle_row(uint8_t row) {
- /* PINxn */
- _SFR_IO8(row_pins[row].input_addr) = _BV(row_pins[row].bit);
+bool matrix_is_modified(void)
+{
+#if (DEBOUNCING_DELAY > 0)
+ if (debouncing) return false;
+#endif
+ return true;
}
-static matrix_row_t read_cols(void) {
- matrix_row_t state = 0;
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- /* PINxn */
- if (!(_SFR_IO8(col_pins[c].input_addr) & _BV(col_pins[c].bit))) {
- state |= (matrix_row_t)1 << c;
- }
- }
- return state;
+inline
+bool matrix_is_on(uint8_t row, uint8_t col)
+{
+ return (matrix[row] & ((matrix_row_t)1<col));
}
-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
+ return matrix[row] & matrix_mask[row];
+#else
return matrix[row];
+#endif
}
-#else
-uint8_t matrix_scan(void) {
- static matrix_col_t debouncing_matrix[MATRIX_COLS];
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- toggle_col(c);
- matrix_col_t state = read_rows();
- if (debouncing_matrix[c] != state) {
- debouncing_matrix[c] = state;
- debouncing_delay = DEBOUNCING_DELAY;
- }
- toggle_col(c);
- }
- if (debouncing_delay >= 0) {
- dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
- --debouncing_delay;
- if (debouncing_delay >= 0) {
- wait_ms(1);
- }
- else {
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- matrix[c] = debouncing_matrix[c];
- }
- }
+void matrix_print(void)
+{
+ print_matrix_header();
+
+ for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
+ phex(row); print(": ");
+ print_matrix_row(row);
+ print("\n");
}
- matrix_scan_quantum();
- return 1;
}
-static void toggle_col(uint8_t col) {
- /* PINxn */
- _SFR_IO8(col_pins[col].input_addr) = _BV(col_pins[col].bit);
+uint8_t matrix_key_count(void)
+{
+ uint8_t count = 0;
+ for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
+ count += matrix_bitpop(i);
+ }
+ return count;
}
-static matrix_col_t read_rows(void) {
- matrix_col_t state = 0;
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- /* PINxn */
- if (!(_SFR_IO8(row_pins[r].input_addr) & _BV(row_pins[r].bit))) {
- state |= (matrix_col_t)1 << r;
- }
+
+
+#if (DIODE_DIRECTION == COL2ROW)
+
+static void init_cols(void)
+{
+ for(uint8_t x = 0; x < MATRIX_COLS; x++) {
+ uint8_t pin = col_pins[x];
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
}
- return state;
}
-matrix_row_t matrix_get_row(uint8_t row) {
- matrix_row_t state = 0;
- matrix_col_t mask = (matrix_col_t)1 << row;
- for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
- if (matrix[c] & mask) {
- state |= (matrix_row_t)1 << c;
- }
+static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
+{
+ // Store last value of row prior to reading
+ matrix_row_t last_row_value = current_matrix[current_row];
+
+ // Clear data in matrix row
+ current_matrix[current_row] = 0;
+
+ // Select row and wait for row selecton to stabilize
+ select_row(current_row);
+ wait_us(30);
+
+ // For each col...
+ for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
+
+ // Select the col pin to read (active low)
+ uint8_t pin = col_pins[col_index];
+ uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
+
+ // Populate the matrix row with the state of the col pin
+ current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
}
- return state;
+
+ // Unselect row
+ unselect_row(current_row);
+
+ return (last_row_value != current_matrix[current_row]);
}
-#endif
+static void select_row(uint8_t row)
+{
+ uint8_t pin = row_pins[row];
+ _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
+ _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
+}
-bool matrix_is_modified(void) {
- if (debouncing_delay >= 0) return false;
- return true;
+static void unselect_row(uint8_t row)
+{
+ uint8_t pin = row_pins[row];
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
}
-bool matrix_is_on(uint8_t row, uint8_t col) {
- return matrix_get_row(row) & (matrix_row_t)1 << col;
+static void unselect_rows(void)
+{
+ for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
+ uint8_t pin = row_pins[x];
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
+ }
}
-void matrix_print(void) {
- dprintln("Human-readable matrix state:");
- for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
- dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
+#elif (DIODE_DIRECTION == ROW2COL)
+
+static void init_rows(void)
+{
+ for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
+ uint8_t pin = row_pins[x];
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
}
}
-uint8_t matrix_key_count(void) {
- uint8_t count = 0;
- for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
- count += bitpop16(matrix_get_row(r));
+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
+ select_col(current_col);
+ wait_us(30);
+
+ // For each row...
+ 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 ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
+ {
+ // Pin LO, set col bit
+ current_matrix[row_index] |= (ROW_SHIFTER << current_col);
+ }
+ 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))
+ {
+ matrix_changed = true;
+ }
}
- return count;
+
+ // Unselect col
+ unselect_col(current_col);
+
+ return matrix_changed;
+}
+
+static void select_col(uint8_t col)
+{
+ uint8_t pin = col_pins[col];
+ _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
+ _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
}
+
+static void unselect_col(uint8_t col)
+{
+ uint8_t pin = col_pins[col];
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
+}
+
+static void unselect_cols(void)
+{
+ for(uint8_t x = 0; x < MATRIX_COLS; x++) {
+ uint8_t pin = col_pins[x];
+ _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
+ _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
+ }
+}
+
+#endif