matrix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "sixkeyboard.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. DDRC &= ~(1<<7);
  65. PORTC |= (1<<7);
  66. DDRB &= ~(1<<7 | 1<<5);
  67. PORTB |= (1<<7 | 1<<5);
  68. DDRD &= ~(1<<6 | 1<<4 | 1<<1);
  69. PORTD |= (1<<6 | 1<<4 | 1<<1);
  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] = (PINC&(1<<7) ? 0 : (1<<0)) | (PINB&(1<<7) ? 0 : (1<<1)) | (PINB&(1<<5) ? 0 : (1<<2));
  80. matrix_stage[1] = (PIND&(1<<6) ? 0 : (1<<0)) | (PIND&(1<<1) ? 0 : (1<<1)) | (PIND&(1<<4) ? 0 : (1<<2));
  81. if (memcmp(matrix_debouncing, matrix_stage, sizeof(matrix)) != 0) {
  82. debouncing = true;
  83. debouncing_time = timer_read();
  84. }
  85. matrix_debouncing[0] = matrix_stage[0];
  86. matrix_debouncing[1] = matrix_stage[1];
  87. if (debouncing && (timer_elapsed(debouncing_time) > 20)) {
  88. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  89. matrix[i] = matrix_debouncing[i];
  90. }
  91. debouncing = false;
  92. }
  93. matrix_scan_quantum();
  94. return 1;
  95. }
  96. bool matrix_is_modified(void)
  97. {
  98. return true;
  99. }
  100. inline
  101. bool matrix_is_on(uint8_t row, uint8_t col)
  102. {
  103. return (matrix[row] & ((matrix_row_t)1<<col));
  104. }
  105. inline
  106. matrix_row_t matrix_get_row(uint8_t row)
  107. {
  108. return matrix[row];
  109. }
  110. void matrix_print(void)
  111. {
  112. }
  113. uint8_t matrix_key_count(void)
  114. {
  115. uint8_t count = 0;
  116. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  117. count += bitpop16(matrix[i]);
  118. }
  119. return count;
  120. }