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

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