keymap.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /* Copyright 2021 3araht
  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 QMK_KEYBOARD_H
  17. #include "version.h"
  18. // define which MIDI ch to use.
  19. // Note that (actual MIDI ch# - 1) -> 0 .. 15 is used for coding.
  20. // ch1
  21. #define DEFAULT_MAIN_CH_NUMBER 0
  22. // ch3
  23. #define DEFAULT_SUB_CH_NUMBER 2
  24. // ch2
  25. #define ALT_MAIN_CH_NUMBER 1
  26. // ch4
  27. #define ALT_SUB_CH_NUMBER 3
  28. static uint8_t midi_left_ch = DEFAULT_SUB_CH_NUMBER; // By default, DEFAULT_SUB_CH_NUMBER is used for left side when separated.
  29. // By default( when use_alt_ch_gr == false), DEFAULT ch group (DEFAULT_MAIN_CH_NUMBER for entirely, or right side when separated, DEFAULT_SUB_CH_NUMBER for left side) is used.
  30. // When false, ALT ch group (ALT_MAIN_CH_NUMBER for entirely, or right side when separated, ALT_SUB_CH_NUMBER for left side) is used.
  31. static bool use_alt_ch_gr = false;
  32. // Defines names for use in layer keycodes and the keymap
  33. enum layer_names {
  34. _BASE, // Base layer, shift mode, single channel.
  35. _SEPALEFTOCT, // 1st oct channel separated version. Shift mode.
  36. _SEPAHALF, // Half channel separated version. Shift mode.
  37. _SEPARIGHTOCT, // 2nd oct channel separated version. Shift mode.
  38. _TRANS, // Transpose feature is enabled instead of shift mode, single channel.
  39. _FLIPBASE, // Horizontal flipped version entirely. single channel.
  40. _FLIPTRANS, // Horizontal flipped version entirely. Transpose is used. single channel.
  41. _FN // FuNction layer. This must be at the end of the enumurate to use the range from _LS_FN ... _LS_FN_MAX for FN layer LED settings.
  42. };
  43. // Layer State
  44. #define _LS_BASE (1UL << _BASE)
  45. #define _LS_SEPALEFTOCT (1UL << _SEPALEFTOCT)
  46. #define _LS_SEPAHALF (1UL << _SEPAHALF)
  47. #define _LS_SEPARIGHTOCT (1UL << _SEPARIGHTOCT)
  48. #define _LS_FLIPBASE (1UL << _FLIPBASE)
  49. #define _LS_TRANS (1UL << _BASE | 1UL << _TRANS)
  50. #define _LS_SEPALEFTOCT_T (1UL << _SEPALEFTOCT | 1UL << _TRANS)
  51. #define _LS_SEPAHALF_T (1UL << _SEPAHALF | 1UL << _TRANS)
  52. #define _LS_SEPARIGHTOCT_T (1UL << _SEPARIGHTOCT | 1UL << _TRANS)
  53. #define _LS_FLIPTRANS (1UL << _FLIPBASE | 1UL << _FLIPTRANS)
  54. #define _LS_FN (1UL << _FN)
  55. #define _LS_MAX (_LS_FN << 1)
  56. // Don't change the DEFAULT_SCALE_COL value below. It must be 0.
  57. #define DEFAULT_SCALE_COL 0
  58. static uint8_t scale_indicator_col = DEFAULT_SCALE_COL;
  59. static bool trans_mode_indicator_loc_sel = true; // when it is true, the location is _KEY01, _KEY13, ...
  60. // use led indicator or not.
  61. static bool led_indicator_enable = true;
  62. // Defines the keycodes used by our macros in process_record_user
  63. enum custom_keycodes {
  64. SHIFT_L = SAFE_RANGE,
  65. SHIFT_R,
  66. TGLINDI, // ToGgLe INDIcator
  67. TGLINTR, // ToGgLe INdicator location {(_KEY01, _KEY13, _KEY25, _KEY37) or (_KEY02, _KEY14, _KEY26) / (_KEY12, _KEY24, _KEY36)}in TRans mode
  68. TGLTRNS, // ToGgLe TRaNS and shift
  69. TGLCHGR, // ToGgLe CH GRoup
  70. VERSION,
  71. B_BASE, // border set to the left end.
  72. B_LEFT, // border set to the 1st left octave.
  73. B_CENTER, // border set to the center.
  74. B_RIGHT, // border set to the 1st right octave.
  75. B_FLIP, // border set to the right end.
  76. // MY tone for _FLIPHALF and _FLIPLEFTOCT layers to distinguish the notes to avoid sustain effect, etc.
  77. // Since they are flipped, their subscripts are not MY_ but YM_, to make them easier to tell.
  78. YM_TONE_MIN,
  79. YM_C = YM_TONE_MIN,
  80. YM_Cs,
  81. YM_Db = YM_Cs,
  82. YM_D,
  83. YM_Ds,
  84. YM_Eb = YM_Ds,
  85. YM_E,
  86. YM_F,
  87. YM_Fs,
  88. YM_Gb = YM_Fs,
  89. YM_G,
  90. YM_Gs,
  91. YM_Ab = YM_Gs,
  92. YM_A,
  93. YM_As,
  94. YM_Bb = YM_As,
  95. YM_B,
  96. YM_C_1,
  97. YM_Cs_1,
  98. YM_Db_1 = YM_Cs_1,
  99. YM_D_1,
  100. YM_Ds_1,
  101. YM_Eb_1 = YM_Ds_1,
  102. YM_E_1,
  103. YM_F_1,
  104. YM_Fs_1,
  105. YM_Gb_1 = YM_Fs_1,
  106. YM_G_1,
  107. YM_Gs_1,
  108. YM_Ab_1 = YM_Gs_1,
  109. YM_A_1,
  110. YM_As_1,
  111. YM_Bb_1 = YM_As_1,
  112. YM_B_1,
  113. YM_C_2,
  114. YM_Cs_2,
  115. YM_Db_2 = YM_Cs_2,
  116. YM_D_2,
  117. YM_Ds_2,
  118. YM_Eb_2 = YM_Ds_2,
  119. YM_E_2,
  120. YM_F_2,
  121. YM_Fs_2,
  122. YM_Gb_2 = YM_Fs_2,
  123. YM_G_2,
  124. YM_Gs_2,
  125. YM_Ab_2 = YM_Gs_2,
  126. YM_A_2,
  127. YM_As_2,
  128. YM_Bb_2 = YM_As_2,
  129. YM_B_2,
  130. YM_C_3,
  131. YM_Cs_3,
  132. YM_Db_3 = YM_Cs_3,
  133. YM_D_3,
  134. YM_Ds_3,
  135. YM_Eb_3 = YM_Ds_3,
  136. YM_E_3,
  137. YM_F_3,
  138. YM_Fs_3,
  139. YM_Gb_3 = YM_Fs_3,
  140. YM_G_3,
  141. YM_Gs_3,
  142. YM_Ab_3 = YM_Gs_3,
  143. YM_A_3,
  144. YM_As_3,
  145. YM_Bb_3 = YM_As_3,
  146. YM_B_3,
  147. YM_C_4,
  148. YM_Cs_4,
  149. YM_Db_4 = YM_Cs_4,
  150. YM_D_4,
  151. YM_Ds_4,
  152. YM_Eb_4 = YM_Ds_4,
  153. YM_E_4,
  154. YM_F_4,
  155. YM_Fs_4,
  156. YM_Gb_4 = YM_Fs_4,
  157. YM_G_4,
  158. YM_Gs_4,
  159. YM_Ab_4 = YM_Gs_4,
  160. YM_A_4,
  161. YM_As_4,
  162. YM_Bb_4 = YM_As_4,
  163. YM_B_4,
  164. YM_C_5,
  165. YM_Cs_5,
  166. YM_Db_5 = YM_Cs_5,
  167. YM_D_5,
  168. YM_Ds_5,
  169. YM_Eb_5 = YM_Ds_5,
  170. YM_E_5,
  171. YM_F_5,
  172. YM_Fs_5,
  173. YM_Gb_5 = YM_Fs_5,
  174. YM_G_5,
  175. YM_Gs_5,
  176. YM_Ab_5 = YM_Gs_5,
  177. YM_A_5,
  178. YM_As_5,
  179. YM_Bb_5 = YM_As_5,
  180. YM_B_5,
  181. YM_C_6,
  182. YM_TONE_MAX = YM_C_6
  183. };
  184. #define MY_TONE_COUNT (YM_TONE_MAX - YM_TONE_MIN + 1)
  185. static uint8_t my_tone_status[MY_TONE_COUNT];
  186. // Long press: go to _FN layer, tap: MUTE
  187. #define FN_MUTE LT(_FN, KC_MUTE)
  188. // Used to set octave to 0
  189. extern midi_config_t midi_config;
  190. static bool is_trans_mode = false; // By default, shift mode is chosen.
  191. static uint8_t key_separator_col = _KEY01; // (_KEY01 .. _KEY37). By default, _KEY01 (= _BASE layer) is chosen. _KEY13 = *LEFT, _KEY19 = *HALF, _KEY25 = *RIGHT, _KEY37 = _FLIPBASE and _FLIPTRANS.
  192. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  193. /* Base */
  194. [_BASE] = LAYOUT(
  195. FN_MUTE, MI_SUST,
  196. MI_BNDU,
  197. SHIFT_L, SHIFT_R, MI_C2, MI_D2, MI_E2, MI_Fs2, MI_Ab2, MI_Bb2, MI_C3, MI_D3, MI_E3, MI_Fs3, MI_Ab3, MI_Bb3, MI_C4, MI_D4, MI_E4, MI_Fs4, MI_Ab4, MI_Bb4, MI_C5,
  198. MI_BNDD, MI_Db2, MI_Eb2, MI_F2, MI_G2, MI_A2, MI_B2, MI_Db3, MI_Eb3, MI_F3, MI_G3, MI_A3, MI_B3, MI_Db4, MI_Eb4, MI_F4, MI_G4, MI_A4, MI_B4
  199. ),
  200. /* 1 octave on the left side is ch2, others are ch1 (normal) */
  201. [_SEPALEFTOCT] = LAYOUT(
  202. _______, _______,
  203. _______,
  204. SHIFT_L, SHIFT_R, YM_C_2, YM_D_2, YM_E_2, YM_Fs_2, YM_Ab_2, YM_Bb_2, MI_C3, MI_D3, MI_E3, MI_Fs3, MI_Ab3, MI_Bb3, MI_C4, MI_D4, MI_E4, MI_Fs4, MI_Ab4, MI_Bb4, MI_C5,
  205. _______, YM_Db_2, YM_Eb_2, YM_F_2, YM_G_2, YM_A_2, YM_B_2, MI_Db3, MI_Eb3, MI_F3, MI_G3, MI_A3, MI_B3, MI_Db4, MI_Eb4, MI_F4, MI_G4, MI_A4, MI_B4
  206. ),
  207. /* Half ch2, half ch1 (normal) */
  208. [_SEPAHALF] = LAYOUT(
  209. _______, _______,
  210. _______,
  211. SHIFT_L, SHIFT_R, YM_C_2, YM_D_2, YM_E_2, YM_Fs_2, YM_Ab_2, YM_Bb_2, YM_C_3, YM_D_3, YM_E_3, MI_Fs3, MI_Ab3, MI_Bb3, MI_C4, MI_D4, MI_E4, MI_Fs4, MI_Ab4, MI_Bb4, MI_C5,
  212. _______, YM_Db_2, YM_Eb_2, YM_F_2, YM_G_2, YM_A_2, YM_B_2, YM_Db_3, YM_Eb_3, YM_F_3, MI_G3, MI_A3, MI_B3, MI_Db4, MI_Eb4, MI_F4, MI_G4, MI_A4, MI_B4
  213. ),
  214. /* 2 octave on the left side is ch2, others are ch1 (normal) */
  215. [_SEPARIGHTOCT] = LAYOUT(
  216. _______, _______,
  217. _______,
  218. SHIFT_L, SHIFT_R, YM_C_2, YM_D_2, YM_E_2, YM_Fs_2, YM_Ab_2, YM_Bb_2, YM_C_3, YM_D_3, YM_E_3, YM_Fs_3, YM_Ab_3, YM_Bb_3, MI_C4, MI_D4, MI_E4, MI_Fs4, MI_Ab4, MI_Bb4, MI_C5,
  219. _______, YM_Db_2, YM_Eb_2, YM_F_2, YM_G_2, YM_A_2, YM_B_2, YM_Db_3, YM_Eb_3, YM_F_3, YM_G_3, YM_A_3, YM_B_3, MI_Db4, MI_Eb4, MI_F4, MI_G4, MI_A4, MI_B4
  220. ),
  221. /* TRANS This layer must locate 1 layer below _FN layer. */
  222. [_TRANS] = LAYOUT(
  223. _______, _______,
  224. _______,
  225. MI_TRSD, MI_TRSU, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  226. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  227. ),
  228. /* Flip Base SFIFTUP and SHIFT_L are swapped. */
  229. [_FLIPBASE] = LAYOUT(
  230. FN_MUTE, MI_SUST,
  231. MI_BNDU,
  232. SHIFT_L, SHIFT_R, MI_C5, MI_Bb4, MI_Ab4, MI_Fs4, MI_E4, MI_D4, MI_C4, MI_Bb3, MI_Ab3, MI_Fs3, MI_E3, MI_D3, MI_C3, MI_Bb2, MI_Ab2, MI_Fs2, MI_E2, MI_D2, MI_C2,
  233. MI_BNDD, MI_B4, MI_A4, MI_G4, MI_F4, MI_Eb4, MI_Db4, MI_B3, MI_A3, MI_G3, MI_F3, MI_Eb3, MI_Db3, MI_B2, MI_A2, MI_G2, MI_F2, MI_Eb2, MI_Db2
  234. ),
  235. /* Flip Trans This layer must locate 1 layer above _FLIPBASE layer. MI_TRSU and MI_TRSD are swapped. */
  236. [_FLIPTRANS] = LAYOUT(
  237. _______, _______,
  238. _______,
  239. MI_TRSU, MI_TRSD, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  240. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  241. ),
  242. [_FN] = LAYOUT(
  243. _______, XXXXXXX,
  244. MI_VELU,
  245. MI_OCTD, MI_OCTU, B_BASE, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, B_LEFT, XXXXXXX, XXXXXXX, B_CENTER, XXXXXXX, XXXXXXX, B_RIGHT, XXXXXXX, XXXXXXX, XXXXXXX, VERSION, XXXXXXX, B_FLIP,
  246. MI_VELD, TGLINTR, TGLTRNS, TGLCHGR, XXXXXXX, XXXXXXX, RGB_SAD, RGB_SAI, RGB_HUD, RGB_HUI, RGB_SPD, RGB_SPI, RGB_VAD, RGB_VAI, RGB_RMOD, RGB_MOD, EE_CLR, TGLINDI, RGB_TOG
  247. )
  248. };
  249. #if defined(ENCODER_MAP_ENABLE)
  250. const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = {
  251. [_BASE] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) },
  252. [_SEPALEFTOCT] = { ENCODER_CCW_CW(_______, _______) },
  253. [_SEPAHALF] = { ENCODER_CCW_CW(_______, _______) },
  254. [_SEPARIGHTOCT] = { ENCODER_CCW_CW(_______, _______) },
  255. [_TRANS] = { ENCODER_CCW_CW(_______, _______) },
  256. [_FLIPBASE] = { ENCODER_CCW_CW(KC_VOLD, KC_VOLU) },
  257. [_FLIPTRANS] = { ENCODER_CCW_CW(_______, _______) },
  258. [_FN] = { ENCODER_CCW_CW(RGB_RMOD, RGB_MOD) },
  259. };
  260. #endif
  261. // commom codes called from eeconfig_init_user() and keyboard_post_init_user().
  262. void my_init(void){
  263. // Set octave to 0
  264. midi_config.octave = QK_MIDI_OCTAVE_0 - MIDI_OCTAVE_MIN;
  265. // avoid using 127 since it is used as a special number in some sound sources.
  266. midi_config.velocity = MIDI_INITIAL_VELOCITY;
  267. default_layer_set(_LS_BASE);
  268. layer_state_set(_LS_BASE);
  269. #ifdef RGB_MATRIX_ENABLE
  270. rgb_matrix_sethsv(HSV_BLUE);
  271. // party mode (for LED soldering test. Enable rainbow color effect, and disable led_indicator to check all LEDs)
  272. rgb_matrix_mode(RGB_MATRIX_RAINBOW_MOVING_CHEVRON);
  273. led_indicator_enable = false;
  274. #endif // RGB_MATRIX_ENABLE
  275. }
  276. void eeconfig_init_user(void) { // EEPROM is getting reset!
  277. midi_init();
  278. #ifdef RGB_MATRIX_ENABLE
  279. rgb_matrix_enable();
  280. rgb_matrix_set_speed(RGB_MATRIX_DEFAULT_SPD);
  281. #endif // RGB_MATRIX_ENABLE
  282. my_init(); // commom codes called from eeconfig_init_user() and keyboard_post_init_user().
  283. }
  284. void keyboard_post_init_user(void) {
  285. for (uint8_t i = 0; i < MY_TONE_COUNT; i++) {
  286. my_tone_status[i] = MIDI_INVALID_NOTE;
  287. }
  288. my_init(); // commom codes called from eeconfig_init_user() and keyboard_post_init_user().
  289. }
  290. void reset_scale_indicator(void) {
  291. // reset transpose value and scale_indicator_col to default.
  292. midi_config.transpose = 0;
  293. scale_indicator_col = DEFAULT_SCALE_COL;
  294. trans_mode_indicator_loc_sel = true;
  295. }
  296. void reset_all(void) {
  297. reset_scale_indicator();
  298. is_trans_mode = false; // trans mode is disabled by default.
  299. }
  300. void my_process_midi4single_note(uint8_t channel, uint16_t keycode, keyrecord_t *record, uint8_t *my_tone_status) {
  301. uint8_t mytone = keycode - YM_TONE_MIN;
  302. uint16_t mykeycode = mytone + MIDI_TONE_MIN;
  303. // uint16_t mykeycode = keycode - YM_TONE_MIN;
  304. // uint8_t mytone = mykeycode - MIDI_TONE_MIN;
  305. uint8_t velocity = midi_config.velocity;
  306. // uprintf("keycode=%u,mykeycode=%u,mytone =%u,velo = %u\n", keycode, mykeycode, mytone, velocity);
  307. if (record->event.pressed) {
  308. if (my_tone_status[mytone] == MIDI_INVALID_NOTE) {
  309. uint8_t note = midi_compute_note(mykeycode);
  310. midi_send_noteon(&midi_device, channel, note, velocity);
  311. dprintf("midi noteon channel:%d note:%d mytone:%d velocity:%d\n", channel, note, mytone, velocity);
  312. // uprintf("midi noteon channel:%d note:%d mytone:%d velocity:%d\n", channel, note, mytone, velocity);
  313. my_tone_status[mytone] = note; // store root_note status.
  314. }
  315. } else {
  316. uint8_t note = my_tone_status[mytone];
  317. if (note != MIDI_INVALID_NOTE) {
  318. midi_send_noteoff(&midi_device, channel, note, velocity);
  319. dprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  320. // uprintf("midi noteoff channel:%d note:%d velocity:%d\n", channel, note, velocity);
  321. }
  322. my_tone_status[mytone] = MIDI_INVALID_NOTE;
  323. }
  324. }
  325. void select_layer_state_set(void) {
  326. switch (key_separator_col) {
  327. case _KEY01:
  328. if (is_trans_mode) {
  329. layer_state_set(_LS_TRANS);
  330. } else {
  331. layer_state_set(_LS_BASE);
  332. }
  333. break;
  334. case _KEY13:
  335. if (is_trans_mode) {
  336. layer_state_set(_LS_SEPALEFTOCT_T);
  337. } else {
  338. layer_state_set(_LS_SEPALEFTOCT);
  339. }
  340. break;
  341. case _KEY19:
  342. if (is_trans_mode) {
  343. layer_state_set(_LS_SEPAHALF_T);
  344. } else {
  345. layer_state_set(_LS_SEPAHALF);
  346. }
  347. break;
  348. case _KEY25:
  349. if (is_trans_mode) {
  350. layer_state_set(_LS_SEPARIGHTOCT_T);
  351. } else {
  352. layer_state_set(_LS_SEPARIGHTOCT);
  353. }
  354. break;
  355. case _KEY37:
  356. if (is_trans_mode) {
  357. layer_state_set(_LS_FLIPTRANS);
  358. } else {
  359. layer_state_set(_LS_FLIPBASE);
  360. }
  361. break;
  362. }
  363. }
  364. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  365. // uprintf("keycode=%u, YM_C_3=%u, YM_Db_2 =%u, YM_MIN = %u, YM_MAX = %u\n", keycode, YM_C_3, YM_Db_2, YM_TONE_MIN, YM_TONE_MAX);
  366. switch (keycode) {
  367. case VERSION: // Output firmware info.
  368. if (record->event.pressed) {
  369. SEND_STRING(QMK_KEYBOARD ":" QMK_KEYMAP " @ " QMK_VERSION " | " QMK_BUILDDATE);
  370. }
  371. break;
  372. // Layer-related settings.
  373. // reset_scale_indicator() first, followed by each modification, and then change the default layer.
  374. // 1, separator column modification
  375. case B_BASE:
  376. if (record->event.pressed) {
  377. reset_all();
  378. key_separator_col = _KEY01;
  379. select_layer_state_set();
  380. }
  381. break;
  382. case B_LEFT:
  383. if (record->event.pressed) {
  384. reset_all();
  385. key_separator_col = _KEY13;
  386. select_layer_state_set();
  387. }
  388. break;
  389. case B_CENTER:
  390. if (record->event.pressed) {
  391. reset_all();
  392. key_separator_col = _KEY19;
  393. select_layer_state_set();
  394. }
  395. break;
  396. case B_RIGHT:
  397. if (record->event.pressed) {
  398. reset_all();
  399. key_separator_col = _KEY25;
  400. select_layer_state_set();
  401. }
  402. break;
  403. case B_FLIP:
  404. if (record->event.pressed) {
  405. reset_all();
  406. key_separator_col = _KEY37;
  407. select_layer_state_set();
  408. }
  409. break;
  410. // 2, Toggle scale shift mode and transpose mode
  411. case TGLTRNS:
  412. if (record->event.pressed) {
  413. reset_scale_indicator();
  414. is_trans_mode = !is_trans_mode;
  415. select_layer_state_set();
  416. }
  417. break;
  418. // SHIFT_L and SHIFT_R can be pressed only when layer is either _BASE, _FLIPBASE.
  419. case SHIFT_L:
  420. if (record->event.pressed) {
  421. switch (layer_state) {
  422. case _LS_BASE:
  423. case _LS_SEPALEFTOCT:
  424. case _LS_SEPAHALF:
  425. case _LS_SEPARIGHTOCT:
  426. case _LS_FLIPBASE:
  427. scale_indicator_col = shift_led_indicator_left(scale_indicator_col);
  428. break;
  429. }
  430. }
  431. break;
  432. case SHIFT_R:
  433. if (record->event.pressed) {
  434. switch (layer_state) {
  435. case _LS_BASE:
  436. case _LS_SEPALEFTOCT:
  437. case _LS_SEPAHALF:
  438. case _LS_SEPARIGHTOCT:
  439. case _LS_FLIPBASE:
  440. scale_indicator_col = shift_led_indicator_right(scale_indicator_col);
  441. break;
  442. }
  443. }
  444. break;
  445. case TGLINDI:
  446. if (record->event.pressed) {
  447. led_indicator_enable = !led_indicator_enable;
  448. }
  449. break;
  450. case TGLINTR:
  451. if (record->event.pressed) {
  452. switch (layer_state) {
  453. // main function of the TGLINTR part 1. alternate the status of trans_mode_indicator_loc_sel.
  454. case _LS_TRANS | (1UL << _FN):
  455. case _LS_SEPALEFTOCT_T | (1UL << _FN):
  456. case _LS_SEPAHALF_T | (1UL << _FN):
  457. case _LS_SEPARIGHTOCT_T | (1UL << _FN):
  458. trans_mode_indicator_loc_sel = !trans_mode_indicator_loc_sel;
  459. // when trans_mode_indicator_loc_sel == false, change the scale indicator and transpose.
  460. scale_indicator_col = trans_mode_indicator_loc_sel ? 0:1;
  461. // when TGLINTR is pressed, it also change the initial transpose setting to follow the scale indicator.
  462. if (scale_indicator_col == 1) {
  463. midi_config.transpose = -1;
  464. } else {
  465. midi_config.transpose = 0;
  466. }
  467. break;
  468. // main function of the TGLINTR part 2. alternate the status of trans_mode_indicator_loc_sel.
  469. case _LS_FLIPTRANS | (1UL << _FN):
  470. trans_mode_indicator_loc_sel = !trans_mode_indicator_loc_sel;
  471. // when trans_mode_indicator_loc_sel == false, change the scale indicator and transpose.
  472. scale_indicator_col = trans_mode_indicator_loc_sel ? 0:11;
  473. // when TGLINTR is pressed, it also change the initial transpose setting to follow the scale indicator.
  474. if (scale_indicator_col == 11) {
  475. midi_config.transpose = -1;
  476. } else {
  477. midi_config.transpose = 0;
  478. }
  479. break;
  480. // special treatment when TGLINTR is pressed in _LS_FLIPBASE layer.
  481. case _LS_FLIPBASE | (1UL << _FN): // when in FLIPBASE layer && non-Trans mode, change it to Trans mode.
  482. trans_mode_indicator_loc_sel = false;
  483. scale_indicator_col = 11;
  484. midi_config.transpose = -1;
  485. is_trans_mode = true;
  486. select_layer_state_set();
  487. break;
  488. // special treatment when TGLINTR is pressed in other non-Trans layer.
  489. default : // when other layers = non-Trans mode, change it to Trans mode.
  490. trans_mode_indicator_loc_sel = false;
  491. scale_indicator_col = 1;
  492. midi_config.transpose = -1;
  493. is_trans_mode = true;
  494. select_layer_state_set();
  495. }
  496. }
  497. break;
  498. case TGLCHGR:
  499. if (record->event.pressed) {
  500. use_alt_ch_gr = !use_alt_ch_gr;
  501. if (use_alt_ch_gr) {
  502. midi_config.channel = ALT_MAIN_CH_NUMBER;
  503. midi_left_ch = ALT_SUB_CH_NUMBER;
  504. } else { // default
  505. midi_config.channel = DEFAULT_MAIN_CH_NUMBER;
  506. midi_left_ch = DEFAULT_SUB_CH_NUMBER;
  507. }
  508. }
  509. break;
  510. case YM_TONE_MIN ... YM_TONE_MAX: // MY tone
  511. // uprintf("keycode=%u, YM_C_3=%u, YM_Db_2 =%u, YM_MIN = %u, YM_MAX = %u\n", keycode, YM_C_3, YM_Db_2, YM_TONE_MIN, YM_TONE_MAX);
  512. // DO NOT THROW BELOW into 'if (record->event.pressed) {}' STATEMENT SINCE IT IS USED IN THE FUNCTION BELOW.
  513. my_process_midi4single_note(midi_left_ch, keycode, record, my_tone_status);
  514. break;
  515. }
  516. return true;
  517. }
  518. #ifdef RGB_MATRIX_ENABLE
  519. void set_led_scale_indicator(uint8_t r, uint8_t g, uint8_t b) {
  520. uint8_t max_scale_indicator_led_loop;
  521. uint8_t i;
  522. if (led_indicator_enable) { // turn on indicators when enabled.
  523. max_scale_indicator_led_loop = ( scale_indicator_col == DEFAULT_SCALE_COL ) ? 12 : 9;
  524. for (i = 0; i < max_scale_indicator_led_loop; i++) {
  525. rgb_matrix_set_color(led_scale_indicator[scale_indicator_col][i], r, g, b);
  526. }
  527. }
  528. }
  529. bool rgb_matrix_indicators_user(void) {
  530. // uint32_t mode = rgblight_get_mode();
  531. if (rgb_matrix_is_enabled()) { // turn the lights on when it is enabled.
  532. // uint8_t max_scale_indicator_led_loop;
  533. uint8_t i;
  534. switch (layer_state) {
  535. case _LS_BASE:
  536. set_led_scale_indicator(BASE_LAYER_COLOR);
  537. break;
  538. case _LS_FLIPBASE:
  539. set_led_scale_indicator(FLIPB_LAYER_COLOR);
  540. for (i = 0; i < 3; i++) {
  541. rgb_matrix_set_color(led_single_col_indicator[_KEY37][i], FLIP_BORDER_COLOR); // right end
  542. }
  543. break;
  544. case _LS_TRANS:
  545. set_led_scale_indicator(TRANS_LAYER_COLOR);
  546. break;
  547. case _LS_SEPALEFTOCT_T:
  548. set_led_scale_indicator(SEPALEFT_T_LAYER_COLOR);
  549. for (i = 0; i < 3; i++) {
  550. rgb_matrix_set_color(led_single_col_indicator[_KEY13][i], FLIP_BORDER_COLOR); // Left
  551. }
  552. break;
  553. case _LS_SEPAHALF_T:
  554. set_led_scale_indicator(SEPAHALF_T_LAYER_COLOR);
  555. for (i = 0; i < 3; i++) {
  556. rgb_matrix_set_color(led_single_col_indicator[_KEY19][i], FLIP_BORDER_COLOR); // Center
  557. }
  558. break;
  559. case _LS_SEPARIGHTOCT_T:
  560. set_led_scale_indicator(SEPARIGHT_T_LAYER_COLOR);
  561. for (i = 0; i < 3; i++) {
  562. rgb_matrix_set_color(led_single_col_indicator[_KEY25][i], FLIP_BORDER_COLOR); // Right
  563. }
  564. break;
  565. case _LS_FLIPTRANS:
  566. set_led_scale_indicator(FLIPT_LAYER_COLOR);
  567. for (i = 0; i < 3; i++) {
  568. rgb_matrix_set_color(led_single_col_indicator[_KEY37][i], FLIP_BORDER_COLOR); // right end
  569. }
  570. break;
  571. case _LS_SEPALEFTOCT:
  572. set_led_scale_indicator(SEPALEFT_LAYER_COLOR);
  573. for (i = 0; i < 3; i++) {
  574. rgb_matrix_set_color(led_single_col_indicator[_KEY13][i], FLIP_BORDER_COLOR); // Left
  575. }
  576. break;
  577. case _LS_SEPAHALF:
  578. set_led_scale_indicator(SEPAHALF_LAYER_COLOR);
  579. for (i = 0; i < 3; i++) {
  580. rgb_matrix_set_color(led_single_col_indicator[_KEY19][i], FLIP_BORDER_COLOR); // Center
  581. }
  582. break;
  583. case _LS_SEPARIGHTOCT:
  584. set_led_scale_indicator(SEPARIGHT_LAYER_COLOR);
  585. for (i = 0; i < 3; i++) {
  586. rgb_matrix_set_color(led_single_col_indicator[_KEY25][i], FLIP_BORDER_COLOR); // Right
  587. }
  588. break;
  589. case _LS_FN ... _LS_MAX: // When Mute Button is long-pressed, the previous layers are still active.
  590. for (i = 1; i < 5; i++) {
  591. rgb_matrix_set_color(i, RGB_DARKSPRINGGREEN); // up(1) down(4) left(3) right(2) keys
  592. }
  593. rgb_matrix_set_color(led_single_col_indicator[_KEY02][0], RGB_DARKSPRINGGREEN); // TGLTRNS
  594. rgb_matrix_set_color(led_single_col_indicator[_KEY04][0], RGB_DARKSPRINGGREEN); // TGLINTR
  595. rgb_matrix_set_color(led_single_col_indicator[_KEY06][0], RGB_DARKSPRINGGREEN); // TGLCHGR
  596. for (i = 0; i < 3; i++) {
  597. rgb_matrix_set_color(led_single_col_indicator[_KEY01][i], BASE_LAYER_COLOR); // B_BASE
  598. rgb_matrix_set_color(led_single_col_indicator[_KEY13][i], SEPALEFT_LAYER_COLOR); // B_LEFT
  599. rgb_matrix_set_color(led_single_col_indicator[_KEY19][i], SEPAHALF_LAYER_COLOR); // B_CENTER
  600. rgb_matrix_set_color(led_single_col_indicator[_KEY25][i], SEPARIGHT_LAYER_COLOR); // B_RIGHT
  601. rgb_matrix_set_color(led_single_col_indicator[_KEY37][i], FLIPB_LAYER_COLOR); // B_FLIP
  602. }
  603. for (i = _KEY12; i < _KEY37; i+=2){ // even numbers from _KEY12 to _KEY36 are LED related settings.
  604. // turn on the bottom row only to keep the visibility of the RGB MATRIX effects.
  605. rgb_matrix_set_color(led_single_col_indicator[i][0], RGB_DARKSPRINGGREEN); // // LED related settings.
  606. }
  607. break;
  608. }
  609. }
  610. return false;
  611. }
  612. #endif // RGB_MATRIX_ENABLE