Skip to content

Commit

Permalink
Fix regression: INPUT_STATE for analog/MIDI/GPIO
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jfedor2 committed Jan 15, 2025
1 parent 10e864c commit 8c76e58
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
4 changes: 2 additions & 2 deletions firmware/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
31 changes: 20 additions & 11 deletions firmware/src/remapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion firmware/src/remapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8c76e58

Please sign in to comment.