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.

477 lines
12 KiB

  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. #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_quantum(void) {
  75. matrix_init_kb();
  76. }
  77. __attribute__ ((weak))
  78. void matrix_scan_quantum(void) {
  79. matrix_scan_kb();
  80. }
  81. __attribute__ ((weak))
  82. void matrix_init_kb(void) {
  83. matrix_init_user();
  84. }
  85. __attribute__ ((weak))
  86. void matrix_scan_kb(void) {
  87. matrix_scan_user();
  88. }
  89. __attribute__ ((weak))
  90. void matrix_init_user(void) {
  91. }
  92. __attribute__ ((weak))
  93. void matrix_scan_user(void) {
  94. }
  95. inline
  96. uint8_t matrix_rows(void)
  97. {
  98. return MATRIX_ROWS;
  99. }
  100. inline
  101. uint8_t matrix_cols(void)
  102. {
  103. return MATRIX_COLS;
  104. }
  105. void matrix_init(void)
  106. {
  107. debug_enable = true;
  108. debug_matrix = true;
  109. debug_mouse = true;
  110. // initialize row and col
  111. unselect_rows();
  112. init_cols();
  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. // Start read
  172. err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
  173. if (err) goto i2c_error;
  174. if (!err) {
  175. int i;
  176. for (i = 0; i < ROWS_PER_HAND-1; ++i) {
  177. matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
  178. }
  179. matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
  180. i2c_master_stop();
  181. } else {
  182. i2c_error: // the cable is disconnceted, or something else went wrong
  183. i2c_reset_state();
  184. return err;
  185. }
  186. return 0;
  187. }
  188. #else // USE_SERIAL
  189. int serial_transaction(void) {
  190. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  191. if (serial_update_buffers()) {
  192. return 1;
  193. }
  194. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  195. matrix[slaveOffset+i] = serial_slave_buffer[i];
  196. }
  197. #ifdef BACKLIGHT_ENABLE
  198. // Write backlight level for slave to read
  199. serial_master_buffer[SERIAL_LED_ADDR] = get_backlight_level();
  200. #endif
  201. return 0;
  202. }
  203. #endif
  204. uint8_t matrix_scan(void)
  205. {
  206. uint8_t ret = _matrix_scan();
  207. #ifdef USE_I2C
  208. if( i2c_transaction() ) {
  209. #else // USE_SERIAL
  210. if( serial_transaction() ) {
  211. #endif
  212. // turn on the indicator led when halves are disconnected
  213. TXLED1;
  214. error_count++;
  215. if (error_count > ERROR_DISCONNECT_COUNT) {
  216. // reset other half if disconnected
  217. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  218. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  219. matrix[slaveOffset+i] = 0;
  220. }
  221. }
  222. } else {
  223. // turn off the indicator led on no error
  224. TXLED0;
  225. error_count = 0;
  226. }
  227. matrix_scan_quantum();
  228. return ret;
  229. }
  230. void matrix_slave_scan(void) {
  231. _matrix_scan();
  232. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  233. #ifdef USE_I2C
  234. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  235. i2c_slave_buffer[i] = matrix[offset+i];
  236. }
  237. #else // USE_SERIAL
  238. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  239. serial_slave_buffer[i] = matrix[offset+i];
  240. }
  241. #ifdef BACKLIGHT_ENABLE
  242. // Read backlight level sent from master and update level on slave
  243. backlight_set(serial_master_buffer[SERIAL_LED_ADDR]);
  244. #endif
  245. #endif
  246. }
  247. bool matrix_is_modified(void)
  248. {
  249. if (debouncing) return false;
  250. return true;
  251. }
  252. inline
  253. bool matrix_is_on(uint8_t row, uint8_t col)
  254. {
  255. return (matrix[row] & ((matrix_row_t)1<<col));
  256. }
  257. inline
  258. matrix_row_t matrix_get_row(uint8_t row)
  259. {
  260. return matrix[row];
  261. }
  262. void matrix_print(void)
  263. {
  264. print("\nr/c 0123456789ABCDEF\n");
  265. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  266. phex(row); print(": ");
  267. pbin_reverse16(matrix_get_row(row));
  268. print("\n");
  269. }
  270. }
  271. uint8_t matrix_key_count(void)
  272. {
  273. uint8_t count = 0;
  274. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  275. count += bitpop16(matrix[i]);
  276. }
  277. return count;
  278. }
  279. #if (DIODE_DIRECTION == COL2ROW)
  280. static void init_cols(void)
  281. {
  282. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  283. uint8_t pin = col_pins[x];
  284. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  285. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  286. }
  287. }
  288. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  289. {
  290. // Store last value of row prior to reading
  291. matrix_row_t last_row_value = current_matrix[current_row];
  292. // Clear data in matrix row
  293. current_matrix[current_row] = 0;
  294. // Select row and wait for row selecton to stabilize
  295. select_row(current_row);
  296. wait_us(30);
  297. // For each col...
  298. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  299. // Select the col pin to read (active low)
  300. uint8_t pin = col_pins[col_index];
  301. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  302. // Populate the matrix row with the state of the col pin
  303. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  304. }
  305. // Unselect row
  306. unselect_row(current_row);
  307. return (last_row_value != current_matrix[current_row]);
  308. }
  309. static void select_row(uint8_t row)
  310. {
  311. uint8_t pin = row_pins[row];
  312. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  313. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  314. }
  315. static void unselect_row(uint8_t row)
  316. {
  317. uint8_t pin = row_pins[row];
  318. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  319. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  320. }
  321. static void unselect_rows(void)
  322. {
  323. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  324. uint8_t pin = row_pins[x];
  325. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  326. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  327. }
  328. }
  329. #elif (DIODE_DIRECTION == ROW2COL)
  330. static void init_rows(void)
  331. {
  332. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  333. uint8_t pin = row_pins[x];
  334. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  335. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  336. }
  337. }
  338. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  339. {
  340. bool matrix_changed = false;
  341. // Select col and wait for col selecton to stabilize
  342. select_col(current_col);
  343. wait_us(30);
  344. // For each row...
  345. for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
  346. {
  347. // Store last value of row prior to reading
  348. matrix_row_t last_row_value = current_matrix[row_index];
  349. // Check row pin state
  350. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  351. {
  352. // Pin LO, set col bit
  353. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  354. }
  355. else
  356. {
  357. // Pin HI, clear col bit
  358. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  359. }
  360. // Determine if the matrix changed state
  361. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  362. {
  363. matrix_changed = true;
  364. }
  365. }
  366. // Unselect col
  367. unselect_col(current_col);
  368. return matrix_changed;
  369. }
  370. static void select_col(uint8_t col)
  371. {
  372. uint8_t pin = col_pins[col];
  373. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  374. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  375. }
  376. static void unselect_col(uint8_t col)
  377. {
  378. uint8_t pin = col_pins[col];
  379. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  380. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  381. }
  382. static void unselect_cols(void)
  383. {
  384. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  385. uint8_t pin = col_pins[x];
  386. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  387. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  388. }
  389. }
  390. #endif