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.

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