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.

278 lines
8.0 KiB

  1. /* Copyright 2020 Jumail Mundekkat / MxBlue
  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. * Extended from the work done by fcoury: https://github.com/qmk/qmk_firmware/pull/4915
  17. */
  18. #include "mxss_frontled.h"
  19. #include "tmk_core/common/eeprom.h"
  20. #include "rgblight.h"
  21. #include "via.h"
  22. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  23. // Variables for controlling front LED application
  24. uint8_t fled_mode; // Mode for front LEDs
  25. uint8_t fled_val; // Brightness for front leds (0 - 255)
  26. LED_TYPE fleds[2]; // Front LED rgb values for indicator mode use
  27. // Layer indicator colors
  28. __attribute__ ((weak))
  29. hs_set layer_colors[FRONTLED_COLOR_MAXCNT];
  30. // Caps lock indicator color
  31. __attribute__ ((weak))
  32. hs_set caps_color;
  33. __attribute__ ((weak))
  34. size_t lc_size = sizeof(layer_colors) / sizeof(hs_set);
  35. void fled_init(void) {
  36. // If EEPROM config exists, load it
  37. // If VIA EEPROM exists, FLED config should too
  38. if (via_eeprom_is_valid()) {
  39. fled_load_conf();
  40. // Else, default config
  41. } else {
  42. // Default mode/brightness
  43. fled_mode = FLED_RGB;
  44. fled_val = 10 * FLED_VAL_STEP;
  45. // Default colors
  46. caps_color.hue = 0;
  47. caps_color.sat = 255;
  48. layer_colors[0].hue = 0;
  49. layer_colors[0].sat = 0;
  50. layer_colors[1].hue = 86;
  51. layer_colors[1].sat = 255;
  52. layer_colors[2].hue = 36;
  53. layer_colors[2].sat = 255;
  54. layer_colors[3].hue = 185;
  55. layer_colors[3].sat = 255;
  56. fled_update_conf(); // Store default config to EEPROM
  57. }
  58. // Set default values for leds
  59. setrgb(0, 0, 0, &fleds[0]);
  60. setrgb(0, 0, 0, &fleds[1]);
  61. // Handle lighting for indicator mode
  62. if (fled_mode == FLED_INDI) {
  63. fled_lock_update(host_keyboard_led_state());
  64. fled_layer_update(layer_state);
  65. }
  66. }
  67. void process_record_fled(uint16_t keycode, keyrecord_t *record) {
  68. // Handle custom keycodes for front LED operation
  69. switch (keycode) {
  70. case FLED_MOD: // Change between front LED operation modes (off, indicator, RGB)
  71. if (record->event.pressed)
  72. fled_mode_cycle();
  73. break;
  74. case FLED_VAI: // Increase the brightness of the front LEDs by FLED_VAL_STEP
  75. if (record->event.pressed)
  76. fled_val_increase();
  77. break;
  78. case FLED_VAD: // Decrease the brightness of the front LEDs by FLED_VAL_STEP
  79. if (record->event.pressed)
  80. fled_val_decrease();
  81. break;
  82. default:
  83. break; // Process all other keycodes normally
  84. }
  85. return;
  86. }
  87. void fled_load_conf(void) {
  88. // Load config
  89. fled_config fled_conf;
  90. fled_conf.raw = eeprom_read_byte(FRONTLED_CONF_ADDR);
  91. fled_mode = fled_conf.mode;
  92. fled_val = fled_conf.val * FLED_VAL_STEP;
  93. // Load color data
  94. uint8_t stored_cnt = eeprom_read_byte(FRONTLED_COLOR_CNT_ADDR);
  95. uint16_t *color_ptr = FRONTLED_COLOR_ADDR;
  96. caps_color.raw = eeprom_read_word(color_ptr); // Should always store at least 1 color
  97. for (uint8_t i = 1; i < stored_cnt; i++) {
  98. if (i == lc_size) // Can't load more layers than we have available
  99. break;
  100. layer_colors[i].raw = eeprom_read_word(&color_ptr[i]);
  101. }
  102. layer_colors[0].raw = 0; // hue = sat = 0 for layer 0
  103. }
  104. // Store current front led config in EEPROM
  105. void fled_update_conf(void)
  106. {
  107. // Create storage struct and set values
  108. fled_config conf;
  109. conf.mode = fled_mode;
  110. // Small hack to ensure max value is stored correctly
  111. if (fled_val == 255)
  112. conf.val = 256 / FLED_VAL_STEP;
  113. else
  114. conf.val = fled_val / FLED_VAL_STEP;
  115. // Store config
  116. eeprom_update_byte(FRONTLED_CONF_ADDR, conf.raw);
  117. // Store color data
  118. uint16_t *color_ptr = FRONTLED_COLOR_ADDR;
  119. eeprom_update_word(color_ptr, caps_color.raw);
  120. // Start from 1, layer 0 is not modifiable and therefore not persisted
  121. uint8_t i = 1;
  122. for (; i < lc_size; i++) {
  123. if (i == FRONTLED_COLOR_MAXCNT) // Can't store more than the EEPROM we have available
  124. break;
  125. eeprom_update_word(&color_ptr[i], layer_colors[i].raw);
  126. }
  127. eeprom_update_byte(FRONTLED_COLOR_CNT_ADDR, i); // For safety, store the count of colors stored
  128. }
  129. // Custom keycode functions
  130. void fled_mode_cycle(void)
  131. {
  132. // FLED -> FLED_RGB -> FLED_INDI
  133. switch (fled_mode) {
  134. case FLED_OFF:
  135. fled_mode = FLED_RGB;
  136. rgblight_timer_enable();
  137. break;
  138. case FLED_RGB:
  139. fled_mode = FLED_INDI;
  140. break;
  141. case FLED_INDI:
  142. fled_mode = FLED_OFF;
  143. break;
  144. }
  145. // Update stored config
  146. fled_update_conf();
  147. rgblight_set();
  148. }
  149. void fled_val_increase(void)
  150. {
  151. // Increase val by FLED_VAL_STEP, handling the upper edge case
  152. if (fled_val + FLED_VAL_STEP > 255)
  153. fled_val = 255;
  154. else
  155. fled_val += FLED_VAL_STEP;
  156. // Update stored config
  157. fled_update_conf();
  158. // Update and set LED state
  159. if (fled_mode == FLED_INDI) {
  160. fled_layer_update(layer_state);
  161. fled_lock_update(host_keyboard_led_state());
  162. } else {
  163. rgblight_set();
  164. }
  165. }
  166. void fled_val_decrease(void)
  167. {
  168. // Decrease val by FLED_VAL_STEP, handling the lower edge case
  169. if (fled_val - FLED_VAL_STEP > 255)
  170. fled_val = 255;
  171. else
  172. fled_val -= FLED_VAL_STEP;
  173. // Update stored config
  174. fled_update_conf();
  175. // Update and set LED state
  176. if (fled_mode == FLED_INDI) {
  177. fled_layer_update(layer_state);
  178. fled_lock_update(host_keyboard_led_state());
  179. } else {
  180. rgblight_set();
  181. }
  182. }
  183. void fled_layer_update(layer_state_t state) {
  184. // Determine and set colour of layer LED according to current layer
  185. // if hue = sat = 0, leave LED off
  186. uint8_t layer = get_highest_layer(state);
  187. if (layer < lc_size && !(layer_colors[layer].hue == 0 && layer_colors[layer].sat == 0)) {
  188. sethsv(layer_colors[layer].hue, layer_colors[layer].sat, fled_val, &fleds[1]);
  189. } else {
  190. setrgb(0, 0, 0, &fleds[1]);
  191. }
  192. }
  193. void fled_lock_update(led_t led_state) {
  194. // Set indicator LED appropriately, whether it is used or not
  195. if (led_state.caps_lock) {
  196. sethsv(caps_color.hue, caps_color.sat, fled_val, &fleds[0]);
  197. } else {
  198. setrgb(0, 0, 0, &fleds[0]);
  199. }
  200. rgblight_set();
  201. }
  202. void set_fled_layer_color(uint8_t layer, hs_set hs) {
  203. // Update layer colors and refresh LEDs
  204. layer_colors[layer] = hs;
  205. fled_layer_update(layer_state);
  206. fled_update_conf();
  207. }
  208. hs_set get_fled_layer_color(uint8_t layer) {
  209. return layer_colors[layer];
  210. }
  211. void set_fled_caps_color(hs_set hs) {
  212. // Update caplock color and refresh LEDs
  213. caps_color = hs;
  214. fled_lock_update(host_keyboard_led_state());
  215. fled_update_conf();
  216. }
  217. hs_set get_fled_caps_color(void) {
  218. return caps_color;
  219. }
  220. // Fallback eeprom functions if VIA is not enabled
  221. #ifndef VIA_ENABLE
  222. // Can be called in an overriding via_init_kb() to test if keyboard level code usage of
  223. // EEPROM is invalid and use/save defaults.
  224. bool via_eeprom_is_valid(void)
  225. {
  226. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  227. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  228. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  229. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  230. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  231. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  232. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  233. }
  234. #endif