Skip to content

Commit

Permalink
fix: handle args for rotaryEncoder
Browse files Browse the repository at this point in the history
don't pass args to callback if 'None'.
reorder args for callback to pass functionCallArgs first, if definied.
added functionCallArgs to rotaryEncoder definition, to support other functionCalls then volume.
  • Loading branch information
AlvinSchiller authored Apr 22, 2024
1 parent d76a7ef commit 17ffbbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
36 changes: 32 additions & 4 deletions components/gpio_control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Up to now the following input devices are implemented:

* **RotaryEncoder**:
Control of a rotary encoder, for example KY040, see also in [Wiki](https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki/Audio-RotaryKnobVolume).
It can be configured using `pinUp` and `PiNDown` (use GPIO numbers here), `functionCallUp`, `functionCallDown`, and `timeBase` see [extended documentation below](#rotaryencoder).
It can be configured using `Pin1` and `Pin2` (use GPIO numbers here), `functionCall1`, `functionCall2` see [extended documentation below](#rotaryencoder).

* **TwoButtonControl**:
This Device uses two Buttons and implements a third action if both buttons are pressed together. See [extended documentation below](#twobuttoncontrol).
Expand Down Expand Up @@ -172,19 +172,47 @@ Furthermore, the following settings can be used as described for the [regular bu
A RotaryEncoder can be created using an `ini` entry like this:

```bash
[VolumeControl]
[RotaryVolumeControl]
enabled: True
Type: RotaryEncoder
Pin1: 7
Pin2: 8
timeBase: 0.02
timeBase: 0.1
functionCall1: functionCallVolU
functionCall2: functionCallVolD
```

Pin1 and FunctionCall1 correspond to rotary direction "up", while Pin2 and FunctionCall2 correspond to "down".
* **enabled**: This needs to be `True` for the extended shutdown button to work.
* **Pin1**: GPIO number corresponding to rotary direction "clockwise" ('CLK')
* **Pin2**: GPIO number corresponding to rotary direction "counter clockwise" ('DT')
* **functionCall1**: function called for every rotation step corresponding to rotary direction "clockwise". See below for passed arguments. See [function documentation below](#functions).
* **functionCall2**: function called for every rotation step corresponding to rotary direction "counter clockwise". See below for passed arguments. See [function documentation below](#functions).
* **timeBase**: Factor used for calculating the rotation value base on rotation speed, defaults to `0.1`. Use `0` for deactivating rotation speed influence.
Example:
* a single rotation step leads to the value 1 passed to the function.
* steady rotation of two to or more steps, leads to the value 1 for the first call and the value 2 for all further calls.
* speeding up rotation of two to or more steps, leads to the value 1 for the first call, the value 2 for the second, the value 3 for the third and so on.
* **functionCall1Args**: Arguments for `functionCall1`, defaults to `None`. If defined takes precedence over rotation value. Arguments are ignored, if `functionCall1` does not take any.
* **functionCall2Args**: Arguments for `functionCall2`, defaults to `None`. If defined takes precedence over rotation value. Arguments are ignored, if `functionCall1` does not take any.

Note that the old configuration entries PinUp/PinDown and functionCallUp/functionCallDown are deprecated and might stop working in future.


```bash
[RotatrySeekingControl]
enabled: True
Type: RotaryEncoder
Pin1: 7
Pin2: 8
timeBase: 0.1
functionCall1: functionCallPlayerSeekFwd
functionCall1Args: 5
functionCall2: functionCallPlayerSeekBack
functionCall2Args: 5
```

In this example, the encoder will be used to seek for- and backwards by 5 seconds on every rotation step. The rotation value will **NOT** be used in this case as the function args are defined!

### StatusLED

A StatusLED can be created using an `ini` entry like this:
Expand Down
18 changes: 9 additions & 9 deletions components/gpio_control/function_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def __init__(self):
def functionCallShutdown(self, *args):
function_call("{command} -c=shutdown".format(command=self.playout_control), shell=True)

def functionCallVolU(self, steps=None):
def functionCallVolU(self, steps=None, *args):
if steps is None:
function_call("{command} -c=volumeup".format(command=self.playout_control), shell=True)
else:
function_call("{command} -c=volumeup -v={value}".format(value=steps,
command=self.playout_control),
shell=True)

def functionCallVolD(self, steps=None):
def functionCallVolD(self, steps=None, *args):
if steps is None:
function_call("{command} -c=volumedown".format(command=self.playout_control), shell=True)
else:
Expand Down Expand Up @@ -65,22 +65,22 @@ def functionCallPlayerStop(self, *args):
function_call("{command} -c=playerstop".format(command=self.playout_control),
shell=True)

def functionCallPlayerSeekFwd(self, seconds=None):
def functionCallPlayerSeekFwd(self, seconds=None, *args):
if seconds is None:
seconds = 10
function_call("{command} -c=playerseek -v=+{value}".format(command=self.playout_control, value=seconds), shell=True)

def functionCallPlayerSeekBack(self, seconds=None):
def functionCallPlayerSeekBack(self, seconds=None, *args):
if seconds is None:
seconds = 10
function_call("{command} -c=playerseek -v=-{value}".format(command=self.playout_control, value=seconds), shell=True)

def functionCallPlayerSeekFarFwd(self, seconds=None):
def functionCallPlayerSeekFarFwd(self, seconds=None, *args):
if seconds is None:
seconds = 60
function_call("{command} -c=playerseek -v=+{value}".format(command=self.playout_control, value=seconds), shell=True)

def functionCallPlayerSeekFarBack(self, seconds=None):
def functionCallPlayerSeekFarBack(self, seconds=None, *args):
if seconds is None:
seconds = 60
function_call("{command} -c=playerseek -v=-{value}".format(command=self.playout_control, value=seconds), shell=True)
Expand All @@ -94,15 +94,15 @@ def functionCallPlayerRandomCard(self, *args):
def functionCallPlayerRandomFolder(self, *args):
function_call("{command} -c=randomfolder".format(command=self.playout_control), shell=True)

def functionCallBluetoothToggle(self, mode=None):
def functionCallBluetoothToggle(self, mode=None, *args):
if mode is None:
mode = 'toggle'
function_call("{command} -c=bluetoothtoggle -v={value}".format(command=self.playout_control, value=mode), shell=True)

def functionCallTriggerPlayCardId(self, cardid):
def functionCallTriggerPlayCardId(self, cardid, *args):
function_call("{command} --cardid={value}".format(command=self.rfid_trigger, value = cardid), shell=True)

def functionCallTriggerPlayFolder(self, folder):
def functionCallTriggerPlayFolder(self, folder, *args):
function_call("{command} --dir={value}".format(command=self.rfid_trigger, value = folder), shell=True)

def getFunctionCall(self, functionName):
Expand Down
9 changes: 6 additions & 3 deletions components/gpio_control/gpio_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ def getFunctionCall(self, function_name, function_args):
try:
if function_name is not None and function_name != 'None':
functionCall = getattr(self.function_calls, function_name)
return (lambda *args: functionCall(*args, function_args))
if function_args is not None and function_args != 'None':
return (lambda *args: functionCall(function_args, *args))
else:
return (lambda *args: functionCall(*args))
except AttributeError:
self.logger.error('Could not find FunctionCall {function_name}'.format(function_name=function_name))
return lambda *args: None
Expand Down Expand Up @@ -77,8 +80,8 @@ def generate_device(self, config, deviceName):
elif device_type == 'RotaryEncoder':
return RotaryEncoder(config.getint('Pin1'),
config.getint('Pin2'),
self.getFunctionCall(config.get('functionCall1'), None),
self.getFunctionCall(config.get('functionCall2'), None),
self.getFunctionCall(config.get('functionCall1'), config.get('functionCall1Args', fallback=None)),
self.getFunctionCall(config.get('functionCall2'), config.get('functionCall2Args', fallback=None)),
config.getfloat('timeBase', fallback=0.1),
name=deviceName)
elif device_type == 'ShutdownButton':
Expand Down

0 comments on commit 17ffbbd

Please sign in to comment.