rotary works

This commit is contained in:
PiFestim 2022-07-29 17:49:26 +01:00
parent 6294807ed3
commit 1a2adbffdf

114
rotary.py
View File

@ -1,78 +1,42 @@
import time def next_band():
from pigpio_encoder.rotary import Rotary global CURRENT_BAND
CURRENT_BAND = next(band)
counter = bands[CURRENT_BAND][0]
print(counter)
print(CURRENT_BAND)
# Bands dict return counter
bands = {
"80m":[3.5, 4],
"40m":[7, 7.3],
"20m":[14, 14.35],
"10m":[28, 29.7],
"2m":[144,146],
"70cm":[430,440]
}
# iterate over band def prev_band():
band = iter(bands) global CURRENT_BAND
CURRENT_BAND = None CURRENT_BAND = list(bands.keys())[list(bands.keys()).index(CURRENT_BAND)-1]
print(CURRENT_BAND)
counter = bands[CURRENT_BAND][0] + 1
return counter
def setup_encoder(ranges:list, scale:float=.25): def main_loop():
# make the minimum and maximum deviate by .1 try:
minimum, maximum = ranges counter = next_band()
minimum -= .1 while True:
maximum += .1 clkState = GPIO.input(clk)
dtState = GPIO.input(dt)
# prints current value swState = GPIO.input(sw)
def rotary_callback(counter): if swState == GPIO.HIGH:
print("Counter value: ", round(counter, 2)) print("yeet")
if clkState != clkLastState:
if counter > bands[CURRENT_BAND][1]:
# init rotary counter = next_band()
my_rotary = Rotary( if counter < bands[CURRENT_BAND][0]:
clk_gpio=17, counter = prev_band()
dt_gpio=27, if dtState != clkState:
sw_gpio=22 counter += step
) else:
counter -= step
# setup rotary print(round(counter, 3))
my_rotary.setup_rotary( clkLastState = clkState
min=minimum, sleep(0.01)
max=maximum, except KeyboardInterrupt:
scale=scale, print("\nBye")
debounce=300, finally:
rotary_callback=rotary_callback print("AAAAA")
) GPIO.cleanup()
#my_rotary.watch()
return my_rotary
def change_band(plus:bool=False, minus:bool=False):
global CURRENT_BAND
if plus:
# rollover positive next
CURRENT_BAND = next(band)
if minus:
try:
# rollover negative previous
bands_keys = list(bands)
CURRENT_BAND = bands[bands_keys[bands_keys.index(CURRENT_BAND)-1]]
print(CURRENT_BAND)
except IndexError:
pass
# new encoder
encoder = setup_encoder(bands[CURRENT_BAND])
print(CURRENT_BAND)
return encoder
def main():
encoder = change_band(plus=True)
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)
time.sleep(.5)
if __name__ == "__main__":
main()