matrix.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "matrix.h"
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include "wait.h"
  19. #include "action_layer.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "debounce.h"
  24. #include "gergoplex.h"
  25. #ifdef BALLER
  26. # include <avr/interrupt.h>
  27. # include "pointing_device.h"
  28. #endif
  29. #ifndef DEBOUNCE
  30. # define DEBOUNCE 5
  31. #endif
  32. // ATmega pin defs
  33. #define COL1 (1 << 6)
  34. #define COL2 (1 << 5)
  35. #define COL3 (1 << 4)
  36. #define COL4 (1 << 1)
  37. /* matrix state(1:on, 0:off) */
  38. static matrix_row_t matrix[MATRIX_ROWS];
  39. /*
  40. * matrix state(1:on, 0:off)
  41. * contains the raw values without debounce filtering of the last read cycle.
  42. */
  43. static matrix_row_t raw_matrix[MATRIX_ROWS];
  44. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  45. // Right-hand side only pins, the left side is controlled my MCP
  46. static const pin_t row_pins[MATRIX_ROWS_PER_SIDE] = MATRIX_ROW_PINS;
  47. // Debouncing: store for each key the number of scans until it's eligible to
  48. // change. When scanning the matrix, ignore any changes in keys that have
  49. // already changed in the last DEBOUNCE scans.
  50. static matrix_row_t read_cols(uint8_t row);
  51. static void init_cols(void);
  52. static void unselect_rows(void);
  53. static void select_row(uint8_t row);
  54. static uint8_t mcp23018_reset_loop;
  55. __attribute__((weak)) void matrix_init_user(void) {}
  56. __attribute__((weak)) void matrix_scan_user(void) {}
  57. __attribute__((weak)) void matrix_scan_kb(void) {
  58. matrix_scan_user();
  59. }
  60. void matrix_init(void) {
  61. // initialize row and col
  62. mcp23018_status = init_mcp23018();
  63. unselect_rows();
  64. init_cols();
  65. // initialize matrix state: all keys off
  66. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  67. matrix[i] = 0;
  68. raw_matrix[i] = 0;
  69. }
  70. debounce_init();
  71. matrix_init_kb();
  72. }
  73. void matrix_power_up(void) {
  74. mcp23018_status = init_mcp23018();
  75. unselect_rows();
  76. init_cols();
  77. // initialize matrix state: all keys off
  78. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  79. matrix[i] = 0;
  80. }
  81. }
  82. // Reads and stores a row, returning
  83. // whether a change occurred.
  84. static inline bool store_raw_matrix_row(uint8_t index) {
  85. matrix_row_t temp = read_cols(index);
  86. if (raw_matrix[index] != temp) {
  87. raw_matrix[index] = temp;
  88. return true;
  89. }
  90. return false;
  91. }
  92. uint8_t matrix_scan(void) {
  93. if (mcp23018_status) { // if there was an error
  94. if (++mcp23018_reset_loop == 0) {
  95. // if (++mcp23018_reset_loop >= 1300) {
  96. // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  97. // this will be approx bit more frequent than once per second
  98. print("trying to reset mcp23018\n");
  99. mcp23018_status = init_mcp23018();
  100. if (mcp23018_status) {
  101. print("left side not responding\n");
  102. } else {
  103. print("left side attached\n");
  104. }
  105. }
  106. }
  107. bool changed = false;
  108. for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
  109. // select rows from left and right hands
  110. uint8_t left_index = i;
  111. uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
  112. select_row(left_index);
  113. select_row(right_index);
  114. // we don't need a 30us delay anymore, because selecting a
  115. // left-hand row requires more than 30us for i2c.
  116. changed |= store_raw_matrix_row(left_index);
  117. changed |= store_raw_matrix_row(right_index);
  118. unselect_rows();
  119. }
  120. debounce(raw_matrix, matrix, changed);
  121. matrix_scan_kb();
  122. #ifdef DEBUG_MATRIX
  123. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  124. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  125. if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
  126. #endif
  127. return 1;
  128. }
  129. inline bool matrix_is_on(uint8_t row, uint8_t col) {
  130. return (matrix[row] & ((matrix_row_t)1 << col));
  131. }
  132. inline matrix_row_t matrix_get_row(uint8_t row) {
  133. return matrix[row];
  134. }
  135. void matrix_print(void) {
  136. print("\nr/c 0123456789ABCDEF\n");
  137. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  138. print_hex8(row);
  139. print(": ");
  140. print_bin_reverse16(matrix_get_row(row));
  141. print("\n");
  142. }
  143. }
  144. // Remember this means ROWS
  145. static void init_cols(void) {
  146. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  147. gpio_set_pin_input_high(col_pins[col]);
  148. }
  149. }
  150. static matrix_row_t read_cols(uint8_t row) {
  151. if (row < 5) {
  152. if (mcp23018_status) { // if there was an error
  153. return 0;
  154. } else {
  155. uint8_t data = 0;
  156. mcp23018_status = i2c_receive(I2C_ADDR, &data, 1, I2C_TIMEOUT);
  157. #ifdef DEBUG_MATRIX
  158. if (~data != 0x00) xprintf("I2C: %d\n", ~data);
  159. #endif
  160. return ~data;
  161. }
  162. } else {
  163. return ~((((PINF & COL4) >> 1) | ((PINF & (COL1 | COL2 | COL3)) >> 3)) & 0xF);
  164. }
  165. }
  166. // Row pin configuration
  167. static void unselect_rows(void) {
  168. // no need to unselect on mcp23018, because the select step sets all
  169. // the other row bits high, and it's not changing to a different direction
  170. for (uint8_t row = 0; row < MATRIX_ROWS_PER_SIDE; row++) {
  171. gpio_set_pin_input(row_pins[row]);
  172. gpio_write_pin_low(row_pins[row]);
  173. }
  174. }
  175. static void select_row(uint8_t row) {
  176. if (row < 5) {
  177. // select on mcp23018
  178. if (mcp23018_status) { // do nothing on error
  179. } else { // set active row low : 0 // set other rows hi-Z : 1
  180. uint8_t data;
  181. data = 0xFF & ~(1 << (row + 1));
  182. mcp23018_status = i2c_write_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
  183. }
  184. } else {
  185. gpio_set_pin_output(row_pins[row - MATRIX_ROWS_PER_SIDE]);
  186. gpio_write_pin_low(row_pins[row - MATRIX_ROWS_PER_SIDE]);
  187. }
  188. }