Add invert analog input option
This commit is contained in:
@@ -44,6 +44,7 @@ EXPORTS
|
||||
mapper_set_action_map
|
||||
mapper_set_analog_map
|
||||
mapper_set_analog_sensitivity
|
||||
mapper_set_analog_invert
|
||||
mapper_set_light_map
|
||||
mapper_set_nanalogs
|
||||
mapper_set_nlights
|
||||
|
||||
@@ -25,6 +25,7 @@ void mapper_set_action_map(
|
||||
uint8_t action, uint8_t page, uint8_t bit, const struct mapped_action *ma);
|
||||
bool mapper_set_analog_map(uint8_t analog, const struct mapped_analog *ma);
|
||||
bool mapper_set_analog_sensitivity(uint8_t analog, int32_t sensitivity);
|
||||
bool mapper_set_analog_invert(uint8_t analog, bool invert);
|
||||
void mapper_set_nanalogs(uint8_t nanalogs);
|
||||
void mapper_set_nlights(uint8_t nlights);
|
||||
void mapper_set_light_map(const struct mapped_light *ml, uint8_t game_light);
|
||||
|
||||
@@ -190,6 +190,11 @@ bool mapper_set_analog_sensitivity(uint8_t analog, int32_t sensitivity)
|
||||
return mapper_impl_set_analog_sensitivity(mapper_inst, analog, sensitivity);
|
||||
}
|
||||
|
||||
bool mapper_set_analog_invert(uint8_t analog, bool invert)
|
||||
{
|
||||
return mapper_impl_set_analog_invert(mapper_inst, analog, invert);
|
||||
}
|
||||
|
||||
void mapper_set_light_map(const struct mapped_light *ml, uint8_t game_light)
|
||||
{
|
||||
mapper_impl_set_light_map(mapper_inst, ml, game_light);
|
||||
|
||||
@@ -26,8 +26,10 @@ struct analog_mapping {
|
||||
bool bound;
|
||||
bool valid;
|
||||
bool absolute;
|
||||
double affine_scale;
|
||||
double affine_bias;
|
||||
bool invert;
|
||||
double analog_min;
|
||||
double analog_max;
|
||||
double inv_analog_range;
|
||||
uint8_t pos;
|
||||
};
|
||||
|
||||
@@ -169,9 +171,11 @@ static void analog_mapping_bind(struct analog_mapping *am)
|
||||
|
||||
ctl = &controls[am->src.control_no];
|
||||
|
||||
am->affine_bias = ctl->value_min;
|
||||
am->affine_scale = ctl->value_max - ctl->value_min;
|
||||
am->analog_min = ctl->value_min;
|
||||
am->analog_max = ctl->value_max;
|
||||
am->inv_analog_range = 1.0 / ((int64_t)ctl->value_max - ctl->value_min);
|
||||
am->absolute = !(ctl->flags & HID_FLAG_RELATIVE);
|
||||
am->invert = false;
|
||||
am->valid = true;
|
||||
|
||||
read_fail:
|
||||
@@ -187,6 +191,7 @@ static void analog_mapping_update(struct analog_mapping *am)
|
||||
{
|
||||
double tmp;
|
||||
int32_t value;
|
||||
int8_t delta;
|
||||
|
||||
if (!am->bound) {
|
||||
analog_mapping_bind(am);
|
||||
@@ -201,10 +206,24 @@ static void analog_mapping_update(struct analog_mapping *am)
|
||||
}
|
||||
|
||||
if (am->absolute) {
|
||||
tmp = (value - am->affine_bias) / am->affine_scale;
|
||||
|
||||
if (am->invert) {
|
||||
tmp = am->analog_max - value;
|
||||
} else {
|
||||
tmp = value - am->analog_min;
|
||||
}
|
||||
|
||||
// Scale the input value to [0,1] range
|
||||
tmp *= am->inv_analog_range;
|
||||
am->pos = (uint8_t) ((tmp + 0.5) * 256.0);
|
||||
} else {
|
||||
am->pos += (int8_t) (value * exp(am->sensitivity / 256.0));
|
||||
delta = (int8_t) (value * exp(am->sensitivity / 256.0));
|
||||
|
||||
if (am->invert) {
|
||||
delta *= -1;
|
||||
}
|
||||
|
||||
am->pos += delta;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -531,6 +550,18 @@ bool mapper_impl_set_analog_sensitivity(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mapper_impl_set_analog_invert(
|
||||
struct mapper *m, uint8_t analog, bool invert)
|
||||
{
|
||||
if (analog >= m->nanalogs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
m->analogs[analog].invert = invert;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void mapper_impl_set_light_map(
|
||||
struct mapper *m, const struct mapped_light *ml, uint8_t game_light)
|
||||
{
|
||||
|
||||
@@ -73,6 +73,8 @@ bool mapper_impl_set_analog_map(
|
||||
struct mapper *m, uint8_t analog, const struct mapped_analog *ma);
|
||||
bool mapper_impl_set_analog_sensitivity(
|
||||
struct mapper *m, uint8_t analog, int32_t sensitivity);
|
||||
bool mapper_impl_set_analog_invert(
|
||||
struct mapper *m, uint8_t analog, bool invert);
|
||||
void mapper_impl_set_light_map(
|
||||
struct mapper *m, const struct mapped_light *ml, uint8_t game_light);
|
||||
void mapper_impl_set_nanalogs(struct mapper *m, uint8_t nanalogs);
|
||||
|
||||
Reference in New Issue
Block a user