Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,37 @@ while True:

**Note:** The interruption of the module when running in threads is not yet handled, you might have to kill it by yourself 🔪

### Controlling Volume level for souncard (needs python-alsaaudio 'sudo apt-get install python-alsaaudio')

```python
#!/usr/bin/python
# import sys to set extra python path
import sys

sys.path.append("/home/pi/.local/lib/python2.7/site-packages")

# Import required python modules, os is probably not required
from pyky040 import pyky040
import os
import alsaaudio

# Set M as variable for long command
m = alsaaudio.Mixer('Speaker', cardindex=1)

# Define your callback and use python alsaaudio to set volume (scale_position comes from pyky040)
def my_callback(scale_position):
m.setvolume (scale_position)

# Init the encoder pins (chane gpio numbers according to your setup)
my_encoder = pyky040.Encoder(CLK=17, DT=18, SW=26)

# Setup the options and callbacks (see documentation)
my_encoder.setup(scale_min=0, scale_max=100, step=1, scale_start=m.getvolume()[0], chg_callback=my_callback)

# Launch the listener
my_encoder.watch()

```
## Documentation

#### `Encoder(CLK=x, DT=y, SW=z)`
Expand All @@ -115,6 +146,7 @@ Setup the behavior of the module. All of the following keyword arguments are opt
- `sw_callback (function)` When the encoder switch is pressed

- Scale mode
- `scale_start (int/float)`Starting position of Scale, Set it to 0 if you dont want to set a custom value
- `scale_min (int/float)` Scale minimum
- `scale_max (int/float)` Scale maximum
- `loop (boolean)` Loop mode (defaults to `False`)
Expand Down
11 changes: 5 additions & 6 deletions pyky040/pyky040.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Encoder:
sw = None

polling_interval = None # Polling interval (in ms)
sw_debounce_time = 250 # Debounce time (for switch only)
sw_debounce_time = 100 # Debounce time (for switch only)

step = 1 # Scale step from min to max
max_counter = 100 # Scale max
Expand Down Expand Up @@ -54,11 +54,10 @@ def setup(self, **params):
else:
self.counter_loop = False

self.counter = self.min_counter + 0
if 'scale_min' in params:
self.min_counter = params['scale_min']
if 'scale_max' in params:
self.max_counter = params['scale_max']
self.min_counter = params['scale_min']
self.counter = params['scale_start']
self.max_counter = params['scale_max']

if 'step' in params:
self.step = params['step']
if 'inc_callback' in params:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

setup(
name='pyky040',
version='0.1.3',
version='0.1.5',
description='High-level interface for the KY040 rotary encoder and switch.',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down