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.

378 lines
9.5 KiB

  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #include <avr/wdt.h>
  20. #include <avr/interrupt.h>
  21. #include <util/delay.h>
  22. #endif
  23. #include "wait.h"
  24. #include "print.h"
  25. #include "debug.h"
  26. #include "util.h"
  27. #include "matrix.h"
  28. #include "timer.h"
  29. #include "i2c_slave.h"
  30. #include "lufa.h"
  31. #include "quantum.h"
  32. #define SLAVE_I2C_ADDRESS 0x32
  33. /* Set 0 if debouncing isn't needed */
  34. #ifndef DEBOUNCE
  35. # define DEBOUNCE 5
  36. #endif
  37. #if (DEBOUNCE > 0)
  38. static uint16_t debouncing_time;
  39. static bool debouncing = false;
  40. #endif
  41. #if (MATRIX_COLS <= 8)
  42. # define print_matrix_header() print("\nr/c 01234567\n")
  43. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  44. # define ROW_SHIFTER ((uint8_t)1)
  45. #elif (MATRIX_COLS <= 16)
  46. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  47. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  48. # define ROW_SHIFTER ((uint16_t)1)
  49. #elif (MATRIX_COLS <= 32)
  50. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  51. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  52. # define ROW_SHIFTER ((uint32_t)1)
  53. #endif
  54. #ifdef MATRIX_MASKED
  55. extern const matrix_row_t matrix_mask[];
  56. #endif
  57. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  58. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  59. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  60. #endif
  61. /* matrix state(1:on, 0:off) */
  62. static matrix_row_t matrix[MATRIX_ROWS];
  63. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  64. #if (DIODE_DIRECTION == COL2ROW)
  65. static void init_cols(void);
  66. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  67. static void unselect_rows(void);
  68. static void select_row(uint8_t row);
  69. static void unselect_row(uint8_t row);
  70. #elif (DIODE_DIRECTION == ROW2COL)
  71. static void init_rows(void);
  72. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  73. static void unselect_cols(void);
  74. static void unselect_col(uint8_t col);
  75. static void select_col(uint8_t col);
  76. #endif
  77. __attribute__ ((weak))
  78. void matrix_init_quantum(void) {
  79. matrix_init_kb();
  80. }
  81. __attribute__ ((weak))
  82. void matrix_scan_quantum(void) {
  83. matrix_scan_kb();
  84. }
  85. __attribute__ ((weak))
  86. void matrix_init_kb(void) {
  87. matrix_init_user();
  88. }
  89. __attribute__ ((weak))
  90. void matrix_scan_kb(void) {
  91. matrix_scan_user();
  92. }
  93. __attribute__ ((weak))
  94. void matrix_init_user(void) {
  95. }
  96. __attribute__ ((weak))
  97. void matrix_scan_user(void) {
  98. }
  99. inline
  100. uint8_t matrix_rows(void) {
  101. return MATRIX_ROWS;
  102. }
  103. inline
  104. uint8_t matrix_cols(void) {
  105. return MATRIX_COLS;
  106. }
  107. void matrix_init(void) {
  108. // initialize row and col
  109. #if (DIODE_DIRECTION == COL2ROW)
  110. unselect_rows();
  111. init_cols();
  112. #elif (DIODE_DIRECTION == ROW2COL)
  113. unselect_cols();
  114. init_rows();
  115. #endif
  116. // initialize matrix state: all keys off
  117. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  118. matrix[i] = 0;
  119. matrix_debouncing[i] = 0;
  120. }
  121. matrix_init_quantum();
  122. }
  123. uint8_t matrix_scan(void)
  124. {
  125. #if (DIODE_DIRECTION == COL2ROW)
  126. // Set row, read cols
  127. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  128. # if (DEBOUNCE > 0)
  129. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  130. if (matrix_changed) {
  131. debouncing = true;
  132. debouncing_time = timer_read();
  133. }
  134. # else
  135. read_cols_on_row(matrix, current_row);
  136. # endif
  137. }
  138. #elif (DIODE_DIRECTION == ROW2COL)
  139. // Set col, read rows
  140. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  141. # if (DEBOUNCE > 0)
  142. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  143. if (matrix_changed) {
  144. debouncing = true;
  145. debouncing_time = timer_read();
  146. }
  147. # else
  148. read_rows_on_col(matrix, current_col);
  149. # endif
  150. }
  151. #endif
  152. # if (DEBOUNCE > 0)
  153. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  154. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  155. matrix[i] = matrix_debouncing[i];
  156. }
  157. debouncing = false;
  158. }
  159. # endif
  160. i2c_slave_reg[1] = 0x55;
  161. for (uint8_t i = 0; i < MATRIX_ROWS; i++){
  162. i2c_slave_reg[i+2] = matrix[i]; //send matrix over i2c
  163. }
  164. matrix_scan_quantum();
  165. return 1;
  166. }
  167. inline
  168. bool matrix_is_on(uint8_t row, uint8_t col)
  169. {
  170. return (matrix[row] & ((matrix_row_t)1<<col));
  171. }
  172. inline
  173. matrix_row_t matrix_get_row(uint8_t row)
  174. {
  175. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  176. // switch blocker installed and the switch is always pressed.
  177. #ifdef MATRIX_MASKED
  178. return matrix[row] & matrix_mask[row];
  179. #else
  180. return matrix[row];
  181. #endif
  182. }
  183. void matrix_print(void)
  184. {
  185. print_matrix_header();
  186. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  187. print_hex8(row); print(": ");
  188. print_matrix_row(row);
  189. print("\n");
  190. }
  191. }
  192. #if (DIODE_DIRECTION == COL2ROW)
  193. static void init_cols(void)
  194. {
  195. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  196. uint8_t pin = col_pins[x];
  197. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  198. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  199. }
  200. }
  201. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  202. {
  203. // Store last value of row prior to reading
  204. matrix_row_t last_row_value = current_matrix[current_row];
  205. // Clear data in matrix row
  206. current_matrix[current_row] = 0;
  207. // Select row and wait for row selecton to stabilize
  208. select_row(current_row);
  209. wait_us(30);
  210. // For each col...
  211. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  212. // Select the col pin to read (active low)
  213. uint8_t pin = col_pins[col_index];
  214. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  215. // Populate the matrix row with the state of the col pin
  216. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  217. }
  218. // Unselect row
  219. unselect_row(current_row);
  220. return (last_row_value != current_matrix[current_row]);
  221. }
  222. static void select_row(uint8_t row)
  223. {
  224. uint8_t pin = row_pins[row];
  225. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  226. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  227. }
  228. static void unselect_row(uint8_t row)
  229. {
  230. uint8_t pin = row_pins[row];
  231. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  232. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  233. }
  234. static void unselect_rows(void)
  235. {
  236. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  237. uint8_t pin = row_pins[x];
  238. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  239. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  240. }
  241. }
  242. #elif (DIODE_DIRECTION == ROW2COL)
  243. static void init_rows(void)
  244. {
  245. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  246. uint8_t pin = row_pins[x];
  247. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  248. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  249. }
  250. }
  251. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  252. {
  253. bool matrix_changed = false;
  254. // Select col and wait for col selecton to stabilize
  255. select_col(current_col);
  256. wait_us(30);
  257. // For each row...
  258. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  259. {
  260. // Store last value of row prior to reading
  261. matrix_row_t last_row_value = current_matrix[row_index];
  262. // Check row pin state
  263. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  264. {
  265. // Pin LO, set col bit
  266. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  267. }
  268. else
  269. {
  270. // Pin HI, clear col bit
  271. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  272. }
  273. // Determine if the matrix changed state
  274. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  275. {
  276. matrix_changed = true;
  277. }
  278. }
  279. // Unselect col
  280. unselect_col(current_col);
  281. return matrix_changed;
  282. }
  283. static void select_col(uint8_t col)
  284. {
  285. uint8_t pin = col_pins[col];
  286. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  287. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  288. }
  289. static void unselect_col(uint8_t col)
  290. {
  291. uint8_t pin = col_pins[col];
  292. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  293. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  294. }
  295. static void unselect_cols(void)
  296. {
  297. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  298. uint8_t pin = col_pins[x];
  299. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  300. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  301. }
  302. }
  303. #endif
  304. //this replases tmk code
  305. void matrix_setup(void){
  306. i2c_slave_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c
  307. sei(); //enable interupts
  308. }