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.

423 lines
10 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. #ifdef BACKLIGHT_ENABLE
  30. #include "backlight.h"
  31. extern backlight_config_t backlight_config;
  32. #endif
  33. #include "serial.h"
  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 SERIAL_LED_ADDR 0x00
  52. #define ROWS_PER_HAND (MATRIX_ROWS/2)
  53. static uint8_t error_count = 0;
  54. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  55. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  56. /* matrix state(1:on, 0:off) */
  57. static matrix_row_t matrix[MATRIX_ROWS];
  58. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  59. #if (DIODE_DIRECTION == COL2ROW)
  60. static void init_cols(void);
  61. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  62. static void unselect_rows(void);
  63. static void select_row(uint8_t row);
  64. static void unselect_row(uint8_t row);
  65. #elif (DIODE_DIRECTION == ROW2COL)
  66. static void init_rows(void);
  67. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  68. static void unselect_cols(void);
  69. static void unselect_col(uint8_t col);
  70. static void select_col(uint8_t col);
  71. #endif
  72. __attribute__ ((weak))
  73. void matrix_init_kb(void) {
  74. matrix_init_user();
  75. }
  76. __attribute__ ((weak))
  77. void matrix_scan_kb(void) {
  78. matrix_scan_user();
  79. }
  80. __attribute__ ((weak))
  81. void matrix_init_user(void) {
  82. }
  83. __attribute__ ((weak))
  84. void matrix_scan_user(void) {
  85. }
  86. inline
  87. uint8_t matrix_rows(void)
  88. {
  89. return MATRIX_ROWS;
  90. }
  91. inline
  92. uint8_t matrix_cols(void)
  93. {
  94. return MATRIX_COLS;
  95. }
  96. void matrix_init(void)
  97. {
  98. debug_enable = true;
  99. debug_matrix = true;
  100. debug_mouse = true;
  101. // initialize row and col
  102. unselect_rows();
  103. init_cols();
  104. TX_RX_LED_INIT;
  105. // initialize matrix state: all keys off
  106. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  107. matrix[i] = 0;
  108. matrix_debouncing[i] = 0;
  109. }
  110. matrix_init_quantum();
  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. int serial_transaction(void) {
  153. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  154. if (serial_update_buffers()) {
  155. return 1;
  156. }
  157. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  158. matrix[slaveOffset+i] = serial_slave_buffer[i];
  159. }
  160. #ifdef BACKLIGHT_ENABLE
  161. // Write backlight level for slave to read
  162. serial_master_buffer[SERIAL_LED_ADDR] = backlight_config.enable ? backlight_config.level : 0;
  163. #endif
  164. return 0;
  165. }
  166. uint8_t matrix_scan(void)
  167. {
  168. uint8_t ret = _matrix_scan();
  169. if( serial_transaction() ) {
  170. // turn on the indicator led when halves are disconnected
  171. TXLED1;
  172. error_count++;
  173. if (error_count > ERROR_DISCONNECT_COUNT) {
  174. // reset other half if disconnected
  175. int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
  176. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  177. matrix[slaveOffset+i] = 0;
  178. }
  179. }
  180. } else {
  181. // turn off the indicator led on no error
  182. TXLED0;
  183. error_count = 0;
  184. }
  185. matrix_scan_quantum();
  186. return ret;
  187. }
  188. void matrix_slave_scan(void) {
  189. _matrix_scan();
  190. int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
  191. for (int i = 0; i < ROWS_PER_HAND; ++i) {
  192. serial_slave_buffer[i] = matrix[offset+i];
  193. }
  194. #ifdef BACKLIGHT_ENABLE
  195. // Read backlight level sent from master and update level on slave
  196. backlight_set(serial_master_buffer[SERIAL_LED_ADDR]);
  197. #endif
  198. }
  199. bool matrix_is_modified(void)
  200. {
  201. if (debouncing) return false;
  202. return true;
  203. }
  204. inline
  205. bool matrix_is_on(uint8_t row, uint8_t col)
  206. {
  207. return (matrix[row] & ((matrix_row_t)1<<col));
  208. }
  209. inline
  210. matrix_row_t matrix_get_row(uint8_t row)
  211. {
  212. return matrix[row];
  213. }
  214. void matrix_print(void)
  215. {
  216. print("\nr/c 0123456789ABCDEF\n");
  217. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  218. phex(row); print(": ");
  219. pbin_reverse16(matrix_get_row(row));
  220. print("\n");
  221. }
  222. }
  223. uint8_t matrix_key_count(void)
  224. {
  225. uint8_t count = 0;
  226. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  227. count += bitpop16(matrix[i]);
  228. }
  229. return count;
  230. }
  231. #if (DIODE_DIRECTION == COL2ROW)
  232. static void init_cols(void)
  233. {
  234. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  235. uint8_t pin = col_pins[x];
  236. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  237. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  238. }
  239. }
  240. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  241. {
  242. // Store last value of row prior to reading
  243. matrix_row_t last_row_value = current_matrix[current_row];
  244. // Clear data in matrix row
  245. current_matrix[current_row] = 0;
  246. // Select row and wait for row selecton to stabilize
  247. select_row(current_row);
  248. wait_us(30);
  249. // For each col...
  250. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  251. // Select the col pin to read (active low)
  252. uint8_t pin = col_pins[col_index];
  253. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  254. // Populate the matrix row with the state of the col pin
  255. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  256. }
  257. // Unselect row
  258. unselect_row(current_row);
  259. return (last_row_value != current_matrix[current_row]);
  260. }
  261. static void select_row(uint8_t row)
  262. {
  263. uint8_t pin = row_pins[row];
  264. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  265. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  266. }
  267. static void unselect_row(uint8_t row)
  268. {
  269. uint8_t pin = row_pins[row];
  270. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  271. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  272. }
  273. static void unselect_rows(void)
  274. {
  275. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  276. uint8_t pin = row_pins[x];
  277. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  278. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  279. }
  280. }
  281. #elif (DIODE_DIRECTION == ROW2COL)
  282. static void init_rows(void)
  283. {
  284. for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
  285. uint8_t pin = row_pins[x];
  286. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  287. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  288. }
  289. }
  290. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  291. {
  292. bool matrix_changed = false;
  293. // Select col and wait for col selecton to stabilize
  294. select_col(current_col);
  295. wait_us(30);
  296. // For each row...
  297. for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
  298. {
  299. // Store last value of row prior to reading
  300. matrix_row_t last_row_value = current_matrix[row_index];
  301. // Check row pin state
  302. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  303. {
  304. // Pin LO, set col bit
  305. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  306. }
  307. else
  308. {
  309. // Pin HI, clear col bit
  310. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  311. }
  312. // Determine if the matrix changed state
  313. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  314. {
  315. matrix_changed = true;
  316. }
  317. }
  318. // Unselect col
  319. unselect_col(current_col);
  320. return matrix_changed;
  321. }
  322. static void select_col(uint8_t col)
  323. {
  324. uint8_t pin = col_pins[col];
  325. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  326. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  327. }
  328. static void unselect_col(uint8_t col)
  329. {
  330. uint8_t pin = col_pins[col];
  331. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  332. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  333. }
  334. static void unselect_cols(void)
  335. {
  336. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  337. uint8_t pin = col_pins[x];
  338. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  339. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  340. }
  341. }
  342. #endif