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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2011 Jun Wako <[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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "usb_hid.h"
  17. #include "keycode.h"
  18. #include "util.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "matrix.h"
  22. /* KEY CODE to Matrix
  23. *
  24. * HID keycode(1 byte):
  25. * Higher 5 bits indicates ROW and lower 3 bits COL.
  26. *
  27. * 7 6 5 4 3 2 1 0
  28. * +---------------+
  29. * | ROW | COL |
  30. * +---------------+
  31. *
  32. * Matrix space(16 * 16):
  33. * r\c0123456789ABCDEF
  34. * 0 +----------------+
  35. * : | |
  36. * : | |
  37. * 16 +----------------+
  38. */
  39. #define ROW_MASK 0xF0
  40. #define COL_MASK 0x0F
  41. #define CODE(row, col) (((row) << 4) | (col))
  42. #define ROW(code) (((code) & ROW_MASK) >> 4)
  43. #define COL(code) ((code) & COL_MASK)
  44. #define ROW_BITS(code) (1 << COL(code))
  45. uint8_t matrix_rows(void) { return MATRIX_ROWS; }
  46. uint8_t matrix_cols(void) { return MATRIX_COLS; }
  47. void matrix_init(void) {}
  48. bool matrix_has_ghost(void) { return false; }
  49. static bool matrix_is_mod =false;
  50. uint8_t matrix_scan(void) {
  51. static uint16_t last_time_stamp = 0;
  52. if (last_time_stamp != usb_hid_time_stamp) {
  53. last_time_stamp = usb_hid_time_stamp;
  54. matrix_is_mod = true;
  55. } else {
  56. matrix_is_mod = false;
  57. }
  58. return 1;
  59. }
  60. bool matrix_is_modified(void) {
  61. return matrix_is_mod;
  62. }
  63. bool matrix_is_on(uint8_t row, uint8_t col) {
  64. uint8_t code = CODE(row, col);
  65. if (IS_MOD(code)) {
  66. if (usb_hid_keyboard_report.mods & ROW_BITS(code)) {
  67. return true;
  68. }
  69. }
  70. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  71. if (usb_hid_keyboard_report.keys[i] == code) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. matrix_row_t matrix_get_row(uint8_t row) {
  78. uint16_t row_bits = 0;
  79. if (IS_MOD(CODE(row, 0)) && usb_hid_keyboard_report.mods) {
  80. row_bits |= usb_hid_keyboard_report.mods;
  81. }
  82. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  83. if (IS_ANY(usb_hid_keyboard_report.keys[i])) {
  84. if (row == ROW(usb_hid_keyboard_report.keys[i])) {
  85. row_bits |= ROW_BITS(usb_hid_keyboard_report.keys[i]);
  86. }
  87. }
  88. }
  89. return row_bits;
  90. }
  91. uint8_t matrix_key_count(void) {
  92. uint8_t count = 0;
  93. count += bitpop(usb_hid_keyboard_report.mods);
  94. for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
  95. if (IS_ANY(usb_hid_keyboard_report.keys[i])) {
  96. count++;
  97. }
  98. }
  99. return count;
  100. }
  101. void matrix_print(void) {
  102. print("\nr/c 01234567\n");
  103. for (uint8_t row = 0; row < matrix_rows(); row++) {
  104. phex(row); print(": ");
  105. pbin_reverse(matrix_get_row(row));
  106. #ifdef MATRIX_HAS_GHOST
  107. if (matrix_has_ghost_in_row(row)) {
  108. print(" <ghost");
  109. }
  110. #endif
  111. print("\n");
  112. }
  113. }