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.

214 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. void shutdown_user (void) {
  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. }
  74. __attribute__ ((weak))
  75. void suspend_power_down_keymap(void) {}
  76. /**
  77. * Set rgb_matrix suspend state to true if not already
  78. */
  79. void suspend_power_down_user(void) {
  80. #ifdef RGB_MATRIX_ENABLE
  81. if (!g_suspend_state) {
  82. rgb_matrix_set_suspend_state(true);
  83. }
  84. #endif //RGB_MATRIX_ENABLE
  85. suspend_power_down_keymap();
  86. }
  87. __attribute__ ((weak))
  88. void suspend_wakeup_init_keymap(void) {}
  89. /**
  90. * Set rgb_matrix suspend state to false if not already
  91. */
  92. void suspend_wakeup_init_user(void) {
  93. #ifdef RGB_MATRIX_ENABLE
  94. if (g_suspend_state) {
  95. rgb_matrix_set_suspend_state(false);
  96. }
  97. #endif //RGB_MATRIX_ENABLE
  98. suspend_wakeup_init_keymap();
  99. }
  100. __attribute__ ((weak))
  101. void matrix_scan_keymap(void) {}
  102. /**
  103. * Checks for Super CMDTAB
  104. */
  105. void matrix_scan_user(void) {
  106. matrix_scan_cmd_tab();
  107. #ifdef STOPLIGHT_LED
  108. matrix_scan_led_stoplight();
  109. #endif
  110. matrix_scan_keymap();
  111. }
  112. __attribute__ ((weak))
  113. layer_state_t default_layer_state_set_keymap(layer_state_t state) {
  114. return state;
  115. }
  116. /**
  117. * For macropads, if a new default layer is set from DF()
  118. * then automatically set that layer with layer_move()
  119. */
  120. layer_state_t default_layer_state_set_user(layer_state_t state) {
  121. #if defined(IS_MACROPAD)
  122. layer_move(get_highest_layer(state));
  123. #endif
  124. return default_layer_state_set_keymap(state);
  125. }
  126. __attribute__ ((weak))
  127. layer_state_t layer_state_set_keymap(layer_state_t state) {
  128. return state;
  129. }
  130. /**
  131. * Do RGB things (like layer indication) on layer change
  132. */
  133. layer_state_t layer_state_set_user(layer_state_t state) {
  134. #if defined(RGBLIGHT_ENABLE) || defined(RGB_MATRIX_ENABLE)
  135. state = layer_state_set_rgb(state);
  136. #endif // RGBLIGHT_ENABLE
  137. return layer_state_set_keymap(state);
  138. }
  139. __attribute__((weak)) bool led_update_keymap(led_t led_state) { return true; }
  140. bool led_update_user(led_t led_state) {
  141. #ifdef STOPLIGHT_LED
  142. if (stoplight_led.is_active) {
  143. return false;
  144. }
  145. #endif
  146. return led_update_keymap(led_state);
  147. }
  148. #ifdef STOPLIGHT_LED
  149. void led_stoplight_start(void) {
  150. writePin(TOP_LED, LED_ON(false));
  151. writePin(MIDDLE_LED, LED_ON(false));
  152. writePin(BOTTOM_LED, LED_ON(false));
  153. stoplight_led.is_active = true;
  154. stoplight_led.timer = timer_read();
  155. };
  156. void led_stoplight_set(pin_t pin) {
  157. writePin(pin, LED_ON(true));
  158. };
  159. void led_stoplight_end(void) {
  160. // Reset timer and status variables
  161. stoplight_led.is_active = false;
  162. stoplight_led.index = 0;
  163. stoplight_led.timer = 0;
  164. led_update_kb(host_keyboard_led_state());
  165. };
  166. void matrix_scan_led_stoplight(void) {
  167. if (stoplight_led.is_active) {
  168. if (timer_elapsed(stoplight_led.timer) > (1000 * (stoplight_led.index + 1))) {
  169. switch (stoplight_led.index){
  170. case 0:
  171. led_stoplight_set(TOP_LED);
  172. stoplight_led.index++;
  173. break;
  174. case 1:
  175. led_stoplight_set(MIDDLE_LED);
  176. stoplight_led.index++;
  177. break;
  178. case 2:
  179. led_stoplight_set(BOTTOM_LED);
  180. stoplight_led.index++;
  181. break;
  182. default:
  183. led_stoplight_end();
  184. break;
  185. }
  186. }
  187. }
  188. };
  189. #endif