Revision: 45575
http://sourceforge.net/p/vice-emu/code/45575
Author: compyx
Date: 2025-03-24 06:45:21 +0000 (Mon, 24 Mar 2025)
Log Message:
-----------
Joystick: add simple calibration data struct
Add `joystick_calibration_t` members to axes, buttons and hats. Right now just
containing an `invert` flag and thresholds for axes. When we decide to support
using axes for mice, paddles etc we can add deadzones.
Modified Paths:
--------------
branches/compyx/joymap-001/vice/src/joyport/joystick.c
branches/compyx/joymap-001/vice/src/joyport/joystick.h
Modified: branches/compyx/joymap-001/vice/src/joyport/joystick.c
===================================================================
--- branches/compyx/joymap-001/vice/src/joyport/joystick.c 2025-03-23 17:33:32 UTC (rev 45574)
+++ branches/compyx/joymap-001/vice/src/joyport/joystick.c 2025-03-24 06:45:21 UTC (rev 45575)
@@ -3658,6 +3658,45 @@
}
+/** \brief Initialize joystick host input calibration
+ *
+ * \param[in] calibration calibration for an input
+ */
+void joystick_calibration_init(joystick_calibration_t *calibration)
+{
+ calibration->invert = false;
+ calibration->threshold.negative = 0;
+ calibration->threshold.positive = 0;
+}
+
+
+/** \brief Apply default calibration parameters on an axis
+ *
+ * Set thresholds for \a axis just like the old drivers interpreted axis values.
+ * For an entire range (minimum to maximum) that would be 0-25%: negative,
+ * 25-75%: neutral, 75-100%: positive.
+ *
+ * \param[in] axis joystick axis
+ */
+static void joystick_calibration_default_for_axis(joystick_axis_t *axis)
+{
+ if (!axis->digital) {
+ int32_t range = axis->maximum - axis->minimum;
+
+ /* add one to get proper range, but only if the result fits */
+ if (range < INT32_MAX) {
+ range++;
+ }
+ /* default: 0-25% negative, 25-75% neutral, 75-100% positive */
+ axis->calibration.threshold.negative = axis->minimum + (range / 4);
+ axis->calibration.threshold.positive = axis->minimum + ((range / 4) * 3);
+ } else {
+ axis->calibration.threshold.negative = -1;
+ axis->calibration.threshold.positive = +1;
+ }
+}
+
+
/** \brief Initialize joystick axis
*
* Intialize \a axis to default values, including its \c positive and \c negative
@@ -3679,6 +3718,7 @@
axis->digital = false;
joystick_mapping_init(&axis->mapping.negative);
joystick_mapping_init(&axis->mapping.positive);
+ joystick_calibration_init(&axis->calibration);
}
/** \brief Initialize joystick button
@@ -3695,6 +3735,7 @@
button->prev = 0;
button->index = -1;
joystick_mapping_init(&button->mapping);
+ joystick_calibration_init(&button->calibration);
}
/** \brief Initialize joystick hat
@@ -3714,6 +3755,7 @@
joystick_mapping_init(&hat->mapping.down);
joystick_mapping_init(&hat->mapping.left);
joystick_mapping_init(&hat->mapping.right);
+ joystick_calibration_init(&hat->calibration);
}
@@ -3840,6 +3882,14 @@
joydev->axes = lib_realloc(joydev->axes,
sizeof *joydev->axes * (size_t)joydev->max_axes);
}
+
+ /* set default calibration */
+ joystick_calibration_default_for_axis(axis);
+ printf("AXIS %d: min: %d, N-threshold: %d, P-threshold: %d, max: %d\n",
+ joydev->num_axes,
+ axis->minimum, axis->calibration.threshold.negative,
+ axis->calibration.threshold.positive, axis->maximum);
+
joydev->axes[joydev->num_axes++] = axis;
}
Modified: branches/compyx/joymap-001/vice/src/joyport/joystick.h
===================================================================
--- branches/compyx/joymap-001/vice/src/joyport/joystick.h 2025-03-23 17:33:32 UTC (rev 45574)
+++ branches/compyx/joymap-001/vice/src/joyport/joystick.h 2025-03-24 06:45:21 UTC (rev 45575)
@@ -175,6 +175,15 @@
} value;
} joystick_mapping_t;
+/** \brief Calibration for a host input
+ */
+typedef struct joystick_calibration_s {
+ bool invert; /**< invert value */
+ struct {
+ int32_t negative; /**< axis threshold: V <= T: active input */
+ int32_t positive; /**< axis threshold: V >= T: active input */
+ } threshold;
+} joystick_calibration_t;
/** \brief Joystick button object
*
@@ -181,12 +190,12 @@
* Information on a host device button input.
*/
typedef struct joystick_button_s {
- uint32_t code; /**< event code */
- char *name; /**< button name */
- int32_t prev; /**< previous polled value */
- int32_t index; /**< index in buttons array */
- joystick_mapping_t mapping; /**< button mapping */
- /* TODO: add calibration data struct */
+ uint32_t code; /**< event code */
+ char *name; /**< button name */
+ int32_t prev; /**< previous polled value */
+ int32_t index; /**< index in buttons array */
+ joystick_mapping_t mapping; /**< button mapping */
+ joystick_calibration_t calibration; /**< button calibration */
} joystick_button_t;
@@ -212,7 +221,7 @@
pot. TODO: support pot values other than on/off so
emulated paddles and mice can be mapped to axes. */
- /* TODO: add calibration data */
+ joystick_calibration_t calibration; /**< axis calibration */
} joystick_axis_t;
@@ -232,6 +241,8 @@
joystick_mapping_t left; /**< mapping for 'left' direction */
joystick_mapping_t right; /**< mapping for 'right' direction */
} mapping; /**< mappings per direction */
+ joystick_calibration_t calibration; /* XXX: no idea if this makes sense
+ for hats */
} joystick_hat_t;
@@ -421,6 +432,7 @@
joystick_hat_t *hat);
void joystick_mapping_init (joystick_mapping_t *mapping);
+void joystick_calibration_init (joystick_calibration_t *calibration);
joystick_axis_t *joystick_axis_new (const char *name);
joystick_axis_t *joystick_axis_from_code (joystick_device_t *joydev,
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|