matrix.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
  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 "action_layer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "sixkeyboard.h"
  27. #include <string.h>
  28. /* matrix state(1:on, 0:off) */
  29. static matrix_row_t matrix[MATRIX_ROWS];
  30. static matrix_row_t matrix_stage[MATRIX_ROWS];
  31. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  32. static uint16_t debouncing_time;
  33. static bool debouncing = false;
  34. __attribute__ ((weak))
  35. void matrix_init_kb(void) {
  36. matrix_init_user();
  37. }
  38. __attribute__ ((weak))
  39. void matrix_scan_kb(void) {
  40. matrix_scan_user();
  41. }
  42. __attribute__ ((weak))
  43. void matrix_init_user(void) {
  44. }
  45. __attribute__ ((weak))
  46. void matrix_scan_user(void) {
  47. }
  48. inline
  49. uint8_t matrix_rows(void)
  50. {
  51. return MATRIX_ROWS;
  52. }
  53. inline
  54. uint8_t matrix_cols(void)
  55. {
  56. return MATRIX_COLS;
  57. }
  58. void matrix_init(void)
  59. {
  60. DDRC &= ~(1<<7);
  61. PORTC |= (1<<7);
  62. DDRB &= ~(1<<7 | 1<<5);
  63. PORTB |= (1<<7 | 1<<5);
  64. DDRD &= ~(1<<6 | 1<<4 | 1<<1);
  65. PORTD |= (1<<6 | 1<<4 | 1<<1);
  66. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  67. matrix[i] = 0;
  68. matrix_debouncing[i] = 0;
  69. matrix_stage[i] = 0;
  70. }
  71. matrix_init_quantum();
  72. }
  73. uint8_t matrix_scan(void)
  74. {
  75. matrix_stage[0] = (PINC&(1<<7) ? 0 : (1<<0)) | (PINB&(1<<7) ? 0 : (1<<1)) | (PINB&(1<<5) ? 0 : (1<<2));
  76. matrix_stage[1] = (PIND&(1<<6) ? 0 : (1<<0)) | (PIND&(1<<1) ? 0 : (1<<1)) | (PIND&(1<<4) ? 0 : (1<<2));
  77. if (memcmp(matrix_debouncing, matrix_stage, sizeof(matrix)) != 0) {
  78. debouncing = true;
  79. debouncing_time = timer_read();
  80. }
  81. matrix_debouncing[0] = matrix_stage[0];
  82. matrix_debouncing[1] = matrix_stage[1];
  83. if (debouncing && (timer_elapsed(debouncing_time) > 20)) {
  84. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  85. matrix[i] = matrix_debouncing[i];
  86. }
  87. debouncing = false;
  88. }
  89. matrix_scan_quantum();
  90. return 1;
  91. }
  92. bool matrix_is_modified(void)
  93. {
  94. return true;
  95. }
  96. inline
  97. bool matrix_is_on(uint8_t row, uint8_t col)
  98. {
  99. return (matrix[row] & ((matrix_row_t)1<<col));
  100. }
  101. inline
  102. matrix_row_t matrix_get_row(uint8_t row)
  103. {
  104. return matrix[row];
  105. }
  106. void matrix_print(void)
  107. {
  108. }
  109. uint8_t matrix_key_count(void)
  110. {
  111. uint8_t count = 0;
  112. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  113. count += bitpop16(matrix[i]);
  114. }
  115. return count;
  116. }