Revision: 45664
http://sourceforge.net/p/vice-emu/code/45664
Author: compyx
Date: 2025-05-08 06:21:00 +0000 (Thu, 08 May 2025)
Log Message:
-----------
Joystick: split axis and button interpretation/calibration into separate functions
Use seperate functions `joystick_axis_direction()` and `joystick_button_pressed()`
to interpret raw values and apply calibration so the UI can also use these
functions if required for mapping/calibration.
Modified Paths:
--------------
branches/compyx/joymap-003/vice/src/joyport/joystick.c
branches/compyx/joymap-003/vice/src/joyport/joystick.h
Modified: branches/compyx/joymap-003/vice/src/joyport/joystick.c
===================================================================
--- branches/compyx/joymap-003/vice/src/joyport/joystick.c 2025-05-06 14:40:09 UTC (rev 45663)
+++ branches/compyx/joymap-003/vice/src/joyport/joystick.c 2025-05-08 06:21:00 UTC (rev 45664)
@@ -3039,33 +3039,30 @@
}
-/** \brief Handle joystick axis event
+/** \brief Interpret raw axis value as direction
*
+ * Apply calibration and transform raw \a value into a direction for an
+ * emulated device.
+ *
+ * Determine if we need to invert the raw value and then apply thresholds from
+ * the calibration data to calculate the direction for an emulated joystick.
+ *
* \param[in] axis joystick axis
- * \param[in] value raw value for \a axis
+ * \param[in] value raw value of \a axis
+ *
+ * \return emulated joystick direction
*/
-void joy_axis_event(joystick_axis_t *axis, int32_t value)
+joystick_axis_value_t joystick_axis_direction(joystick_axis_t *axis, int32_t value)
{
- joystick_axis_value_t direction = JOY_AXIS_MIDDLE;
- joystick_axis_value_t prev = axis->prev;
- int joyport = axis->device->joyport;
+ joystick_axis_value_t direction = JOY_AXIS_MIDDLE;
-#if !(defined(USE_SDLUI) || defined(USE_SDL2UI) || defined(USE_HEADLESSUI))
- unsigned int poll_state = axis->device->status & JOY_POLL_MASK;
-
- if (poll_state == JOY_POLL_NONE) {
- return;
- } else if (poll_state == JOY_POLL_UI) {
- joystick_ui_event(axis, JOY_INPUT_AXIS, value);
- return;
- }
-#endif
- /* digital axes don't require calibration: */
+ /* digital axis? */
if (axis->digital) {
/* calibration: invert value? */
if (axis->calibration.invert) {
value *= -1;
}
+ /* no thresholds for digital axes */
if (value < 0) {
direction = JOY_AXIS_NEGATIVE;
} else if (value > 0) {
@@ -3086,11 +3083,7 @@
}
center = axis->maximum - (range / 2);
-#if 0
- printf("%s(): inverting %d: [%d-%d] -> %d\n",
- __func__, value, axis->minimum, axis->maximum, center - value);
-#endif
- value = center - value;
+ value = center - value;
}
if (value <= axis->calibration.threshold.negative) {
direction = JOY_AXIS_NEGATIVE;
@@ -3099,6 +3092,33 @@
}
}
+ return direction;
+}
+
+
+/** \brief Handle joystick axis event
+ *
+ * \param[in] axis joystick axis
+ * \param[in] value raw value for \a axis
+ */
+void joy_axis_event(joystick_axis_t *axis, int32_t value)
+{
+ joystick_axis_value_t direction;
+ joystick_axis_value_t prev = axis->prev;
+ int joyport = axis->device->joyport;
+
+#if !(defined(USE_SDLUI) || defined(USE_SDL2UI) || defined(USE_HEADLESSUI))
+ unsigned int poll_state = axis->device->status & JOY_POLL_MASK;
+
+ if (poll_state == JOY_POLL_NONE) {
+ return;
+ } else if (poll_state == JOY_POLL_UI) {
+ joystick_ui_event(axis, JOY_INPUT_AXIS, value);
+ return;
+ }
+#endif
+
+ direction = joystick_axis_direction(axis, value);
if (direction == prev) {
return;
}
@@ -3126,6 +3146,28 @@
}
+/** \brief Interpret raw button value
+ *
+ * Determine pressed state of \a button by interpreting \a value and applying
+ * calibration (just invert if required).
+ *
+ * \param[in] button joystick button
+ * \param[in] value raw value of \a button
+ *
+ * \return \c 1 if pressed, \c 0 if released
+ */
+int32_t joystick_button_pressed(joystick_button_t *button, int32_t value)
+{
+ int32_t pressed = value ? 1 : 0;
+
+ if (button->calibration.invert) {
+ pressed = !pressed;
+ }
+ return pressed;
+}
+
+
+
/** \brief Handle joystick button event
*
* \param[in] button joystick button
@@ -3133,7 +3175,7 @@
*/
void joy_button_event(joystick_button_t *button, int32_t value)
{
- int32_t pressed = value ? 1 : 0;
+ /* TODO: reinstate this: */
#if 0
int num_buttons = joystick_devices[joynum].num_buttons;
int joy_pin = joystick_devices[joynum].button_mapping[button].value.joy_pin;
@@ -3160,6 +3202,8 @@
#endif
if (value != button->prev) {
+ int32_t pressed = joystick_button_pressed(button, value);
+
DBG(("joy_button_event: joy: %s, button: %d (%s) pressed: %d\n",
button->device->name, button->index, button->name, pressed));
joy_perform_event(&button->mapping, button->device->joyport, pressed);
@@ -3803,6 +3847,23 @@
}
+/** \brief Get index of device in registered devices list
+ *
+ * \param[in] joydev joystick device
+ *
+ * \return index in devices list or -1 if not found
+ */
+int joystick_device_index(joystick_device_t *joydev)
+{
+ for (size_t index = 0; index < num_joystick_devices; index++) {
+ if (joystick_devices[index] == joydev) {
+ return (int)index;
+ }
+ }
+ return -1;
+}
+
+
/** \brief Set joystick device name
*
* Set name of \a joydev to \a name, deallocating the old name if present.
Modified: branches/compyx/joymap-003/vice/src/joyport/joystick.h
===================================================================
--- branches/compyx/joymap-003/vice/src/joyport/joystick.h 2025-05-06 14:40:09 UTC (rev 45663)
+++ branches/compyx/joymap-003/vice/src/joyport/joystick.h 2025-05-08 06:21:00 UTC (rev 45664)
@@ -486,6 +486,7 @@
bool joystick_device_open (joystick_device_t *joydev,
unsigned int mode);
void joystick_device_close (joystick_device_t *joydev);
+int joystick_device_index (joystick_device_t *joydev);
joystick_device_t *joystick_device_by_index (int index);
int joystick_device_count (void);
@@ -508,19 +509,23 @@
joystick_axis_t *joystick_axis_new (const char *name);
joystick_axis_t *joystick_axis_from_code (joystick_device_t *joydev,
- uint32_t code);
+ uint32_t code);
void joystick_axis_free (joystick_axis_t *axis);
void joystick_axis_clear_mappings(joystick_axis_t *axis);
+joystick_axis_value_t joystick_axis_direction(joystick_axis_t *axis,
+ int32_t value);
joystick_button_t *joystick_button_new (const char *name);
joystick_button_t *joystick_button_from_code (joystick_device_t *joydev,
- uint32_t code);
+ uint32_t code);
void joystick_button_free (joystick_button_t *button);
void joystick_button_clear_mappings(joystick_button_t *button);
+int32_t joystick_button_pressed (joystick_button_t *button,
+ int32_t value);
joystick_hat_t *joystick_hat_new (const char *name);
joystick_hat_t *joystick_hat_from_code (joystick_device_t *joydev,
- uint32_t code);
+ uint32_t code);
void joystick_hat_free (joystick_hat_t *hat);
void joystick_hat_clear_mappings(joystick_hat_t *hat);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|