launch_1.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2021 System76
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. #include "quantum.h"
  18. #include "eeprom.h"
  19. #include "usb_mux.h"
  20. // clang-format off
  21. bool eeprom_is_valid(void) {
  22. return (
  23. eeprom_read_word(((void *)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
  24. eeprom_read_byte(((void *)EEPROM_VERSION_ADDR)) == EEPROM_VERSION
  25. );
  26. }
  27. // clang-format on
  28. void eeprom_set_valid(bool valid) {
  29. eeprom_update_word(((void *)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
  30. eeprom_update_byte(((void *)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
  31. }
  32. void bootmagic_lite_reset_eeprom(void) {
  33. // Set the keyboard-specific EEPROM state as invalid
  34. eeprom_set_valid(false);
  35. // Set the TMK/QMK EEPROM state as invalid
  36. eeconfig_disable();
  37. }
  38. // The lite version of TMK's bootmagic based on Wilba.
  39. // 100% less potential for accidentally making the keyboard do stupid things.
  40. void bootmagic_lite(void) {
  41. // Perform multiple scans because debouncing can't be turned off.
  42. matrix_scan();
  43. #if defined(DEBOUNCE) && DEBOUNCE > 0
  44. wait_ms(DEBOUNCE * 2);
  45. #else
  46. wait_ms(30);
  47. #endif
  48. matrix_scan();
  49. // If the configured key (commonly Esc) is held down on power up,
  50. // reset the EEPROM valid state and jump to bootloader.
  51. uint8_t row = 0; // BOOTMAGIC_LITE_ROW;
  52. uint8_t col = 0; // BOOTMAGIC_LITE_COLUMN;
  53. if (matrix_get_row(row) & (1 << col)) {
  54. bootmagic_lite_reset_eeprom();
  55. // Jump to bootloader.
  56. bootloader_jump();
  57. }
  58. }
  59. void system76_ec_rgb_eeprom(bool write);
  60. void system76_ec_rgb_layer(layer_state_t layer_state);
  61. void system76_ec_unlock(void);
  62. bool system76_ec_is_unlocked(void);
  63. extern rgb_config_t layer_rgb[DYNAMIC_KEYMAP_LAYER_COUNT];
  64. void matrix_init_kb(void) {
  65. usb_mux_init();
  66. bootmagic_lite();
  67. if (!eeprom_is_valid()) {
  68. dynamic_keymap_reset();
  69. dynamic_keymap_macro_reset();
  70. system76_ec_rgb_eeprom(true);
  71. eeprom_set_valid(true);
  72. } else {
  73. system76_ec_rgb_eeprom(false);
  74. }
  75. system76_ec_rgb_layer(layer_state);
  76. matrix_init_user();
  77. }
  78. void housekeeping_task_kb(void) {
  79. usb_mux_event();
  80. }
  81. #define LEVEL(value) (uint8_t)(((uint16_t)value) * ((uint16_t)RGB_MATRIX_MAXIMUM_BRIGHTNESS) / ((uint16_t)255))
  82. // clang-format off
  83. static const uint8_t levels[] = {
  84. LEVEL(48),
  85. LEVEL(72),
  86. LEVEL(96),
  87. LEVEL(144),
  88. LEVEL(192),
  89. LEVEL(255)
  90. };
  91. // clang-format on
  92. static uint8_t toggle_level = RGB_MATRIX_MAXIMUM_BRIGHTNESS;
  93. extern bool input_disabled;
  94. static void set_value_all_layers(uint8_t value) {
  95. if (!system76_ec_is_unlocked()) {
  96. for (int8_t layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  97. layer_rgb[layer].hsv.v = value;
  98. }
  99. system76_ec_rgb_layer(layer_state);
  100. }
  101. }
  102. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  103. if (input_disabled) {
  104. return false;
  105. }
  106. if (!process_record_user(keycode, record)) {
  107. return false;
  108. }
  109. switch (keycode) {
  110. case QK_BOOT:
  111. if (record->event.pressed) {
  112. system76_ec_unlock();
  113. }
  114. #ifdef SYSTEM76_EC
  115. return false;
  116. #else
  117. return true;
  118. #endif
  119. case QK_RGB_MATRIX_VALUE_DOWN:
  120. if (record->event.pressed) {
  121. uint8_t level = rgb_matrix_config.hsv.v;
  122. for (int i = sizeof(levels) - 1; i >= 0; i--) {
  123. if (levels[i] < level) {
  124. level = levels[i];
  125. break;
  126. }
  127. }
  128. set_value_all_layers(level);
  129. }
  130. return false;
  131. case QK_RGB_MATRIX_VALUE_UP:
  132. if (record->event.pressed) {
  133. uint8_t level = rgb_matrix_config.hsv.v;
  134. for (int i = 0; i < sizeof(levels); i++) {
  135. if (levels[i] > level) {
  136. level = levels[i];
  137. break;
  138. }
  139. }
  140. set_value_all_layers(level);
  141. }
  142. return false;
  143. case QK_RGB_MATRIX_TOGGLE:
  144. if (record->event.pressed) {
  145. uint8_t level = 0;
  146. if (rgb_matrix_config.hsv.v == 0) {
  147. level = toggle_level;
  148. } else {
  149. toggle_level = rgb_matrix_config.hsv.v;
  150. }
  151. set_value_all_layers(level);
  152. }
  153. return false;
  154. }
  155. return true;
  156. }
  157. layer_state_t layer_state_set_kb(layer_state_t layer_state) {
  158. system76_ec_rgb_layer(layer_state);
  159. return layer_state_set_user(layer_state);
  160. }
  161. #ifdef CONSOLE_ENABLE
  162. void keyboard_post_init_kb(void) {
  163. debug_enable = true;
  164. debug_matrix = false;
  165. debug_keyboard = false;
  166. keyboard_post_init_user();
  167. }
  168. #endif // CONSOLE_ENABLE