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.

222 lines
7.0 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. switch (keycode) {
  80. case FN_MO13:
  81. if (record->event.pressed) {
  82. layer_on(1);
  83. update_tri_layer(1, 2, 3);
  84. } else {
  85. layer_off(1);
  86. update_tri_layer(1, 2, 3);
  87. }
  88. return false;
  89. break;
  90. case FN_MO23:
  91. if (record->event.pressed) {
  92. layer_on(2);
  93. update_tri_layer(1, 2, 3);
  94. } else {
  95. layer_off(2);
  96. update_tri_layer(1, 2, 3);
  97. }
  98. return false;
  99. break;
  100. }
  101. return process_record_user(keycode, record);
  102. }
  103. void led_set_kb(uint8_t usb_led)
  104. {
  105. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  106. backlight_set_indicator_state(usb_led);
  107. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  108. led_set_user(usb_led);
  109. }
  110. void suspend_power_down_kb(void)
  111. {
  112. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  113. backlight_set_suspend_state(true);
  114. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  115. }
  116. void suspend_wakeup_init_kb(void)
  117. {
  118. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  119. backlight_set_suspend_state(false);
  120. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  121. }
  122. // Moving this to the bottom of this source file is a workaround
  123. // for an intermittent compiler error for Atmel compiler.
  124. #ifdef VIA_ENABLE
  125. void raw_hid_receive_kb(uint8_t *data, uint8_t length) {
  126. uint8_t *command_id = &(data[0]);
  127. uint8_t *command_data = &(data[1]);
  128. switch ( *command_id )
  129. {
  130. #if RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  131. case id_lighting_set_value:
  132. {
  133. backlight_config_set_value(command_data);
  134. break;
  135. }
  136. case id_lighting_get_value:
  137. {
  138. backlight_config_get_value(command_data);
  139. break;
  140. }
  141. case id_lighting_save:
  142. {
  143. backlight_config_save();
  144. break;
  145. }
  146. #endif // RGB_BACKLIGHT_ENABLED || MONO_BACKLIGHT_ENABLED
  147. default:
  148. {
  149. // Unhandled message.
  150. *command_id = id_unhandled;
  151. *command_data = *command_data; // force use of variable
  152. break;
  153. }
  154. }
  155. // DO NOT call raw_hid_send(data,length) here, let caller do this
  156. }
  157. #endif // VIA_ENABLE
  158. //
  159. // In the case of VIA being disabled, we still need to check if
  160. // keyboard level EEPROM memory is valid before loading.
  161. // Thus these are copies of the same functions in VIA, since
  162. // the backlight settings reuse VIA's EEPROM magic/version,
  163. // and the ones in via.c won't be compiled in.
  164. //
  165. // Yes, this is sub-optimal, and is only here for completeness
  166. // (i.e. catering to the 1% of people that want wilba.tech LED bling
  167. // AND want persistent settings BUT DON'T want to use dynamic keymaps/VIA).
  168. //
  169. #ifndef VIA_ENABLE
  170. bool via_eeprom_is_valid(void)
  171. {
  172. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  173. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  174. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  175. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  176. return (eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0 ) == magic0 &&
  177. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1 ) == magic1 &&
  178. eeprom_read_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2 ) == magic2 );
  179. }
  180. void via_eeprom_set_valid(bool valid)
  181. {
  182. char *p = QMK_BUILDDATE; // e.g. "2019-11-05-11:29:54"
  183. uint8_t magic0 = ( ( p[2] & 0x0F ) << 4 ) | ( p[3] & 0x0F );
  184. uint8_t magic1 = ( ( p[5] & 0x0F ) << 4 ) | ( p[6] & 0x0F );
  185. uint8_t magic2 = ( ( p[8] & 0x0F ) << 4 ) | ( p[9] & 0x0F );
  186. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+0, valid ? magic0 : 0xFF);
  187. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+1, valid ? magic1 : 0xFF);
  188. eeprom_update_byte( (void*)VIA_EEPROM_MAGIC_ADDR+2, valid ? magic2 : 0xFF);
  189. }
  190. void via_eeprom_reset(void)
  191. {
  192. // Set the VIA specific EEPROM state as invalid.
  193. via_eeprom_set_valid(false);
  194. // Set the TMK/QMK EEPROM state as invalid.
  195. eeconfig_disable();
  196. }
  197. #endif // VIA_ENABLE