matrix.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  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 QMK_KEYBOARD_H
  15. #include "protocol/serial.h"
  16. /*
  17. * Matrix Array usage:
  18. *
  19. * ROW: 16(4bits)
  20. * COL: 8(3bits)
  21. *
  22. * 8bit wide
  23. * +---------+
  24. * 0|00 ... 07|
  25. * 1|08 ... 0F|
  26. * :| ... |
  27. * :| ... |
  28. * E|70 ... 77|
  29. * F|78 ... 7F|
  30. * +---------+
  31. */
  32. static uint8_t matrix[MATRIX_ROWS];
  33. #define ROW(code) ((code>>3)&0xF)
  34. #define COL(code) (code&0x07)
  35. static bool is_modified = false;
  36. __attribute__ ((weak))
  37. void matrix_init_kb(void) {
  38. matrix_init_user();
  39. }
  40. __attribute__ ((weak))
  41. void matrix_scan_kb(void) {
  42. matrix_scan_user();
  43. }
  44. __attribute__ ((weak))
  45. void matrix_init_user(void) {
  46. }
  47. __attribute__ ((weak))
  48. void matrix_scan_user(void) {
  49. }
  50. inline
  51. uint8_t matrix_rows(void)
  52. {
  53. return MATRIX_ROWS;
  54. }
  55. inline
  56. uint8_t matrix_cols(void)
  57. {
  58. return MATRIX_COLS;
  59. }
  60. void matrix_init(void)
  61. {
  62. /* DDRD |= (1<<6); */
  63. /* PORTD |= (1<<6); */
  64. debug_enable = true;
  65. serial_init();
  66. // initialize matrix state: all keys off
  67. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  68. /* // wait for keyboard coming up */
  69. /* // otherwise LED status update fails */
  70. /* print("Reseting "); */
  71. /* while (1) { */
  72. /* print("."); */
  73. /* while (serial_recv()); */
  74. /* serial_send(0x01); */
  75. /* _delay_ms(500); */
  76. /* if (serial_recv() == 0xFF) { */
  77. /* _delay_ms(500); */
  78. /* if (serial_recv() == 0x04) */
  79. /* break; */
  80. /* } */
  81. /* } */
  82. /* print(" Done\n"); */
  83. /* PORTD &= ~(1<<6); */
  84. matrix_init_quantum();
  85. return;
  86. }
  87. uint8_t matrix_scan(void)
  88. {
  89. uint8_t code;
  90. code = serial_recv();
  91. if (!code) return 0;
  92. debug_hex(code); debug(" ");
  93. switch (code) {
  94. case 0xFF: // reset success: FF 04
  95. print("reset: ");
  96. _delay_ms(500);
  97. code = serial_recv();
  98. xprintf("%02X\n", code);
  99. if (code == 0x04) {
  100. // LED status
  101. led_set(host_keyboard_leds());
  102. }
  103. return 0;
  104. case 0xFE: // layout: FE <layout>
  105. print("layout: ");
  106. _delay_ms(500);
  107. xprintf("%02X\n", serial_recv());
  108. return 0;
  109. case 0x7E: // reset fail: 7E 01
  110. print("reset fail: ");
  111. _delay_ms(500);
  112. xprintf("%02X\n", serial_recv());
  113. return 0;
  114. case 0x7F:
  115. // all keys up
  116. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  117. return 0;
  118. }
  119. if (code&0x80) {
  120. // break code
  121. if (matrix_is_on(ROW(code), COL(code))) {
  122. matrix[ROW(code)] &= ~(1<<COL(code));
  123. }
  124. } else {
  125. // make code
  126. if (!matrix_is_on(ROW(code), COL(code))) {
  127. matrix[ROW(code)] |= (1<<COL(code));
  128. }
  129. }
  130. matrix_scan_quantum();
  131. return code;
  132. }
  133. bool matrix_is_modified(void)
  134. {
  135. return is_modified;
  136. }
  137. inline
  138. bool matrix_has_ghost(void)
  139. {
  140. return false;
  141. }
  142. inline
  143. bool matrix_is_on(uint8_t row, uint8_t col)
  144. {
  145. return (matrix[row] & (1<<col));
  146. }
  147. inline
  148. uint8_t matrix_get_row(uint8_t row)
  149. {
  150. return matrix[row];
  151. }
  152. void matrix_print(void)
  153. {
  154. print("\nr/c 01234567\n");
  155. for (uint8_t row = 0; row < matrix_rows(); row++) {
  156. phex(row); print(": ");
  157. pbin_reverse(matrix_get_row(row));
  158. print("\n");
  159. }
  160. }
  161. uint8_t matrix_key_count(void)
  162. {
  163. uint8_t count = 0;
  164. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  165. count += bitpop(matrix[i]);
  166. }
  167. return count;
  168. }