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.

199 lines
6.4 KiB

  1. /* Copyright 2018 Jason Williams (Wilba)
  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 "quantum.h"
  17. // Check that no backlight functions are called
  18. #if RGB_BACKLIGHT_ENABLED
  19. #include "keyboards/wilba_tech/wt_rgb_backlight.h"
  20. #endif // RGB_BACKLIGHT_ENABLED
  21. #if MONO_BACKLIGHT_ENABLED
  22. #include "keyboards/wilba_tech/wt_mono_backlight.h"
  23. #endif // MONO_BACKLIGHT_ENABLED
  24. #include "via.h"
  25. #ifndef VIA_ENABLE
  26. #include "tmk_core/common/eeprom.h"
  27. #include "version.h" // for QMK_BUILDDATE used in EEPROM magic
  28. #endif
  29. // Called from via_init() if VIA_ENABLE
  30. // Called from matrix_init_kb() if not VIA_ENABLE
  31. void via_init_kb(void)
  32. {
  33. // If the EEPROM has the magic, the data is good.
  34. // OK to load from EEPROM
  35. if (via_eeprom_is_valid()) {
  36. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  37. backlight_config_load();
  38. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  39. } else {
  40. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  41. // If the EEPROM has not been saved before, or is out of date,
  42. // save the default values to the EEPROM. Default values
  43. // come from construction of the backlight_config instance.
  44. backlight_config_save();
  45. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  46. // DO NOT set EEPROM valid here, let caller do this
  47. }
  48. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  49. // Initialize LED drivers for backlight.
  50. backlight_init_drivers();
  51. backlight_timer_init();
  52. backlight_timer_enable();
  53. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  54. }
  55. void matrix_init_kb(void)
  56. {
  57. // If VIA is disabled, we still need to load backlight settings.
  58. // Call via_init_kb() the same way as via_init(), with setting
  59. // EEPROM valid afterwards.
  60. #ifndef VIA_ENABLE
  61. via_init_kb();
  62. via_eeprom_set_valid(true);
  63. #endif // VIA_ENABLE
  64. matrix_init_user();
  65. }
  66. void matrix_scan_kb(void)
  67. {
  68. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  69. // This only updates the LED driver buffers if something has changed.
  70. backlight_update_pwm_buffers();
  71. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  72. matrix_scan_user();
  73. }
  74. bool process_record_kb(uint16_t keycode, keyrecord_t *record)
  75. {
  76. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  77. process_record_backlight(keycode, record);
  78. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  79. return process_record_user(keycode, record);
  80. }
  81. void led_set_kb(uint8_t usb_led)
  82. {
  83. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  84. backlight_set_indicator_state(usb_led);
  85. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  86. led_set_user(usb_led);
  87. }
  88. void suspend_power_down_kb(void)
  89. {
  90. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  91. backlight_set_suspend_state(true);
  92. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  93. }
  94. void suspend_wakeup_init_kb(void)
  95. {
  96. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  97. backlight_set_suspend_state(false);
  98. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  99. }
  100. // Moving this to the bottom of this source file is a workaround
  101. // for an intermittent compiler error for Atmel compiler.
  102. #ifdef VIA_ENABLE
  103. void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
  104. uint8_t *command_id = &(data[0]);
  105. uint8_t *command_data = &(data[1]);
  106. switch ( *command_id )
  107. {
  108. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  109. case id_lighting_set_value:
  110. {
  111. backlight_config_set_value(command_data);
  112. break;
  113. }
  114. case id_lighting_get_value:
  115. {
  116. backlight_config_get_value(command_data);
  117. break;
  118. }
  119. case id_lighting_save:
  120. {
  121. backlight_config_save();
  122. break;
  123. }
  124. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  125. default:
  126. {
  127. // Unhandled message.
  128. *command_id = id_unhandled;
  129. *command_data = *command_data; // force use of variable
  130. break;
  131. }
  132. }
  133. // DO NOT call raw_hid_send(data,length) here, let caller do this
  134. }
  135. #endif // VIA_ENABLE
  136. //
  137. // In the case of VIA being disabled, we still need to check if
  138. // keyboard level EEPROM memory is valid before loading.
  139. // Thus these are copies of the same functions in VIA, since
  140. // the backlight settings reuse VIA's EEPROM magic/version,
  141. // and the ones in via.c won't be compiled in.
  142. //
  143. // Yes, this is sub-optimal, and is only here for completeness
  144. // (i.e. catering to the 1% of people that want wilba.tech LED bling
  145. // AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
  146. //
  147. #ifndef VIA_ENABLE
  148. bool via_eeprom_is_valid(void)
  149. {
  150. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  151. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  152. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  153. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  154. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  155. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  156. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  157. }
  158. void via_eeprom_set_valid(bool valid)
  159. {
  160. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  161. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  162. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  163. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  164. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
  165. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
  166. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
  167. }
  168. void via_eeprom_reset(void)
  169. {
  170. // Set the VIA specific EEPROM state as invalid.
  171. via_eeprom_set_valid(false);
  172. // Set the TMK/QMK EEPROM state as invalid.
  173. eeconfig_disable();
  174. }
  175. #endif // VIA_ENABLE