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.

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