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.

303 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. // if(serial_slave_DATA_CORRUPT()){
  145. // TXLED0;
  146. int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
  147. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  148. matrix[offset+i] = serial_master_buffer[i];
  149. }
  150. // }else{
  151. // TXLED1;
  152. // }
  153. matrix_scan_quantum();
  154. }
  155. return 1;
  156. }
  157. uint8_t matrix_master_scan(void) {
  158. int ret = _matrix_scan();
  159. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  160. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  161. serial_master_buffer[i] = matrix[offset+i];
  162. }
  163. if( serial_transaction() ) {
  164. // turn on the indicator led when halves are disconnected
  165. TXLED1;
  166. error_count++;
  167. if (error_count > ERROR_DISCONNECT_COUNT) {
  168. // reset other half if disconnected
  169. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  170. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  171. matrix[slaveOffset+i] = 0;
  172. }
  173. }
  174. } else {
  175. // turn off the indicator led on no error
  176. TXLED0;
  177. error_count = 0;
  178. }
  179. matrix_scan_quantum();
  180. return ret;
  181. }
  182. void matrix_slave_scan(void) {
  183. _matrix_scan();
  184. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  185. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  186. serial_slave_buffer[i] = matrix[offset+i];
  187. }
  188. }
  189. bool matrix_is_modified(void)
  190. {
  191. if (debouncing) return false;
  192. return true;
  193. }
  194. inline
  195. bool matrix_is_on(uint8_t row, uint8_t col)
  196. {
  197. return (matrix[row] & ((matrix_row_t)1<<col));
  198. }
  199. inline
  200. matrix_row_t matrix_get_row(uint8_t row)
  201. {
  202. return matrix[row];
  203. }
  204. void matrix_print(void)
  205. {
  206. print("\nr/c 0123456789ABCDEF\n");
  207. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  208. phex(row); print(": ");
  209. pbin_reverse16(matrix_get_row(row));
  210. print("\n");
  211. }
  212. }
  213. uint8_t matrix_key_count(void)
  214. {
  215. uint8_t count = 0;
  216. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  217. count += bitpop16(matrix[i]);
  218. }
  219. return count;
  220. }
  221. static void init_cols(void)
  222. {
  223. for(int x = 0; x < MATRIX_COLS; x++) {
  224. _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
  225. _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
  226. }
  227. }
  228. static matrix_row_t read_cols(void)
  229. {
  230. matrix_row_t result = 0;
  231. for(int x = 0; x < MATRIX_COLS; x++) {
  232. result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
  233. }
  234. return result;
  235. }
  236. static void unselect_rows(void)
  237. {
  238. for(int x = 0; x < ROWS_PER_HAND; x++) {
  239. _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
  240. _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
  241. }
  242. }
  243. static void select_row(uint8_t row)
  244. {
  245. _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
  246. _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
  247. }