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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2014 Kai Ryu <[email protected]>
  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 <util/delay.h>
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "kimera.h"
  26. #include "keymap_in_eeprom.h"
  27. #ifndef DEBOUNCE
  28. # define DEBOUNCE 5
  29. #endif
  30. static uint8_t debouncing = DEBOUNCE;
  31. /* matrix state(1:on, 0:off) */
  32. static matrix_row_t matrix[MATRIX_ROWS];
  33. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  34. extern uint8_t row_count;
  35. extern uint8_t col_count;
  36. extern uint8_t data[EXP_COUNT][EXP_PORT_COUNT];
  37. inline
  38. uint8_t matrix_rows(void)
  39. {
  40. return row_count;
  41. }
  42. inline
  43. uint8_t matrix_cols(void)
  44. {
  45. #ifndef TWO_HEADED_KIMERA
  46. return col_count;
  47. #else
  48. return col_count / 2;
  49. #endif
  50. }
  51. void matrix_init(void)
  52. {
  53. // disable JTAG
  54. MCUCR = (1<<JTD);
  55. MCUCR = (1<<JTD);
  56. kimera_init();
  57. // initialize row and col
  58. unselect_rows();
  59. // initialize matrix state: all keys off
  60. for (uint8_t i=0; i < matrix_rows(); i++) {
  61. matrix[i] = 0;
  62. matrix_debouncing[i] = 0;
  63. }
  64. PORTD &= ~(1<<PD4);
  65. }
  66. uint8_t matrix_scan(void)
  67. {
  68. kimera_scan();
  69. for (uint8_t i = 0; i < matrix_rows(); i++) {
  70. select_row(i);
  71. _delay_us(30); // without this wait read unstable value.
  72. #ifndef TWO_HEADED_KIMERA
  73. matrix_row_t cols = read_cols();
  74. #else
  75. matrix_row_t cols = read_cols();
  76. if (i < 8) {
  77. cols &= 0xFFFFUL;
  78. }
  79. else {
  80. cols >>= 16;
  81. }
  82. #endif
  83. if (matrix_debouncing[i] != cols) {
  84. matrix_debouncing[i] = cols;
  85. if (debouncing) {
  86. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  87. }
  88. debouncing = DEBOUNCE;
  89. }
  90. unselect_rows();
  91. }
  92. if (debouncing) {
  93. if (--debouncing) {
  94. _delay_ms(1);
  95. } else {
  96. for (uint8_t i = 0; i < matrix_rows(); i++) {
  97. matrix[i] = matrix_debouncing[i];
  98. }
  99. }
  100. }
  101. return 1;
  102. }
  103. bool matrix_is_modified(void)
  104. {
  105. if (debouncing) return false;
  106. return true;
  107. }
  108. inline
  109. bool matrix_is_on(uint8_t row, uint8_t col)
  110. {
  111. return (matrix[row] & ((matrix_row_t)1<<col));
  112. }
  113. inline
  114. matrix_row_t matrix_get_row(uint8_t row)
  115. {
  116. return matrix[row];
  117. }
  118. void matrix_print(void)
  119. {
  120. print("\nr/c 0123456789ABCDEF\n");
  121. for (uint8_t row = 0; row < matrix_rows(); row++) {
  122. phex(row); print(": ");
  123. pbin_reverse16(matrix_get_row(row));
  124. print("\n");
  125. }
  126. }
  127. uint8_t matrix_key_count(void)
  128. {
  129. uint8_t count = 0;
  130. for (uint8_t i = 0; i < matrix_rows(); i++) {
  131. count += bitpop16(matrix[i]);
  132. }
  133. return count;
  134. }