keymap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright 2021 foxx1337 at yahoo dot com
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include QMK_KEYBOARD_H
  18. #include "hid_protocol.h"
  19. enum ctrl_keycodes {
  20. U_T_AUTO = SAFE_RANGE, //USB Extra Port Toggle Auto Detect / Always Active
  21. U_T_AGCR, //USB Toggle Automatic GCR control
  22. DBG_TOG, //DEBUG Toggle On / Off
  23. DBG_MTRX, //DEBUG Toggle Matrix Prints
  24. DBG_KBD, //DEBUG Toggle Keyboard Prints
  25. DBG_MOU, //DEBUG Toggle Mouse Prints
  26. MD_BOOT, //Restart into bootloader after hold timeout
  27. };
  28. #define TIMEOUT_ACTIVITY 300000 // 300 seconds before lights go off
  29. uint32_t time_last_activity;
  30. bool is_led_timeout;
  31. led_flags_t led_state;
  32. keymap_config_t keymap_config;
  33. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  34. [0] = LAYOUT(
  35. KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_SCRL, KC_PAUS,
  36. KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, KC_INS, KC_HOME, KC_PGUP,
  37. KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN,
  38. KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT,
  39. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP,
  40. KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, MO(1), KC_APP, KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT
  41. ),
  42. [1] = LAYOUT(
  43. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_MUTE, _______, _______,
  44. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_MPLY, KC_MSTP, KC_VOLU,
  45. _______, RGB_SPD, RGB_VAI, RGB_SPI, RGB_HUI, RGB_SAI, _______, U_T_AUTO,U_T_AGCR,_______, _______, _______, _______, _______, KC_MPRV, KC_MNXT, KC_VOLD,
  46. _______, RGB_RMOD,RGB_VAD, RGB_MOD, RGB_HUD, RGB_SAD, _______, _______, _______, _______, _______, _______, _______,
  47. _______, RGB_TOG, _______, _______, _______, MD_BOOT, NK_TOGG, _______, _______, _______, _______, _______, _______,
  48. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  49. ),
  50. /*
  51. [X] = LAYOUT(
  52. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  53. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  54. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  55. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  56. _______, _______, _______, _______, _______, _______, NK_TOGG, _______, _______, _______, _______, _______, _______,
  57. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  58. ),
  59. */
  60. };
  61. void change_led_state(bool is_off) {
  62. is_led_timeout = is_off;
  63. if (is_led_timeout) {
  64. led_state = rgb_matrix_get_flags();
  65. if (led_state != LED_FLAG_NONE) {
  66. rgb_matrix_set_flags(LED_FLAG_NONE);
  67. rgb_matrix_disable_noeeprom();
  68. }
  69. } else {
  70. if (led_state != LED_FLAG_NONE) {
  71. rgb_matrix_set_flags(led_state);
  72. rgb_matrix_enable_noeeprom();
  73. }
  74. }
  75. }
  76. // Runs just one time when the keyboard initializes.
  77. void matrix_init_user(void) {
  78. time_last_activity = timer_read32();
  79. };
  80. // Runs constantly in the background, in a loop.
  81. void matrix_scan_user(void) {
  82. if (!is_led_timeout && timer_elapsed32(time_last_activity) > TIMEOUT_ACTIVITY) {
  83. change_led_state(true);
  84. }
  85. };
  86. #define MODS_SHIFT (get_mods() & MOD_MASK_SHIFT)
  87. #define MODS_CTRL (get_mods() & MOD_MASK_CTRL)
  88. #define MODS_ALT (get_mods() & MOD_MASK_ALT)
  89. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  90. static uint32_t key_timer;
  91. time_last_activity = timer_read32();
  92. if (is_led_timeout) {
  93. change_led_state(false);
  94. }
  95. switch (keycode) {
  96. case U_T_AUTO:
  97. if (record->event.pressed && MODS_SHIFT && MODS_CTRL) {
  98. TOGGLE_FLAG_AND_PRINT(usb_extra_manual, "USB extra port manual mode");
  99. }
  100. return false;
  101. case U_T_AGCR:
  102. if (record->event.pressed && MODS_SHIFT && MODS_CTRL) {
  103. TOGGLE_FLAG_AND_PRINT(usb_gcr_auto, "USB GCR auto mode");
  104. }
  105. return false;
  106. case DBG_TOG:
  107. if (record->event.pressed) {
  108. TOGGLE_FLAG_AND_PRINT(debug_enable, "Debug mode");
  109. }
  110. return false;
  111. case DBG_MTRX:
  112. if (record->event.pressed) {
  113. TOGGLE_FLAG_AND_PRINT(debug_matrix, "Debug matrix");
  114. }
  115. return false;
  116. case DBG_KBD:
  117. if (record->event.pressed) {
  118. TOGGLE_FLAG_AND_PRINT(debug_keyboard, "Debug keyboard");
  119. }
  120. return false;
  121. case DBG_MOU:
  122. if (record->event.pressed) {
  123. TOGGLE_FLAG_AND_PRINT(debug_mouse, "Debug mouse");
  124. }
  125. return false;
  126. case MD_BOOT:
  127. if (record->event.pressed) {
  128. key_timer = timer_read32();
  129. } else {
  130. if (timer_elapsed32(key_timer) >= 500) {
  131. reset_keyboard();
  132. }
  133. }
  134. return false;
  135. case RGB_TOG:
  136. if (record->event.pressed) {
  137. switch (rgb_matrix_get_flags()) {
  138. case LED_FLAG_ALL: {
  139. rgb_matrix_set_flags(LED_FLAG_KEYLIGHT | LED_FLAG_MODIFIER);
  140. rgb_matrix_set_color_all(0, 0, 0);
  141. }
  142. break;
  143. case LED_FLAG_KEYLIGHT | LED_FLAG_MODIFIER: {
  144. rgb_matrix_set_flags(LED_FLAG_UNDERGLOW);
  145. rgb_matrix_set_color_all(0, 0, 0);
  146. }
  147. break;
  148. case LED_FLAG_UNDERGLOW: {
  149. rgb_matrix_set_flags(LED_FLAG_NONE);
  150. rgb_matrix_disable_noeeprom();
  151. }
  152. break;
  153. default: {
  154. rgb_matrix_set_flags(LED_FLAG_ALL);
  155. rgb_matrix_enable_noeeprom();
  156. }
  157. break;
  158. }
  159. }
  160. return false;
  161. default:
  162. return true; //Process all other keycodes normally
  163. }
  164. }
  165. void raw_hid_say_hello(void) {
  166. const char *ctrl = CTRL_HID_GREETING_VERSION;
  167. uint8_t i = 0;
  168. while (ctrl[i] != 0 && i + 2 < RAW_EPSIZE) {
  169. raw_hid_buffer[1 + i] = ctrl[i];
  170. i++;
  171. }
  172. raw_hid_buffer[i] = CTRL_HID_EOM;
  173. }
  174. void raw_hid_lights_toggle(void) {
  175. change_led_state(!is_led_timeout);
  176. raw_hid_buffer[1] = CTRL_HID_OK;
  177. raw_hid_buffer[2] = (uint8_t) is_led_timeout;
  178. raw_hid_buffer[3] = CTRL_HID_EOM;
  179. }
  180. void raw_hid_led(uint8_t *data) {
  181. //rgb_matrix_set_color(data[1], data[2], data[3], data[4]);
  182. const uint8_t led = data[1];
  183. if (led >= RGB_MATRIX_LED_COUNT) {
  184. raw_hid_buffer[1] = CTRL_HID_NOK;
  185. raw_hid_buffer[2] = RGB_MATRIX_LED_COUNT;
  186. raw_hid_buffer[3] = CTRL_HID_EOM;
  187. return;
  188. }
  189. rgb_matrix_led_state[led].r = data[2];
  190. rgb_matrix_led_state[led].g = data[3];
  191. rgb_matrix_led_state[led].b = data[4];
  192. raw_hid_buffer[1] = CTRL_HID_OK;
  193. raw_hid_buffer[2] = CTRL_HID_EOM;
  194. }
  195. void raw_hid_leds(uint8_t *data) {
  196. const uint8_t first_led = data[1];
  197. const uint8_t number_leds = data[2];
  198. uint8_t i = 0;
  199. while (i < number_leds && first_led + i < RGB_MATRIX_LED_COUNT && i * 3 + 5 < RAW_EPSIZE) {
  200. rgb_matrix_led_state[first_led + i].r = data[3 + i * 3 + 0];
  201. rgb_matrix_led_state[first_led + i].g = data[3 + i * 3 + 1];
  202. rgb_matrix_led_state[first_led + i].b = data[3 + i * 3 + 2];
  203. i++;
  204. }
  205. raw_hid_buffer[1] = CTRL_HID_OK;
  206. raw_hid_buffer[2] = i;
  207. raw_hid_buffer[3] = CTRL_HID_EOM;
  208. }
  209. void raw_hid_rgbmatrix_mode(uint8_t *data) {
  210. const uint8_t mode = data[1];
  211. if (mode >= RGB_MATRIX_EFFECT_MAX) {
  212. raw_hid_buffer[1] = CTRL_HID_NOK;
  213. raw_hid_buffer[2] = RGB_MATRIX_EFFECT_MAX - 1;
  214. raw_hid_buffer[3] = CTRL_HID_EOM;
  215. return;
  216. }
  217. rgb_matrix_mode_noeeprom(mode);
  218. raw_hid_buffer[1] = CTRL_HID_OK;
  219. raw_hid_buffer[2] = CTRL_HID_EOM;
  220. }
  221. void raw_hid_receive(uint8_t *data, uint8_t length) {
  222. switch (*data) {
  223. case CTRL_HID_HELLO:
  224. raw_hid_say_hello();
  225. break;
  226. case CTRL_HID_LIGHTS_TOGGLE:
  227. raw_hid_lights_toggle();
  228. break;
  229. case CTRL_HID_LED:
  230. raw_hid_led(data);
  231. break;
  232. case CTRL_HID_LEDS:
  233. raw_hid_leds(data);
  234. break;
  235. case CTRL_HID_RGBMATRIX_MODE:
  236. raw_hid_rgbmatrix_mode(data);
  237. break;
  238. }
  239. raw_hid_perform_send();
  240. }