moonlander.c 13 KB

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