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.

469 lines
11 KiB

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