matrix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "split_util.h"
  26. #include "pro_micro.h"
  27. #include "config.h"
  28. #include "timer.h"
  29. #ifdef USE_I2C
  30. # include "i2c.h"
  31. #else // USE_SERIAL
  32. # include "serial.h"
  33. #endif
  34. #ifndef DEBOUNCING_DELAY
  35. # define DEBOUNCING_DELAY 5
  36. #endif
  37. #if (DEBOUNCING_DELAY > 0)
  38. static uint16_t debouncing_time;
  39. static bool debouncing = false;
  40. #endif
  41. #if (MATRIX_COLS <= 8)
  42. # define print_matrix_header() print("\nr/c 01234567\n")
  43. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  44. # define matrix_bitpop(i) bitpop(matrix[i])
  45. # define ROW_SHIFTER ((uint8_t)1)
  46. #else
  47. # error "Currently only supports 8 COLS"
  48. #endif
  49. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  50. #define ERROR_DISCONNECT_COUNT 5
  51. #define ROWS_PER_HAND (MATRIX_ROWS/2)
  52. static uint8_t error_count = 0;
  53. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  54. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  55. /* matrix state(1:on, 0:off) */
  56. static matrix_row_t matrix[MATRIX_ROWS];
  57. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  58. #if (DIODE_DIRECTION == COL2ROW)
  59. static void init_cols(void);
  60. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  61. static void unselect_rows(void);
  62. static void select_row(uint8_t row);
  63. static void unselect_row(uint8_t row);
  64. #elif (DIODE_DIRECTION == ROW2COL)
  65. static void init_rows(void);
  66. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  67. static void unselect_cols(void);
  68. static void unselect_col(uint8_t col);
  69. static void select_col(uint8_t col);
  70. #endif
  71. __attribute__ ((weak))
  72. void matrix_init_kb(void) {
  73. matrix_init_user();
  74. }
  75. __attribute__ ((weak))
  76. void matrix_scan_kb(void) {
  77. matrix_scan_user();
  78. }
  79. __attribute__ ((weak))
  80. void matrix_init_user(void) {
  81. }
  82. __attribute__ ((weak))
  83. void matrix_scan_user(void) {
  84. }
  85. inline
  86. uint8_t matrix_rows(void)
  87. {
  88. return MATRIX_ROWS;
  89. }
  90. inline
  91. uint8_t matrix_cols(void)
  92. {
  93. return MATRIX_COLS;
  94. }
  95. void matrix_init(void)
  96. {
  97. debug_enable = true;
  98. debug_matrix = true;
  99. debug_mouse = true;
  100. // initialize row and col
  101. unselect_rows();
  102. init_cols();
  103. TX_RX_LED_INIT;
  104. // initialize matrix state: all keys off
  105. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  106. matrix[i] = 0;
  107. matrix_debouncing[i] = 0;
  108. }
  109. matrix_init_quantum();
  110. }
  111. uint8_t _matrix_scan(void)
  112. {
  113. int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
  114. #if (DIODE_DIRECTION == COL2ROW)
  115. // Set row, read cols
  116. for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
  117. # if (DEBOUNCING_DELAY > 0)
  118. bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
  119. if (matrix_changed) {
  120. debouncing = true;
  121. debouncing_time = timer_read();
  122. PORTD ^= (1 << 2);
  123. }
  124. # else
  125. read_cols_on_row(matrix+offset, current_row);
  126. # endif
  127. }
  128. #elif (DIODE_DIRECTION == ROW2COL)
  129. // Set col, read rows
  130. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  131. # if (DEBOUNCING_DELAY > 0)
  132. bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
  133. if (matrix_changed) {
  134. debouncing = true;
  135. debouncing_time = timer_read();
  136. }
  137. # else
  138. read_rows_on_col(matrix+offset, current_col);
  139. # endif
  140. }
  141. #endif
  142. # if (DEBOUNCING_DELAY > 0)
  143. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  144. for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
  145. matrix[i+offset] = matrix_debouncing[i+offset];
  146. }
  147. debouncing = false;
  148. }
  149. # endif
  150. return 1;
  151. }
  152. #ifdef USE_I2C
  153. // Get rows from other half over i2c
  154. int i2c_transaction(void) {
  155. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  156. int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
  157. if (err) goto i2c_error;
  158. // start of matrix stored at 0x00
  159. err = i2c_master_write(0x00);
  160. if (err) goto i2c_error;
  161. // Start read
  162. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  163. if (err) goto i2c_error;
  164. if (!err) {
  165. int i;
  166. for (i = 0; i < ROWS_PER_HAND-1; ++i) {
  167. matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
  168. }
  169. matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
  170. i2c_master_stop();
  171. } else {
  172. i2c_error: // the cable is disconnceted, or something else went wrong
  173. i2c_reset_state();
  174. return err;
  175. }
  176. return 0;
  177. }
  178. #else // USE_SERIAL
  179. int serial_transaction(void) {
  180. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  181. if (serial_update_buffers()) {
  182. return 1;
  183. }
  184. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  185. matrix[slaveOffset+i] = serial_slave_buffer[i];
  186. }
  187. return 0;
  188. }
  189. #endif
  190. uint8_t matrix_scan(void)
  191. {
  192. uint8_t ret = _matrix_scan();
  193. #ifdef USE_I2C
  194. if( i2c_transaction() ) {
  195. #else // USE_SERIAL
  196. if( serial_transaction() ) {
  197. #endif
  198. // turn on the indicator led when halves are disconnected
  199. TXLED1;
  200. error_count++;
  201. if (error_count > ERROR_DISCONNECT_COUNT) {
  202. // reset other half if disconnected
  203. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  204. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  205. matrix[slaveOffset+i] = 0;
  206. }
  207. }
  208. } else {
  209. // turn off the indicator led on no error
  210. TXLED0;
  211. error_count = 0;
  212. }
  213. matrix_scan_quantum();
  214. return ret;
  215. }
  216. void matrix_slave_scan(void) {
  217. _matrix_scan();
  218. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  219. #ifdef USE_I2C
  220. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  221. i2c_slave_buffer[i] = matrix[offset+i];
  222. }
  223. #else // USE_SERIAL
  224. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  225. serial_slave_buffer[i] = matrix[offset+i];
  226. }
  227. #endif
  228. }
  229. bool matrix_is_modified(void)
  230. {
  231. if (debouncing) return false;
  232. return true;
  233. }
  234. inline
  235. bool matrix_is_on(uint8_t row, uint8_t col)
  236. {
  237. return (matrix[row] & ((matrix_row_t)1<<col));
  238. }
  239. inline
  240. matrix_row_t matrix_get_row(uint8_t row)
  241. {
  242. return matrix[row];
  243. }
  244. void matrix_print(void)
  245. {
  246. print("\nr/c 0123456789ABCDEF\n");
  247. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  248. phex(row); print(": ");
  249. pbin_reverse16(matrix_get_row(row));
  250. print("\n");
  251. }
  252. }
  253. uint8_t matrix_key_count(void)
  254. {
  255. uint8_t count = 0;
  256. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  257. count += bitpop16(matrix[i]);
  258. }
  259. return count;
  260. }
  261. #if (DIODE_DIRECTION == COL2ROW)
  262. static void init_cols(void)
  263. {
  264. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  265. uint8_t pin = col_pins[x];
  266. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  267. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  268. }
  269. }
  270. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  271. {
  272. // Store last value of row prior to reading
  273. matrix_row_t last_row_value = current_matrix[current_row];
  274. // Clear data in matrix row
  275. current_matrix[current_row] = 0;
  276. // Select row and wait for row selecton to stabilize
  277. select_row(current_row);
  278. wait_us(30);
  279. // For each col...
  280. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  281. // Select the col pin to read (active low)
  282. uint8_t pin = col_pins[col_index];
  283. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  284. // Populate the matrix row with the state of the col pin
  285. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  286. }
  287. // Unselect row
  288. unselect_row(current_row);
  289. return (last_row_value != current_matrix[current_row]);
  290. }
  291. static void select_row(uint8_t row)
  292. {
  293. uint8_t pin = row_pins[row];
  294. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  295. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  296. }
  297. static void unselect_row(uint8_t row)
  298. {
  299. uint8_t pin = row_pins[row];
  300. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  301. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  302. }
  303. static void unselect_rows(void)
  304. {
  305. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  306. uint8_t pin = row_pins[x];
  307. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  308. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  309. }
  310. }
  311. #elif (DIODE_DIRECTION == ROW2COL)
  312. static void init_rows(void)
  313. {
  314. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  315. uint8_t pin = row_pins[x];
  316. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  317. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  318. }
  319. }
  320. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  321. {
  322. bool matrix_changed = false;
  323. // Select col and wait for col selecton to stabilize
  324. select_col(current_col);
  325. wait_us(30);
  326. // For each row...
  327. for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
  328. {
  329. // Store last value of row prior to reading
  330. matrix_row_t last_row_value = current_matrix[row_index];
  331. // Check row pin state
  332. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  333. {
  334. // Pin LO, set col bit
  335. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  336. }
  337. else
  338. {
  339. // Pin HI, clear col bit
  340. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  341. }
  342. // Determine if the matrix changed state
  343. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  344. {
  345. matrix_changed = true;
  346. }
  347. }
  348. // Unselect col
  349. unselect_col(current_col);
  350. return matrix_changed;
  351. }
  352. static void select_col(uint8_t col)
  353. {
  354. uint8_t pin = col_pins[col];
  355. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  356. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  357. }
  358. static void unselect_col(uint8_t col)
  359. {
  360. uint8_t pin = col_pins[col];
  361. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  362. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  363. }
  364. static void unselect_cols(void)
  365. {
  366. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  367. uint8_t pin = col_pins[x];
  368. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  369. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  370. }
  371. }
  372. #endif