From 5bafdd1511d37373022f809cfc2a0589a1d8de38 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 28 Jun 2023 17:30:26 +0200 Subject: Add scale transpose and prettier colors --- src/app.c | 208 ++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 102 insertions(+), 106 deletions(-) (limited to 'src/app.c') diff --git a/src/app.c b/src/app.c index 06f43a9..0c3806b 100644 --- a/src/app.c +++ b/src/app.c @@ -220,12 +220,22 @@ typedef struct Color { int b; } Color; -Color colors[] = { - { MAXLED, MAXLED, MAXLED }, // White - { MAXLED, 0, 0 }, // Red - { 0, MAXLED, 0 }, // Green - { 0, 0, MAXLED }, // Blue -}; +#define DRAW(PAD, CLR) hal_plot_led(TYPEPAD, (PAD), (CLR).r, (CLR).g, (CLR).b) +#define BLACK (Color){ 0 , 0 , 0 } +#define WHITE (Color){ MAXLED , MAXLED , MAXLED } +#define GREY (Color){ MAXLED / 2 , MAXLED / 2 , MAXLED / 2 } +#define RED (Color){ 62 , 8 , 15 } +#define RED_DIM (Color){ MAXLED / 5 , 0 , 0 } +#define YELLOW (Color){ 61 , 58 , 25 } +#define YELLOW_DIM (Color){ 5 , 5 , 0 } +#define PURPLE (Color){ 20 , 0 , 40 } +#define PURPLE_DIM (Color){ 2 , 0 , 5 } +#define GREEN (Color){ 17 , 53 , 36 } +#define BLUE (Color){ 0 , 34 , 52 } +#define CYAN (Color){ 8 , 62 , 56 } +#define ORANGE (Color){ MAXLED , 40 , 23 } +#define PINK (Color){ 61 , 34 , 39 } +#define TEAL (Color){ 0 , MAXLED / 2 , MAXLED / 2 } typedef struct Voice { u8 active; @@ -248,6 +258,7 @@ typedef struct State { Mode mode; // Keyboard. u8 kbd_octave; + u8 kbd_root; } State; // Globals. @@ -326,48 +337,48 @@ kbd_find_note(int index) { if (index >= POLY_PAD_ROW_0 && index <= (POLY_PAD_ROW_0 + 7)) { row = POLY_PAD_ROW_0; black = 1; - note = 12 * (state.kbd_octave + 5); + note = 12 * (state.kbd_octave + 5) + state.kbd_root; } if (index >= POLY_PAD_ROW_1 && index <= (POLY_PAD_ROW_1 + 7)) { row = POLY_PAD_ROW_1; - note = 12 * (state.kbd_octave + 5); + note = 12 * (state.kbd_octave + 5) + state.kbd_root; } // B if (index >= POLY_PAD_ROW_2 && index <= (POLY_PAD_ROW_2 + 7)) { row = POLY_PAD_ROW_2; black = 1; - note = 12 * (state.kbd_octave + 4); + note = 12 * (state.kbd_octave + 4) + state.kbd_root; } if (index >= POLY_PAD_ROW_3 && index <= (POLY_PAD_ROW_3 + 7)) { row = POLY_PAD_ROW_3; - note = 12 * (state.kbd_octave + 4); + note = 12 * (state.kbd_octave + 4) + state.kbd_root; } // C if (index >= POLY_PAD_ROW_4 && index <= (POLY_PAD_ROW_4 + 7)) { row = POLY_PAD_ROW_4; black = 1; - note = 12 * (state.kbd_octave + 3); + note = 12 * (state.kbd_octave + 3) + state.kbd_root; } if (index >= POLY_PAD_ROW_5 && index <= (POLY_PAD_ROW_5 + 7)) { row = POLY_PAD_ROW_5; - note = 12 * (state.kbd_octave + 3); + note = 12 * (state.kbd_octave + 3) + state.kbd_root; } // D if (index >= POLY_PAD_ROW_6 && index <= (POLY_PAD_ROW_6 + 7)) { row = POLY_PAD_ROW_6; black = 1; - note = 12 * (state.kbd_octave + 2); + note = 12 * (state.kbd_octave + 2) + state.kbd_root; } if (index >= POLY_PAD_ROW_7 && index <= (POLY_PAD_ROW_7 + 7)) { row = POLY_PAD_ROW_7; - note = 12 * (state.kbd_octave + 2); + note = 12 * (state.kbd_octave + 2) + state.kbd_root; } // Invalid pad index. - if (note < 0) { + if (note < 0 || note > 127) { return -1; } @@ -478,6 +489,9 @@ app_surface_event(u8 type, u8 index, u8 value) { } break; case MOD_POLY_KBD: { if (index >= POLY_TOP_ROW && index <= (POLY_TOP_ROW + 7)) { + if (!value) { + return; + } switch (index) { // Transposition. case POLY_PAD_UP: { @@ -486,6 +500,12 @@ app_surface_event(u8 type, u8 index, u8 value) { case POLY_PAD_DOWN: { if (state.kbd_octave > 0) { state.kbd_octave--; } } break; + case POLY_PAD_RIGHT: { + if (state.kbd_root < 12) { state.kbd_root++; } + } break; + case POLY_PAD_LEFT: { + if (state.kbd_root > 0) { state.kbd_root--; } + } break; // Different sections. case POLY_PAD_SESSION: { state.mode = MOD_POLY_MAIN; @@ -496,10 +516,8 @@ app_surface_event(u8 type, u8 index, u8 value) { return; } u8 channel = 0; - // TODO: Velocity on/off configuration. - // TODO: Velocity curve adjustment. + // TODO: Velocity on/off/curve configuration. // TODO: Aftertouch? - // TODO: Different colors for different octaves. // TODO: Per KBD channel selection. int note = kbd_find_note(index); if (note == -1) { @@ -674,21 +692,23 @@ app_cable_event(u8 type, u8 value) { } } if (midi_in_connected && midi_out_connected) { - hal_plot_led(TYPESETUP, 0, 0, value, 0); + Color clr = CYAN; + hal_plot_led(TYPESETUP, 0, clr.r, clr.g, clr.b); } else { - hal_plot_led(TYPESETUP, 0, value, 0, 0); + Color clr = RED; + hal_plot_led(TYPESETUP, 0, clr.r, clr.g, clr.b); } } void -print_number(u8 n, u16 r, u16 g, u16 b, u8 x, u8 y) { +print_number(u8 n, Color clr, u8 x, u8 y) { u16 number = font_numbers[n]; for (u8 row = 0; row < 5; row++) { u8 pos = 11 + 10 * (7 - y) - row * 10 + x; u16 cur = number >> (13 - 3 * row); for (u8 i = 0; i < 3; i++) { if ((cur >> (2 - i)) & 0x1) { - hal_plot_led(TYPEPAD, pos + i, r, g, b); + DRAW(pos + i, clr); } } } @@ -696,15 +716,6 @@ print_number(u8 n, u16 r, u16 g, u16 b, u8 x, u8 y) { void update(void) { - // for (int i = 0; i < PAD_COUNT; ++i) { - // // Get the first detected button n_voices. - // u16 x = (99 * g_ADC[i]) >> 12; - // if (x > 3) { - // n_voices = x; - // return; - // } - // } - // n_voices = 0; if (save_state) { hal_write_flash(0, (u8*)&state, sizeof(State)); save_state = 0; @@ -714,79 +725,59 @@ update(void) { void clear_pads(void) { for (int i = 0; i < PAD_COUNT; ++i) { - hal_plot_led(TYPEPAD, ADC_MAP[i], 0, 0, 0); + DRAW(ADC_MAP[i], BLACK); } - hal_plot_led(TYPEPAD, POLY_PAD_SESSION, 0, 0, 0); - hal_plot_led(TYPEPAD, POLY_PAD_NOTE, 0, 0, 0); + DRAW(POLY_PAD_SESSION, BLACK); + DRAW(POLY_PAD_NOTE, BLACK); } void draw_poly_active_button(void) { if (state.active) { - hal_plot_led(TYPEPAD, POLY_PAD_ACTIVE, 0, MAXLED, 0); + DRAW(POLY_PAD_ACTIVE, GREEN); } else { - hal_plot_led(TYPEPAD, POLY_PAD_ACTIVE, MAXLED, 0, 0); + DRAW(POLY_PAD_ACTIVE, RED); } } void draw_poly_steal_button(void) { switch (state.stealing) { - case POLY_STEAL_OFF: { - hal_plot_led(TYPEPAD, POLY_PAD_STEAL, MAXLED, 0, 0); - } break; - case POLY_STEAL_OLD: { - hal_plot_led(TYPEPAD, POLY_PAD_STEAL, 0, 24, 24); - } break; - case POLY_STEAL_HIGH: { - hal_plot_led(TYPEPAD, POLY_PAD_STEAL, 0, 24, 0); - } break; - case POLY_STEAL_LOW: { - hal_plot_led(TYPEPAD, POLY_PAD_STEAL, 0, 0, 24); - } break; + case POLY_STEAL_OFF: { DRAW(POLY_PAD_STEAL, RED); } break; + case POLY_STEAL_OLD: { DRAW(POLY_PAD_STEAL, CYAN); } break; + case POLY_STEAL_HIGH: { DRAW(POLY_PAD_STEAL, ORANGE); } break; + case POLY_STEAL_LOW: { DRAW(POLY_PAD_STEAL, PURPLE); } break; } } void draw_poly_main(void) { - hal_plot_led(TYPEPAD, POLY_PAD_SESSION, MAXLED / 2, MAXLED / 2, MAXLED / 2); - print_number((n_voices / 10) % 10, MAXLED, MAXLED, MAXLED, 0, 0); - print_number( n_voices % 10, MAXLED, MAXLED, MAXLED, 4, 0); + DRAW(POLY_PAD_SESSION, GREY); + print_number((n_voices / 10) % 10, WHITE, 0, 0); + print_number(n_voices % 10, WHITE, 4, 0); draw_poly_active_button(); draw_poly_steal_button(); } void draw_min_channel(void) { - // for (u8 i = 0; i < 8; i++) { - // if (state.ch_max < 8 && i > state.ch_max) { - // clr = colors[1]; - // } else if (i != state.ch_min){ - // clr = colors[2]; - // } - // hal_plot_led(TYPEPAD, POLY_PAD_MIN_CH_0 + i, clr.r, clr.g, clr.b); - // } for (u8 i = 0; i < 8; i++) { if (i == state.ch_min) { - hal_plot_led(TYPEPAD, POLY_PAD_MIN_CH_0 + i, MAXLED, MAXLED, MAXLED); - continue; - } - if (state.ch_max < 8 && i > state.ch_max) { - hal_plot_led(TYPEPAD, POLY_PAD_MIN_CH_0 + i, 5, 0, 0); - continue; + DRAW(POLY_PAD_MIN_CH_0 + i, GREY); + } else if (state.ch_max >= 8 || i <= state.ch_max) { + DRAW(POLY_PAD_MIN_CH_0 + i, RED); + } else { + DRAW(POLY_PAD_MIN_CH_0 + i, RED_DIM); } - hal_plot_led(TYPEPAD, POLY_PAD_MIN_CH_0 + i, MAXLED / 2, 0, 0); } for (u8 i = 0; i < 8; i++) { if ((i + 8) == state.ch_min) { - hal_plot_led(TYPEPAD, POLY_PAD_MIN_CH_8 + i, MAXLED, MAXLED, MAXLED); - continue; - } - if (state.ch_max < 16 && (i + 8) > state.ch_max) { - hal_plot_led(TYPEPAD, POLY_PAD_MIN_CH_8 + i, 5, 0, 0); - continue; + DRAW(POLY_PAD_MIN_CH_8 + i, GREY); + } else if (state.ch_max >= 16 || (i + 8) <= state.ch_max) { + DRAW(POLY_PAD_MIN_CH_8 + i, RED); + } else { + DRAW(POLY_PAD_MIN_CH_8 + i, RED_DIM); } - hal_plot_led(TYPEPAD, POLY_PAD_MIN_CH_8 + i, MAXLED / 2, 0, 0); } } @@ -794,25 +785,21 @@ void draw_max_channel(void) { for (u8 i = 0; i < 8; i++) { if (i == state.ch_max) { - hal_plot_led(TYPEPAD, POLY_PAD_MAX_CH_0 + i, MAXLED, MAXLED, MAXLED); - continue; - } - if (i < state.ch_min) { - hal_plot_led(TYPEPAD, POLY_PAD_MAX_CH_0 + i, 5, 5, 0); - continue; + DRAW(POLY_PAD_MAX_CH_0 + i, GREY); + } else if (i < state.ch_min) { + DRAW(POLY_PAD_MAX_CH_0 + i, YELLOW_DIM); + } else { + DRAW(POLY_PAD_MAX_CH_0 + i, YELLOW); } - hal_plot_led(TYPEPAD, POLY_PAD_MAX_CH_0 + i, MAXLED / 2, MAXLED / 2, 0); } for (u8 i = 0; i < 8; i++) { if ((i + 8) == state.ch_max) { - hal_plot_led(TYPEPAD, POLY_PAD_MAX_CH_8 + i, MAXLED, MAXLED, MAXLED); - continue; - } - if (state.ch_min >= 8 && (i + 8) < state.ch_min) { - hal_plot_led(TYPEPAD, POLY_PAD_MAX_CH_8 + i, 5, 5, 0); - continue; + DRAW(POLY_PAD_MAX_CH_8 + i, GREY); + } else if (state.ch_min >= 8 && (i + 8) < state.ch_min) { + DRAW(POLY_PAD_MAX_CH_8 + i, YELLOW_DIM); + } else { + DRAW(POLY_PAD_MAX_CH_8 + i, YELLOW); } - hal_plot_led(TYPEPAD, POLY_PAD_MAX_CH_8 + i, MAXLED / 2, MAXLED / 2, 0); } } @@ -820,16 +807,16 @@ void draw_sel_channel(void) { for (u8 i = 0; i < 8; i++) { if (state.ch_listen[i]) { - hal_plot_led(TYPEPAD, POLY_PAD_SEL_CH_0 + i, 20, 0, 40); + DRAW(POLY_PAD_SEL_CH_0 + i, PURPLE); } else { - hal_plot_led(TYPEPAD, POLY_PAD_SEL_CH_0 + i, 2, 0, 5); + DRAW(POLY_PAD_SEL_CH_0 + i, PURPLE_DIM); } } for (u8 i = 0; i < 8; i++) { if (state.ch_listen[i + 8]) { - hal_plot_led(TYPEPAD, POLY_PAD_SEL_CH_8 + i, 20, 0, 40); + DRAW(POLY_PAD_SEL_CH_8 + i, PURPLE); } else { - hal_plot_led(TYPEPAD, POLY_PAD_SEL_CH_8 + i, 2, 0, 5); + DRAW(POLY_PAD_SEL_CH_8 + i, PURPLE_DIM); } } } @@ -848,27 +835,35 @@ draw_kbd(int loc, Color clr) { if (loc < 0 || loc >= 7) { return; } - Color clr_top = clr; - Color clr_bot = (Color){clr_top.r / 4, clr_top.g / 4, clr_top.b / 4 }; int idx_top = row_idx[loc]; int idx_bot = row_idx[loc + 1]; - hal_plot_led(TYPEPAD, idx_top + 1, clr_bot.r, clr_bot.g, clr_bot.b); - hal_plot_led(TYPEPAD, idx_top + 2, clr_bot.r, clr_bot.g, clr_bot.b); - hal_plot_led(TYPEPAD, idx_top + 4, clr_bot.r, clr_bot.g, clr_bot.b); - hal_plot_led(TYPEPAD, idx_top + 5, clr_bot.r, clr_bot.g, clr_bot.b); - hal_plot_led(TYPEPAD, idx_top + 6, clr_bot.r, clr_bot.g, clr_bot.b); - for (u8 i = 0; i < 8; i++) { - hal_plot_led(TYPEPAD, idx_bot + i, clr_top.r, clr_top.g, clr_top.b); - } + Color colors[] = { + PINK, BLUE, GREEN, YELLOW, ORANGE, RED, CYAN, + }; + Color root_clr = colors[state.kbd_octave + 3 - loc / 2]; + DRAW(idx_top + 1, state.kbd_root == 1 ? WHITE : root_clr); + DRAW(idx_top + 2, state.kbd_root == 3 ? WHITE : root_clr); + DRAW(idx_top + 4, state.kbd_root == 6 ? WHITE : root_clr); + DRAW(idx_top + 5, state.kbd_root == 8 ? WHITE : root_clr); + DRAW(idx_top + 6, state.kbd_root == 10 ? WHITE : root_clr); + + DRAW(idx_bot + 0, state.kbd_root == 0 ? WHITE : root_clr); + DRAW(idx_bot + 1, state.kbd_root == 2 ? WHITE : root_clr); + DRAW(idx_bot + 2, state.kbd_root == 4 ? WHITE : root_clr); + DRAW(idx_bot + 3, state.kbd_root == 5 ? WHITE : root_clr); + DRAW(idx_bot + 4, state.kbd_root == 7 ? WHITE : root_clr); + DRAW(idx_bot + 5, state.kbd_root == 9 ? WHITE : root_clr); + DRAW(idx_bot + 6, state.kbd_root == 11 ? WHITE : root_clr); + DRAW(idx_bot + 7, state.kbd_root == 12 ? WHITE : root_clr); } void draw_poly_kbd(void) { - hal_plot_led(TYPEPAD, POLY_PAD_NOTE, MAXLED / 2, MAXLED / 2, MAXLED / 2); - draw_kbd(0, colors[0]); - draw_kbd(2, colors[1]); - draw_kbd(4, colors[2]); - draw_kbd(6, colors[3]); + DRAW(POLY_PAD_NOTE, GREY); + draw_kbd(0, PURPLE); + draw_kbd(2, PURPLE); + draw_kbd(4, PURPLE); + draw_kbd(6, PURPLE); } void @@ -908,6 +903,7 @@ app_init(const u16 *adc_raw) { .ch_listen = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, .mode = MOD_POLY_MAIN, .kbd_octave = 0, + .kbd_root = 0, }; } -- cgit v1.2.1