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.

317 lines
7.0 KiB

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