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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Command From System To Keyboard
  40. * 0x01 Reset
  41. * 0x02 Bell On
  42. * 0x03 Bell Off
  43. * 0x0A Click On
  44. * 0x0B Click Off
  45. * 0x0E LED
  46. * 0x0F Layout
  47. *
  48. * Command From Keyboard To System
  49. * 0x7F Idle
  50. * 0xFE Layout Response
  51. * 0xFF Reset Response(followed by 0x04)
  52. *
  53. * bit: 3 2 1 0
  54. * LED: CapsLk ScrLk Compose NumLk
  55. *
  56. * Connector
  57. * 8Pin mini DIN
  58. * 8 7 6
  59. * 5 4 3
  60. * 2 1
  61. * receptacle
  62. *
  63. * 1: GND
  64. * 2: GND
  65. * 3: 5V
  66. * 4: RX/TX(Mouse)
  67. * 5: RX
  68. * 6: TX
  69. * 7: GND
  70. * 8: 5V
  71. */
  72. static uint8_t matrix[MATRIX_ROWS];
  73. #define ROW(code) ((code>>3)&0xF)
  74. #define COL(code) (code&0x07)
  75. static bool is_modified = false;
  76. inline
  77. uint8_t matrix_rows(void)
  78. {
  79. return MATRIX_ROWS;
  80. }
  81. inline
  82. uint8_t matrix_cols(void)
  83. {
  84. return MATRIX_COLS;
  85. }
  86. void matrix_init(void)
  87. {
  88. print_enable = true;
  89. debug_enable = true;
  90. serial_init();
  91. // initialize matrix state: all keys off
  92. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  93. return;
  94. }
  95. uint8_t matrix_scan(void)
  96. {
  97. is_modified = false;
  98. uint8_t code;
  99. code = serial_recv();
  100. if (code == 0) {
  101. return 0;
  102. }
  103. phex(code); print("(");
  104. code = ~code;
  105. phex(code); print(")");
  106. return 0;
  107. if (code&0x80) {
  108. // break code
  109. if (matrix_is_on(ROW(code), COL(code))) {
  110. matrix[ROW(code)] &= ~(1<<COL(code));
  111. is_modified = true;
  112. }
  113. } else {
  114. // make code
  115. if (!matrix_is_on(ROW(code), COL(code))) {
  116. matrix[ROW(code)] |= (1<<COL(code));
  117. is_modified = true;
  118. }
  119. }
  120. return code;
  121. }
  122. bool matrix_is_modified(void)
  123. {
  124. return is_modified;
  125. }
  126. inline
  127. bool matrix_has_ghost(void)
  128. {
  129. return false;
  130. }
  131. inline
  132. bool matrix_is_on(uint8_t row, uint8_t col)
  133. {
  134. return (matrix[row] & (1<<col));
  135. }
  136. inline
  137. uint8_t matrix_get_row(uint8_t row)
  138. {
  139. return matrix[row];
  140. }
  141. void matrix_print(void)
  142. {
  143. print("\nr/c 01234567\n");
  144. for (uint8_t row = 0; row < matrix_rows(); row++) {
  145. phex(row); print(": ");
  146. pbin_reverse(matrix_get_row(row));
  147. print("\n");
  148. }
  149. }
  150. uint8_t matrix_key_count(void)
  151. {
  152. uint8_t count = 0;
  153. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  154. count += bitpop(matrix[i]);
  155. }
  156. return count;
  157. }