gourdo1_encoder.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Copyright 2021 Jonavin Eng @Jonavin
  2. Copyright 2022 gourdo1 <gourdo1@outlook.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 QMK_KEYBOARD_H
  15. #include "gourdo1.h"
  16. #ifdef ENCODER_ENABLE
  17. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  18. #define DYNAMIC_KEYMAP_LAYER_COUNT 4 //default in case this is not already defined elsewhere
  19. #endif
  20. #ifndef ENCODER_DEFAULTACTIONS_INDEX
  21. #define ENCODER_DEFAULTACTIONS_INDEX 0 // can select encoder index if there are multiple encoders
  22. #endif
  23. static uint16_t key_timer;
  24. void encoder_action_volume(bool clockwise) {
  25. if (clockwise) {
  26. tap_code(KC_VOLU);
  27. if (timer_elapsed(key_timer) < 50) {
  28. tap_code(KC_VOLU); // if less than 50ms have passed, hit vol up again.
  29. key_timer = timer_read();
  30. } else {
  31. key_timer = timer_read();
  32. // do nothing if 50ms or more have passed
  33. }
  34. }
  35. else {
  36. tap_code(KC_VOLD);
  37. if (timer_elapsed(key_timer) < 100) {
  38. tap_code(KC_VOLD); // if less than 100ms have passed, hit vol down twice.
  39. tap_code(KC_VOLD);
  40. key_timer = timer_read();
  41. } else {
  42. key_timer = timer_read();
  43. // do nothing if 100ms or more have passed
  44. }
  45. }
  46. }
  47. void encoder_action_mediatrack(bool clockwise) {
  48. if (clockwise)
  49. tap_code(KC_MEDIA_NEXT_TRACK);
  50. else
  51. tap_code(KC_MEDIA_PREV_TRACK);
  52. }
  53. void encoder_action_navword(bool clockwise) {
  54. if (clockwise)
  55. tap_code16(LCTL(KC_RGHT));
  56. else
  57. tap_code16(LCTL(KC_LEFT));
  58. }
  59. void encoder_action_navpage(bool clockwise) {
  60. if (clockwise)
  61. tap_code16(KC_PGUP);
  62. else
  63. tap_code16(KC_PGDN);
  64. }
  65. // LAYER HANDLING
  66. uint8_t selected_layer = 0;
  67. uint8_t get_selected_layer(void) {
  68. return selected_layer;
  69. }
  70. void encoder_action_layerchange(bool clockwise) {
  71. if (clockwise) {
  72. if(selected_layer < (DYNAMIC_KEYMAP_LAYER_COUNT - 1)) {
  73. selected_layer ++;
  74. layer_move(selected_layer);
  75. }
  76. } else {
  77. if (selected_layer > 0) {
  78. selected_layer --;
  79. layer_move(selected_layer);
  80. }
  81. }
  82. }
  83. #ifdef RGB_MATRIX_ENABLE
  84. void encoder_action_rgb_speed(bool clockwise) {
  85. if (clockwise)
  86. rgb_matrix_increase_speed_noeeprom();
  87. else
  88. rgb_matrix_decrease_speed_noeeprom();
  89. }
  90. void encoder_action_rgb_hue(bool clockwise) {
  91. if (clockwise)
  92. rgb_matrix_increase_hue_noeeprom();
  93. else
  94. rgb_matrix_decrease_hue_noeeprom();
  95. }
  96. void encoder_action_rgb_saturation(bool clockwise) {
  97. if (clockwise)
  98. rgb_matrix_increase_sat_noeeprom();
  99. else
  100. rgb_matrix_decrease_sat_noeeprom();
  101. }
  102. void encoder_action_rgb_brightness(bool clockwise) {
  103. if (clockwise)
  104. rgb_matrix_increase_val_noeeprom();
  105. else
  106. rgb_matrix_decrease_val_noeeprom();
  107. }
  108. void encoder_action_rgb_mode(bool clockwise) {
  109. if (clockwise)
  110. rgb_matrix_step_noeeprom();
  111. else
  112. rgb_matrix_step_reverse_noeeprom();
  113. }
  114. #elif defined(RGBLIGHT_ENABLE)
  115. void encoder_action_rgb_speed(bool clockwise) {
  116. if (clockwise)
  117. rgblight_increase_speed_noeeprom();
  118. else
  119. rgblight_decrease_speed_noeeprom();
  120. }
  121. void encoder_action_rgb_hue(bool clockwise) {
  122. if (clockwise)
  123. rgblight_increase_hue_noeeprom();
  124. else
  125. rgblight_decrease_hue_noeeprom();
  126. }
  127. void encoder_action_rgb_saturation(bool clockwise) {
  128. if (clockwise)
  129. rgblight_increase_sat_noeeprom();
  130. else
  131. rgblight_decrease_sat_noeeprom();
  132. }
  133. void encoder_action_rgb_brightness(bool clockwise) {
  134. if (clockwise)
  135. rgblight_increase_val_noeeprom();
  136. else
  137. rgblight_decrease_val_noeeprom();
  138. }
  139. void encoder_action_rgb_mode(bool clockwise) {
  140. if (clockwise)
  141. rgblight_step_noeeprom();
  142. else
  143. rgblight_step_reverse_noeeprom();
  144. }
  145. #endif // RGB_MATRIX_ENABLE || RGBLIGHT_ENABLE
  146. #ifdef ALTTAB_SCROLL_ENABLE
  147. bool is_tab_scrolling = false;
  148. bool is_alt_tab_active = false;
  149. uint16_t alt_tab_timer = 0;
  150. void encoder_toggle_alttabscroll(void) {
  151. is_tab_scrolling = !is_tab_scrolling;
  152. }
  153. void encoder_action_alttabscroll(bool clockwise) {
  154. if (clockwise) {
  155. if (!is_alt_tab_active) {
  156. is_alt_tab_active = true;
  157. register_mods(MOD_RALT);
  158. }
  159. tap_code16(KC_TAB);
  160. }
  161. else {
  162. tap_code16(S(KC_TAB));
  163. }
  164. alt_tab_timer = timer_read();
  165. }
  166. void encoder_tick_alttabscroll(void) {
  167. if (is_alt_tab_active) {
  168. if (timer_elapsed(alt_tab_timer) > 600) {
  169. unregister_mods(MOD_RALT);
  170. is_alt_tab_active = false;
  171. }
  172. }
  173. }
  174. #endif // ALTTAB_SCROLL_ENABLE
  175. #endif // ENCODER_ENABLE
  176. #if defined(ENCODER_ENABLE) && defined(ENCODER_DEFAULTACTIONS_ENABLE) // Encoder Functionality
  177. __attribute__((weak)) bool encoder_update_keymap(uint8_t index, bool clockwise) { return true; }
  178. bool encoder_update_user(uint8_t index, bool clockwise) {
  179. if (!encoder_update_keymap(index, clockwise)) { return false; }
  180. if (index != ENCODER_DEFAULTACTIONS_INDEX) {return true;} // exit if the index doesn't match
  181. uint8_t mods_state = get_mods();
  182. if (mods_state & MOD_BIT(KC_LSFT) ) { // If you are holding L shift, encoder changes layers
  183. encoder_action_layerchange(clockwise);
  184. } else if (mods_state & MOD_BIT(KC_RSFT) ) { // If you are holding R shift, Page up/dn
  185. unregister_mods(MOD_BIT(KC_RSFT));
  186. encoder_action_navpage(clockwise);
  187. register_mods(MOD_BIT(KC_RSFT));
  188. } else if (mods_state & MOD_BIT(KC_LCTL)) { // if holding Left Ctrl, navigate next/prev word
  189. encoder_action_navword(clockwise);
  190. } else if (mods_state & MOD_BIT(KC_LALT)) { // if holding Left Alt, change media next/prev track
  191. encoder_action_mediatrack(clockwise);
  192. } else {
  193. switch(get_highest_layer(layer_state)) {
  194. case _FN1:
  195. #ifdef IDLE_TIMEOUT_ENABLE
  196. timeout_update_threshold(clockwise);
  197. #endif
  198. break;
  199. default:
  200. #ifdef ALTTAB_SCROLL_ENABLE
  201. if (is_tab_scrolling)
  202. encoder_action_alttabscroll(clockwise);
  203. else
  204. encoder_action_volume(clockwise); // Otherwise it just changes volume
  205. #else
  206. encoder_action_volume(clockwise); // Otherwise it just changes volume
  207. #endif // ALTTAB_SCROLL_ENABLE
  208. break;
  209. }
  210. }
  211. return false;
  212. }
  213. #endif // ENCODER_ENABLE