matrix.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "uart.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. __attribute__ ((weak))
  36. void matrix_init_kb(void) {
  37. matrix_init_user();
  38. }
  39. __attribute__ ((weak))
  40. void matrix_scan_kb(void) {
  41. matrix_scan_user();
  42. }
  43. __attribute__ ((weak))
  44. void matrix_init_user(void) {
  45. }
  46. __attribute__ ((weak))
  47. void matrix_scan_user(void) {
  48. }
  49. inline
  50. uint8_t matrix_rows(void)
  51. {
  52. return MATRIX_ROWS;
  53. }
  54. inline
  55. uint8_t matrix_cols(void)
  56. {
  57. return MATRIX_COLS;
  58. }
  59. void matrix_init(void)
  60. {
  61. /* DDRD |= (1<<6); */
  62. /* PORTD |= (1<<6); */
  63. debug_enable = true;
  64. uart_init(1200);
  65. // initialize matrix state: all keys off
  66. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  67. /* // wait for keyboard coming up */
  68. /* // otherwise LED status update fails */
  69. /* print("Reseting "); */
  70. /* while (1) { */
  71. /* print("."); */
  72. /* while (uart_read()); */
  73. /* uart_write(0x01); */
  74. /* _delay_ms(500); */
  75. /* if (uart_read() == 0xFF) { */
  76. /* _delay_ms(500); */
  77. /* if (uart_read() == 0x04) */
  78. /* break; */
  79. /* } */
  80. /* } */
  81. /* print(" Done\n"); */
  82. /* PORTD &= ~(1<<6); */
  83. matrix_init_kb();
  84. return;
  85. }
  86. uint8_t matrix_scan(void)
  87. {
  88. uint8_t code;
  89. code = uart_read();
  90. if (!code) return 0;
  91. debug_hex(code); debug(" ");
  92. switch (code) {
  93. case 0xFF: // reset success: FF 04
  94. print("reset: ");
  95. _delay_ms(500);
  96. code = uart_read();
  97. xprintf("%02X\n", code);
  98. if (code == 0x04) {
  99. // LED status
  100. led_set(host_keyboard_leds());
  101. }
  102. return 0;
  103. case 0xFE: // layout: FE <layout>
  104. print("layout: ");
  105. _delay_ms(500);
  106. xprintf("%02X\n", uart_read());
  107. return 0;
  108. case 0x7E: // reset fail: 7E 01
  109. print("reset fail: ");
  110. _delay_ms(500);
  111. xprintf("%02X\n", uart_read());
  112. return 0;
  113. case 0x7F:
  114. // all keys up
  115. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  116. return 0;
  117. }
  118. if (code&0x80) {
  119. // break code
  120. if (matrix_is_on(ROW(code), COL(code))) {
  121. matrix[ROW(code)] &= ~(1<<COL(code));
  122. }
  123. } else {
  124. // make code
  125. if (!matrix_is_on(ROW(code), COL(code))) {
  126. matrix[ROW(code)] |= (1<<COL(code));
  127. }
  128. }
  129. matrix_scan_kb();
  130. return code;
  131. }
  132. inline
  133. bool matrix_has_ghost(void)
  134. {
  135. return false;
  136. }
  137. inline
  138. bool matrix_is_on(uint8_t row, uint8_t col)
  139. {
  140. return (matrix[row] & (1<<col));
  141. }
  142. inline
  143. uint8_t matrix_get_row(uint8_t row)
  144. {
  145. return matrix[row];
  146. }
  147. void matrix_print(void)
  148. {
  149. print("\nr/c 01234567\n");
  150. for (uint8_t row = 0; row < matrix_rows(); row++) {
  151. print_hex8(row); print(": ");
  152. print_bin_reverse8(matrix_get_row(row));
  153. print("\n");
  154. }
  155. }