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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 "matrix.h"
  21. #include "debug.h"
  22. #include "protocol/serial.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. static uint8_t matrix[MATRIX_ROWS];
  40. #define ROW(code) ((code>>3)&0xF)
  41. #define COL(code) (code&0x07)
  42. static void pc98_inhibit_repeat(void)
  43. {
  44. uint8_t code;
  45. while (serial_recv()) ;
  46. RETRY:
  47. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  48. _delay_ms(500);
  49. serial_send(0x9C);
  50. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  51. _delay_ms(100);
  52. while (!(code = serial_recv())) ;
  53. print("PC98: send 9C: "); print_hex8(code); print("\n");
  54. if (code != 0xFA) goto RETRY;
  55. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  56. _delay_ms(100);
  57. serial_send(0x70);
  58. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  59. _delay_ms(100);
  60. //code = serial_recv();
  61. while (!(code = serial_recv())) ;
  62. print("PC98: send 70: "); print_hex8(code); print("\n");
  63. if (code != 0xFA) goto RETRY;
  64. }
  65. void matrix_init(void)
  66. {
  67. PC98_RST_DDR |= (1<<PC98_RST_BIT);
  68. PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
  69. PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
  70. PC98_RST_PORT |= (1<<PC98_RST_BIT);
  71. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  72. PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
  73. serial_init();
  74. // PC98 reset
  75. /*
  76. PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
  77. _delay_us(15);
  78. PC98_RST_PORT |= (1<<PC98_RST_BIT);
  79. _delay_us(13);
  80. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  81. */
  82. _delay_ms(500);
  83. pc98_inhibit_repeat();
  84. // PC98 ready
  85. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  86. // initialize matrix state: all keys off
  87. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  88. debug("init\n");
  89. return;
  90. }
  91. uint8_t matrix_scan(void)
  92. {
  93. uint16_t code;
  94. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  95. _delay_us(30);
  96. code = serial_recv2();
  97. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  98. if (code == -1) return 0;
  99. if (code == 0x60) {
  100. pc98_inhibit_repeat();
  101. /*
  102. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  103. _delay_ms(100);
  104. serial_send(0x96);
  105. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  106. */
  107. return 0;
  108. }
  109. print_hex8(code); print(" ");
  110. if (code&0x80) {
  111. // break code
  112. if (matrix_is_on(ROW(code), COL(code))) {
  113. matrix[ROW(code)] &= ~(1<<COL(code));
  114. }
  115. } else {
  116. // make code
  117. if (!matrix_is_on(ROW(code), COL(code))) {
  118. matrix[ROW(code)] |= (1<<COL(code));
  119. }
  120. }
  121. return code;
  122. }
  123. inline
  124. uint8_t matrix_get_row(uint8_t row)
  125. {
  126. return matrix[row];
  127. }