moonlander.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* Copyright 2020 ZSA Technology Labs, Inc <@zsa>
  2. * Copyright 2020 Jack Humbert <jack.humb@gmail.com>
  3. * Copyright 2020 Christopher Courtney <drashna@live.com> (@drashna)
  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. #ifdef WEBUSB_ENABLE
  20. # include "webusb.h"
  21. #endif
  22. keyboard_config_t keyboard_config;
  23. bool mcp23018_leds[3] = {0, 0, 0};
  24. bool is_launching = false;
  25. #ifdef DYNAMIC_MACRO_ENABLE
  26. static bool is_dynamic_recording = false;
  27. void dynamic_macro_record_start_user(void) { is_dynamic_recording = true; }
  28. void dynamic_macro_record_end_user(int8_t direction) {
  29. is_dynamic_recording = false;
  30. ML_LED_3(false);
  31. }
  32. #endif
  33. void moonlander_led_task(void) {
  34. if (is_launching) {
  35. ML_LED_1(false);
  36. ML_LED_2(false);
  37. ML_LED_3(false);
  38. ML_LED_4(false);
  39. ML_LED_5(false);
  40. ML_LED_6(false);
  41. ML_LED_1(true);
  42. wait_ms(250);
  43. ML_LED_2(true);
  44. wait_ms(250);
  45. ML_LED_3(true);
  46. wait_ms(250);
  47. ML_LED_4(true);
  48. wait_ms(250);
  49. ML_LED_5(true);
  50. wait_ms(250);
  51. ML_LED_6(true);
  52. wait_ms(250);
  53. ML_LED_1(false);
  54. wait_ms(250);
  55. ML_LED_2(false);
  56. wait_ms(250);
  57. ML_LED_3(false);
  58. wait_ms(250);
  59. ML_LED_4(false);
  60. wait_ms(250);
  61. ML_LED_5(false);
  62. wait_ms(250);
  63. ML_LED_6(false);
  64. wait_ms(250);
  65. is_launching = false;
  66. layer_state_set_kb(layer_state);
  67. }
  68. #ifdef DYNAMIC_MACRO_ENABLE
  69. else if (is_dynamic_recording) {
  70. ML_LED_3(true);
  71. wait_ms(100);
  72. ML_LED_3(false);
  73. wait_ms(155);
  74. }
  75. #endif
  76. #ifdef WEBUSB_ENABLE
  77. else if (webusb_state.pairing == true) {
  78. static uint8_t led_mask;
  79. ML_LED_1(false);
  80. ML_LED_2(false);
  81. ML_LED_3(false);
  82. ML_LED_4(false);
  83. ML_LED_5(false);
  84. ML_LED_6(false);
  85. if (!led_mask) {
  86. led_mask = 1;
  87. } else {
  88. led_mask++;
  89. if (led_mask > 12) led_mask = 1;
  90. }
  91. switch (led_mask) {
  92. case 1:
  93. case 12:
  94. ML_LED_1(true);
  95. break;
  96. case 2:
  97. case 11:
  98. ML_LED_2(true);
  99. break;
  100. case 3:
  101. case 10:
  102. ML_LED_3(true);
  103. break;
  104. case 4:
  105. case 9:
  106. ML_LED_4(true);
  107. break;
  108. case 5:
  109. case 8:
  110. ML_LED_5(true);
  111. break;
  112. case 6:
  113. case 7:
  114. ML_LED_6(true);
  115. break;
  116. }
  117. wait_ms(150);
  118. }
  119. #endif
  120. }
  121. static THD_WORKING_AREA(waLEDThread, 128);
  122. static THD_FUNCTION(LEDThread, arg) {
  123. (void)arg;
  124. chRegSetThreadName("LEDThread");
  125. while (true) {
  126. moonlander_led_task();
  127. }
  128. }
  129. void keyboard_pre_init_kb(void) {
  130. setPinOutput(B5);
  131. setPinOutput(B4);
  132. setPinOutput(B3);
  133. writePinLow(B5);
  134. writePinLow(B4);
  135. writePinLow(B3);
  136. chThdCreateStatic(waLEDThread, sizeof(waLEDThread), NORMALPRIO - 16, LEDThread, NULL);
  137. /* the array is initialized to 0, no need to re-set it here */
  138. // mcp23018_leds[0] = 0; // blue
  139. // mcp23018_leds[1] = 0; // green
  140. // mcp23018_leds[2] = 0; // red
  141. keyboard_pre_init_user();
  142. }
  143. #if !defined(MOONLANDER_USER_LEDS)
  144. layer_state_t layer_state_set_kb(layer_state_t state) {
  145. state = layer_state_set_user(state);
  146. if (is_launching || !keyboard_config.led_level) return state;
  147. ML_LED_1(false);
  148. ML_LED_2(false);
  149. ML_LED_3(false);
  150. ML_LED_4(false);
  151. ML_LED_5(false);
  152. ML_LED_6(false);
  153. uint8_t layer = get_highest_layer(state);
  154. switch (layer) {
  155. case 1:
  156. ML_LED_1(1);
  157. ML_LED_4(1);
  158. break;
  159. case 2:
  160. ML_LED_2(1);
  161. ML_LED_5(1);
  162. break;
  163. case 3:
  164. ML_LED_3(1);
  165. break;
  166. case 4:
  167. ML_LED_4(1);
  168. break;
  169. case 5:
  170. ML_LED_5(1);
  171. break;
  172. case 6:
  173. ML_LED_6(1);
  174. break;
  175. default:
  176. break;
  177. }
  178. return state;
  179. }
  180. #endif
  181. #ifdef RGB_MATRIX_ENABLE
  182. // clang-format off
  183. const is31_led g_is31_leds[DRIVER_LED_TOTAL] = {
  184. /* Refer to IS31 manual for these locations
  185. * driver
  186. * | R location
  187. * | | G location
  188. * | | | B location
  189. * | | | | */
  190. {0, C3_2, C1_1, C4_2}, // 1
  191. {0, C2_2, C1_2, C4_3},
  192. {0, C2_3, C1_3, C3_3},
  193. {0, C2_4, C1_4, C3_4},
  194. {0, C2_5, C1_5, C3_5},
  195. {0, C2_6, C1_6, C3_6},
  196. {0, C2_7, C1_7, C3_7},
  197. {0, C2_8, C1_8, C3_8},
  198. {0, C3_1, C2_1, C4_1},
  199. {0, C7_8, C6_8, C8_8}, // 10
  200. {0, C7_7, C6_7, C9_8},
  201. {0, C8_7, C6_6, C9_7},
  202. {0, C8_6, C7_6, C9_6},
  203. {0, C8_5, C7_5, C9_5},
  204. {0, C8_4, C7_4, C9_4},
  205. {0, C8_3, C7_3, C9_3},
  206. {0, C8_2, C7_2, C9_2},
  207. {0, C8_1, C7_1, C9_1},
  208. {0, C3_10, C1_9, C4_10}, // 19
  209. {0, C2_10, C1_10, C4_11},
  210. {0, C2_11, C1_11, C3_11},
  211. {0, C2_12, C1_12, C3_12},
  212. {0, C2_13, C1_13, C3_13},
  213. {0, C2_14, C1_14, C3_14},
  214. {0, C2_15, C1_15, C3_15},
  215. {0, C2_16, C1_16, C3_16},
  216. {0, C3_9, C2_9, C4_9},
  217. {0, C7_16, C6_16, C8_16}, // 28
  218. {0, C7_15, C6_15, C9_16},
  219. {0, C8_15, C6_14, C9_15},
  220. {0, C8_10, C7_10, C9_10},
  221. {0, C8_9, C7_9, C9_9},
  222. {0, C8_11, C7_11, C9_11},
  223. {0, C8_12, C7_12, C9_12},
  224. {0, C8_13, C7_13, C9_13},
  225. {0, C8_14, C7_14, C9_14},
  226. {1, C3_2, C1_1, C4_2}, // 1
  227. {1, C2_2, C1_2, C4_3},
  228. {1, C2_3, C1_3, C3_3},
  229. {1, C2_4, C1_4, C3_4},
  230. {1, C2_5, C1_5, C3_5},
  231. {1, C2_6, C1_6, C3_6},
  232. {1, C2_7, C1_7, C3_7},
  233. {1, C2_8, C1_8, C3_8},
  234. {1, C3_1, C2_1, C4_1},
  235. {1, C7_8, C6_8, C8_8}, // 10
  236. {1, C7_7, C6_7, C9_8},
  237. {1, C8_7, C6_6, C9_7},
  238. {1, C8_6, C7_6, C9_6},
  239. {1, C8_5, C7_5, C9_5},
  240. {1, C8_4, C7_4, C9_4},
  241. {1, C8_3, C7_3, C9_3},
  242. {1, C8_2, C7_2, C9_2},
  243. {1, C8_1, C7_1, C9_1},
  244. {1, C3_10, C1_9, C4_10}, // 19
  245. {1, C2_10, C1_10, C4_11},
  246. {1, C2_11, C1_11, C3_11},
  247. {1, C2_12, C1_12, C3_12},
  248. {1, C2_13, C1_13, C3_13},
  249. {1, C2_14, C1_14, C3_14},
  250. {1, C2_15, C1_15, C3_15},
  251. {1, C2_16, C1_16, C3_16},
  252. {1, C3_9, C2_9, C4_9},
  253. {1, C7_16, C6_16, C8_16}, // 28
  254. {1, C7_15, C6_15, C9_16},
  255. {1, C8_15, C6_14, C9_15},
  256. {1, C8_10, C7_10, C9_10},
  257. {1, C8_9, C7_9, C9_9},
  258. {1, C8_11, C7_11, C9_11},
  259. {1, C8_12, C7_12, C9_12},
  260. {1, C8_13, C7_13, C9_13},
  261. {1, C8_14, C7_14, C9_14},
  262. };
  263. led_config_t g_led_config = { {
  264. { 0, 5, 10, 15, 20, 25, 29 },
  265. { 1, 6, 11, 16, 21, 26, 30 },
  266. { 2, 7, 12, 17, 22, 27, 31 },
  267. { 3, 8, 13, 18, 23, 28, NO_LED },
  268. { 4, 9, 14, 19, 24, NO_LED, NO_LED },
  269. { 32, 33, 34, 35, NO_LED, NO_LED, NO_LED },
  270. { 65, 61, 56, 51, 46, 41, 36 },
  271. { 66, 62, 57, 52, 47, 42, 37 },
  272. { 67, 63, 58, 53, 48, 43, 38 },
  273. { NO_LED, 64, 59, 54, 49, 44, 39 },
  274. { NO_LED, NO_LED, 60, 55, 50, 45, 40 },
  275. { NO_LED, NO_LED, NO_LED, 71, 70, 69, 68 },
  276. }, {
  277. { 0, 0 }, { 0, 12 }, { 0, 25 }, { 0, 38 }, { 0, 51 },
  278. { 17, 0 }, { 17, 12 }, { 17, 25 }, { 17, 38 }, { 17, 51 },
  279. { 34, 0 }, { 34, 12 }, { 34, 25 }, { 34, 38 }, { 34, 51 },
  280. { 51, 0 }, { 51, 12 }, { 51, 25 }, { 51, 38 }, { 51, 51 },
  281. { 68, 0 }, { 68, 12 }, { 68, 25 }, { 68, 38 }, { 68, 51 },
  282. { 86, 0 }, { 86, 12 }, { 86, 25 }, { 86, 38 },
  283. { 105, 0 }, { 105, 12 }, { 105, 25 },
  284. { 90, 55 }, { 105, 68 }, { 116, 86 }, { 116, 59 },
  285. { 250, 0 }, { 250, 12 }, { 250, 25 }, { 250, 38 }, { 250, 51 },
  286. { 233, 0 }, { 233, 12 }, { 233, 25 }, { 233, 38 }, { 233, 51 },
  287. { 216, 0 }, { 216, 12 }, { 216, 25 }, { 216, 38 }, { 216, 51 },
  288. { 198, 0 }, { 198, 12 }, { 198, 25 }, { 198, 38 }, { 198, 51 },
  289. { 181, 0 }, { 181, 12 }, { 181, 25 }, { 181, 38 }, { 181, 51 },
  290. { 163, 0 }, { 163, 12 }, { 163, 25 }, { 163, 38 },
  291. { 146, 0 }, { 146, 12 }, { 146, 25 },
  292. { 161, 55 }, { 161, 68 }, { 146, 86 }, { 131, 59 }
  293. }, {
  294. 1, 1, 1, 1, 1, 4,
  295. 4, 4, 4, 1, 4, 4,
  296. 4, 4, 1, 4, 4, 4,
  297. 4, 1, 4, 4, 4, 4,
  298. 1, 4, 4, 4, 4, 4,
  299. 4, 4, 1, 1, 1, 1,
  300. 1, 1, 1, 1, 1, 4,
  301. 4, 4, 4, 1, 4, 4,
  302. 4, 4, 1, 4, 4, 4,
  303. 4, 1, 4, 4, 4, 4,
  304. 1, 4, 4, 4, 4, 4,
  305. 4, 4, 1, 1, 1, 1
  306. } };
  307. // clang-format on
  308. void suspend_power_down_kb(void) {
  309. rgb_matrix_set_suspend_state(true);
  310. suspend_power_down_user();
  311. }
  312. void suspend_wakeup_init_kb(void) {
  313. rgb_matrix_set_suspend_state(false);
  314. suspend_wakeup_init_user();
  315. }
  316. #endif
  317. #ifdef AUDIO_ENABLE
  318. bool music_mask_kb(uint16_t keycode) {
  319. switch (keycode) {
  320. case QK_LAYER_TAP ... QK_ONE_SHOT_LAYER_MAX:
  321. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_MOD_MAX:
  322. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  323. case AU_ON ... MUV_DE:
  324. case RESET:
  325. case EEP_RST:
  326. return false;
  327. default:
  328. return music_mask_user(keycode);
  329. }
  330. }
  331. #endif
  332. #ifdef SWAP_HANDS_ENABLE
  333. // swap-hands action needs a matrix to define the swap
  334. // clang-format off
  335. const keypos_t PROGMEM hand_swap_config[MATRIX_ROWS][MATRIX_COLS] = {
  336. /* Left hand, matrix positions */
  337. {{6,6}, {5,6}, {4,6}, {3,6}, {2,6}, {1,6},{0,6}},
  338. {{6,7}, {5,7}, {4,7}, {3,7}, {2,7}, {1,7},{0,7}},
  339. {{6,8}, {5,8}, {4,8}, {3,8}, {2,8}, {1,8},{0,8}},
  340. {{6,9}, {5,9}, {4,9}, {3,9}, {2,9}, {1,9},{0,9}},
  341. {{6,10},{5,10},{4,10},{3,10},{2,10},{1,10},{0,10}},
  342. {{6,11},{5,11},{4,11},{3,11},{2,11},{1,11},{0,11}},
  343. /* Right hand, matrix positions */
  344. {{6,0}, {5,0}, {4,0}, {3,0}, {2,0}, {1,0},{0,0}},
  345. {{6,1}, {5,1}, {4,1}, {3,1}, {2,1}, {1,1},{0,1}},
  346. {{6,2}, {5,2}, {4,2}, {3,2}, {2,2}, {1,2},{0,2}},
  347. {{6,3}, {5,3}, {4,3}, {3,3}, {2,3}, {1,3},{0,3}},
  348. {{6,4}, {5,4}, {4,4}, {3,4}, {2,4}, {1,4},{0,4}},
  349. {{6,5}, {5,5}, {4,5}, {3,5}, {2,5}, {1,5},{0,5}},
  350. };
  351. // clang-format on
  352. void keyboard_post_init_kb(void) {
  353. rgb_matrix_enable_noeeprom();
  354. keyboard_post_init_user();
  355. }
  356. #endif
  357. #if defined(AUDIO_ENABLE) && defined(MUSIC_MAP)
  358. // clang-format off
  359. const uint8_t music_map[MATRIX_ROWS][MATRIX_COLS] = LAYOUT_moonlander(
  360. 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
  361. 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  362. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  363. 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  364. 8, 9, 10, 11, 12, 3, 4, 13, 14, 15, 16, 17,
  365. 0, 1, 2, 5, 6, 7
  366. );
  367. // clang-format on
  368. #endif
  369. #ifdef ORYX_CONFIGURATOR
  370. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  371. switch (keycode) {
  372. #ifdef WEBUSB_ENABLE
  373. case WEBUSB_PAIR:
  374. if (!record->event.pressed && !webusb_state.pairing) layer_state_set_kb(layer_state);
  375. break;
  376. #endif
  377. #if !defined(MOONLANDER_USER_LEDS)
  378. case LED_LEVEL:
  379. if (record->event.pressed) {
  380. keyboard_config.led_level ^= 1;
  381. eeconfig_update_kb(keyboard_config.raw);
  382. if (keyboard_config.led_level) {
  383. layer_state_set_kb(layer_state);
  384. } else {
  385. ML_LED_1(false);
  386. ML_LED_2(false);
  387. ML_LED_3(false);
  388. ML_LED_4(false);
  389. ML_LED_5(false);
  390. ML_LED_6(false);
  391. }
  392. }
  393. break;
  394. #endif
  395. #ifdef RGB_MATRIX_ENABLE
  396. case TOGGLE_LAYER_COLOR:
  397. if (record->event.pressed) {
  398. keyboard_config.disable_layer_led ^= 1;
  399. if (keyboard_config.disable_layer_led) rgb_matrix_set_color_all(0, 0, 0);
  400. eeconfig_update_kb(keyboard_config.raw);
  401. }
  402. break;
  403. case RGB_TOG:
  404. if (record->event.pressed) {
  405. switch (rgb_matrix_get_flags()) {
  406. case LED_FLAG_ALL: {
  407. rgb_matrix_set_flags(LED_FLAG_NONE);
  408. keyboard_config.rgb_matrix_enable = false;
  409. rgb_matrix_set_color_all(0, 0, 0);
  410. } break;
  411. default: {
  412. rgb_matrix_set_flags(LED_FLAG_ALL);
  413. keyboard_config.rgb_matrix_enable = true;
  414. } break;
  415. }
  416. eeconfig_update_kb(keyboard_config.raw);
  417. }
  418. return false;
  419. #endif
  420. }
  421. return process_record_user(keycode, record);
  422. }
  423. #endif
  424. void matrix_init_kb(void) {
  425. keyboard_config.raw = eeconfig_read_kb();
  426. if (!keyboard_config.led_level && !keyboard_config.led_level_res) {
  427. keyboard_config.led_level = true;
  428. keyboard_config.led_level_res = 0b11;
  429. eeconfig_update_kb(keyboard_config.raw);
  430. }
  431. #ifdef RGB_MATRIX_ENABLE
  432. if (keyboard_config.rgb_matrix_enable) {
  433. rgb_matrix_set_flags(LED_FLAG_ALL);
  434. } else {
  435. rgb_matrix_set_flags(LED_FLAG_NONE);
  436. }
  437. #endif
  438. }
  439. void eeconfig_init_kb(void) { // EEPROM is getting reset!
  440. keyboard_config.raw = 0;
  441. keyboard_config.rgb_matrix_enable = true;
  442. keyboard_config.led_level = true;
  443. keyboard_config.led_level_res = 0b11;
  444. eeconfig_update_kb(keyboard_config.raw);
  445. eeconfig_init_user();
  446. }