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.

119 lines
5.6 KiB

  1. /* Copyright 2023 Cipulot
  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 3 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 "ec_switch_matrix.h"
  17. #include "quantum.h"
  18. void eeconfig_init_kb(void) {
  19. // Default values
  20. eeprom_ec_config.num.h = 0;
  21. eeprom_ec_config.num.s = 0;
  22. eeprom_ec_config.num.v = 60;
  23. eeprom_ec_config.num.enabled = true;
  24. eeprom_ec_config.caps.h = 0;
  25. eeprom_ec_config.caps.s = 0;
  26. eeprom_ec_config.caps.v = 60;
  27. eeprom_ec_config.caps.enabled = true;
  28. eeprom_ec_config.scroll.h = 0;
  29. eeprom_ec_config.scroll.s = 0;
  30. eeprom_ec_config.scroll.v = 60;
  31. eeprom_ec_config.scroll.enabled = true;
  32. eeprom_ec_config.actuation_mode = DEFAULT_ACTUATION_MODE;
  33. eeprom_ec_config.mode_0_actuation_threshold = DEFAULT_MODE_0_ACTUATION_LEVEL;
  34. eeprom_ec_config.mode_0_release_threshold = DEFAULT_MODE_0_RELEASE_LEVEL;
  35. eeprom_ec_config.mode_1_initial_deadzone_offset = DEFAULT_MODE_1_INITIAL_DEADZONE_OFFSET;
  36. eeprom_ec_config.mode_1_actuation_offset = DEFAULT_MODE_1_ACTUATION_OFFSET;
  37. eeprom_ec_config.mode_1_release_offset = DEFAULT_MODE_1_RELEASE_OFFSET;
  38. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  39. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  40. eeprom_ec_config.bottoming_reading[row][col] = DEFAULT_BOTTOMING_READING;
  41. }
  42. }
  43. // Write default value to EEPROM now
  44. eeconfig_update_kb_datablock(&eeprom_ec_config);
  45. eeconfig_init_user();
  46. }
  47. // On Keyboard startup
  48. void keyboard_post_init_kb(void) {
  49. // Read custom menu variables from memory
  50. eeconfig_read_kb_datablock(&eeprom_ec_config);
  51. // Set runtime values to EEPROM values
  52. ec_config.actuation_mode = eeprom_ec_config.actuation_mode;
  53. ec_config.mode_0_actuation_threshold = eeprom_ec_config.mode_0_actuation_threshold;
  54. ec_config.mode_0_release_threshold = eeprom_ec_config.mode_0_release_threshold;
  55. ec_config.mode_1_initial_deadzone_offset = eeprom_ec_config.mode_1_initial_deadzone_offset;
  56. ec_config.mode_1_actuation_offset = eeprom_ec_config.mode_1_actuation_offset;
  57. ec_config.mode_1_release_offset = eeprom_ec_config.mode_1_release_offset;
  58. ec_config.bottoming_calibration = false;
  59. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  60. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  61. ec_config.bottoming_calibration_starter[row][col] = true;
  62. ec_config.bottoming_reading[row][col] = eeprom_ec_config.bottoming_reading[row][col];
  63. ec_config.rescaled_mode_0_actuation_threshold[row][col] = rescale(ec_config.mode_0_actuation_threshold, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
  64. ec_config.rescaled_mode_0_release_threshold[row][col] = rescale(ec_config.mode_0_release_threshold, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
  65. ec_config.rescaled_mode_1_initial_deadzone_offset[row][col] = rescale(ec_config.mode_1_initial_deadzone_offset, 0, 1023, ec_config.noise_floor[row][col], eeprom_ec_config.bottoming_reading[row][col]);
  66. }
  67. }
  68. // Set the RGB LEDs range that will be used for the effects
  69. rgblight_set_effect_range(3, 66);
  70. // Call the indicator callback to set the indicator color
  71. indicators_callback();
  72. keyboard_post_init_user();
  73. }
  74. // This function gets called when caps, num, scroll change
  75. bool led_update_kb(led_t led_state) {
  76. indicators_callback();
  77. return true;
  78. }
  79. // This function is called when layers change
  80. layer_state_t layer_state_set_user(layer_state_t state) {
  81. indicators_callback();
  82. return state;
  83. }
  84. // INDICATOR CALLBACK ------------------------------------------------------------------------------
  85. /* LED index to physical position
  86. *
  87. * LED2 | LED1 | LED0
  88. * -----+------+--------
  89. * Num | Caps | Scroll |
  90. */
  91. bool indicators_callback(void) {
  92. if ((eeprom_ec_config.num.enabled) && (host_keyboard_led_state().num_lock))
  93. sethsv(eeprom_ec_config.num.h, eeprom_ec_config.num.s, eeprom_ec_config.num.v, (rgb_led_t *)&led[NUM_INDICATOR_INDEX]);
  94. else
  95. sethsv(0, 0, 0, (rgb_led_t *)&led[NUM_INDICATOR_INDEX]);
  96. if ((eeprom_ec_config.caps.enabled) && (host_keyboard_led_state().caps_lock))
  97. sethsv(eeprom_ec_config.caps.h, eeprom_ec_config.caps.s, eeprom_ec_config.caps.v, (rgb_led_t *)&led[CAPS_INDICATOR_INDEX]);
  98. else
  99. sethsv(0, 0, 0, (rgb_led_t *)&led[CAPS_INDICATOR_INDEX]);
  100. if ((eeprom_ec_config.scroll.enabled) && (host_keyboard_led_state().scroll_lock))
  101. sethsv(eeprom_ec_config.scroll.h, eeprom_ec_config.scroll.s, eeprom_ec_config.scroll.v, (rgb_led_t *)&led[SCROLL_INDICATOR_INDEX]);
  102. else
  103. sethsv(0, 0, 0, (rgb_led_t *)&led[SCROLL_INDICATOR_INDEX]);
  104. return true;
  105. }