logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (4024B)


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