matrix.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Note for ErgoDox EZ customizers: Here be dragons!
  3. This is not a file you want to be messing with.
  4. All of the interesting stuff for you is under keymaps/ :)
  5. Love, Erez
  6. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * scan matrix
  20. */
  21. #include <stdint.h>
  22. #include <stdbool.h>
  23. #include <avr/io.h>
  24. #include <util/delay.h>
  25. #include "action_layer.h"
  26. #include "print.h"
  27. #include "debug.h"
  28. #include "util.h"
  29. #include "matrix.h"
  30. #include "nano.h"
  31. #include <string.h>
  32. /* matrix state(1:on, 0:off) */
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. static matrix_row_t matrix_stage[MATRIX_ROWS];
  35. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  36. static uint16_t debouncing_time;
  37. static bool debouncing = false;
  38. __attribute__ ((weak))
  39. void matrix_init_kb(void) {
  40. matrix_init_user();
  41. }
  42. __attribute__ ((weak))
  43. void matrix_scan_kb(void) {
  44. matrix_scan_user();
  45. }
  46. __attribute__ ((weak))
  47. void matrix_init_user(void) {
  48. }
  49. __attribute__ ((weak))
  50. void matrix_scan_user(void) {
  51. }
  52. inline
  53. uint8_t matrix_rows(void)
  54. {
  55. return MATRIX_ROWS;
  56. }
  57. inline
  58. uint8_t matrix_cols(void)
  59. {
  60. return MATRIX_COLS;
  61. }
  62. void matrix_init(void)
  63. {
  64. DDRF &= ~(1<<4 | 1<<5 | 1<<6 | 1<<7);
  65. PORTF |= (1<<4 | 1<<5 | 1<<6 | 1<<7);
  66. DDRC &= ~(1<<6);
  67. PORTC |= (1<<6);
  68. DDRD &= ~(1<<0 | 1<<1 | 1<<4);
  69. PORTD |= (1<<0 | 1<<1 | 1<<4);
  70. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  71. matrix[i] = 0;
  72. matrix_debouncing[i] = 0;
  73. matrix_stage[i] = 0;
  74. }
  75. matrix_init_quantum();
  76. }
  77. uint8_t matrix_scan(void)
  78. {
  79. matrix_stage[0] =
  80. (PINF&(1<<4) ? 0 : (1<<0)) |
  81. (PINF&(1<<5) ? 0 : (1<<1)) |
  82. (PINF&(1<<6) ? 0 : (1<<2)) |
  83. (PINF&(1<<7) ? 0 : (1<<3));
  84. matrix_stage[1] =
  85. (PIND&(1<<1) ? 0 : (1<<0)) |
  86. (PIND&(1<<0) ? 0 : (1<<1)) |
  87. (PIND&(1<<4) ? 0 : (1<<2)) |
  88. (PINC&(1<<6) ? 0 : (1<<3));
  89. if (memcmp(matrix_debouncing, matrix_stage, sizeof(matrix)) != 0) {
  90. debouncing = true;
  91. debouncing_time = timer_read();
  92. }
  93. matrix_debouncing[0] = matrix_stage[0];
  94. matrix_debouncing[1] = matrix_stage[1];
  95. if (debouncing && (timer_elapsed(debouncing_time) > 20)) {
  96. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  97. matrix[i] = matrix_debouncing[i];
  98. }
  99. debouncing = false;
  100. }
  101. matrix_scan_quantum();
  102. return 1;
  103. }
  104. bool matrix_is_modified(void)
  105. {
  106. return true;
  107. }
  108. inline
  109. bool matrix_is_on(uint8_t row, uint8_t col)
  110. {
  111. return (matrix[row] & ((matrix_row_t)1<<col));
  112. }
  113. inline
  114. matrix_row_t matrix_get_row(uint8_t row)
  115. {
  116. return matrix[row];
  117. }
  118. void matrix_print(void)
  119. {
  120. }
  121. uint8_t matrix_key_count(void)
  122. {
  123. uint8_t count = 0;
  124. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  125. count += bitpop16(matrix[i]);
  126. }
  127. return count;
  128. }