2
0

wt_main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "keyboards/zeal60/zeal60_keycodes.h" // Temporary hack
  20. #include "raw_hid.h"
  21. #include "dynamic_keymap.h"
  22. #include "timer.h"
  23. #include "tmk_core/common/eeprom.h"
  24. bool eeprom_is_valid(void)
  25. {
  26. return (eeprom_read_word(((void*)EEPROM_MAGIC_ADDR)) == EEPROM_MAGIC &&
  27. eeprom_read_byte(((void*)EEPROM_VERSION_ADDR)) == EEPROM_VERSION);
  28. }
  29. void eeprom_set_valid(bool valid)
  30. {
  31. eeprom_update_word(((void*)EEPROM_MAGIC_ADDR), valid ? EEPROM_MAGIC : 0xFFFF);
  32. eeprom_update_byte(((void*)EEPROM_VERSION_ADDR), valid ? EEPROM_VERSION : 0xFF);
  33. }
  34. void eeprom_reset(void)
  35. {
  36. // Set the Zeal60 specific EEPROM state as invalid.
  37. eeprom_set_valid(false);
  38. // Set the TMK/QMK EEPROM state as invalid.
  39. eeconfig_disable();
  40. }
  41. #ifdef RAW_ENABLE
  42. void raw_hid_receive( uint8_t *data, uint8_t length )
  43. {
  44. uint8_t *command_id = &(data[0]);
  45. uint8_t *command_data = &(data[1]);
  46. switch ( *command_id )
  47. {
  48. case id_get_protocol_version:
  49. {
  50. command_data[0] = PROTOCOL_VERSION >> 8;
  51. command_data[1] = PROTOCOL_VERSION & 0xFF;
  52. break;
  53. }
  54. case id_get_keyboard_value:
  55. {
  56. if ( command_data[0] == id_uptime )
  57. {
  58. uint32_t value = timer_read32();
  59. command_data[1] = (value >> 24 ) & 0xFF;
  60. command_data[2] = (value >> 16 ) & 0xFF;
  61. command_data[3] = (value >> 8 ) & 0xFF;
  62. command_data[4] = value & 0xFF;
  63. }
  64. else
  65. {
  66. *command_id = id_unhandled;
  67. }
  68. break;
  69. }
  70. #ifdef DYNAMIC_KEYMAP_ENABLE
  71. case id_dynamic_keymap_get_keycode:
  72. {
  73. uint16_t keycode = dynamic_keymap_get_keycode( command_data[0], command_data[1], command_data[2] );
  74. command_data[3] = keycode >> 8;
  75. command_data[4] = keycode & 0xFF;
  76. break;
  77. }
  78. case id_dynamic_keymap_set_keycode:
  79. {
  80. dynamic_keymap_set_keycode( command_data[0], command_data[1], command_data[2], ( command_data[3] << 8 ) | command_data[4] );
  81. break;
  82. }
  83. case id_dynamic_keymap_reset:
  84. {
  85. dynamic_keymap_reset();
  86. break;
  87. }
  88. case id_dynamic_keymap_macro_get_count:
  89. {
  90. command_data[0] = dynamic_keymap_macro_get_count();
  91. break;
  92. }
  93. case id_dynamic_keymap_macro_get_buffer_size:
  94. {
  95. uint16_t size = dynamic_keymap_macro_get_buffer_size();
  96. command_data[0] = size >> 8;
  97. command_data[1] = size & 0xFF;
  98. break;
  99. }
  100. case id_dynamic_keymap_macro_get_buffer:
  101. {
  102. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  103. uint16_t size = command_data[2]; // size <= 28
  104. dynamic_keymap_macro_get_buffer( offset, size, &command_data[3] );
  105. break;
  106. }
  107. case id_dynamic_keymap_macro_set_buffer:
  108. {
  109. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  110. uint16_t size = command_data[2]; // size <= 28
  111. dynamic_keymap_macro_set_buffer( offset, size, &command_data[3] );
  112. break;
  113. }
  114. case id_dynamic_keymap_macro_reset:
  115. {
  116. dynamic_keymap_macro_reset();
  117. break;
  118. }
  119. case id_dynamic_keymap_get_layer_count:
  120. {
  121. command_data[0] = dynamic_keymap_get_layer_count();
  122. break;
  123. }
  124. case id_dynamic_keymap_get_buffer:
  125. {
  126. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  127. uint16_t size = command_data[2]; // size <= 28
  128. dynamic_keymap_get_buffer( offset, size, &command_data[3] );
  129. break;
  130. }
  131. case id_dynamic_keymap_set_buffer:
  132. {
  133. uint16_t offset = ( command_data[0] << 8 ) | command_data[1];
  134. uint16_t size = command_data[2]; // size <= 28
  135. dynamic_keymap_set_buffer( offset, size, &command_data[3] );
  136. break;
  137. }
  138. #endif // DYNAMIC_KEYMAP_ENABLE
  139. case id_eeprom_reset:
  140. {
  141. eeprom_reset();
  142. break;
  143. }
  144. case id_bootloader_jump:
  145. {
  146. // Need to send data back before the jump
  147. // Informs host that the command is handled
  148. raw_hid_send( data, length );
  149. // Give host time to read it
  150. wait_ms(100);
  151. bootloader_jump();
  152. break;
  153. }
  154. default:
  155. {
  156. // Unhandled message.
  157. *command_id = id_unhandled;
  158. break;
  159. }
  160. }
  161. // Return same buffer with values changed
  162. raw_hid_send( data, length );
  163. }
  164. #endif
  165. void main_init(void)
  166. {
  167. // If the EEPROM has the magic, the data is good.
  168. // OK to load from EEPROM.
  169. if (eeprom_is_valid()) {
  170. //backlight_config_load();
  171. } else {
  172. // If the EEPROM has not been saved before, or is out of date,
  173. // save the default values to the EEPROM. Default values
  174. // come from construction of the zeal_backlight_config instance.
  175. //backlight_config_save();
  176. #ifdef DYNAMIC_KEYMAP_ENABLE
  177. // This resets the keymaps in EEPROM to what is in flash.
  178. dynamic_keymap_reset();
  179. // This resets the macros in EEPROM to nothing.
  180. dynamic_keymap_macro_reset();
  181. #endif
  182. // Save the magic number last, in case saving was interrupted
  183. eeprom_set_valid(true);
  184. }
  185. // Initialize LED drivers for backlight.
  186. backlight_init_drivers();
  187. backlight_timer_init();
  188. backlight_timer_enable();
  189. }
  190. void bootmagic_lite(void)
  191. {
  192. // The lite version of TMK's bootmagic.
  193. // 100% less potential for accidentally making the
  194. // keyboard do stupid things.
  195. // We need multiple scans because debouncing can't be turned off.
  196. matrix_scan();
  197. wait_ms(DEBOUNCING_DELAY);
  198. wait_ms(DEBOUNCING_DELAY);
  199. matrix_scan();
  200. // If the Esc (matrix 0,0) is held down on power up,
  201. // reset the EEPROM valid state and jump to bootloader.
  202. if ( matrix_get_row(0) & (1<<0) ) {
  203. eeprom_reset();
  204. bootloader_jump();
  205. }
  206. }
  207. void matrix_init_kb(void)
  208. {
  209. bootmagic_lite();
  210. main_init();
  211. matrix_init_user();
  212. }
  213. void matrix_scan_kb(void)
  214. {
  215. // This only updates the LED driver buffers if something has changed.
  216. backlight_update_pwm_buffers();
  217. matrix_scan_user();
  218. }
  219. bool process_record_kb(uint16_t keycode, keyrecord_t *record)
  220. {
  221. switch(keycode) {
  222. case FN_MO13:
  223. if (record->event.pressed) {
  224. layer_on(1);
  225. update_tri_layer(1, 2, 3);
  226. } else {
  227. layer_off(1);
  228. update_tri_layer(1, 2, 3);
  229. }
  230. return false;
  231. break;
  232. case FN_MO23:
  233. if (record->event.pressed) {
  234. layer_on(2);
  235. update_tri_layer(1, 2, 3);
  236. } else {
  237. layer_off(2);
  238. update_tri_layer(1, 2, 3);
  239. }
  240. return false;
  241. break;
  242. }
  243. #ifdef DYNAMIC_KEYMAP_ENABLE
  244. // Handle macros
  245. if (record->event.pressed) {
  246. if ( keycode >= MACRO00 && keycode <= MACRO15 )
  247. {
  248. uint8_t id = keycode - MACRO00;
  249. dynamic_keymap_macro_send(id);
  250. return false;
  251. }
  252. }
  253. #endif //DYNAMIC_KEYMAP_ENABLE
  254. return process_record_user(keycode, record);
  255. }