nibble_encoder.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright 2020 Jay Greco
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "nibble_encoder.h"
  17. uint8_t encoder_value = 0x20,
  18. encoder_mode = ENC_MODE_VOLUME,
  19. enabled_encoder_modes = 0x1F;
  20. uint16_t retrieve_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior) {
  21. #ifdef DYNAMIC_KEYMAP_ENABLE
  22. void* addr = (void*)(EEPROM_CUSTOM_ENCODER + (encoder_idx * 6) + (behavior * 2));
  23. uint16_t keycode = eeprom_read_byte(addr) << 8;
  24. keycode |= eeprom_read_byte(addr + 1);
  25. return keycode;
  26. #else
  27. return 0;
  28. #endif
  29. }
  30. void set_custom_encoder_config(uint8_t encoder_idx, uint8_t behavior, uint16_t new_code) {
  31. #ifdef DYNAMIC_KEYMAP_ENABLE
  32. void* addr = (void*)(EEPROM_CUSTOM_ENCODER + (encoder_idx * 6) + (behavior * 2));
  33. eeprom_update_byte(addr, (uint8_t)(new_code >> 8));
  34. eeprom_update_byte(addr + 1, (uint8_t)(new_code & 0xFF));
  35. #endif
  36. }
  37. void pre_encoder_mode_change(void) {
  38. dprintf("Changing encoder mode: %u\n", encoder_mode);
  39. }
  40. void post_encoder_mode_change(void) {
  41. dprintf("Encoder mode: %u\n", encoder_mode);
  42. }
  43. //???
  44. void change_encoder_mode(bool clockwise) {
  45. pre_encoder_mode_change();
  46. if(enabled_encoder_modes == 0){
  47. enabled_encoder_modes = 0x1F;
  48. }
  49. do {
  50. if(!clockwise){
  51. if (encoder_mode == 0){
  52. encoder_mode = _NUM_ENCODER_MODES - 1;
  53. } else{
  54. encoder_mode = encoder_mode - 1;
  55. }
  56. } else {
  57. encoder_mode = (encoder_mode + 1) % _NUM_ENCODER_MODES;
  58. }
  59. } while(((1 << encoder_mode) & enabled_encoder_modes) == 0);
  60. post_encoder_mode_change();
  61. }
  62. uint16_t handle_encoder_cw(void) {
  63. dprintf("Encoder mode: %u\n", encoder_mode);
  64. uint16_t mapped_code = 0;
  65. switch(encoder_mode){
  66. default:
  67. break;
  68. case ENC_MODE_VOLUME:
  69. mapped_code = KC_VOLU;
  70. break;
  71. case ENC_MODE_MEDIA:
  72. mapped_code = KC_MEDIA_NEXT_TRACK;
  73. break;
  74. case ENC_MODE_SCROLL:
  75. mapped_code = KC_WH_D;
  76. break;
  77. case ENC_MODE_BACKLIGHT:
  78. mapped_code = RGB_VAI;
  79. break;
  80. #ifdef DYNAMIC_KEYMAP_ENABLE
  81. case ENC_MODE_CUSTOM0:
  82. mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_CW);
  83. break;
  84. case ENC_MODE_CUSTOM1:
  85. mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_CW);
  86. break;
  87. case ENC_MODE_CUSTOM2:
  88. mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_CW);
  89. break;
  90. #endif
  91. }
  92. return mapped_code;
  93. }
  94. uint16_t handle_encoder_ccw(void) {
  95. dprintf("Encoder mode: %u\n", encoder_mode);
  96. uint16_t mapped_code = 0;
  97. switch(encoder_mode){
  98. default:
  99. break;
  100. case ENC_MODE_VOLUME:
  101. mapped_code = KC_VOLD;
  102. break;
  103. case ENC_MODE_MEDIA:
  104. mapped_code = KC_MEDIA_PREV_TRACK;
  105. break;
  106. case ENC_MODE_SCROLL:
  107. mapped_code = KC_WH_U;
  108. break;
  109. case ENC_MODE_BACKLIGHT:
  110. mapped_code = RGB_VAD;
  111. break;
  112. #ifdef DYNAMIC_KEYMAP_ENABLE
  113. case ENC_MODE_CUSTOM0:
  114. mapped_code = retrieve_custom_encoder_config(0, ENC_CUSTOM_CCW);
  115. break;
  116. case ENC_MODE_CUSTOM1:
  117. mapped_code = retrieve_custom_encoder_config(1, ENC_CUSTOM_CCW);
  118. break;
  119. case ENC_MODE_CUSTOM2:
  120. mapped_code = retrieve_custom_encoder_config(2, ENC_CUSTOM_CCW);
  121. break;
  122. #endif
  123. }
  124. return mapped_code;
  125. }