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.9KB

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