matrix.c 11 KB

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