From 8c76e589b088d0ed40c8cb3ec98251ee1dc3060b Mon Sep 17 00:00:00 2001 From: Jacek Fedorynski Date: Wed, 15 Jan 2025 20:43:27 +0100 Subject: [PATCH] Fix regression: INPUT_STATE for analog/MIDI/GPIO Auto-scaling absolute values to 0-255 range broke INPUT_STATE expression operation for analog, MIDI and GPIO inputs because INPUT_STATE reads raw state and we were only setting the scaled state. We now set both raw and scaled states for analog, MIDI and GPIO inputs. --- firmware/src/main.cc | 4 ++-- firmware/src/remapper.cc | 31 ++++++++++++++++++++----------- firmware/src/remapper.h | 2 +- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/firmware/src/main.cc b/firmware/src/main.cc index 361d82c..818f6b4 100644 --- a/firmware/src/main.cc +++ b/firmware/src/main.cc @@ -126,7 +126,7 @@ bool read_gpio(uint64_t now) { if (last_gpio_change[i] + gpio_debounce_time <= now) { uint32_t usage = GPIO_USAGE_PAGE | i; int32_t state = !(gpio_state & bit); // active low - set_input_state(usage, state); + set_input_state(usage, state, state); if (monitor_enabled) { monitor_usage(usage, state, 0); } @@ -173,7 +173,7 @@ bool read_adc() { prev_adc_state[i] = state; } uint32_t usage = ADC_USAGE_PAGE | i; - set_input_state(usage, state); + set_input_state(usage, state, state >> 4); if (monitor_enabled) { monitor_usage(usage, state, 0); } diff --git a/firmware/src/remapper.cc b/firmware/src/remapper.cc index 62f7c39..4863d4b 100644 --- a/firmware/src/remapper.cc +++ b/firmware/src/remapper.cc @@ -1669,46 +1669,55 @@ void handle_received_midi(uint8_t hub_port, uint8_t* midi_msg) { hub_port = HUB_PORT_NONE; } uint32_t usage = 0; - int32_t val = 0; + int32_t raw_val = 0; + int32_t scaled_val = 0; // ignore cable number and CIN switch (midi_msg[1] & 0xF0) { case 0x80: // note off usage = MIDI_USAGE_PAGE | ((midi_msg[1] | 0x10) << 8) | midi_msg[2]; - val = 0; // note off velocity not exposed + raw_val = 0; // note off velocity not exposed + scaled_val = 0; break; case 0x90: // note on case 0xA0: // polyphonic key pressure (aftertouch) case 0xB0: // control change usage = MIDI_USAGE_PAGE | (midi_msg[1] << 8) | midi_msg[2]; - val = midi_msg[3]; + raw_val = midi_msg[3]; + scaled_val = raw_val * 255 / 127; break; case 0xC0: // program change case 0xD0: // channel pressure (aftertouch) usage = MIDI_USAGE_PAGE | (midi_msg[1] << 8); - val = midi_msg[2]; + raw_val = midi_msg[2]; + scaled_val = raw_val * 255 / 127; break; case 0xE0: // pitch bend change usage = MIDI_USAGE_PAGE | (midi_msg[1] << 8); - val = (uint16_t) (midi_msg[3] << 7) | midi_msg[2]; + raw_val = (uint16_t) (midi_msg[3] << 7) | midi_msg[2]; + scaled_val = raw_val >> 6; break; default: break; } if (usage != 0) { - set_input_state(usage, val, 0); + set_input_state(usage, raw_val, scaled_val, 0); if (hub_port != HUB_PORT_NONE) { - set_input_state(usage, val, hub_port); + set_input_state(usage, raw_val, scaled_val, hub_port); } if (monitor_enabled) { - monitor_usage(usage, val, hub_port); + monitor_usage(usage, raw_val, hub_port); } } } -void set_input_state(uint32_t usage, int32_t state, uint8_t hub_port) { - int32_t* state_ptr = get_state_ptr(usage, hub_port); +void set_input_state(uint32_t usage, int32_t state_raw, int32_t state_scaled, uint8_t hub_port) { + int32_t* state_ptr = get_state_ptr(usage, hub_port, false, true); if (state_ptr != NULL) { - *state_ptr = state; + *state_ptr = state_raw; + } + state_ptr = get_state_ptr(usage, hub_port, false, false); + if (state_ptr != NULL) { + *state_ptr = state_scaled; } } diff --git a/firmware/src/remapper.h b/firmware/src/remapper.h index f1dcffb..f28baf8 100644 --- a/firmware/src/remapper.h +++ b/firmware/src/remapper.h @@ -20,7 +20,7 @@ void set_mapping_from_config(); void handle_received_report(const uint8_t* report, int len, uint16_t interface, uint8_t external_report_id = 0); void do_handle_received_report(const uint8_t* report, int len, uint16_t interface, uint8_t external_report_id = 0); void handle_received_midi(uint8_t hub_port, uint8_t* midi_msg); -void set_input_state(uint32_t usage, int32_t state, uint8_t hub_port = 0); +void set_input_state(uint32_t usage, int32_t state_raw, int32_t state_scaled, uint8_t hub_port = 0); void extra_init(); void read_report(bool* new_report, bool* tick);