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.

459 lines
11 KiB

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