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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "debug.h"
  23. /*
  24. * Matrix Array usage:
  25. *
  26. * ROW: 16
  27. * COL:8
  28. *
  29. * 8bit wide
  30. * +---------+
  31. * 0|00 ... 07|
  32. * 1|08 ... 0F|
  33. * :| ... |
  34. * :| ... |
  35. * E|70 ... 77|
  36. * F|78 ... 7F|
  37. * +---------+
  38. *
  39. */
  40. static uint8_t matrix[MATRIX_ROWS];
  41. #define ROW(code) ((code>>3)&0xF)
  42. #define COL(code) (code&0x07)
  43. void matrix_init(void)
  44. {
  45. news_init();
  46. // initialize matrix state: all keys off
  47. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  48. return;
  49. }
  50. uint8_t matrix_scan(void)
  51. {
  52. uint8_t code;
  53. code = news_recv();
  54. if (code == 0) {
  55. return 0;
  56. }
  57. phex(code); print(" ");
  58. if (code&0x80) {
  59. // break code
  60. if (matrix_is_on(ROW(code), COL(code))) {
  61. matrix[ROW(code)] &= ~(1<<COL(code));
  62. }
  63. } else {
  64. // make code
  65. if (!matrix_is_on(ROW(code), COL(code))) {
  66. matrix[ROW(code)] |= (1<<COL(code));
  67. }
  68. }
  69. return code;
  70. }
  71. inline
  72. uint8_t matrix_get_row(uint8_t row)
  73. {
  74. return matrix[row];
  75. }