wt_main.c 7.0 KB

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