-
Is there an easy way to implement a Switch as an input device instead of a button? I am using an old radio as enclosure and would like to re-use the existing elements. I would love to use a switch in a way that the phoniebox toggle play and pause whenever the switch is changed. Thanks a lot in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I tried to cobble something together in the file class Switch(NameMixin, ButtonBase):
"""
A single switch that runs two different actions depending if the button is pressed or released.
:param pull_up: See #Button
:param active_state: See #Button
:param bounce_time: See #Button
:param hold_time: See #Button
:param pin_factory: See #Button
:param name: See #Button
"""
def __init__(
self, pin=None, *, pull_up=True, active_state=None, bounce_time=None,
hold_time=1, pin_factory=None, name=None):
super().__init__(
pin=pin, pull_up=pull_up, active_state=active_state,
bounce_time=bounce_time, pin_factory=pin_factory, name=name)
self._button.hold_time = hold_time
self._button.when_pressed = self._on_activation
self._button.when_released = self._on_deactivation
self._switch_on_callback = None
self._switch_off_callback = None
def _on_activation(self):
if self._switch_on_callback:
self._switch_on_callback()
def _on_deactivation(self):
if self._switch_off_callback:
self._switch_off_callback()
@property
def switch_on(self):
return self._short_press_callback
@switch_on.setter
def switch_on(self, func: Callable):
self._switch_on_callback = func
@property
def switch_off(self):
return self._switch_off_callback
@switch_off.setter
def switch_off(self, func: Callable):
self._switch_off_callback = func
@property
def hold_time(self):
return self._button.hold_time
def set_rpc_actions(self, action_config):
self.switch_on_press = self._decode_rpc_action('switch_on', action_config)
self.switch_off_press = self._decode_rpc_action('switch_off', action_config) I then added the following in TogglePlayback:
type: Switch
kwargs:
pin: 17
pull_up:
active_state: true
actions:
switch_on:
alias: toggle
switch_off:
alias: toggle The switch then also shows up in the log file:
gpioinfo also shows GPIO17 as in use. The expected behavior would be that the play/pause state is toggled when the switch is flipped - instead nothing happens. Would appreciate any guidance on how to troubleshoot. |
Beta Was this translation helpful? Give feedback.
-
In case someone is struggling with the same question in the future the following code works for me: class Switch(NameMixin, ButtonBase):
"""
A single switch that runs the on_press action whenever it is toggled.
:param pull_up: See #Button
:param active_state: See #Button
:param bounce_time: See #Button
:param hold_time: See #Button
:param pin_factory: See #Button
:param name: See #Button
"""
def __init__(
self, pin=None, *, pull_up=True, active_state=None,
bounce_time=None,
hold_time=1, hold_repeat=False,
pin_factory=None,
name=None):
super().__init__(
pin=pin, pull_up=pull_up, active_state=active_state,
bounce_time=bounce_time, pin_factory=pin_factory, name=name)
self._button.hold_time = hold_time
self._button.hold_repeat = hold_repeat
@property
def on_press(self):
"""
The function to run when the device has been pressed
"""
return self._button.when_pressed
@on_press.setter
def on_press(self, func):
self._button.when_pressed = func
self._button.when_released = func
@property
def hold_time(self):
return self._button.hold_time
@property
def hold_repeat(self):
return self._button.hold_repeat
def set_rpc_actions(self, action_config):
self.on_press = self._decode_rpc_action('on_press', action_config) |
Beta Was this translation helpful? Give feedback.
In case someone is struggling with the same question in the future the following code works for me: