logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

moonlander.c (12002B)


  1. /* Copyright 2020 ZSA Technology Labs, Inc <@zsa>
  2. * Copyright 2020 Jack Humbert <jack.humb@gmail.com>
  3. * Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "moonlander.h"
  19. keyboard_config_t keyboard_config;
  20. bool mcp23018_leds[3] = {0, 0, 0};
  21. bool is_launching = false;
  22. #if defined(DEFERRED_EXEC_ENABLE)
  23. # if defined(DYNAMIC_MACRO_ENABLE)
  24. deferred_token dynamic_macro_token = INVALID_DEFERRED_TOKEN;
  25. static uint32_t dynamic_macro_led(uint32_t trigger_time, void *cb_arg) {
  26. static bool led_state = true;
  27. if (!is_launching) {
  28. led_state = !led_state;
  29. ML_LED_3(led_state);
  30. }
  31. return 100;
  32. }
  33. bool dynamic_macro_record_start_kb(int8_t direction) {
  34. if (!dynamic_macro_record_start_user(direction)) {
  35. return false;
  36. }
  37. if (dynamic_macro_token == INVALID_DEFERRED_TOKEN) {
  38. ML_LED_3(true);
  39. dynamic_macro_token = defer_exec(100, dynamic_macro_led, NULL);
  40. }
  41. return true;
  42. }
  43. bool dynamic_macro_record_end_kb(int8_t direction) {
  44. if (!dynamic_macro_record_end_user(direction)) {
  45. return false;
  46. }
  47. if (cancel_deferred_exec(dynamic_macro_token)) {
  48. dynamic_macro_token = INVALID_DEFERRED_TOKEN;
  49. ML_LED_3(false);
  50. }
  51. return false;
  52. }
  53. # endif
  54. static uint32_t startup_exec(uint32_t trigger_time, void *cb_arg) {
  55. static uint8_t startup_loop = 0;
  56. switch (startup_loop++) {
  57. case 0:
  58. ML_LED_1(true);
  59. ML_LED_2(false);
  60. ML_LED_3(false);
  61. ML_LED_4(false);
  62. ML_LED_5(false);
  63. ML_LED_6(false);
  64. break;
  65. case 1:
  66. ML_LED_2(true);
  67. break;
  68. case 2:
  69. ML_LED_3(true);
  70. break;
  71. case 3:
  72. ML_LED_4(true);
  73. break;
  74. case 4:
  75. ML_LED_5(true);
  76. break;
  77. case 5:
  78. ML_LED_6(true);
  79. break;
  80. case 6:
  81. ML_LED_1(false);
  82. break;
  83. case 7:
  84. ML_LED_2(false);
  85. break;
  86. case 8:
  87. ML_LED_3(false);
  88. break;
  89. case 9:
  90. ML_LED_4(false);
  91. break;
  92. case 10:
  93. ML_LED_5(false);
  94. break;
  95. case 11:
  96. ML_LED_6(false);
  97. break;
  98. case 12:
  99. is_launching = false;
  100. layer_state_set_kb(layer_state);
  101. return 0;
  102. }
  103. return 250;
  104. }
  105. #endif
  106. void keyboard_pre_init_kb(void) {
  107. gpio_set_pin_output(B5);
  108. gpio_set_pin_output(B4);
  109. gpio_set_pin_output(B3);
  110. gpio_write_pin_low(B5);
  111. gpio_write_pin_low(B4);
  112. gpio_write_pin_low(B3);
  113. keyboard_pre_init_user();
  114. }
  115. #if !defined(MOONLANDER_USER_LEDS)
  116. layer_state_t layer_state_set_kb(layer_state_t state) {
  117. state = layer_state_set_user(state);
  118. if (is_launching || !keyboard_config.led_level) return state;
  119. bool LED_1 = false;
  120. bool LED_2 = false;
  121. bool LED_3 = false;
  122. bool LED_4 = false;
  123. bool LED_5 = false;
  124. #if !defined(CAPS_LOCK_STATUS)
  125. bool LED_6 = false;
  126. #endif
  127. uint8_t layer = get_highest_layer(state);
  128. switch (layer) {
  129. case 1:
  130. LED_1 = true;
  131. LED_4 = true;
  132. break;
  133. case 2:
  134. LED_2 = true;
  135. LED_5 = true;
  136. break;
  137. case 3:
  138. LED_3 = true;
  139. #if !defined(CAPS_LOCK_STATUS)
  140. LED_6 = true;
  141. #endif
  142. break;
  143. case 4:
  144. LED_4 = true;
  145. break;
  146. case 5:
  147. LED_5 = true;
  148. break;
  149. case 6:
  150. #if !defined(CAPS_LOCK_STATUS)
  151. LED_6 = true;
  152. #endif
  153. break;
  154. default:
  155. break;
  156. }
  157. ML_LED_1(LED_1);
  158. ML_LED_2(LED_2);
  159. ML_LED_3(LED_3);
  160. ML_LED_4(LED_4);
  161. ML_LED_5(LED_5);
  162. #if !defined(CAPS_LOCK_STATUS)
  163. ML_LED_6(LED_6);
  164. #endif
  165. return state;
  166. }
  167. #endif
  168. #ifdef RGB_MATRIX_ENABLE
  169. // clang-format off
  170. const is31fl3731_led_t PROGMEM g_is31fl3731_leds[IS31FL3731_LED_COUNT] = {
  171. {0, C3_2, C1_1, C4_2},
  172. {0, C2_2, C1_2, C4_3},
  173. {0, C2_3, C1_3, C3_3},
  174. {0, C2_4, C1_4, C3_4},
  175. {0, C2_5, C1_5, C3_5},
  176. {0, C2_6, C1_6, C3_6},
  177. {0, C2_7, C1_7, C3_7},
  178. {0, C2_8, C1_8, C3_8},
  179. {0, C3_1, C2_1, C4_1},
  180. {0, C7_8, C6_8, C8_8},
  181. {0, C7_7, C6_7, C9_8},
  182. {0, C8_7, C6_6, C9_7},
  183. {0, C8_6, C7_6, C9_6},
  184. {0, C8_5, C7_5, C9_5},
  185. {0, C8_4, C7_4, C9_4},
  186. {0, C8_3, C7_3, C9_3},
  187. {0, C8_2, C7_2, C9_2},
  188. {0, C8_1, C7_1, C9_1},
  189. {0, C3_10, C1_9, C4_10},
  190. {0, C2_10, C1_10, C4_11},
  191. {0, C2_11, C1_11, C3_11},
  192. {0, C2_12, C1_12, C3_12},
  193. {0, C2_13, C1_13, C3_13},
  194. {0, C2_14, C1_14, C3_14},
  195. {0, C2_15, C1_15, C3_15},
  196. {0, C2_16, C1_16, C3_16},
  197. {0, C3_9, C2_9, C4_9},
  198. {0, C7_16, C6_16, C8_16},
  199. {0, C7_15, C6_15, C9_16},
  200. {0, C8_15, C6_14, C9_15},
  201. {0, C8_10, C7_10, C9_10},
  202. {0, C8_9, C7_9, C9_9},
  203. {0, C8_11, C7_11, C9_11},
  204. {0, C8_12, C7_12, C9_12},
  205. {0, C8_13, C7_13, C9_13},
  206. {0, C8_14, C7_14, C9_14},
  207. {1, C3_2, C1_1, C4_2},
  208. {1, C2_2, C1_2, C4_3},
  209. {1, C2_3, C1_3, C3_3},
  210. {1, C2_4, C1_4, C3_4},
  211. {1, C2_5, C1_5, C3_5},
  212. {1, C2_6, C1_6, C3_6},
  213. {1, C2_7, C1_7, C3_7},
  214. {1, C2_8, C1_8, C3_8},
  215. {1, C3_1, C2_1, C4_1},
  216. {1, C7_8, C6_8, C8_8},
  217. {1, C7_7, C6_7, C9_8},
  218. {1, C8_7, C6_6, C9_7},
  219. {1, C8_6, C7_6, C9_6},
  220. {1, C8_5, C7_5, C9_5},
  221. {1, C8_4, C7_4, C9_4},
  222. {1, C8_3, C7_3, C9_3},
  223. {1, C8_2, C7_2, C9_2},
  224. {1, C8_1, C7_1, C9_1},
  225. {1, C3_10, C1_9, C4_10},
  226. {1, C2_10, C1_10, C4_11},
  227. {1, C2_11, C1_11, C3_11},
  228. {1, C2_12, C1_12, C3_12},
  229. {1, C2_13, C1_13, C3_13},
  230. {1, C2_14, C1_14, C3_14},
  231. {1, C2_15, C1_15, C3_15},
  232. {1, C2_16, C1_16, C3_16},
  233. {1, C3_9, C2_9, C4_9},
  234. {1, C7_16, C6_16, C8_16},
  235. {1, C7_15, C6_15, C9_16},
  236. {1, C8_15, C6_14, C9_15},
  237. {1, C8_10, C7_10, C9_10},
  238. {1, C8_9, C7_9, C9_9},
  239. {1, C8_11, C7_11, C9_11},
  240. {1, C8_12, C7_12, C9_12},
  241. {1, C8_13, C7_13, C9_13},
  242. {1, C8_14, C7_14, C9_14},
  243. };
  244. // clang-format on
  245. #endif
  246. #ifdef AUDIO_ENABLE
  247. bool music_mask_kb(uint16_t keycode) {
  248. switch (keycode) {
  249. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  250. case QK_TO ... QK_TO_MAX:
  251. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  252. case QK_DEF_LAYER ... QK_DEF_LAYER_MAX:
  253. case QK_PERSISTENT_DEF_LAYER ... QK_PERSISTENT_DEF_LAYER_MAX:
  254. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
  255. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
  256. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  257. case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
  258. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
  259. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  260. case AU_ON ... AU_PREV:
  261. case QK_BOOT:
  262. case QK_CLEAR_EEPROM:
  263. return false;
  264. default:
  265. return music_mask_user(keycode);
  266. }
  267. }
  268. #endif
  269. #ifdef SWAP_HANDS_ENABLE
  270. // swap-hands action needs a matrix to define the swap
  271. // clang-format off
  272. const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  273. /* Left hand, matrix positions */
  274. {{6,6}, {5,6}, {4,6}, {3,6}, {2,6}, {1,6},{0,6}},
  275. {{6,7}, {5,7}, {4,7}, {3,7}, {2,7}, {1,7},{0,7}},
  276. {{6,8}, {5,8}, {4,8}, {3,8}, {2,8}, {1,8},{0,8}},
  277. {{6,9}, {5,9}, {4,9}, {3,9}, {2,9}, {1,9},{0,9}},
  278. {{6,10},{5,10},{4,10},{3,10},{2,10},{1,10},{0,10}},
  279. {{6,11},{5,11},{4,11},{3,11},{2,11},{1,11},{0,11}},
  280. /* Right hand, matrix positions */
  281. {{6,0}, {5,0}, {4,0}, {3,0}, {2,0}, {1,0},{0,0}},
  282. {{6,1}, {5,1}, {4,1}, {3,1}, {2,1}, {1,1},{0,1}},
  283. {{6,2}, {5,2}, {4,2}, {3,2}, {2,2}, {1,2},{0,2}},
  284. {{6,3}, {5,3}, {4,3}, {3,3}, {2,3}, {1,3},{0,3}},
  285. {{6,4}, {5,4}, {4,4}, {3,4}, {2,4}, {1,4},{0,4}},
  286. {{6,5}, {5,5}, {4,5}, {3,5}, {2,5}, {1,5},{0,5}},
  287. };
  288. // clang-format on
  289. #endif
  290. #if defined(AUDIO_ENABLE) && defined(MUSIC_MAP)
  291. // clang-format off
  292. __attribute__ ((weak))
  293. const uint8_t music_map[MATRIX_ROWS][MATRIX_COLS] = {
  294. {58, 59, 60, 61, 62, 63, 64},
  295. {44, 45, 46, 47, 48, 49, 50},
  296. {30, 31, 32, 33, 34, 35, 36},
  297. {18, 19, 20, 21, 22, 23, 0},
  298. { 8, 9, 10, 11, 12, 0, 0},
  299. { 0, 1, 2, 3, 0, 0, 0},
  300. {65, 66, 67, 68, 69, 70, 71},
  301. {51, 52, 53, 54, 55, 56, 57},
  302. {37, 38, 39, 40, 41, 42, 43},
  303. { 0, 24, 25, 26, 27, 28, 29},
  304. { 0, 0, 13, 14, 15, 16, 17},
  305. { 0, 0, 0, 4, 5, 6, 7}
  306. };
  307. // clang-format on
  308. #endif
  309. #ifdef CAPS_LOCK_STATUS
  310. void led_update_ports(led_t led_state) {
  311. ML_LED_6(led_state.caps_lock);
  312. }
  313. #endif
  314. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  315. if (!process_record_user(keycode, record)) { return false; }
  316. switch (keycode) {
  317. #if !defined(MOONLANDER_USER_LEDS)
  318. case LED_LEVEL:
  319. if (record->event.pressed) {
  320. keyboard_config.led_level ^= 1;
  321. eeconfig_update_kb(keyboard_config.raw);
  322. if (keyboard_config.led_level) {
  323. layer_state_set_kb(layer_state);
  324. } else {
  325. ML_LED_1(false);
  326. ML_LED_2(false);
  327. ML_LED_3(false);
  328. ML_LED_4(false);
  329. ML_LED_5(false);
  330. ML_LED_6(false);
  331. }
  332. }
  333. break;
  334. #endif
  335. #ifdef RGB_MATRIX_ENABLE
  336. case TOGGLE_LAYER_COLOR:
  337. if (record->event.pressed) {
  338. keyboard_config.disable_layer_led ^= 1;
  339. if (keyboard_config.disable_layer_led) rgb_matrix_set_color_all(0, 0, 0);
  340. eeconfig_update_kb(keyboard_config.raw);
  341. }
  342. break;
  343. case QK_RGB_MATRIX_TOGGLE:
  344. if (record->event.pressed) {
  345. switch (rgb_matrix_get_flags()) {
  346. case LED_FLAG_ALL: {
  347. rgb_matrix_set_flags(LED_FLAG_NONE);
  348. keyboard_config.rgb_matrix_enable = false;
  349. rgb_matrix_set_color_all(0, 0, 0);
  350. } break;
  351. default: {
  352. rgb_matrix_set_flags(LED_FLAG_ALL);
  353. keyboard_config.rgb_matrix_enable = true;
  354. } break;
  355. }
  356. eeconfig_update_kb(keyboard_config.raw);
  357. }
  358. return false;
  359. #endif
  360. }
  361. return true;
  362. }
  363. void keyboard_post_init_kb(void) {
  364. keyboard_config.raw = eeconfig_read_kb();
  365. if (!keyboard_config.led_level && !keyboard_config.led_level_res) {
  366. keyboard_config.led_level = true;
  367. keyboard_config.led_level_res = 0b11;
  368. eeconfig_update_kb(keyboard_config.raw);
  369. }
  370. #ifdef RGB_MATRIX_ENABLE
  371. rgb_matrix_enable_noeeprom();
  372. #endif
  373. #if defined(DEFERRED_EXEC_ENABLE)
  374. is_launching = true;
  375. defer_exec(500, startup_exec, NULL);
  376. #endif
  377. keyboard_post_init_user();
  378. }
  379. void eeconfig_init_kb(void) { // EEPROM is getting reset!
  380. keyboard_config.raw = 0;
  381. keyboard_config.rgb_matrix_enable = true;
  382. keyboard_config.led_level = true;
  383. keyboard_config.led_level_res = 0b11;
  384. eeconfig_update_kb(keyboard_config.raw);
  385. eeconfig_init_user();
  386. }