matrix.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. Copyright 2012-2017 Jun Wako, Jack Humbert
  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 <stdint.h>
  15. #include <stdbool.h>
  16. #if defined(__AVR__)
  17. #include <avr/io.h>
  18. #endif
  19. #include "wait.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #include "timer.h"
  25. #include "sx60.h"
  26. /* Set 0 if debouncing isn't needed */
  27. #ifndef DEBOUNCE
  28. # define DEBOUNCE 5
  29. #endif
  30. #if (DEBOUNCE > 0)
  31. static uint16_t debouncing_time;
  32. static bool debouncing = false;
  33. #endif
  34. #if (MATRIX_COLS <= 8)
  35. # define print_matrix_header() print("\nr/c 01234567\n")
  36. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  37. #elif (MATRIX_COLS <= 16)
  38. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  39. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  40. #elif (MATRIX_COLS <= 32)
  41. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  42. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  43. #endif
  44. #ifdef MATRIX_MASKED
  45. extern const matrix_row_t matrix_mask[];
  46. #endif
  47. static const uint8_t col_pins[ATMEGA_COLS] = MATRIX_COL_PINS;
  48. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  49. /* matrix state(1:on, 0:off) */
  50. static matrix_row_t matrix[MATRIX_ROWS];
  51. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  52. static uint8_t mcp23018_reset_loop;
  53. static void init_cols(void);
  54. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  55. static void unselect_rows(void);
  56. static void select_row(uint8_t row);
  57. __attribute__ ((weak))
  58. void matrix_init_kb(void) {
  59. matrix_init_user();
  60. }
  61. __attribute__ ((weak))
  62. void matrix_scan_kb(void) {
  63. matrix_scan_user();
  64. }
  65. __attribute__ ((weak))
  66. void matrix_init_user(void) {
  67. }
  68. __attribute__ ((weak))
  69. void matrix_scan_user(void) {
  70. }
  71. inline
  72. uint8_t matrix_rows(void) {
  73. return MATRIX_ROWS;
  74. }
  75. inline
  76. uint8_t matrix_cols(void) {
  77. return MATRIX_COLS;
  78. }
  79. void matrix_init(void) {
  80. mcp23018_status = true;
  81. /* initialize row and col */
  82. unselect_rows();
  83. init_cols();
  84. /* initialize matrix state: all keys off */
  85. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = 0;
  87. matrix_debouncing[i] = 0;
  88. }
  89. matrix_init_kb();
  90. }
  91. uint8_t matrix_scan(void)
  92. {
  93. if (mcp23018_status) {
  94. /* if there was an error */
  95. if (++mcp23018_reset_loop == 0) {
  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. /* Set row, read cols */
  108. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  109. # if (DEBOUNCE > 0)
  110. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  111. if (matrix_changed) {
  112. debouncing = true;
  113. debouncing_time = timer_read();
  114. }
  115. # else
  116. read_cols_on_row(matrix, current_row);
  117. # endif
  118. }
  119. # if (DEBOUNCE > 0)
  120. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  121. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  122. matrix[i] = matrix_debouncing[i];
  123. }
  124. debouncing = false;
  125. }
  126. # endif
  127. matrix_scan_kb();
  128. return 1;
  129. }
  130. inline
  131. bool matrix_is_on(uint8_t row, uint8_t col)
  132. {
  133. return (matrix[row] & ((matrix_row_t)1<<col));
  134. }
  135. inline
  136. matrix_row_t matrix_get_row(uint8_t row)
  137. {
  138. /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  139. switch blocker installed and the switch is always pressed. */
  140. #ifdef MATRIX_MASKED
  141. return matrix[row] & matrix_mask[row];
  142. #else
  143. return matrix[row];
  144. #endif
  145. }
  146. void matrix_print(void)
  147. {
  148. print_matrix_header();
  149. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  150. print_hex8(row); print(": ");
  151. print_matrix_row(row);
  152. print("\n");
  153. }
  154. }
  155. static void init_cols(void)
  156. {
  157. for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
  158. uint8_t pin = col_pins[x];
  159. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  160. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  161. }
  162. }
  163. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  164. {
  165. /* Store last value of row prior to reading */
  166. matrix_row_t last_row_value = current_matrix[current_row];
  167. /* Clear data in matrix row */
  168. current_matrix[current_row] = 0;
  169. /* Select row and wait for row selecton to stabilize */
  170. select_row(current_row);
  171. wait_us(30);
  172. if (mcp23018_status) {
  173. /* if there was an error */
  174. return 0;
  175. } else {
  176. uint8_t data = 0;
  177. mcp23018_status = i2c_read_register(I2C_ADDR, GPIOA, &data, 1, I2C_TIMEOUT);
  178. if (!mcp23018_status) {
  179. current_matrix[current_row] |= (~((uint16_t)data) << 8);
  180. }
  181. }
  182. /* For each col... */
  183. for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
  184. /* Select the col pin to read (active low) */
  185. uint8_t pin = col_pins[col_index];
  186. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  187. /* Populate the matrix row with the state of the col pin */
  188. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  189. }
  190. /* Unselect row */
  191. unselect_rows();
  192. return (last_row_value != current_matrix[current_row]);
  193. }
  194. static void select_row(uint8_t row)
  195. {
  196. if (mcp23018_status) {
  197. /* if there was an error do nothing */
  198. } else {
  199. /* set active row low : 0
  200. set active row output : 1
  201. set other rows hi-Z : 1 */
  202. uint8_t port = 0xFF & ~(1<<abs(row-4));
  203. mcp23018_status = i2c_write_register(I2C_ADDR, GPIOB, &port, 1, I2C_TIMEOUT);
  204. }
  205. uint8_t pin = row_pins[row];
  206. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); /* OUT */
  207. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW */
  208. }
  209. static void unselect_rows(void)
  210. {
  211. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  212. uint8_t pin = row_pins[x];
  213. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  214. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  215. }
  216. }