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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright 2014 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 "action_util.h"
  23. #include "protocol/serial.h"
  24. /*
  25. * Not use Matrix.
  26. *
  27. * ROW: 16(4bits)
  28. * COL: 16(4bits)
  29. *
  30. * 8bit wide
  31. * +---------+
  32. * 0|00 ... 0F|
  33. * 1|08 ... 1F|
  34. * :| ... |
  35. * :| ... |
  36. * E|E0 ... EF|
  37. * F|F0 ... FF|
  38. * +---------+
  39. */
  40. inline
  41. uint8_t matrix_rows(void)
  42. {
  43. return MATRIX_ROWS;
  44. }
  45. inline
  46. uint8_t matrix_cols(void)
  47. {
  48. return MATRIX_COLS;
  49. }
  50. void matrix_init(void)
  51. {
  52. debug_matrix = true;
  53. serial_init();
  54. debug("init\n");
  55. return;
  56. }
  57. static void type_key(uint16_t keycode)
  58. {
  59. if (keycode == 0) return;
  60. uint8_t mods = keycode>>8;
  61. uint8_t key = keycode&0xFF;
  62. if (mods) {
  63. add_mods(mods);
  64. send_keyboard_report();
  65. }
  66. add_key(key);
  67. send_keyboard_report();
  68. del_key(key);
  69. send_keyboard_report();
  70. if (mods) {
  71. del_mods(mods);
  72. send_keyboard_report();
  73. }
  74. }
  75. static uint16_t code2key(uint8_t code)
  76. {
  77. // ASCII to key combination in US laout
  78. switch (code) {
  79. case 0x01 ... 0x08: // Ctrl-[a-z]
  80. return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
  81. case 0x09: return KC_TAB; // TAB(Ctrl-i)
  82. case 0x0A ... 0x0C: // Ctrl-[a-z]
  83. return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
  84. case 0x0D: return KC_ENTER; // Enter(Ctrl-m)
  85. case 0x0E ... 0x1A: // Ctrl-[a-z]
  86. return MOD_BIT(KC_LCTRL)<<8 | (KC_A + (code-0x01));
  87. case 0x1B: return KC_ESC;
  88. case 0x1C: return KC_RIGHT;
  89. case 0x1D: return KC_LEFT;
  90. case 0x1E: return KC_UP;
  91. case 0x1F: return KC_DOWN;
  92. case 0x20: return KC_SPACE;
  93. case 0x21: return MOD_BIT(KC_LSHIFT)<<8 | KC_1; // !
  94. case 0x22: return MOD_BIT(KC_LSHIFT)<<8 | KC_QUOTE; // "
  95. case 0x23: return MOD_BIT(KC_LSHIFT)<<8 | KC_3; // #
  96. case 0x24: return MOD_BIT(KC_LSHIFT)<<8 | KC_4; // $
  97. case 0x25: return MOD_BIT(KC_LSHIFT)<<8 | KC_5; // %
  98. case 0x26: return MOD_BIT(KC_LSHIFT)<<8 | KC_7; // &
  99. case 0x27: return KC_QUOTE; // '
  100. case 0x28: return MOD_BIT(KC_LSHIFT)<<8 | KC_9; // (
  101. case 0x29: return MOD_BIT(KC_LSHIFT)<<8 | KC_0; // )
  102. case 0x2A: return MOD_BIT(KC_LSHIFT)<<8 | KC_8; // *
  103. case 0x2B: return MOD_BIT(KC_LSHIFT)<<8 | KC_EQUAL; // +
  104. case 0x2C: return KC_COMMA; // ,
  105. case 0x2D: return KC_MINUS; // -
  106. case 0x2E: return KC_DOT; // .
  107. case 0x2F: return KC_SLASH; // /
  108. case 0x30: return KC_0;
  109. case 0x31 ... 0x39: // 1-9
  110. return KC_1 + (code-0x31);
  111. case 0x3A: return MOD_BIT(KC_LSHIFT)<<8 | KC_SCLN; // :
  112. case 0x3B: return KC_SCLN; // ;
  113. case 0x3C: return MOD_BIT(KC_LSHIFT)<<8 | KC_COMMA; // <
  114. case 0x3D: return KC_EQUAL; // =
  115. case 0x3E: return MOD_BIT(KC_LSHIFT)<<8 | KC_DOT; // >
  116. case 0x3F: return MOD_BIT(KC_LSHIFT)<<8 | KC_SLASH; // ?
  117. case 0x40: return MOD_BIT(KC_LSHIFT)<<8 | KC_2; // @
  118. case 0x41 ... 0x5A: // A-Z
  119. return MOD_BIT(KC_LSHIFT)<<8 | (KC_A + (code-0x41));
  120. case 0x5B: return KC_LBRACKET; // [
  121. case 0x5C: return KC_BSLASH; //
  122. case 0x5D: return KC_RBRACKET; // ]
  123. case 0x5E: return MOD_BIT(KC_LSHIFT)<<8 | KC_6; // ^
  124. case 0x5F: return MOD_BIT(KC_LSHIFT)<<8 | KC_MINUS; // _
  125. case 0x61 ... 0x7A: // a-z
  126. return KC_A + (code-0x61);
  127. case 0x7B: return MOD_BIT(KC_LSHIFT)<<8 | KC_LBRACKET; // {
  128. case 0x7C: return MOD_BIT(KC_LSHIFT)<<8 | KC_BSLASH; // |
  129. case 0x7D: return MOD_BIT(KC_LSHIFT)<<8 | KC_RBRACKET; // }
  130. case 0x7E: return MOD_BIT(KC_LSHIFT)<<8 | KC_GRAVE; // }
  131. case 0x7F: return KC_DELETE; //
  132. }
  133. return 0;
  134. }
  135. uint8_t matrix_scan(void)
  136. {
  137. uint16_t code = serial_recv2();
  138. if (code == -1) {
  139. return 0;
  140. }
  141. print_hex8(code); print(" ");
  142. // echo back
  143. serial_send(code);
  144. type_key(code2key(code));
  145. return code;
  146. }
  147. inline
  148. bool matrix_has_ghost(void)
  149. {
  150. return false;
  151. }
  152. inline
  153. bool matrix_is_on(uint8_t row, uint8_t col)
  154. {
  155. return false;
  156. }
  157. inline
  158. matrix_row_t matrix_get_row(uint8_t row)
  159. {
  160. return 0;
  161. }
  162. void matrix_print(void)
  163. {
  164. print("\nr/c 0123456789ABCDEF\n");
  165. for (uint8_t row = 0; row < matrix_rows(); row++) {
  166. phex(row); print(": ");
  167. pbin_reverse(matrix_get_row(row));
  168. print("\n");
  169. }
  170. }