You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

604 lines
19 KiB

  1. /* Copyright (C) 2022 jonylee@hfd
  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. // clang-format off
  18. #ifdef RGB_MATRIX_ENABLE
  19. const snled27351_led_t PROGMEM g_snled27351_leds[SNLED27351_LED_COUNT] = {
  20. /* Refer to SNLED27351 manual for these locations
  21. * driver
  22. * | R location
  23. * | | G location
  24. * | | | B location
  25. * | | | | */
  26. {1, A_1, B_1, C_1},
  27. {1, A_2, B_2, C_2},
  28. {1, A_3, B_3, C_3},
  29. {1, A_4, B_4, C_4},
  30. {1, A_5, B_5, C_5},
  31. {1, A_6, B_6, C_6},
  32. {1, A_7, B_7, C_7},
  33. {1, A_8, B_8, C_8},
  34. {1, A_9, B_9, C_9},
  35. {1, A_10, B_10, C_10},
  36. {1, A_11, B_11, C_11},
  37. {1, A_12, B_12, C_12},
  38. {1, A_13, B_13, C_13},
  39. {1, A_14, B_14, C_14},
  40. {0, A_1, B_1, C_1},
  41. {0, A_2, B_2, C_2},
  42. {0, A_3, B_3, C_3},
  43. {0, A_4, B_4, C_4},
  44. {0, A_5, B_5, C_5},
  45. {0, A_6, B_6, C_6},
  46. {0, A_7, B_7, C_7},
  47. {0, A_8, B_8, C_8},
  48. {0, A_9, B_9, C_9},
  49. {0, A_10, B_10, C_10},
  50. {0, A_11, B_11, C_11},
  51. {0, A_12, B_12, C_12},
  52. {0, A_13, B_13, C_13},
  53. {0, A_14, B_14, C_14},
  54. {1, D_1, E_1, F_1},
  55. {0, D_1, E_1, F_1},
  56. {0, D_2, E_2, F_2},
  57. {0, D_3, E_3, F_3},
  58. {0, D_4, E_4, F_4},
  59. {0, D_5, E_5, F_5},
  60. {0, D_6, E_6, F_6},
  61. {0, D_7, E_7, F_7},
  62. {0, D_8, E_8, F_8},
  63. {0, D_9, E_9, F_9},
  64. {0, D_10, E_10, F_10},
  65. {0, D_11, E_11, F_11},
  66. {0, D_12, E_12, F_12},
  67. {0, D_13, E_13, F_13},
  68. {0, D_14, E_14, F_14},
  69. {1, D_2, E_2, F_2},
  70. {0, G_1, H_1, I_1},
  71. {0, G_2, H_2, I_2},
  72. {0, G_3, H_3, I_3},
  73. {0, G_4, H_4, I_4},
  74. {0, G_5, H_5, I_5},
  75. {0, G_6, H_6, I_6},
  76. {0, G_7, H_7, I_7},
  77. {0, G_8, H_8, I_8},
  78. {0, G_9, H_9, I_9},
  79. {0, G_10, H_10, I_10},
  80. {0, G_11, H_11, I_11},
  81. {0, G_12, H_12, I_12},
  82. {0, G_13, H_13, I_13},
  83. {1, D_3, E_3, F_3},
  84. {0, J_1, K_1, L_1},
  85. {0, J_2, K_2, L_2},
  86. {0, J_3, K_3, L_3},
  87. {0, J_4, K_4, L_4},
  88. {0, J_5, K_5, L_5},
  89. {0, J_6, K_6, L_6},
  90. {0, J_7, K_7, L_7},
  91. {0, J_8, K_8, L_8},
  92. {0, J_9, K_9, L_9},
  93. {0, J_10, K_10, L_10},
  94. {0, J_11, K_11, L_11},
  95. {0, J_12, K_12, L_12},
  96. {1, D_7, E_7, F_7},
  97. {1, D_4, E_4, F_4},
  98. {0, J_13, K_13, L_13},
  99. {0, J_14, K_14, L_14},
  100. {0, J_15, K_15, L_15},
  101. {0, J_16, K_16, L_16},
  102. {0, G_14, H_14, I_14},
  103. {0, G_15, H_15, I_15},
  104. {0, G_16, H_16, I_16},
  105. {0, D_15, E_15, F_15},
  106. {1, D_6, E_6, F_6},
  107. {1, D_5, E_5, F_5},
  108. {1, G_1, H_1, I_1},
  109. {1, G_2, H_2, I_2},
  110. {1, G_3, H_3, I_3},
  111. {1, G_4, H_4, I_4},
  112. {1, G_5, H_5, I_5},
  113. {1, J_1, K_1, L_1},
  114. {1, J_2, K_2, L_2},
  115. {1, J_3, K_3, L_3},
  116. {1, J_4, K_4, L_4},
  117. {1, J_5, K_5, L_5},
  118. };
  119. led_config_t g_led_config = {
  120. {
  121. { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, NO_LED, NO_LED},
  122. { 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, NO_LED, 28},
  123. { 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, NO_LED, 43},
  124. { 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, NO_LED, 56, NO_LED, 57},
  125. { 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, NO_LED, NO_LED, 69, 70, 71},
  126. { 72, 73, 74, NO_LED, NO_LED, 75, NO_LED, NO_LED, NO_LED, 76, 77, 78, NO_LED, 79, 80, 81}
  127. },
  128. {
  129. { 0, 0}, // 0
  130. { 14, 0}, // 1
  131. { 29, 0}, // 2
  132. { 44, 0}, // 3
  133. { 59, 0}, // 4
  134. { 74, 0}, // 5
  135. { 89, 0}, // 6
  136. {104, 0}, // 7
  137. {119, 0}, // 8
  138. {134, 0}, // 9
  139. {149, 0}, // 10
  140. {164, 0}, // 11
  141. {179, 0}, // 12
  142. {194, 0}, // 13
  143. { 0, 12}, // 14
  144. { 14, 12}, // 15
  145. { 28, 12}, // 16
  146. { 42, 12}, // 17
  147. { 56, 12}, // 18
  148. { 70, 12}, // 19
  149. { 84, 12}, // 20
  150. { 98, 12}, // 21
  151. {112, 12}, // 22
  152. {126, 12}, // 23
  153. {140, 12}, // 24
  154. {154, 12}, // 25
  155. {168, 12}, // 26
  156. {182, 12}, // 27
  157. {224, 12}, // 28
  158. { 0, 25}, // 29
  159. { 14, 25}, // 30
  160. { 28, 25}, // 31
  161. { 42, 25}, // 32
  162. { 56, 25}, // 33
  163. { 70, 25}, // 34
  164. { 84, 25}, // 35
  165. { 98, 25}, // 36
  166. {112, 25}, // 37
  167. {126, 25}, // 38
  168. {140, 25}, // 39
  169. {154, 25}, // 40
  170. {168, 25}, // 41
  171. {182, 25}, // 42
  172. {224, 25}, // 43
  173. { 0, 38}, // 44
  174. { 28, 38}, // 45
  175. { 42, 38}, // 46
  176. { 56, 38}, // 47
  177. { 70, 38}, // 48
  178. { 84, 38}, // 49
  179. { 98, 38}, // 50
  180. {112, 38}, // 51
  181. {126, 38}, // 52
  182. {140, 38}, // 53
  183. {154, 38}, // 54
  184. {168, 38}, // 55
  185. {182, 38}, // 56
  186. {224, 38}, // 57
  187. { 0, 51}, // 58
  188. { 18, 51}, // 59
  189. { 37, 51}, // 60
  190. { 56, 51}, // 61
  191. { 74, 51}, // 62
  192. { 93, 51}, // 63
  193. {112, 51}, // 64
  194. {130, 51}, // 65
  195. {149, 51}, // 66
  196. {168, 51}, // 67
  197. {186, 51}, // 68
  198. {200, 51}, // 69
  199. {214, 51}, // 70
  200. {224, 51}, // 71
  201. { 0, 64}, // 72
  202. { 18, 64}, // 73
  203. { 37, 64}, // 74
  204. { 92, 64}, // 75
  205. {140, 64}, // 76
  206. {154, 64}, // 77
  207. {168, 64}, // 78
  208. {196, 64}, // 80
  209. {210, 64}, // 81
  210. {224, 64}, // 82
  211. {0, 0}, // 68 LED 1
  212. {0, 16}, // 69 LED 2
  213. {0, 32}, // 70 LED 3
  214. {0, 48}, // 71 LED 4
  215. {0, 64}, // 72 LED 5
  216. {224, 0 }, // 78 LED 12
  217. {224, 16}, // 79 LED 13
  218. {224, 32}, // 80 LED 14
  219. {224, 48}, // 81 LED 15
  220. {224, 64}, // 82 LED 16
  221. },
  222. {
  223. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  224. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  225. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  226. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  227. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  228. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  229. 2, 2, 2, 2, 2,
  230. 2, 2, 2, 2, 2,
  231. }
  232. };
  233. bool rgb_matrix_indicators_advanced_kb(uint8_t led_min, uint8_t led_max) {
  234. if (!rgb_matrix_indicators_advanced_user(led_min, led_max)) {
  235. return false;
  236. }
  237. // caps lock red
  238. if (host_keyboard_led_state().caps_lock) {
  239. RGB_MATRIX_INDICATOR_SET_COLOR(44, 255, 0, 0);
  240. } else {
  241. if (!rgb_matrix_get_flags()) {
  242. RGB_MATRIX_INDICATOR_SET_COLOR(44, 0, 0, 0);
  243. }
  244. }
  245. // GUI lock red
  246. if (keymap_config.no_gui) {
  247. RGB_MATRIX_INDICATOR_SET_COLOR(73, 255, 0, 0);
  248. } else {
  249. if (!rgb_matrix_get_flags()) {
  250. RGB_MATRIX_INDICATOR_SET_COLOR(73, 0, 0, 0);
  251. }
  252. }
  253. return true;
  254. }
  255. #endif
  256. enum __layers {
  257. WIN_B,
  258. WIN_FN,
  259. MAC_B,
  260. MAC_FN
  261. };
  262. enum colors {
  263. WHITE,
  264. RED,
  265. GREEN,
  266. BLUE
  267. };
  268. enum colors led_color_status = WHITE;
  269. // clang-format on
  270. static bool fn_make_flag = false;
  271. static bool Lkey_flag = false;
  272. static bool reset_glint_flag = false;
  273. static bool while_test_flag = false;
  274. static bool alarm_flag = false;
  275. static uint16_t current_time = 0;
  276. static uint8_t glint_cnt = 0;
  277. static uint16_t scancode = 0;
  278. static uint8_t alarm_cnt = 0;
  279. static uint8_t RGB_HSV_level;
  280. HSV hsv;
  281. void led_test(uint8_t color);
  282. void clear_eeprom(void);
  283. void rgb_hsv_updata_user(void);
  284. bool dip_switch_update_kb(uint8_t index, bool active) {
  285. if (!dip_switch_update_user(index, active)) {
  286. return false;
  287. }
  288. if (index == 0) {
  289. default_layer_set(1UL << (active ? 2 : 0));
  290. }
  291. if(active){
  292. keymap_config.no_gui = 0;
  293. eeconfig_update_keymap(keymap_config.raw);
  294. }
  295. return true;
  296. }
  297. bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
  298. if (!process_record_user(keycode, record)) {
  299. return false;
  300. }
  301. switch (keycode) {
  302. case MO(WIN_FN):
  303. case MO(MAC_FN):
  304. fn_make_flag = record->event.pressed;
  305. return true;
  306. case KC_ESC:
  307. if (fn_make_flag && record->event.pressed) {
  308. Lkey_flag = true;
  309. current_time = timer_read();
  310. scancode = KC_ESC;
  311. return false;
  312. } else {
  313. Lkey_flag = 0;
  314. }
  315. return true;
  316. case KC_END:
  317. if (fn_make_flag && record->event.pressed) {
  318. if (while_test_flag) {
  319. while_test_flag = false;
  320. rgb_matrix_init();
  321. } else {
  322. Lkey_flag = true;
  323. current_time = timer_read();
  324. scancode = KC_END;
  325. }
  326. return false;
  327. } else {
  328. Lkey_flag = 0;
  329. }
  330. return true;
  331. case KC_LEFT:
  332. if (while_test_flag == true) {
  333. if (record->event.pressed) {
  334. if (glint_cnt == 0)
  335. glint_cnt = 3;
  336. else
  337. glint_cnt--;
  338. if ((glint_cnt % 4) == 0) {
  339. rgb_matrix_sethsv_noeeprom(HSV_WHITE);
  340. } else if ((glint_cnt % 4) == 1) {
  341. rgb_matrix_sethsv_noeeprom(HSV_RED);
  342. } else if ((glint_cnt % 4) == 2) {
  343. rgb_matrix_sethsv_noeeprom(HSV_GREEN);
  344. } else if ((glint_cnt % 4) == 3) {
  345. rgb_matrix_sethsv_noeeprom(HSV_BLUE);
  346. }
  347. }
  348. return false;
  349. }
  350. return true;
  351. case KC_RGHT:
  352. if (while_test_flag == true) {
  353. if (record->event.pressed) {
  354. glint_cnt++;
  355. if (glint_cnt >= 4) glint_cnt = 0;
  356. if ((glint_cnt % 4) == 0) {
  357. rgb_matrix_sethsv_noeeprom(HSV_WHITE);
  358. } else if ((glint_cnt % 4) == 1) {
  359. rgb_matrix_sethsv_noeeprom(HSV_RED);
  360. } else if ((glint_cnt % 4) == 2) {
  361. rgb_matrix_sethsv_noeeprom(HSV_GREEN);
  362. } else if ((glint_cnt % 4) == 3) {
  363. rgb_matrix_sethsv_noeeprom(HSV_BLUE);
  364. }
  365. }
  366. return false;
  367. }
  368. return true;
  369. case DF(WIN_B):
  370. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  371. alarm_flag = true;
  372. rgb_matrix_toggle_noeeprom();
  373. current_time = timer_read();
  374. set_single_persistent_default_layer(WIN_B);
  375. return false;
  376. }
  377. return true;
  378. case DF(MAC_B):
  379. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  380. alarm_flag = true;
  381. rgb_matrix_toggle_noeeprom();
  382. current_time = timer_read();
  383. set_single_persistent_default_layer(MAC_B);
  384. return false;
  385. }
  386. return true;
  387. case RGB_VAI:
  388. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  389. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_val() / (RGB_MATRIX_MAXIMUM_BRIGHTNESS / 4)) < 4) {
  390. RGB_HSV_level++;
  391. rgb_matrix_config.hsv.v = (uint8_t)(RGB_MATRIX_MAXIMUM_BRIGHTNESS / 4) * RGB_HSV_level;
  392. }
  393. rgb_hsv_updata_user();
  394. }
  395. return false;
  396. case RGB_VAD:
  397. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  398. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_val() / (RGB_MATRIX_MAXIMUM_BRIGHTNESS / 4)) > 0) {
  399. RGB_HSV_level--;
  400. rgb_matrix_config.hsv.v = (uint8_t)(RGB_MATRIX_MAXIMUM_BRIGHTNESS / 4) * RGB_HSV_level;
  401. }
  402. rgb_hsv_updata_user();
  403. }
  404. return false;
  405. case RGB_SAI:
  406. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  407. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_sat() / (UINT8_MAX / 4)) < 4) {
  408. RGB_HSV_level++;
  409. rgb_matrix_config.hsv.s = (uint8_t)(UINT8_MAX / 4) * RGB_HSV_level;
  410. }
  411. rgb_hsv_updata_user();
  412. }
  413. return false;
  414. case RGB_SAD:
  415. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  416. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_sat() / (UINT8_MAX / 4)) > 0) {
  417. RGB_HSV_level--;
  418. rgb_matrix_config.hsv.s = (uint8_t)(UINT8_MAX / 4) * RGB_HSV_level;
  419. }
  420. rgb_hsv_updata_user();
  421. }
  422. return false;
  423. case RGB_HUI:
  424. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  425. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_hue() / (UINT8_MAX / 6)) < 6) {
  426. RGB_HSV_level++;
  427. rgb_matrix_config.hsv.h = (uint8_t)(UINT8_MAX / 6) * RGB_HSV_level;
  428. }
  429. rgb_hsv_updata_user();
  430. }
  431. return false;
  432. case RGB_HUD:
  433. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  434. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_hue() / (UINT8_MAX / 6)) > 0) {
  435. RGB_HSV_level--;
  436. rgb_matrix_config.hsv.h = (uint8_t)(UINT8_MAX / 6) * RGB_HSV_level;
  437. }
  438. rgb_hsv_updata_user();
  439. }
  440. return false;
  441. case RGB_SPI:
  442. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  443. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_speed() / (UINT8_MAX / 4)) < 4) {
  444. RGB_HSV_level++;
  445. rgb_matrix_set_speed((uint8_t)(UINT8_MAX / 4) * RGB_HSV_level);
  446. }
  447. }
  448. return false;
  449. case RGB_SPD:
  450. if ((fn_make_flag && record->event.pressed) && (alarm_flag == 0)) {
  451. if ((RGB_HSV_level = (uint8_t)rgb_matrix_get_speed() / (UINT8_MAX / 4)) > 0) {
  452. RGB_HSV_level--;
  453. rgb_matrix_set_speed((uint8_t)(UINT8_MAX / 4) * RGB_HSV_level);
  454. }
  455. }
  456. return false;
  457. case RGB_TOG:
  458. if (record->event.pressed) {
  459. switch (rgb_matrix_get_flags()) {
  460. case LED_FLAG_ALL: {
  461. rgb_matrix_set_flags(LED_FLAG_NONE);
  462. rgb_matrix_set_color_all(0, 0, 0);
  463. } break;
  464. default: {
  465. rgb_matrix_set_flags(LED_FLAG_ALL);
  466. } break;
  467. }
  468. }
  469. if (!rgb_matrix_is_enabled()) {
  470. rgb_matrix_set_flags(LED_FLAG_ALL);
  471. rgb_matrix_enable();
  472. }
  473. return false;
  474. default:
  475. return process_record_user(keycode, record);
  476. }
  477. }
  478. void housekeeping_task_kb(void) {
  479. if (Lkey_flag) {
  480. if (scancode == KC_ESC) {
  481. if (timer_elapsed(current_time) >= 3000) {
  482. Lkey_flag = false;
  483. clear_eeprom();
  484. current_time = timer_read();
  485. reset_glint_flag = true;
  486. glint_cnt = 0;
  487. rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_COLOR);
  488. rgb_matrix_sethsv_noeeprom(HSV_OFF);
  489. }
  490. } else if (scancode == KC_END) {
  491. if (timer_elapsed(current_time) >= 3000) {
  492. Lkey_flag = false;
  493. clear_eeprom();
  494. while_test_flag = true;
  495. glint_cnt = 0;
  496. rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_COLOR);
  497. rgb_matrix_sethsv_noeeprom(HSV_WHITE);
  498. }
  499. }
  500. } else if (reset_glint_flag) {
  501. if ((timer_elapsed(current_time)) >= 300) {
  502. current_time = timer_read();
  503. if (((glint_cnt++) & 0x01) == 0) {
  504. rgb_matrix_sethsv_noeeprom(HSV_RED);
  505. } else {
  506. rgb_matrix_sethsv_noeeprom(HSV_OFF);
  507. }
  508. if (glint_cnt >= 7) {
  509. glint_cnt = 0;
  510. reset_glint_flag = false;
  511. rgb_matrix_init();
  512. }
  513. }
  514. } else if (alarm_cnt != 0) {
  515. alarm_cnt--;
  516. if (alarm_cnt == 0) {
  517. alarm_flag = true;
  518. rgb_matrix_toggle_noeeprom();
  519. current_time = timer_read();
  520. }
  521. } else if (alarm_flag) {
  522. if ((timer_elapsed(current_time)) >= 200) {
  523. rgb_matrix_toggle_noeeprom();
  524. alarm_flag = 0;
  525. }
  526. }
  527. }
  528. void led_test(uint8_t color) {
  529. rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_COLOR);
  530. switch (color) {
  531. case WHITE:
  532. rgb_matrix_sethsv_noeeprom(HSV_WHITE);
  533. break;
  534. case RED:
  535. rgb_matrix_sethsv_noeeprom(HSV_RED);
  536. break;
  537. case GREEN:
  538. rgb_matrix_sethsv_noeeprom(HSV_GREEN);
  539. break;
  540. case BLUE:
  541. rgb_matrix_sethsv_noeeprom(HSV_BLUE);
  542. break;
  543. }
  544. }
  545. void clear_eeprom(void) {
  546. layer_state_t default_layer_temp = default_layer_state;
  547. eeconfig_init();
  548. default_layer_set(default_layer_temp);
  549. #ifdef VIA_ENABLE
  550. // This resets the layout options
  551. via_set_layout_options(VIA_EEPROM_LAYOUT_OPTIONS_DEFAULT);
  552. // This resets the keymaps in EEPROM to what is in flash.
  553. dynamic_keymap_reset();
  554. // This resets the macros in EEPROM to nothing.
  555. dynamic_keymap_macro_reset();
  556. #endif
  557. rgb_matrix_enable_noeeprom();
  558. }
  559. void rgb_hsv_updata_user(void) {
  560. rgb_matrix_sethsv(rgb_matrix_config.hsv.h, rgb_matrix_config.hsv.s, rgb_matrix_config.hsv.v);
  561. }