matrix.c 12 KB

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