keymap.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include QMK_KEYBOARD_H
  2. #ifdef PROTOCOL_LUFA
  3. #include "lufa.h"
  4. #include "split_util.h"
  5. #endif
  6. #ifdef SSD1306OLED
  7. #include "ssd1306.h"
  8. #endif
  9. extern keymap_config_t keymap_config;
  10. #ifdef RGBLIGHT_ENABLE
  11. //Following line allows macro to read current RGB settings
  12. extern rgblight_config_t rgblight_config;
  13. #endif
  14. extern uint8_t is_master;
  15. // Each layer gets a name for readability, which is then used in the keymap matrix below.
  16. // The underscores don't mean anything - you can have a layer called STUFF or any other name.
  17. // Layer names don't all need to be of the same length, obviously, and you can also skip them
  18. // entirely and just use numbers.
  19. #define _QWERTY 0
  20. #define _LOWER 1
  21. #define _RAISE 2
  22. #define _ADJUST 3
  23. enum custom_keycodes {
  24. QWERTY = SAFE_RANGE,
  25. LOWER,
  26. RAISE,
  27. ADJUST,
  28. BACKLIT,
  29. RGBRST
  30. };
  31. enum macro_keycodes {
  32. KC_SAMPLEMACRO,
  33. };
  34. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  35. [_QWERTY] = LAYOUT( \
  36. //,-----------------------------------------. ,-----------------------------------------.
  37. KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P,KC_BSPC,\
  38. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  39. KC_LCTL, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L,KC_SCLN,KC_QUOT,\
  40. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  41. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M,KC_COMM,KC_DOT,KC_SLSH,KC_RSFT,\
  42. //|------+------+------+------+------+------+------| |------+------+------+------+------+------+------|
  43. KC_LGUI, LOWER,KC_SPC, KC_ENT, RAISE,KC_RALT \
  44. //`--------------------' `--------------------'
  45. ),
  46. [_LOWER] = LAYOUT( \
  47. //,-----------------------------------------. ,-----------------------------------------.
  48. KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0,KC_BSPC,\
  49. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  50. KC_LCTL, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_LEFT,KC_DOWN,KC_UP, KC_RIGHT,KC_NO,KC_NO,\
  51. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  52. KC_LSFT, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,\
  53. //|------+------+------+------+------+------+------| |------+------+------+------+------+------+------|
  54. KC_LGUI, LOWER,KC_SPC, KC_ENT, RAISE,KC_RALT \
  55. //`--------------------' `--------------------'
  56. ),
  57. [_RAISE] = LAYOUT( \
  58. //,-----------------------------------------. ,-----------------------------------------.
  59. KC_ESC,KC_EXLM,KC_AT,KC_HASH,KC_DLR,KC_PERC, KC_CIRC,KC_AMPR,KC_ASTR,KC_LPRN,KC_RPRN,KC_BSPC,\
  60. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  61. KC_LCTL, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_MINS,KC_EQL,KC_LCBR,KC_RCBR,KC_PIPE,KC_GRV,\
  62. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  63. KC_LSFT, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_UNDS,KC_PLUS,KC_LBRC,KC_RBRC,KC_BSLS,KC_TILD,\
  64. //|------+------+------+------+------+------+------| |------+------+------+------+------+------+------|
  65. KC_LGUI, LOWER,KC_SPC, KC_ENT, RAISE,KC_RALT \
  66. //`--------------------' `--------------------'
  67. ),
  68. [_ADJUST] = LAYOUT( \
  69. //,-----------------------------------------. ,-----------------------------------------.
  70. RESET,RGBRST, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,\
  71. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  72. RGB_TOG,RGB_HUI,RGB_SAI,RGB_VAI,KC_NO,KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,\
  73. //|------+------+------+------+------+------| |------+------+------+------+------+------|
  74. RGB_MOD,RGB_HUD,RGB_SAD,RGB_VAD,KC_NO,KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO, KC_NO,\
  75. //|------+------+------+------+------+------+------| |------+------+------+------+------+------+------|
  76. KC_LGUI, LOWER,KC_SPC, KC_ENT, RAISE,KC_RALT \
  77. //`--------------------' `--------------------'
  78. )
  79. };
  80. int RGB_current_mode;
  81. void persistent_default_layer_set(uint16_t default_layer) {
  82. eeconfig_update_default_layer(default_layer);
  83. default_layer_set(default_layer);
  84. }
  85. // Setting ADJUST layer RGB back to default
  86. void update_tri_layer_RGB(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  87. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  88. layer_on(layer3);
  89. } else {
  90. layer_off(layer3);
  91. }
  92. }
  93. void matrix_init_user(void) {
  94. #ifdef RGBLIGHT_ENABLE
  95. RGB_current_mode = rgblight_config.mode;
  96. #endif
  97. //SSD1306 OLED init, make sure to add #define SSD1306OLED in config.h
  98. #ifdef SSD1306OLED
  99. iota_gfx_init(!has_usb()); // turns on the display
  100. #endif
  101. }
  102. //SSD1306 OLED update loop, make sure to add #define SSD1306OLED in config.h
  103. #ifdef SSD1306OLED
  104. // When add source files to SRC in rules.mk, you can use functions.
  105. const char *read_layer_state(void);
  106. const char *read_logo(void);
  107. void set_keylog(uint16_t keycode, keyrecord_t *record);
  108. const char *read_keylog(void);
  109. const char *read_keylogs(void);
  110. // const char *read_mode_icon(bool swap);
  111. // const char *read_host_led_state(void);
  112. // void set_timelog(void);
  113. // const char *read_timelog(void);
  114. void matrix_scan_user(void) {
  115. iota_gfx_task();
  116. }
  117. void matrix_render_user(struct CharacterMatrix *matrix) {
  118. if (is_master) {
  119. // If you want to change the display of OLED, you need to change here
  120. matrix_write_ln(matrix, read_layer_state());
  121. matrix_write_ln(matrix, read_keylog());
  122. //matrix_write_ln(matrix, read_keylogs());
  123. //matrix_write_ln(matrix, read_mode_icon(keymap_config.swap_lalt_lgui));
  124. //matrix_write_ln(matrix, read_host_led_state());
  125. //matrix_write_ln(matrix, read_timelog());
  126. } else {
  127. matrix_write(matrix, read_logo());
  128. }
  129. }
  130. void matrix_update(struct CharacterMatrix *dest, const struct CharacterMatrix *source) {
  131. if (memcmp(dest->display, source->display, sizeof(dest->display))) {
  132. memcpy(dest->display, source->display, sizeof(dest->display));
  133. dest->dirty = true;
  134. }
  135. }
  136. void iota_gfx_task_user(void) {
  137. struct CharacterMatrix matrix;
  138. matrix_clear(&matrix);
  139. matrix_render_user(&matrix);
  140. matrix_update(&display, &matrix);
  141. }
  142. #endif//SSD1306OLED
  143. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  144. if (record->event.pressed) {
  145. #ifdef SSD1306OLED
  146. set_keylog(keycode, record);
  147. #endif
  148. // set_timelog();
  149. }
  150. switch (keycode) {
  151. case QWERTY:
  152. if (record->event.pressed) {
  153. persistent_default_layer_set(1UL<<_QWERTY);
  154. }
  155. return false;
  156. case LOWER:
  157. if (record->event.pressed) {
  158. layer_on(_LOWER);
  159. update_tri_layer_RGB(_LOWER, _RAISE, _ADJUST);
  160. } else {
  161. layer_off(_LOWER);
  162. update_tri_layer_RGB(_LOWER, _RAISE, _ADJUST);
  163. }
  164. return false;
  165. case RAISE:
  166. if (record->event.pressed) {
  167. layer_on(_RAISE);
  168. update_tri_layer_RGB(_LOWER, _RAISE, _ADJUST);
  169. } else {
  170. layer_off(_RAISE);
  171. update_tri_layer_RGB(_LOWER, _RAISE, _ADJUST);
  172. }
  173. return false;
  174. case ADJUST:
  175. if (record->event.pressed) {
  176. layer_on(_ADJUST);
  177. } else {
  178. layer_off(_ADJUST);
  179. }
  180. return false;
  181. case RGB_MOD:
  182. #ifdef RGBLIGHT_ENABLE
  183. if (record->event.pressed) {
  184. rgblight_mode(RGB_current_mode);
  185. rgblight_step();
  186. RGB_current_mode = rgblight_config.mode;
  187. }
  188. #endif
  189. return false;
  190. case RGBRST:
  191. #ifdef RGBLIGHT_ENABLE
  192. if (record->event.pressed) {
  193. eeconfig_update_rgblight_default();
  194. rgblight_enable();
  195. RGB_current_mode = rgblight_config.mode;
  196. }
  197. #endif
  198. break;
  199. }
  200. return true;
  201. }