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.

matrix.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright 2014 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 "action.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "ibm4704.h"
  23. #include "matrix.h"
  24. static void matrix_make(uint8_t code);
  25. static void matrix_break(uint8_t code);
  26. static void matrix_clear(void);
  27. /*
  28. * Matrix Array usage:
  29. * IBM 4704 scan codes are assigned into 128(16x8)-cell matrix.
  30. *
  31. * 8bit wide
  32. * +---------+
  33. * 0| |
  34. * :| XX | 00-7F
  35. * f| |
  36. * +---------+
  37. *
  38. * Exceptions:
  39. */
  40. static uint8_t matrix[MATRIX_ROWS];
  41. // scan code bits 7654 3210
  42. // R:row/C:column -RRR RCCC
  43. #define ROW(code) ((code>>3)&0x0f)
  44. #define COL(code) (code&0x07)
  45. inline
  46. uint8_t matrix_rows(void)
  47. {
  48. return MATRIX_ROWS;
  49. }
  50. inline
  51. uint8_t matrix_cols(void)
  52. {
  53. return MATRIX_COLS;
  54. }
  55. static void enable_break(void)
  56. {
  57. uint8_t ret;
  58. for (uint8_t code = 0; code < 0x80; code++) {
  59. while (ibm4704_send(0x80|code) != 0) {
  60. print("z");
  61. }
  62. _delay_ms(1);
  63. ret = ibm4704_recv();
  64. if (ret!=0xFF) {
  65. xprintf("%0X: %0X ", code, ret);
  66. }
  67. }
  68. _delay_us(100);
  69. print("End\n");
  70. while (ibm4704_send(0xFF) != 0) {
  71. print("Z");
  72. } // End
  73. }
  74. void matrix_init(void)
  75. {
  76. debug_enable = true;
  77. ibm4704_init();
  78. matrix_clear();
  79. xprintf("------\n");
  80. enable_break();
  81. //while (ibm4704_send(0x8C) != 0) ;
  82. //while (ibm4704_send(0xFF) != 0) ;
  83. }
  84. /*
  85. * IBM 4704 Scan Code
  86. */
  87. uint8_t matrix_scan(void)
  88. {
  89. uint8_t code = ibm4704_recv();
  90. if (code==0xFF) {
  91. // Not receivd
  92. return 0;
  93. } else if ((code&0x78)==0x78) {
  94. // 0xFF-F8 and 0x7F-78 is not scan code
  95. dprintf("Error: %0X\n", code);
  96. matrix_clear();
  97. return 0;
  98. } else if (code&0x80) {
  99. matrix_make(code);
  100. } else {
  101. matrix_break(code);
  102. }
  103. return 1;
  104. }
  105. inline
  106. bool matrix_has_ghost(void)
  107. {
  108. return false;
  109. }
  110. inline
  111. bool matrix_is_on(uint8_t row, uint8_t col)
  112. {
  113. return (matrix[row] & (1<<col));
  114. }
  115. inline
  116. uint8_t matrix_get_row(uint8_t row)
  117. {
  118. return matrix[row];
  119. }
  120. void matrix_print(void)
  121. {
  122. print("\nr/c 01234567\n");
  123. for (uint8_t row = 0; row < matrix_rows(); row++) {
  124. // TODO: use new function
  125. phex(row); print(": ");
  126. pbin_reverse(matrix_get_row(row));
  127. print("\n");
  128. }
  129. }
  130. uint8_t matrix_key_count(void)
  131. {
  132. uint8_t count = 0;
  133. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  134. count += bitpop(matrix[i]);
  135. }
  136. return count;
  137. }
  138. inline
  139. static void matrix_make(uint8_t code)
  140. {
  141. matrix[ROW(code)] |= 1<<COL(code);
  142. }
  143. inline
  144. static void matrix_break(uint8_t code)
  145. {
  146. matrix[ROW(code)] &= ~(1<<COL(code));
  147. }
  148. inline
  149. static void matrix_clear(void)
  150. {
  151. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  152. }