zeal60.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* Copyright 2017 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 "zeal60.h"
  17. #include "zeal60_api.h"
  18. // Check that no backlight functions are called
  19. #if RGB_BACKLIGHT_ENABLED
  20. #include "rgb_backlight.h"
  21. #endif // BACKLIGHT_ENABLED
  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. #if RGB_BACKLIGHT_ENABLED
  142. case id_backlight_config_set_value:
  143. {
  144. backlight_config_set_value(command_data);
  145. break;
  146. }
  147. case id_backlight_config_get_value:
  148. {
  149. backlight_config_get_value(command_data);
  150. break;
  151. }
  152. case id_backlight_config_save:
  153. {
  154. backlight_config_save();
  155. break;
  156. }
  157. #endif // RGB_BACKLIGHT_ENABLED
  158. case id_eeprom_reset:
  159. {
  160. eeprom_reset();
  161. break;
  162. }
  163. case id_bootloader_jump:
  164. {
  165. // Need to send data back before the jump
  166. // Informs host that the command is handled
  167. raw_hid_send( data, length );
  168. // Give host time to read it
  169. wait_ms(100);
  170. bootloader_jump();
  171. break;
  172. }
  173. default:
  174. {
  175. // Unhandled message.
  176. *command_id = id_unhandled;
  177. break;
  178. }
  179. }
  180. // Return same buffer with values changed
  181. raw_hid_send( data, length );
  182. }
  183. #endif
  184. void main_init(void)
  185. {
  186. // If the EEPROM has the magic, the data is good.
  187. // OK to load from EEPROM.
  188. if (eeprom_is_valid()) {
  189. #if RGB_BACKLIGHT_ENABLED
  190. backlight_config_load();
  191. #endif // RGB_BACKLIGHT_ENABLED
  192. } else {
  193. #if RGB_BACKLIGHT_ENABLED
  194. // If the EEPROM has not been saved before, or is out of date,
  195. // save the default values to the EEPROM. Default values
  196. // come from construction of the zeal_backlight_config instance.
  197. backlight_config_save();
  198. #endif // RGB_BACKLIGHT_ENABLED
  199. #ifdef DYNAMIC_KEYMAP_ENABLE
  200. // This resets the keymaps in EEPROM to what is in flash.
  201. dynamic_keymap_reset();
  202. // This resets the macros in EEPROM to nothing.
  203. dynamic_keymap_macro_reset();
  204. #endif
  205. // Save the magic number last, in case saving was interrupted
  206. eeprom_set_valid(true);
  207. }
  208. #if RGB_BACKLIGHT_ENABLED
  209. // Initialize LED drivers for backlight.
  210. backlight_init_drivers();
  211. backlight_timer_init();
  212. backlight_timer_enable();
  213. #endif // RGB_BACKLIGHT_ENABLED
  214. }
  215. void bootmagic_lite(void)
  216. {
  217. // The lite version of TMK's bootmagic.
  218. // 100% less potential for accidentally making the
  219. // keyboard do stupid things.
  220. // We need multiple scans because debouncing can't be turned off.
  221. matrix_scan();
  222. wait_ms(DEBOUNCING_DELAY);
  223. wait_ms(DEBOUNCING_DELAY);
  224. matrix_scan();
  225. // If the Esc (matrix 0,0) is held down on power up,
  226. // reset the EEPROM valid state and jump to bootloader.
  227. if ( matrix_get_row(0) & (1<<0) ) {
  228. eeprom_reset();
  229. bootloader_jump();
  230. }
  231. }
  232. void matrix_init_kb(void)
  233. {
  234. bootmagic_lite();
  235. main_init();
  236. matrix_init_user();
  237. }
  238. void matrix_scan_kb(void)
  239. {
  240. #if RGB_BACKLIGHT_ENABLED
  241. // This only updates the LED driver buffers if something has changed.
  242. backlight_update_pwm_buffers();
  243. #endif // BACKLIGHT_ENABLED
  244. matrix_scan_user();
  245. }
  246. bool process_record_kb(uint16_t keycode, keyrecord_t *record)
  247. {
  248. #if RGB_BACKLIGHT_ENABLED
  249. process_record_backlight(keycode, record);
  250. #endif // BACKLIGHT_ENABLED
  251. switch(keycode) {
  252. case FN_MO13:
  253. if (record->event.pressed) {
  254. layer_on(1);
  255. update_tri_layer(1, 2, 3);
  256. } else {
  257. layer_off(1);
  258. update_tri_layer(1, 2, 3);
  259. }
  260. return false;
  261. break;
  262. case FN_MO23:
  263. if (record->event.pressed) {
  264. layer_on(2);
  265. update_tri_layer(1, 2, 3);
  266. } else {
  267. layer_off(2);
  268. update_tri_layer(1, 2, 3);
  269. }
  270. return false;
  271. break;
  272. }
  273. #ifdef DYNAMIC_KEYMAP_ENABLE
  274. // Handle macros
  275. if (record->event.pressed) {
  276. if ( keycode >= MACRO00 && keycode <= MACRO15 )
  277. {
  278. uint8_t id = keycode - MACRO00;
  279. dynamic_keymap_macro_send(id);
  280. return false;
  281. }
  282. }
  283. #endif //DYNAMIC_KEYMAP_ENABLE
  284. return process_record_user(keycode, record);
  285. }
  286. // This overrides the one in quantum/keymap_common.c
  287. uint16_t keymap_function_id_to_action( uint16_t function_id )
  288. {
  289. // Zeal60 specific "action functions" are 0xF00 to 0xFFF
  290. // i.e. F(0xF00) to F(0xFFF) are mapped to
  291. // enum zeal60_action_functions by masking last 8 bits.
  292. if ( function_id >= 0x0F00 && function_id <= 0x0FFF )
  293. {
  294. uint8_t id = function_id & 0xFF;
  295. switch ( id ) {
  296. case TRIPLE_TAP_1_3:
  297. case TRIPLE_TAP_2_3:
  298. {
  299. return ACTION_FUNCTION_TAP(id);
  300. break;
  301. }
  302. default:
  303. break;
  304. }
  305. }
  306. return pgm_read_word(&fn_actions[function_id]);
  307. }
  308. // Zeal60 specific "action functions"
  309. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  310. {
  311. switch (id)
  312. {
  313. case TRIPLE_TAP_1_3:
  314. case TRIPLE_TAP_2_3:
  315. if (record->event.pressed) {
  316. layer_on( id == TRIPLE_TAP_1_3 ? 1 : 2 );
  317. if (record->tap.count && !record->tap.interrupted) {
  318. if (record->tap.count >= 3) {
  319. layer_invert(3);
  320. }
  321. } else {
  322. record->tap.count = 0;
  323. }
  324. } else {
  325. layer_off( id == TRIPLE_TAP_1_3 ? 1 : 2 );
  326. }
  327. break;
  328. }
  329. }
  330. void led_set_kb(uint8_t usb_led)
  331. {
  332. #if RGB_BACKLIGHT_ENABLED
  333. backlight_set_indicator_state(usb_led);
  334. #endif // RGB_BACKLIGHT_ENABLED
  335. }
  336. void suspend_power_down_kb(void)
  337. {
  338. #if RGB_BACKLIGHT_ENABLED
  339. backlight_set_suspend_state(true);
  340. #endif // RGB_BACKLIGHT_ENABLED
  341. }
  342. void suspend_wakeup_init_kb(void)
  343. {
  344. #if RGB_BACKLIGHT_ENABLED
  345. backlight_set_suspend_state(false);
  346. #endif // RGB_BACKLIGHT_ENABLED
  347. }