logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (2878B)


  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 "timer.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. gpio_set_pin_input_high(C7);
  61. gpio_set_pin_input_high(B5);
  62. gpio_set_pin_input_high(B7);
  63. gpio_set_pin_input_high(D1);
  64. gpio_set_pin_input_high(D4);
  65. gpio_set_pin_input_high(D6);
  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_kb();
  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_kb();
  90. return 1;
  91. }
  92. inline
  93. bool matrix_is_on(uint8_t row, uint8_t col)
  94. {
  95. return (matrix[row] & ((matrix_row_t)1<<col));
  96. }
  97. inline
  98. matrix_row_t matrix_get_row(uint8_t row)
  99. {
  100. return matrix[row];
  101. }
  102. void matrix_print(void)
  103. {
  104. }