Momentary buttons to set one pin value

More
05 Jan 2022 05:14 - 05 Jan 2022 05:21 #230827 by AgentWD40
My game controller pendant has 4 momentary buttons x, y, a, and b. I would like to assign a value to each of these buttons and each time a button is pressed I’d like to set a pin’s value to the pressed button’s assigned value. I’d like this pins value to persist after the momentary buttons are released and until the next button press overwrites it. Ideas?

This seems like it should be very straight forward but I’m still learning hal and am not seeing any components that fit the bill in the docs. I’m tempted to just write one but really don’t want to if I don’t have to.
Last edit: 05 Jan 2022 05:21 by AgentWD40.

Please Log in or Create an account to join the conversation.

More
05 Jan 2022 07:08 #230837 by rodw
The following user(s) said Thank You: AgentWD40

Please Log in or Create an account to join the conversation.

More
05 Jan 2022 12:43 - 05 Jan 2022 12:44 #230861 by AgentWD40
I saw that one but I'm not sure how it would be implemented. Can you show me how one might configure so that toggling one button on would toggle three others off? At the same time toggling one off should not toggle the others on.
Last edit: 05 Jan 2022 12:44 by AgentWD40.

Please Log in or Create an account to join the conversation.

More
05 Jan 2022 16:33 #230875 by AgentWD40
I whipped up a python component that accomplishes what I'm after.

My first question is can I accomplish the same thing with standard components?

My second question is for something like this what is an appropriate sleep time?
#!/usr/bin/env python3
import hal, time
h = hal.component("select4")

h.newpin("in0", hal.HAL_FLOAT, hal.HAL_IN)
h.newpin("in1", hal.HAL_FLOAT, hal.HAL_IN)
h.newpin("in2", hal.HAL_FLOAT, hal.HAL_IN)
h.newpin("in3", hal.HAL_FLOAT, hal.HAL_IN)

h.newpin("sel0", hal.HAL_BIT, hal.HAL_IN)
h.newpin("sel1", hal.HAL_BIT, hal.HAL_IN)
h.newpin("sel2", hal.HAL_BIT, hal.HAL_IN)
h.newpin("sel3", hal.HAL_BIT, hal.HAL_IN)

h.newpin("out", hal.HAL_FLOAT, hal.HAL_OUT)

h.ready()

last_selection = 'in0'

try:
    while 1:
        time.sleep(0.005)

        if h['sel0']:
            last_selection = 'in0'
        elif h['sel1']:
            last_selection = 'in1'
        elif h['sel2']:
            last_selection = 'in2'
        elif h['sel3']:
            last_selection = 'in3'

        h['out'] = h[last_selection]

except KeyboardInterrupt:
	raise SystemExit

Please Log in or Create an account to join the conversation.

Time to create page: 0.191 seconds
Powered by Kunena Forum