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.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

matrix.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. void matrix_init(void)
  54. {
  55. print_enable = true;
  56. debug_enable = true;
  57. PC98_RST_DDR |= (1<<PC98_RST_BIT);
  58. PC98_RDY_DDR |= (1<<PC98_RDY_BIT);
  59. PC98_RTY_DDR |= (1<<PC98_RTY_BIT);
  60. PC98_RST_PORT |= (1<<PC98_RST_BIT);
  61. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  62. PC98_RTY_PORT |= (1<<PC98_RTY_BIT);
  63. DDRD |= 1<<7;
  64. serial_init();
  65. // PC98 reset
  66. PC98_RST_PORT &= ~(1<<PC98_RST_BIT);
  67. _delay_us(15);
  68. PC98_RST_PORT |= (1<<PC98_RST_BIT);
  69. _delay_us(13);
  70. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  71. // PC98 ready
  72. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  73. // initialize matrix state: all keys off
  74. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  75. debug("init\n");
  76. return;
  77. }
  78. uint8_t matrix_scan(void)
  79. {
  80. is_modified = false;
  81. uint8_t code;
  82. PC98_RDY_PORT |= (1<<PC98_RDY_BIT);
  83. _delay_us(30);
  84. code = serial_recv();
  85. PC98_RDY_PORT &= ~(1<<PC98_RDY_BIT);
  86. if (!code) return 0;
  87. debug_hex(code); debug(" ");
  88. /*
  89. switch (code) {
  90. case 0x7E: // reset fail
  91. case 0xFE: // layout
  92. case 0xFF: // reset success
  93. _delay_ms(500);
  94. // ignore response byte
  95. debug("(response ignored:");
  96. while ((code = serial_recv())) { debug(" "); debug_hex(code); }
  97. debug(") ");
  98. // FALL THROUGH
  99. case 0x7F:
  100. // all keys up
  101. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  102. return 0;
  103. }
  104. if (code&0x80) {
  105. // break code
  106. if (matrix_is_on(ROW(code), COL(code))) {
  107. matrix[ROW(code)] &= ~(1<<COL(code));
  108. is_modified = true;
  109. }
  110. } else {
  111. // make code
  112. if (!matrix_is_on(ROW(code), COL(code))) {
  113. matrix[ROW(code)] |= (1<<COL(code));
  114. is_modified = true;
  115. }
  116. }
  117. */
  118. return code;
  119. }
  120. bool matrix_is_modified(void)
  121. {
  122. return is_modified;
  123. }
  124. inline
  125. bool matrix_has_ghost(void)
  126. {
  127. return false;
  128. }
  129. inline
  130. bool matrix_is_on(uint8_t row, uint8_t col)
  131. {
  132. return (matrix[row] & (1<<col));
  133. }
  134. inline
  135. uint8_t matrix_get_row(uint8_t row)
  136. {
  137. return matrix[row];
  138. }
  139. void matrix_print(void)
  140. {
  141. print("\nr/c 01234567\n");
  142. for (uint8_t row = 0; row < matrix_rows(); row++) {
  143. phex(row); print(": ");
  144. pbin_reverse(matrix_get_row(row));
  145. print("\n");
  146. }
  147. }
  148. uint8_t matrix_key_count(void)
  149. {
  150. uint8_t count = 0;
  151. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  152. count += bitpop(matrix[i]);
  153. }
  154. return count;
  155. }