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.

308 lines
6.8 KiB

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