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.

matrix.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. inline
  35. uint8_t matrix_rows(void)
  36. {
  37. return kimera_matrix_rows();
  38. }
  39. inline
  40. uint8_t matrix_cols(void)
  41. {
  42. return kimera_matrix_cols();
  43. }
  44. void matrix_init(void)
  45. {
  46. // disable JTAG
  47. MCUCR = (1<<JTD);
  48. MCUCR = (1<<JTD);
  49. kimera_init();
  50. // initialize row and col
  51. kimera_unselect_rows();
  52. // initialize matrix state: all keys off
  53. for (uint8_t i=0; i < matrix_rows(); i++) {
  54. matrix[i] = 0;
  55. matrix_debouncing[i] = 0;
  56. }
  57. PORTD &= ~(1<<PD4);
  58. }
  59. uint8_t matrix_scan(void)
  60. {
  61. kimera_scan();
  62. for (uint8_t i = 0; i < matrix_rows(); i++) {
  63. kimera_select_row(i);
  64. _delay_us(30); // without this wait read unstable value.
  65. matrix_row_t cols = kimera_read_cols(i);
  66. if (matrix_debouncing[i] != cols) {
  67. matrix_debouncing[i] = cols;
  68. if (debouncing) {
  69. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  70. }
  71. debouncing = DEBOUNCE;
  72. }
  73. kimera_unselect_rows();
  74. }
  75. if (debouncing) {
  76. if (--debouncing) {
  77. _delay_ms(1);
  78. } else {
  79. for (uint8_t i = 0; i < matrix_rows(); i++) {
  80. matrix[i] = matrix_debouncing[i];
  81. }
  82. }
  83. }
  84. return 1;
  85. }
  86. bool matrix_is_modified(void)
  87. {
  88. if (debouncing) return false;
  89. return true;
  90. }
  91. inline
  92. bool matrix_is_on(uint8_t row, uint8_t col)
  93. {
  94. return (matrix[row] & ((matrix_row_t)1<<col));
  95. }
  96. inline
  97. matrix_row_t matrix_get_row(uint8_t row)
  98. {
  99. return matrix[row];
  100. }
  101. void matrix_print(void)
  102. {
  103. print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n");
  104. for (uint8_t row = 0; row < matrix_rows(); row++) {
  105. phex(row); print(": ");
  106. print_bin_reverse32(matrix_get_row(row));
  107. print("\n");
  108. }
  109. }
  110. uint8_t matrix_key_count(void)
  111. {
  112. uint8_t count = 0;
  113. for (uint8_t i = 0; i < matrix_rows(); i++) {
  114. count += bitpop32(matrix[i]);
  115. }
  116. return count;
  117. }