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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright 2015 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 "timer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "keymap_common.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 = 0;
  33. static matrix_row_t matrix_debouncing = 0;
  34. static matrix_row_t read_cols(void);
  35. static void init_cols(void);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. // initialize cols
  49. init_cols();
  50. }
  51. uint8_t matrix_scan(void)
  52. {
  53. matrix_row_t cols = read_cols();
  54. if (matrix_debouncing != cols) {
  55. matrix_debouncing = cols;
  56. if (debouncing) {
  57. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  58. }
  59. debouncing = DEBOUNCE;
  60. }
  61. if (debouncing) {
  62. if (--debouncing) {
  63. _delay_ms(1);
  64. } else {
  65. matrix = matrix_debouncing;
  66. }
  67. }
  68. return 1;
  69. }
  70. bool matrix_is_modified(void)
  71. {
  72. if (debouncing) return false;
  73. return true;
  74. }
  75. inline
  76. bool matrix_is_on(uint8_t row, uint8_t col)
  77. {
  78. return (matrix & ((matrix_row_t)1<<col));
  79. }
  80. inline
  81. matrix_row_t matrix_get_row(uint8_t row)
  82. {
  83. return matrix;
  84. }
  85. void matrix_print(void)
  86. {
  87. print("\nr/c 0123456789ABCDEF\n");
  88. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  89. phex(row); print(": ");
  90. pbin_reverse16(matrix_get_row(row));
  91. print("\n");
  92. }
  93. }
  94. uint8_t matrix_key_count(void)
  95. {
  96. return bitpop16(matrix);
  97. }
  98. /* Column pin configuration
  99. * col: 0 1 2 3 4
  100. * pin: D0 D1 D2 D3 D4
  101. */
  102. static void init_cols(void)
  103. {
  104. // Input with pull-up(DDR:0, PORT:1)
  105. DDRD &= ~(1<<PD0 | 1<<PD1 | 1<<PD2 | 1<<PD3 | 1<<PD4);
  106. PORTD |= (1<<PD0 | 1<<PD1 | 1<<PD2 | 1<<PD3 | 1<<PD4);
  107. }
  108. /* Column pin configuration
  109. * col: 0 1 2 3 4
  110. * pin: D0 D1 D2 D3 D4
  111. */
  112. static matrix_row_t read_cols(void)
  113. {
  114. return (~PIND) & 0b00011111;
  115. }