Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2016 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 "print.h"
  15. #include "matrix.h"
  16. __attribute__ ((weak))
  17. uint8_t matrix_rows(void)
  18. {
  19. return MATRIX_ROWS;
  20. }
  21. __attribute__ ((weak))
  22. uint8_t matrix_cols(void)
  23. {
  24. return MATRIX_COLS;
  25. }
  26. __attribute__ ((weak))
  27. void matrix_clear(void)
  28. {
  29. }
  30. __attribute__ ((weak))
  31. void matrix_setup(void) {}
  32. __attribute__ ((weak))
  33. bool matrix_is_on(uint8_t row, uint8_t col)
  34. {
  35. return (matrix_get_row(row) & (1<<col));
  36. }
  37. __attribute__ ((weak))
  38. void matrix_print(void)
  39. {
  40. #if (MATRIX_COLS <= 8)
  41. print("r/c 01234567\n");
  42. #elif (MATRIX_COLS <= 16)
  43. print("r/c 0123456789ABCDEF\n");
  44. #elif (MATRIX_COLS <= 32)
  45. print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
  46. #endif
  47. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  48. #if (MATRIX_COLS <= 8)
  49. xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
  50. #elif (MATRIX_COLS <= 16)
  51. xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
  52. #elif (MATRIX_COLS <= 32)
  53. xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
  54. #endif
  55. #ifdef MATRIX_HAS_GHOST
  56. matrix_has_ghost_in_row(row) ? " <ghost" : ""
  57. #else
  58. ""
  59. #endif
  60. );
  61. }
  62. }
  63. #ifdef MATRIX_HAS_GHOST
  64. __attribute__ ((weak))
  65. bool matrix_has_ghost_in_row(uint8_t row)
  66. {
  67. matrix_row_t matrix_row = matrix_get_row(row);
  68. // No ghost exists when less than 2 keys are down on the row
  69. if (((matrix_row - 1) & matrix_row) == 0)
  70. return false;
  71. // Ghost occurs when the row shares column line with other row
  72. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  73. if (i != row && (matrix_get_row(i) & matrix_row))
  74. return true;
  75. }
  76. return false;
  77. }
  78. #endif
  79. __attribute__ ((weak)) void matrix_power_up(void) {}
  80. __attribute__ ((weak)) void matrix_power_down(void) {}