You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
8.0 KiB

  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. # define matrix_bitpop(i) bitpop(matrix[i])
  38. # define ROW_SHIFTER ((uint8_t)1)
  39. #elif (MATRIX_COLS <= 16)
  40. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  41. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  42. # define matrix_bitpop(i) bitpop16(matrix[i])
  43. # define ROW_SHIFTER ((uint16_t)1)
  44. #elif (MATRIX_COLS <= 32)
  45. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  46. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  47. # define matrix_bitpop(i) bitpop32(matrix[i])
  48. # define ROW_SHIFTER ((uint32_t)1)
  49. #endif
  50. #ifdef MATRIX_MASKED
  51. extern const matrix_row_t matrix_mask[];
  52. #endif
  53. static const uint8_t col_pins[ATMEGA_COLS] = MATRIX_COL_PINS;
  54. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  55. /* matrix state(1:on, 0:off) */
  56. static matrix_row_t matrix[MATRIX_ROWS];
  57. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  58. static uint8_t mcp23018_reset_loop;
  59. static void init_cols(void);
  60. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  61. static void unselect_rows(void);
  62. static void select_row(uint8_t row);
  63. __attribute__ ((weak))
  64. void matrix_init_quantum(void) {
  65. matrix_init_kb();
  66. }
  67. __attribute__ ((weak))
  68. void matrix_scan_quantum(void) {
  69. matrix_scan_kb();
  70. }
  71. __attribute__ ((weak))
  72. void matrix_init_kb(void) {
  73. matrix_init_user();
  74. }
  75. __attribute__ ((weak))
  76. void matrix_scan_kb(void) {
  77. matrix_scan_user();
  78. }
  79. __attribute__ ((weak))
  80. void matrix_init_user(void) {
  81. }
  82. __attribute__ ((weak))
  83. void matrix_scan_user(void) {
  84. }
  85. inline
  86. uint8_t matrix_rows(void) {
  87. return MATRIX_ROWS;
  88. }
  89. inline
  90. uint8_t matrix_cols(void) {
  91. return MATRIX_COLS;
  92. }
  93. void matrix_init(void) {
  94. mcp23018_status = true;
  95. /* initialize row and col */
  96. unselect_rows();
  97. init_cols();
  98. /* initialize matrix state: all keys off */
  99. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  100. matrix[i] = 0;
  101. matrix_debouncing[i] = 0;
  102. }
  103. matrix_init_quantum();
  104. }
  105. uint8_t matrix_scan(void)
  106. {
  107. if (mcp23018_status) {
  108. /* if there was an error */
  109. if (++mcp23018_reset_loop == 0) {
  110. /* since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
  111. this will be approx bit more frequent than once per second */
  112. print("trying to reset mcp23018\n");
  113. mcp23018_status = init_mcp23018();
  114. if (mcp23018_status) {
  115. print("left side not responding\n");
  116. } else {
  117. print("left side attached\n");
  118. }
  119. }
  120. }
  121. /* Set row, read cols */
  122. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  123. # if (DEBOUNCE > 0)
  124. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  125. if (matrix_changed) {
  126. debouncing = true;
  127. debouncing_time = timer_read();
  128. }
  129. # else
  130. read_cols_on_row(matrix, current_row);
  131. # endif
  132. }
  133. # if (DEBOUNCE > 0)
  134. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  135. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  136. matrix[i] = matrix_debouncing[i];
  137. }
  138. debouncing = false;
  139. }
  140. # endif
  141. matrix_scan_quantum();
  142. return 1;
  143. }
  144. bool matrix_is_modified(void)
  145. {
  146. #if (DEBOUNCE > 0)
  147. if (debouncing) return false;
  148. #endif
  149. return true;
  150. }
  151. inline
  152. bool matrix_is_on(uint8_t row, uint8_t col)
  153. {
  154. return (matrix[row] & ((matrix_row_t)1<<col));
  155. }
  156. inline
  157. matrix_row_t matrix_get_row(uint8_t row)
  158. {
  159. /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  160. switch blocker installed and the switch is always pressed. */
  161. #ifdef MATRIX_MASKED
  162. return matrix[row] & matrix_mask[row];
  163. #else
  164. return matrix[row];
  165. #endif
  166. }
  167. void matrix_print(void)
  168. {
  169. print_matrix_header();
  170. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  171. print_hex8(row); print(": ");
  172. print_matrix_row(row);
  173. print("\n");
  174. }
  175. }
  176. uint8_t matrix_key_count(void)
  177. {
  178. uint8_t count = 0;
  179. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  180. count += matrix_bitpop(i);
  181. }
  182. return count;
  183. }
  184. static void init_cols(void)
  185. {
  186. for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
  187. uint8_t pin = col_pins[x];
  188. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  189. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  190. }
  191. }
  192. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  193. {
  194. /* Store last value of row prior to reading */
  195. matrix_row_t last_row_value = current_matrix[current_row];
  196. /* Clear data in matrix row */
  197. current_matrix[current_row] = 0;
  198. /* Select row and wait for row selecton to stabilize */
  199. select_row(current_row);
  200. wait_us(30);
  201. if (mcp23018_status) {
  202. /* if there was an error */
  203. return 0;
  204. } else {
  205. uint16_t data = 0;
  206. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  207. mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
  208. mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
  209. data = i2c_readNak();
  210. data = ~data;
  211. out:
  212. i2c_stop();
  213. current_matrix[current_row] |= (data << 8);
  214. }
  215. /* For each col... */
  216. for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
  217. /* Select the col pin to read (active low) */
  218. uint8_t pin = col_pins[col_index];
  219. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  220. /* Populate the matrix row with the state of the col pin */
  221. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  222. }
  223. /* Unselect row */
  224. unselect_rows();
  225. return (last_row_value != current_matrix[current_row]);
  226. }
  227. static void select_row(uint8_t row)
  228. {
  229. if (mcp23018_status) {
  230. /* if there was an error do nothing */
  231. } else {
  232. /* set active row low : 0
  233. set active row output : 1
  234. set other rows hi-Z : 1 */
  235. mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
  236. mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
  237. mcp23018_status = i2c_write(0xFF & ~(1<<abs(row-4))); if (mcp23018_status) goto out;
  238. out:
  239. i2c_stop();
  240. }
  241. uint8_t pin = row_pins[row];
  242. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); /* OUT */
  243. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW */
  244. }
  245. static void unselect_rows(void)
  246. {
  247. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  248. uint8_t pin = row_pins[x];
  249. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
  250. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
  251. }
  252. }