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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright 2012 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 <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "util.h"
  20. #include "news.h"
  21. #include "matrix.h"
  22. /*
  23. * Matrix Array usage:
  24. *
  25. * ROW: 16
  26. * COL:8
  27. *
  28. * 8bit wide
  29. * +---------+
  30. * 0|00 ... 07|
  31. * 1|08 ... 0F|
  32. * :| ... |
  33. * :| ... |
  34. * E|70 ... 77|
  35. * F|78 ... 7F|
  36. * +---------+
  37. *
  38. */
  39. static uint8_t matrix[MATRIX_ROWS];
  40. #define ROW(code) ((code>>3)&0xF)
  41. #define COL(code) (code&0x07)
  42. static bool is_modified = false;
  43. inline
  44. uint8_t matrix_rows(void)
  45. {
  46. return MATRIX_ROWS;
  47. }
  48. inline
  49. uint8_t matrix_cols(void)
  50. {
  51. return MATRIX_COLS;
  52. }
  53. void matrix_init(void)
  54. {
  55. news_init();
  56. // initialize matrix state: all keys off
  57. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  58. return;
  59. }
  60. uint8_t matrix_scan(void)
  61. {
  62. is_modified = false;
  63. uint8_t code;
  64. code = news_recv();
  65. if (code == 0) {
  66. return 0;
  67. }
  68. phex(code); print(" ");
  69. if (code&0x80) {
  70. // break code
  71. if (matrix_is_on(ROW(code), COL(code))) {
  72. matrix[ROW(code)] &= ~(1<<COL(code));
  73. is_modified = true;
  74. }
  75. } else {
  76. // make code
  77. if (!matrix_is_on(ROW(code), COL(code))) {
  78. matrix[ROW(code)] |= (1<<COL(code));
  79. is_modified = true;
  80. }
  81. }
  82. return code;
  83. }
  84. bool matrix_is_modified(void)
  85. {
  86. return is_modified;
  87. }
  88. inline
  89. bool matrix_has_ghost(void)
  90. {
  91. return false;
  92. }
  93. inline
  94. bool matrix_is_on(uint8_t row, uint8_t col)
  95. {
  96. return (matrix[row] & (1<<col));
  97. }
  98. inline
  99. uint8_t matrix_get_row(uint8_t row)
  100. {
  101. return matrix[row];
  102. }
  103. void matrix_print(void)
  104. {
  105. print("\nr/c 01234567\n");
  106. for (uint8_t row = 0; row < matrix_rows(); row++) {
  107. phex(row); print(": ");
  108. pbin_reverse(matrix_get_row(row));
  109. print("\n");
  110. }
  111. }
  112. uint8_t matrix_key_count(void)
  113. {
  114. uint8_t count = 0;
  115. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  116. count += bitpop(matrix[i]);
  117. }
  118. return count;
  119. }