wt_main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include "keyboards/wilba_tech/wt_mono_backlight.h"
  18. #include "keyboards/zeal60/zeal60_api.h" // Temporary hack
  19. #include "raw_hid.h"
  20. #include "dynamic_keymap.h"
  21. #include "timer.h"
  22. #include "tmk_core/common/eeprom.h"
  23. bool eeprom_is_valid(void)
  24. {
  25. return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
  26. eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
  27. }
  28. void eeprom_set_valid(bool valid)
  29. {
  30. eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
  31. eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
  32. }
  33. void eeprom_reset(void)
  34. {
  35. // Set the Zeal60 specific EEPROM state as invalid.
  36. eeprom_set_valid(false);
  37. // Set the TMK/QMK EEPROM state as invalid.
  38. eeconfig_disable();
  39. }
  40. #ifdef RAW_ENABLE
  41. void raw_hid_receive( uint8_t *data, uint8_t length )
  42. {
  43. uint8_t *command_id = &(data[0]);
  44. uint8_t *command_data = &(data[1]);
  45. switch ( *command_id )
  46. {
  47. case id_get_protocol_version:
  48. {
  49. command_data[0] = PROTOCOL_VERSION >> 8;
  50. command_data[1] = PROTOCOL_VERSION & 0xFF;
  51. break;
  52. }
  53. case id_get_keyboard_value:
  54. {
  55. if ( command_data[0] == id_uptime )
  56. {
  57. uint32_t value = timer_read32();
  58. command_data[1] = (value >> 24 ) & 0xFF;
  59. command_data[2] = (value >> 16 ) & 0xFF;
  60. command_data[3] = (value >> 8 ) & 0xFF;
  61. command_data[4] = value & 0xFF;
  62. }
  63. else
  64. {
  65. *command_id = id_unhandled;
  66. }
  67. break;
  68. }
  69. #ifdef DYNAMIC_KEYMAP_ENABLE
  70. case id_dynamic_keymap_get_keycode:
  71. {
  72. uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
  73. command_data[3] = keycode >> 8;
  74. command_data[4] = keycode & 0xFF;
  75. break;
  76. }
  77. case id_dynamic_keymap_set_keycode:
  78. {
  79. dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
  80. break;
  81. }
  82. case id_dynamic_keymap_reset:
  83. {
  84. dynamic_keymap_reset();
  85. break;
  86. }
  87. #endif // DYNAMIC_KEYMAP_ENABLE
  88. case id_backlight_config_set_value:
  89. {
  90. //backlight_config_set_value(command_data);
  91. break;
  92. }
  93. case id_backlight_config_get_value:
  94. {
  95. //backlight_config_get_value(command_data);
  96. break;
  97. }
  98. case id_backlight_config_save:
  99. {
  100. //backlight_config_save();
  101. break;
  102. }
  103. case id_eeprom_reset:
  104. {
  105. eeprom_reset();
  106. break;
  107. }
  108. case id_bootloader_jump:
  109. {
  110. // Need to send data back before the jump
  111. // Informs host that the command is handled
  112. raw_hid_send( data, length );
  113. // Give host time to read it
  114. wait_ms(100);
  115. bootloader_jump();
  116. break;
  117. }
  118. default:
  119. {
  120. // Unhandled message.
  121. *command_id = id_unhandled;
  122. break;
  123. }
  124. }
  125. // Return same buffer with values changed
  126. raw_hid_send( data, length );
  127. }
  128. #endif
  129. void main_init(void)
  130. {
  131. // If the EEPROM has the magic, the data is good.
  132. // OK to load from EEPROM.
  133. if (eeprom_is_valid()) {
  134. //backlight_config_load();
  135. } else {
  136. // If the EEPROM has not been saved before, or is out of date,
  137. // save the default values to the EEPROM. Default values
  138. // come from construction of the zeal_backlight_config instance.
  139. //backlight_config_save();
  140. #ifdef DYNAMIC_KEYMAP_ENABLE
  141. // This resets the keymaps in EEPROM to what is in flash.
  142. dynamic_keymap_reset();
  143. #endif
  144. // Save the magic number last, in case saving was interrupted
  145. eeprom_set_valid(true);
  146. }
  147. // Initialize LED drivers for backlight.
  148. backlight_init_drivers();
  149. backlight_timer_init();
  150. backlight_timer_enable();
  151. }
  152. void bootmagic_lite(void)
  153. {
  154. // The lite version of TMK's bootmagic.
  155. // 100% less potential for accidentally making the
  156. // keyboard do stupid things.
  157. // We need multiple scans because debouncing can't be turned off.
  158. matrix_scan();
  159. wait_ms(DEBOUNCING_DELAY);
  160. wait_ms(DEBOUNCING_DELAY);
  161. matrix_scan();
  162. // If the Esc (matrix 0,0) is held down on power up,
  163. // reset the EEPROM valid state and jump to bootloader.
  164. if ( matrix_get_row(0) & (1<<0) ) {
  165. eeprom_reset();
  166. bootloader_jump();
  167. }
  168. }
  169. void matrix_init_kb(void)
  170. {
  171. bootmagic_lite();
  172. main_init();
  173. matrix_init_user();
  174. }
  175. void matrix_scan_kb(void)
  176. {
  177. // This only updates the LED driver buffers if something has changed.
  178. backlight_update_pwm_buffers();
  179. matrix_scan_user();
  180. }