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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #ifndef DEBOUNCE
  27. # define DEBOUNCE 5
  28. #endif
  29. static uint8_t debouncing = DEBOUNCE;
  30. /* matrix state(1:on, 0:off) */
  31. static matrix_row_t matrix[MATRIX_ROWS];
  32. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  33. inline
  34. uint8_t matrix_rows(void)
  35. {
  36. return MATRIX_ROWS;
  37. }
  38. inline
  39. uint8_t matrix_cols(void)
  40. {
  41. return MATRIX_COLS;
  42. }
  43. void matrix_init(void)
  44. {
  45. // disable JTAG
  46. MCUCR = (1<<JTD);
  47. MCUCR = (1<<JTD);
  48. kimera_init();
  49. // initialize row and col
  50. unselect_rows();
  51. init_cols();
  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. }
  58. uint8_t matrix_scan(void)
  59. {
  60. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  61. select_row(i);
  62. _delay_us(30); // without this wait read unstable value.
  63. matrix_row_t cols = read_cols();
  64. if (matrix_debouncing[i] != cols) {
  65. matrix_debouncing[i] = cols;
  66. if (debouncing) {
  67. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  68. }
  69. debouncing = DEBOUNCE;
  70. }
  71. unselect_rows();
  72. }
  73. if (debouncing) {
  74. if (--debouncing) {
  75. _delay_ms(1);
  76. } else {
  77. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  78. matrix[i] = matrix_debouncing[i];
  79. }
  80. }
  81. }
  82. return 1;
  83. }
  84. bool matrix_is_modified(void)
  85. {
  86. if (debouncing) return false;
  87. return true;
  88. }
  89. inline
  90. bool matrix_is_on(uint8_t row, uint8_t col)
  91. {
  92. return (matrix[row] & ((matrix_row_t)1<<col));
  93. }
  94. inline
  95. matrix_row_t matrix_get_row(uint8_t row)
  96. {
  97. return matrix[row];
  98. }
  99. void matrix_print(void)
  100. {
  101. print("\nr/c 0123456789ABCDEF\n");
  102. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  103. phex(row); print(": ");
  104. pbin_reverse16(matrix_get_row(row));
  105. print("\n");
  106. }
  107. }
  108. uint8_t matrix_key_count(void)
  109. {
  110. uint8_t count = 0;
  111. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  112. count += bitpop16(matrix[i]);
  113. }
  114. return count;
  115. }