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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 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. static void pc98_inhibit_repeat(void)
  54. {
  55. uint8_t code;
  56. while (serial_recv()) ;
  57. RETRY:
  58. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  59. _delay_ms(500);
  60. serial_send(0x9C);
  61. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  62. _delay_ms(100);
  63. while (!(code = serial_recv())) ;
  64. print("PC98: send 9C: "); print_hex8(code); print("\n");
  65. if (code != 0xFA) goto RETRY;
  66. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  67. _delay_ms(100);
  68. serial_send(0x70);
  69. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  70. _delay_ms(100);
  71. //code = serial_recv();
  72. while (!(code = serial_recv())) ;
  73. print("PC98: send 70: "); print_hex8(code); print("\n");
  74. if (code != 0xFA) goto RETRY;
  75. }
  76. void matrix_init(void)
  77. {
  78. print_enable = true;
  79. // debug_enable = true;
  80. // debug_matrix = true;
  81. PC98_RST_DDR |= (1<<PC98_RST_BIT);
  82. PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
  83. PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
  84. PC98_RST_PORT |= (1<<PC98_RST_BIT);
  85. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  86. PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
  87. serial_init();
  88. // PC98 reset
  89. /*
  90. PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
  91. _delay_us(15);
  92. PC98_RST_PORT |= (1<<PC98_RST_BIT);
  93. _delay_us(13);
  94. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  95. */
  96. _delay_ms(500);
  97. pc98_inhibit_repeat();
  98. // PC98 ready
  99. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  100. // initialize matrix state: all keys off
  101. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  102. debug("init\n");
  103. return;
  104. }
  105. uint8_t matrix_scan(void)
  106. {
  107. is_modified = false;
  108. uint16_t code;
  109. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  110. _delay_us(30);
  111. code = serial_recv2();
  112. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  113. if (code == -1) return 0;
  114. if (code == 0x60) {
  115. pc98_inhibit_repeat();
  116. /*
  117. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  118. _delay_ms(100);
  119. serial_send(0x96);
  120. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  121. */
  122. return 0;
  123. }
  124. print_hex8(code); print(" ");
  125. if (code&0x80) {
  126. // break code
  127. if (matrix_is_on(ROW(code), COL(code))) {
  128. matrix[ROW(code)] &= ~(1<<COL(code));
  129. is_modified = true;
  130. }
  131. } else {
  132. // make code
  133. if (!matrix_is_on(ROW(code), COL(code))) {
  134. matrix[ROW(code)] |= (1<<COL(code));
  135. is_modified = true;
  136. }
  137. }
  138. return code;
  139. }
  140. bool matrix_is_modified(void)
  141. {
  142. return is_modified;
  143. }
  144. inline
  145. bool matrix_has_ghost(void)
  146. {
  147. return false;
  148. }
  149. inline
  150. bool matrix_is_on(uint8_t row, uint8_t col)
  151. {
  152. return (matrix[row] & (1<<col));
  153. }
  154. inline
  155. uint8_t matrix_get_row(uint8_t row)
  156. {
  157. return matrix[row];
  158. }
  159. void matrix_print(void)
  160. {
  161. print("\nr/c 01234567\n");
  162. for (uint8_t row = 0; row < matrix_rows(); row++) {
  163. phex(row); print(": ");
  164. pbin_reverse(matrix_get_row(row));
  165. print("\n");
  166. }
  167. }
  168. uint8_t matrix_key_count(void)
  169. {
  170. uint8_t count = 0;
  171. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  172. count += bitpop(matrix[i]);
  173. }
  174. return count;
  175. }