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.

464 lines
11 KiB

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