matrix.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <avr/wdt.h>
  21. #include <avr/interrupt.h>
  22. #include <util/delay.h>
  23. #include "print.h"
  24. #include "debug.h"
  25. #include "util.h"
  26. #include "matrix.h"
  27. #include "i2c.h"
  28. #include "serial.h"
  29. #include "split_util.h"
  30. #include "pro_micro.h"
  31. #include "config.h"
  32. #ifndef DEBOUNCE
  33. # define DEBOUNCE 5
  34. #endif
  35. #define ERROR_DISCONNECT_COUNT 5
  36. static uint8_t debouncing = DEBOUNCE;
  37. static const int ROWS_PER_HAND = MATRIX_ROWS/2;
  38. static uint8_t error_count = 0;
  39. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  40. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  41. /* matrix state(1:on, 0:off) */
  42. static matrix_row_t matrix[MATRIX_ROWS];
  43. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  44. static matrix_row_t read_cols(void);
  45. static void init_cols(void);
  46. static void unselect_rows(void);
  47. static void select_row(uint8_t row);
  48. __attribute__ ((weak))
  49. void matrix_init_quantum(void) {
  50. matrix_init_kb();
  51. }
  52. __attribute__ ((weak))
  53. void matrix_scan_quantum(void) {
  54. matrix_scan_kb();
  55. }
  56. __attribute__ ((weak))
  57. void matrix_init_kb(void) {
  58. matrix_init_user();
  59. }
  60. __attribute__ ((weak))
  61. void matrix_scan_kb(void) {
  62. matrix_scan_user();
  63. }
  64. __attribute__ ((weak))
  65. void matrix_init_user(void) {
  66. }
  67. __attribute__ ((weak))
  68. void matrix_scan_user(void) {
  69. }
  70. inline
  71. uint8_t matrix_rows(void)
  72. {
  73. return MATRIX_ROWS;
  74. }
  75. inline
  76. uint8_t matrix_cols(void)
  77. {
  78. return MATRIX_COLS;
  79. }
  80. void matrix_init(void)
  81. {
  82. debug_enable = true;
  83. debug_matrix = true;
  84. debug_mouse = true;
  85. // initialize row and col
  86. unselect_rows();
  87. init_cols();
  88. TX_RX_LED_INIT;
  89. // initialize matrix state: all keys off
  90. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  91. matrix[i] = 0;
  92. matrix_debouncing[i] = 0;
  93. }
  94. matrix_init_quantum();
  95. }
  96. uint8_t _matrix_scan(void)
  97. {
  98. // Right hand is stored after the left in the matirx so, we need to offset it
  99. int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  100. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  101. select_row(i);
  102. _delay_us(30); // without this wait read unstable value.
  103. matrix_row_t cols = read_cols();
  104. if (matrix_debouncing[i+offset] != cols) {
  105. matrix_debouncing[i+offset] = cols;
  106. debouncing = DEBOUNCE;
  107. }
  108. unselect_rows();
  109. }
  110. if (debouncing) {
  111. if (--debouncing) {
  112. _delay_ms(1);
  113. } else {
  114. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  115. matrix[i+offset] = matrix_debouncing[i+offset];
  116. }
  117. }
  118. }
  119. return 1;
  120. }
  121. // Get rows from other half over i2c
  122. int i2c_transaction(void) {
  123. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  124. int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  125. if (err) goto i2c_error;
  126. // start of matrix stored at 0x00
  127. err = i2c_master_write(0x00);
  128. if (err) goto i2c_error;
  129. // Start read
  130. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  131. if (err) goto i2c_error;
  132. if (!err) {
  133. int i;
  134. for (i = 0; i < ROWS_PER_HAND-1; ++i) {
  135. matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
  136. }
  137. matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
  138. i2c_master_stop();
  139. } else {
  140. i2c_error: // the cable is disconnceted, or something else went wrong
  141. i2c_reset_state();
  142. return err;
  143. }
  144. return 0;
  145. }
  146. #ifndef USE_I2C
  147. int serial_transaction(void) {
  148. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  149. if (serial_update_buffers()) {
  150. return 1;
  151. }
  152. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  153. matrix[slaveOffset+i] = serial_slave_buffer[i];
  154. }
  155. return 0;
  156. }
  157. #endif
  158. uint8_t matrix_scan(void)
  159. {
  160. int ret = _matrix_scan();
  161. #ifdef USE_I2C
  162. if( i2c_transaction() ) {
  163. #else
  164. if( serial_transaction() ) {
  165. #endif
  166. // turn on the indicator led when halves are disconnected
  167. TXLED1;
  168. error_count++;
  169. if (error_count > ERROR_DISCONNECT_COUNT) {
  170. // reset other half if disconnected
  171. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  172. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  173. matrix[slaveOffset+i] = 0;
  174. }
  175. }
  176. } else {
  177. // turn off the indicator led on no error
  178. TXLED0;
  179. error_count = 0;
  180. }
  181. matrix_scan_quantum();
  182. return ret;
  183. }
  184. void matrix_slave_scan(void) {
  185. _matrix_scan();
  186. int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
  187. #ifdef USE_I2C
  188. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  189. /* i2c_slave_buffer[i] = matrix[offset+i]; */
  190. i2c_slave_buffer[i] = matrix[offset+i];
  191. }
  192. #else
  193. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  194. serial_slave_buffer[i] = matrix[offset+i];
  195. }
  196. #endif
  197. }
  198. bool matrix_is_modified(void)
  199. {
  200. if (debouncing) return false;
  201. return true;
  202. }
  203. inline
  204. bool matrix_is_on(uint8_t row, uint8_t col)
  205. {
  206. return (matrix[row] & ((matrix_row_t)1<<col));
  207. }
  208. inline
  209. matrix_row_t matrix_get_row(uint8_t row)
  210. {
  211. return matrix[row];
  212. }
  213. void matrix_print(void)
  214. {
  215. print("\nr/c 0123456789ABCDEF\n");
  216. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  217. phex(row); print(": ");
  218. pbin_reverse16(matrix_get_row(row));
  219. print("\n");
  220. }
  221. }
  222. uint8_t matrix_key_count(void)
  223. {
  224. uint8_t count = 0;
  225. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  226. count += bitpop16(matrix[i]);
  227. }
  228. return count;
  229. }
  230. static void init_cols(void)
  231. {
  232. for(int x = 0; x < MATRIX_COLS; x++) {
  233. _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
  234. _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
  235. }
  236. }
  237. static matrix_row_t read_cols(void)
  238. {
  239. matrix_row_t result = 0;
  240. for(int x = 0; x < MATRIX_COLS; x++) {
  241. result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
  242. }
  243. return result;
  244. }
  245. static void unselect_rows(void)
  246. {
  247. for(int x = 0; x < ROWS_PER_HAND; x++) {
  248. _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
  249. _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
  250. }
  251. }
  252. static void select_row(uint8_t row)
  253. {
  254. _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
  255. _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
  256. }