Multiple Analog Inputs Having 1 Axis Output #9
-
I was wondering if there was a way to have 2 analog inputs (ie: a split throttle handle, 1 for majority of the throttle range, 1 for fine tuning) output into 1 virtual axis. Current workarounds I've tried have led to the pins not updating and conflicts due to value, integer, and float compatibility. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
This sounds like an interesting use case; it's definitely not something that is built into JoystickXL's standard inputs, but it should be achievable using an axis with a I've got some time over the next day or two, and will try to come up with a working example. If you're willing to share which CircuitPython board you're developing for (and which analog inputs you're using), I can try to stick to those in the example. |
Beta Was this translation helpful? Give feedback.
-
It looks like you were pretty close, but were trying to do your analog reads right off of a power1 = AnalogIn(board.A1)
power2 = AnalogIn(board.A0) Then read the values off of those objects when needed: power = int((power1.value * 0.75) + (power2.value * 0.25)) The """This is a simple compound axis example using JoystickXL."""
import board
from analogio import AnalogIn
from joystick_xl.inputs import Axis, VirtualInput
from joystick_xl.joystick import Joystick
p_coarse = AnalogIn(board.A1) # analog input used for coarse (75%) range adjustment
p_fine = AnalogIn(board.A0) # analog input used for fine (25%) range adjustment
joystick = Joystick()
joystick.add_input(
Axis(source=VirtualInput(value=0)), # x-axis using a virtual input
)
while True:
# Here we're manually generating the combined axis value using our two analog
# inputs. We're accessing our joystick's `Axis` collection directly, and setting
# the X-axis `source_value` property, which can be done when an input's source is
# a `VirtualInput` object.
joystick.axis[Axis.X].source_value = int(
(p_coarse.value * 0.75) + (p_fine.value * 0.25)
)
joystick.update() Hopefully that helps - and feel free to share anything you make using JoystickXL in the Show & Tell section here! |
Beta Was this translation helpful? Give feedback.
-
Just in case - here's another example if you actually want all of the axes to report over USB-HID. Using this code, the coarse axis reports as X at full scale, the fine axis reports as Y at full scale, and the combined coarse+fine axis reports as Z. """Another JoystickXL compound axis example where all axes report over USB-HID."""
import board
from joystick_xl.inputs import Axis, VirtualInput
from joystick_xl.joystick import Joystick
joystick = Joystick()
joystick.add_input(
Axis(source=board.A1), # x-axis (coarse)
Axis(source=board.A0), # y-axis (fine)
Axis(source=VirtualInput(value=0)), # z-axis (compound 0.75x + 0.25y)
)
while True:
joystick.axis[Axis.Z].source_value = int(
(joystick.axis[Axis.X].source_value * 0.75)
+ (joystick.axis[Axis.Y].source_value * 0.25)
)
joystick.update() |
Beta Was this translation helpful? Give feedback.
Just in case - here's another example if you actually want all of the axes to report over USB-HID. Using this code, the coarse axis reports as X at full scale, the fine axis reports as Y at full scale, and the combined coarse+fine axis reports as Z.