commented the logic + tidied it up a bit
This commit is contained in:
parent
017f5b29ee
commit
6294807ed3
41
rotary.py
41
rotary.py
@ -1,6 +1,7 @@
|
|||||||
import time
|
import time
|
||||||
from pigpio_encoder.rotary import Rotary
|
from pigpio_encoder.rotary import Rotary
|
||||||
|
|
||||||
|
# Bands dict
|
||||||
bands = {
|
bands = {
|
||||||
"80m":[3.5, 4],
|
"80m":[3.5, 4],
|
||||||
"40m":[7, 7.3],
|
"40m":[7, 7.3],
|
||||||
@ -10,25 +11,33 @@ bands = {
|
|||||||
"70cm":[430,440]
|
"70cm":[430,440]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# iterate over band
|
||||||
band = iter(bands)
|
band = iter(bands)
|
||||||
CURRENT_BAND = next(band)
|
CURRENT_BAND = None
|
||||||
|
|
||||||
def setup_encoder(ranges:list):
|
def setup_encoder(ranges:list, scale:float=.25):
|
||||||
|
# make the minimum and maximum deviate by .1
|
||||||
|
minimum, maximum = ranges
|
||||||
|
minimum -= .1
|
||||||
|
maximum += .1
|
||||||
|
|
||||||
|
# prints current value
|
||||||
def rotary_callback(counter):
|
def rotary_callback(counter):
|
||||||
print("Counter value: ", round(counter, 2))
|
print("Counter value: ", round(counter, 2))
|
||||||
|
|
||||||
|
|
||||||
|
# init rotary
|
||||||
my_rotary = Rotary(
|
my_rotary = Rotary(
|
||||||
clk_gpio=17,
|
clk_gpio=17,
|
||||||
dt_gpio=27,
|
dt_gpio=27,
|
||||||
sw_gpio=22
|
sw_gpio=22
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# setup rotary
|
||||||
my_rotary.setup_rotary(
|
my_rotary.setup_rotary(
|
||||||
min=ranges[0],
|
min=minimum,
|
||||||
max=ranges[1],
|
max=maximum,
|
||||||
scale=.25,
|
scale=scale,
|
||||||
debounce=300,
|
debounce=300,
|
||||||
rotary_callback=rotary_callback
|
rotary_callback=rotary_callback
|
||||||
)
|
)
|
||||||
@ -39,27 +48,29 @@ def setup_encoder(ranges:list):
|
|||||||
def change_band(plus:bool=False, minus:bool=False):
|
def change_band(plus:bool=False, minus:bool=False):
|
||||||
global CURRENT_BAND
|
global CURRENT_BAND
|
||||||
if plus:
|
if plus:
|
||||||
|
# rollover positive next
|
||||||
CURRENT_BAND = next(band)
|
CURRENT_BAND = next(band)
|
||||||
if minus:
|
if minus:
|
||||||
try:
|
try:
|
||||||
CURRENT_BAND = bands[list(bands)[list(bands.keys()).index(CURRENT_BAND)-1]]
|
# rollover negative previous
|
||||||
|
bands_keys = list(bands)
|
||||||
|
CURRENT_BAND = bands[bands_keys[bands_keys.index(CURRENT_BAND)-1]]
|
||||||
print(CURRENT_BAND)
|
print(CURRENT_BAND)
|
||||||
except Exception as e:
|
except IndexError:
|
||||||
print(e)
|
pass
|
||||||
|
|
||||||
|
# new encoder
|
||||||
encoder = setup_encoder(bands[CURRENT_BAND])
|
encoder = setup_encoder(bands[CURRENT_BAND])
|
||||||
print(CURRENT_BAND)
|
print(CURRENT_BAND)
|
||||||
|
|
||||||
return encoder
|
return encoder
|
||||||
|
|
||||||
encoder = setup_encoder(bands[next(band)])
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
global encoder
|
|
||||||
while True:
|
|
||||||
if encoder.counter >= encoder.max:
|
|
||||||
encoder = change_band(plus=True)
|
encoder = change_band(plus=True)
|
||||||
if encoder.counter <= encoder.min:
|
while True:
|
||||||
|
if encoder.counter > bands[CURRENT_BAND][1]:
|
||||||
|
encoder = change_band(plus=True)
|
||||||
|
if encoder.counter < bands[CURRENT_BAND][0]:
|
||||||
encoder = change_band(minus=True)
|
encoder = change_band(minus=True)
|
||||||
time.sleep(.5)
|
time.sleep(.5)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user