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.

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