matrix.c 12 KB

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