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.

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