Keyboard firmwares for Atmel AVR and Cortex-M
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

matrix.c 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "serial.h"
  21. #include "matrix.h"
  22. #include "debug.h"
  23. /*
  24. * Matrix Array usage:
  25. *
  26. * ROW: 16(4bits)
  27. * COL: 8(3bits)
  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. static bool is_modified = false;
  44. void matrix_init(void)
  45. {
  46. serial_init();
  47. // initialize matrix state: all keys off
  48. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  49. return;
  50. }
  51. uint8_t matrix_scan(void)
  52. {
  53. is_modified = false;
  54. uint16_t code;
  55. code = serial_recv2();
  56. if (code == -1) {
  57. return 0;
  58. }
  59. dprintf("%02X\n", code);
  60. if (code&0x80) {
  61. // break code
  62. if (matrix_is_on(ROW(code), COL(code))) {
  63. matrix[ROW(code)] &= ~(1<<COL(code));
  64. is_modified = true;
  65. }
  66. } else {
  67. // make code
  68. if (!matrix_is_on(ROW(code), COL(code))) {
  69. matrix[ROW(code)] |= (1<<COL(code));
  70. is_modified = true;
  71. }
  72. }
  73. return code;
  74. }
  75. inline
  76. uint8_t matrix_get_row(uint8_t row)
  77. {
  78. return matrix[row];
  79. }