選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
このリポジトリはアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュや、課題・プルリクエストのオープンはできません。

matrix.c 3.0KB

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