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.

211 lines
5.3 KiB

  1. /* Copyright 2020 Brandon Schlack
  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 "brandonschlack.h"
  17. user_config_t user_config;
  18. #ifdef STOPLIGHT_LED
  19. static stoplight_led_t stoplight_led;
  20. #endif
  21. /**
  22. * Resets user config in EEPROM
  23. *
  24. * Default is use rgb for layer indication
  25. */
  26. void eeconfig_init_user(void) {
  27. user_config.raw = 0;
  28. user_config.rgb_layer_change = true;
  29. user_config.rgb_theme = 0;
  30. eeconfig_update_user(user_config.raw);
  31. }
  32. __attribute__((weak))
  33. void matrix_init_keymap(void){ }
  34. void matrix_init_user(void) {
  35. matrix_init_keymap();
  36. }
  37. __attribute__((weak))
  38. void keyboard_post_init_keymap(void){ }
  39. /**
  40. * Reads user config from EEPROM,
  41. * calls RGB init if RGBs enabled
  42. */
  43. void keyboard_post_init_user(void){
  44. // Read the user config from EEPROM
  45. user_config.raw = eeconfig_read_user();
  46. // Do Stoplight Animation if enabled
  47. #ifdef STOPLIGHT_LED
  48. led_stoplight_start();
  49. #endif
  50. // Do RGB things if RGBs enabled
  51. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  52. keyboard_post_init_rgb();
  53. #endif
  54. keyboard_post_init_keymap();
  55. }
  56. __attribute__ ((weak))
  57. void shutdown_keymap(void) {}
  58. /**
  59. * On shutdown,
  60. * If RGBs enabled,
  61. * then set RGB color to Red
  62. */
  63. bool shutdown_user(bool jump_to_bootloader) {
  64. #ifdef RGBLIGHT_ENABLE
  65. rgblight_enable_noeeprom();
  66. rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
  67. rgblight_sethsv_noeeprom(0, 255, 127);
  68. #endif // RGBLIGHT_ENABLE
  69. #ifdef RGB_MATRIX_ENABLE
  70. rgb_matrix_set_color_all( 0xFF, 0x00, 0x00 );
  71. #endif //RGB_MATRIX_ENABLE
  72. shutdown_keymap();
  73. return false;
  74. }
  75. __attribute__ ((weak))
  76. void suspend_power_down_keymap(void) {}
  77. /**
  78. * Set rgb_matrix suspend state to true if not already
  79. */
  80. void suspend_power_down_user(void) {
  81. #ifdef RGB_MATRIX_ENABLE
  82. rgb_matrix_set_suspend_state(true);
  83. #endif //RGB_MATRIX_ENABLE
  84. suspend_power_down_keymap();
  85. }
  86. __attribute__ ((weak))
  87. void suspend_wakeup_init_keymap(void) {}
  88. /**
  89. * Set rgb_matrix suspend state to false if not already
  90. */
  91. void suspend_wakeup_init_user(void) {
  92. #ifdef RGB_MATRIX_ENABLE
  93. rgb_matrix_set_suspend_state(false);
  94. #endif //RGB_MATRIX_ENABLE
  95. suspend_wakeup_init_keymap();
  96. }
  97. __attribute__ ((weak))
  98. void matrix_scan_keymap(void) {}
  99. /**
  100. * Checks for Super CMDTAB
  101. */
  102. void matrix_scan_user(void) {
  103. matrix_scan_cmd_tab();
  104. #ifdef STOPLIGHT_LED
  105. matrix_scan_led_stoplight();
  106. #endif
  107. matrix_scan_keymap();
  108. }
  109. __attribute__ ((weak))
  110. layer_state_t default_layer_state_set_keymap(layer_state_t state) {
  111. return state;
  112. }
  113. /**
  114. * For macropads, if a new default layer is set from DF()
  115. * then automatically set that layer with layer_move()
  116. */
  117. layer_state_t default_layer_state_set_user(layer_state_t state) {
  118. #if defined(IS_MACROPAD)
  119. layer_move(get_highest_layer(state));
  120. #endif
  121. return default_layer_state_set_keymap(state);
  122. }
  123. __attribute__ ((weak))
  124. layer_state_t layer_state_set_keymap(layer_state_t state) {
  125. return state;
  126. }
  127. /**
  128. * Do RGB things (like layer indication) on layer change
  129. */
  130. layer_state_t layer_state_set_user(layer_state_t state) {
  131. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  132. state = layer_state_set_rgb(state);
  133. #endif // RGBLIGHT_ENABLE
  134. return layer_state_set_keymap(state);
  135. }
  136. __attribute__((weak)) bool led_update_keymap(led_t led_state) { return true; }
  137. bool led_update_user(led_t led_state) {
  138. #ifdef STOPLIGHT_LED
  139. if (stoplight_led.is_active) {
  140. return false;
  141. }
  142. #endif
  143. return led_update_keymap(led_state);
  144. }
  145. #ifdef STOPLIGHT_LED
  146. void led_stoplight_start(void) {
  147. writePin(TOP_LED, LED_ON(false));
  148. writePin(MIDDLE_LED, LED_ON(false));
  149. writePin(BOTTOM_LED, LED_ON(false));
  150. stoplight_led.is_active = true;
  151. stoplight_led.timer = timer_read();
  152. };
  153. void led_stoplight_set(pin_t pin) {
  154. writePin(pin, LED_ON(true));
  155. };
  156. void led_stoplight_end(void) {
  157. // Reset timer and status variables
  158. stoplight_led.is_active = false;
  159. stoplight_led.index = 0;
  160. stoplight_led.timer = 0;
  161. led_update_kb(host_keyboard_led_state());
  162. };
  163. void matrix_scan_led_stoplight(void) {
  164. if (stoplight_led.is_active) {
  165. if (timer_elapsed(stoplight_led.timer) > (1000 * (stoplight_led.index + 1))) {
  166. switch (stoplight_led.index){
  167. case 0:
  168. led_stoplight_set(TOP_LED);
  169. stoplight_led.index++;
  170. break;
  171. case 1:
  172. led_stoplight_set(MIDDLE_LED);
  173. stoplight_led.index++;
  174. break;
  175. case 2:
  176. led_stoplight_set(BOTTOM_LED);
  177. stoplight_led.index++;
  178. break;
  179. default:
  180. led_stoplight_end();
  181. break;
  182. }
  183. }
  184. }
  185. };
  186. #endif