1
0
mirror of https://github.com/whowechina/iidx_pico.git synced 2024-11-28 00:20:50 +01:00

TT led as key color setting indicator

This commit is contained in:
whowe 2023-04-30 12:38:36 +08:00
parent 8208df8a66
commit e37c6e05d8
5 changed files with 72 additions and 54 deletions

Binary file not shown.

View File

@ -87,12 +87,21 @@ static void core1_loop()
} }
} }
static void boot_usb_check(uint16_t buttons)
{
uint16_t expected = 0x1855; /* YES, NO, 1, 3, 5, 7 */
if ((buttons & expected) == expected) {
reset_usb_boot(0, 2); // usb boot to flash
}
}
static void core0_loop() static void core0_loop()
{ {
while (true) while (true)
{ {
tud_task(); tud_task();
uint16_t buttons = button_read(); uint16_t buttons = button_read();
boot_usb_check(buttons);
uint16_t angle = turntable_read() >> 4; uint16_t angle = turntable_read() >> 4;
if (setup_run(buttons, angle)) { if (setup_run(buttons, angle)) {
rgb_force_display(setup_led_button, setup_led_tt); rgb_force_display(setup_led_button, setup_led_tt);

View File

@ -24,8 +24,7 @@
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
static const uint8_t button_rgb_map[BUTTON_RGB_NUM] = BUTTON_RGB_MAP; static const uint8_t button_rgb_map[BUTTON_RGB_NUM] = BUTTON_RGB_MAP;
static const uint8_t level_val[] = {0, 32, 64, 96, 128, 160, 192, 224, 255}; uint32_t rgb_max_level = 255;
uint32_t rgb_level = 0;
static void trap() {} static void trap() {}
static tt_effect_t effects[10] = { {trap, trap, trap, trap, 0} }; static tt_effect_t effects[10] = { {trap, trap, trap, trap, 0} };
@ -42,9 +41,9 @@ static size_t current_effect = 0;
static inline uint32_t _rgb32(uint32_t c1, uint32_t c2, uint32_t c3, bool gamma_fix) static inline uint32_t _rgb32(uint32_t c1, uint32_t c2, uint32_t c3, bool gamma_fix)
{ {
c1 = c1 * level_val[rgb_level] / 255; c1 = c1 * rgb_max_level / 255;
c2 = c2 * level_val[rgb_level] / 255; c2 = c2 * rgb_max_level / 255;
c3 = c3 * level_val[rgb_level] / 255; c3 = c3 * rgb_max_level / 255;
if (gamma_fix) { if (gamma_fix) {
c1 = ((c1 + 1) * (c1 + 1) - 1) >> 8; c1 = ((c1 + 1) * (c1 + 1) - 1) >> 8;
@ -80,10 +79,10 @@ uint8_t rgb_button_num()
void rgb_set_level(uint8_t level) void rgb_set_level(uint8_t level)
{ {
if (rgb_level == level) { if (rgb_max_level == level) {
return; return;
} }
rgb_level = level; rgb_max_level = level;
effects[current_effect].set_level(level); effects[current_effect].set_level(level);
} }
@ -257,8 +256,6 @@ void rgb_init()
ws2812_program_init(pio0, 0, pio0_offset, BUTTON_RGB_PIN, 800000, false); ws2812_program_init(pio0, 0, pio0_offset, BUTTON_RGB_PIN, 800000, false);
/* We don't start the tt LED program yet */ /* We don't start the tt LED program yet */
rgb_set_level(8);
set_effect(1); set_effect(1);
} }

View File

@ -24,8 +24,8 @@ static uint64_t setup_tick_ms = 0;
#define TVAR(line) CONCAT(a, line) #define TVAR(line) CONCAT(a, line)
#define RUN_EVERY_N_MS(a, ms) { static uint64_t TVAR(__LINE__) = 0; \ #define RUN_EVERY_N_MS(a, ms) { static uint64_t TVAR(__LINE__) = 0; \
if (setup_tick_ms - TVAR(__LINE__) >= ms) { a; TVAR(__LINE__) = setup_tick_ms; } } if (setup_tick_ms - TVAR(__LINE__) >= ms) { a; TVAR(__LINE__) = setup_tick_ms; } }
static uint32_t blink_fast = 0xffffff; static uint32_t blink_fast = 0xffffffff;
static uint32_t blink_slow = 0xffffff; static uint32_t blink_slow = 0xffffffff;
uint32_t setup_led_tt[128]; uint32_t setup_led_tt[128];
uint32_t setup_led_button[BUTTON_RGB_NUM]; uint32_t setup_led_button[BUTTON_RGB_NUM];
@ -59,10 +59,10 @@ static struct {
#define KEY_5 0x0010 #define KEY_5 0x0010
#define KEY_6 0x0020 #define KEY_6 0x0020
#define KEY_7 0x0040 #define KEY_7 0x0040
#define E1 0x0080 #define E1 0x0080
#define E2 0x0100 #define E2 0x0100
#define E3 0x0200 #define E3 0x0200
#define E4 0x0400 #define E4 0x0400
#define AUX_NO 0x0800 #define AUX_NO 0x0800
#define AUX_YES 0x1000 #define AUX_YES 0x1000
@ -89,6 +89,12 @@ static struct {
#define YELLOW button_rgb32(99, 99, 0, false) #define YELLOW button_rgb32(99, 99, 0, false)
#define SILVER button_rgb32(60, 60, 60, false) #define SILVER button_rgb32(60, 60, 60, false)
#define TT_RED tt_rgb32(99, 0, 0, false)
#define TT_GREEN tt_rgb32(0, 99, 0, false)
#define TT_CYAN tt_rgb32(0, 40, 99, false)
#define TT_YELLOW tt_rgb32(99, 99, 0, false)
#define TT_SILVER tt_rgb32(60, 60, 60, false)
typedef void (*mode_func)(); typedef void (*mode_func)();
static void join_mode(setup_mode_t new_mode); static void join_mode(setup_mode_t new_mode);
@ -112,10 +118,6 @@ static void none_loop()
static bool escaped = false; static bool escaped = false;
static uint64_t escape_time = 0; static uint64_t escape_time = 0;
if (PRESSED_ALL(AUX_NO | AUX_YES | KEY_1 | KEY_7)) {
reset_usb_boot(0, 2); // usb boot to flash
}
if (PRESSED_ALL(AUX_YES | AUX_NO)) { if (PRESSED_ALL(AUX_YES | AUX_NO)) {
if (!escaped) { if (!escaped) {
escaped = true; escaped = true;
@ -273,7 +275,7 @@ static void tt_loop()
static struct { static struct {
uint8_t channel; /* 0:E1(Start), 1:E2(Effect), 2:E3(VEFX), 3:E4 */ uint8_t channel; /* 0:E1(Start), 1:E2(Effect), 2:E3(VEFX), 3:E4 */
uint8_t *value; volatile uint8_t *value;
int16_t start_angle; int16_t start_angle;
} analog_ctx; } analog_ctx;
@ -342,40 +344,33 @@ static uint32_t scale_color(uint32_t color, uint8_t value, uint8_t factor)
static void analog_loop() static void analog_loop()
{ {
setup_led_button[LED_E1] = RED; uint32_t colors[4] = { RED, GREEN, CYAN, YELLOW};
setup_led_button[LED_E2] = GREEN; uint32_t tt_colors[4] = { TT_RED, TT_GREEN, TT_CYAN, TT_YELLOW };
setup_led_button[LED_E3] = CYAN;
setup_led_button[LED_E4] = YELLOW;
uint32_t color; for (int i = 0; i < 4; i++) {
if (analog_ctx.channel == 1) { uint32_t color = colors[i];
color = GREEN; if (analog_ctx.channel == i) {
setup_led_button[LED_E2] &= blink_fast; color &= blink_fast;
} else if (analog_ctx.channel == 2) { }
color = CYAN; setup_led_button[LED_E1 + i] = color;
setup_led_button[LED_E3] &= blink_fast;
} else if (analog_ctx.channel == 3) {
color = YELLOW;
setup_led_button[LED_E4] &= blink_fast;
} else {
color = RED;
setup_led_button[LED_E1] &= blink_fast;
} }
int tt_split = (int)*analog_ctx.value * iidx_cfg->tt_led.num / 255; int tt_split = (int)*analog_ctx.value * iidx_cfg->tt_led.num / 255;
for (int i = 1; i < iidx_cfg->tt_led.num - 1; i++) { for (int i = 1; i < iidx_cfg->tt_led.num - 1; i++) {
setup_led_tt[i] = i < tt_split ? color : 0; setup_led_tt[i] = i < tt_split ? tt_colors[analog_ctx.channel] : 0;
} }
int button_split = *analog_ctx.value / 37; int button_split = *analog_ctx.value / 37;
int scale = *analog_ctx.value % 37; int scale = *analog_ctx.value % 37;
for (int i = 0; i < 7; i++) { for (int i = 0; i < 7; i++) {
uint32_t color = colors[analog_ctx.channel];
if (i == button_split) { if (i == button_split) {
setup_led_button[LED_KEY_1 + i] = scale_color(color, scale, 37); color = scale_color(color, scale, 37);
} else { } else if (i > button_split) {
setup_led_button[LED_KEY_1 + i] = i < button_split ? color : 0; color = 0;
} }
setup_led_button[LED_KEY_1 + i] = color;
} }
} }
@ -427,33 +422,50 @@ static void key_rotate()
{ {
int16_t new_value = *key_ctx.value; int16_t new_value = *key_ctx.value;
new_value += input.rotate; new_value += input.rotate;
if (new_value < 0) { if (key_ctx.phase > 0) {
new_value = 0; if (new_value < 0) {
} else if (new_value > 255) { new_value = 0;
new_value = 255; } else if (new_value > 255) {
new_value = 255;
}
} }
*key_ctx.value = new_value; *key_ctx.value = (uint8_t)new_value;
} }
static void key_loop() static void key_loop()
{ {
for (int i = 0; i < 11; i ++) { for (int i = 0; i < 11; i ++) {
if (key_ctx.keys & (1 << i)) { if (key_ctx.keys == 0) {
setup_led_button[i] = rgb32_from_hsv(key_ctx.hsv) & blink_slow; setup_led_button[i] = rgb32_from_hsv(key_ctx.hsv) & blink_slow;
} else if (key_ctx.keys & (1 << i)) {
setup_led_button[i] = rgb32_from_hsv(key_ctx.hsv);
} else { } else {
setup_led_button[i] = rgb32_from_hsv(key_ctx.leds[i]); setup_led_button[i] = 0;
} }
} }
for (unsigned i = 0; i < iidx_cfg->tt_led.num; i++) {
hsv_t hsv = key_ctx.hsv;
unsigned pos = iidx_cfg->tt_led.mode ? i : iidx_cfg->tt_led.num - i - 1;
if (key_ctx.phase == 0) {
hsv.h += pos * 255 / iidx_cfg->tt_led.num;
} else if (key_ctx.phase == 1) {
hsv.s += pos * 255 / iidx_cfg->tt_led.num;
} else {
hsv.v += pos * 255 / iidx_cfg->tt_led.num;
}
setup_led_tt[i] = rgb32_from_hsv(hsv);
}
} }
static void key_enter() static void key_enter()
{ {
key_ctx = (typeof(key_ctx)) { key_ctx = (typeof(key_ctx)) {
.phase = 0, .phase = 0,
.hsv = { .h = 200, .s = 255, .v = 80 }, .hsv = { .h = 200, .s = 255, .v = 128 },
.value = &key_ctx.hsv.h, .value = &key_ctx.hsv.h,
.start_angle = input.angle, .start_angle = input.angle,
.keys = 0x7f, .keys = 0,
.leds = iidx_cfg->key_on, .leds = iidx_cfg->key_on,
}; };
@ -527,7 +539,7 @@ bool setup_run(uint16_t keys, uint16_t angle)
mode_defs[current_mode].key_change(); mode_defs[current_mode].key_change();
} }
RUN_EVERY_N_MS(blink_fast = ~blink_fast, 50); RUN_EVERY_N_MS(blink_fast = ~blink_fast, 100);
RUN_EVERY_N_MS(blink_slow = ~blink_slow, 500); RUN_EVERY_N_MS(blink_slow = ~blink_slow, 500);
mode_defs[current_mode].loop(); mode_defs[current_mode].loop();

View File

@ -26,10 +26,10 @@ enum {
HID_USAGE(HID_USAGE_DESKTOP_JOYSTICK), \ HID_USAGE(HID_USAGE_DESKTOP_JOYSTICK), \
HID_COLLECTION(HID_COLLECTION_APPLICATION), \ HID_COLLECTION(HID_COLLECTION_APPLICATION), \
__VA_ARGS__ HID_USAGE_PAGE(HID_USAGE_PAGE_BUTTON), HID_USAGE_MIN(1), \ __VA_ARGS__ HID_USAGE_PAGE(HID_USAGE_PAGE_BUTTON), HID_USAGE_MIN(1), \
HID_USAGE_MAX(11), \ HID_USAGE_MAX(13), \
HID_LOGICAL_MIN(0), HID_LOGICAL_MAX(1), HID_REPORT_COUNT(11), \ HID_LOGICAL_MIN(0), HID_LOGICAL_MAX(1), HID_REPORT_COUNT(13), \
HID_REPORT_SIZE(1), HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \ HID_REPORT_SIZE(1), HID_INPUT(HID_DATA | HID_VARIABLE | HID_ABSOLUTE), \
HID_REPORT_COUNT(1), HID_REPORT_SIZE(16 - 11), /*Padding*/ \ HID_REPORT_COUNT(1), HID_REPORT_SIZE(16 - 13), /*Padding*/ \
HID_INPUT(HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE), \ HID_INPUT(HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE), \
HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), HID_LOGICAL_MIN(0x00), \ HID_USAGE_PAGE(HID_USAGE_PAGE_DESKTOP), HID_LOGICAL_MIN(0x00), \
HID_LOGICAL_MAX_N(0x00ff, 2), /* Below is Joystick/analog */ \ HID_LOGICAL_MAX_N(0x00ff, 2), /* Below is Joystick/analog */ \