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.

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