From 1a2adbffdf58c04a9ab2aa3d1f4215c7f70732cb Mon Sep 17 00:00:00 2001 From: PiFestim Date: Fri, 29 Jul 2022 17:49:26 +0100 Subject: [PATCH] rotary works --- rotary.py | 114 +++++++++++++++++++----------------------------------- 1 file changed, 39 insertions(+), 75 deletions(-) diff --git a/rotary.py b/rotary.py index 1fcc1f6..f55a4f0 100644 --- a/rotary.py +++ b/rotary.py @@ -1,78 +1,42 @@ -import time -from pigpio_encoder.rotary import Rotary +def next_band(): + global CURRENT_BAND + CURRENT_BAND = next(band) + counter = bands[CURRENT_BAND][0] + print(counter) + print(CURRENT_BAND) -# Bands dict -bands = { - "80m":[3.5, 4], - "40m":[7, 7.3], - "20m":[14, 14.35], - "10m":[28, 29.7], - "2m":[144,146], - "70cm":[430,440] -} + return counter -# iterate over band -band = iter(bands) -CURRENT_BAND = None +def prev_band(): + global CURRENT_BAND + 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): - # make the minimum and maximum deviate by .1 - minimum, maximum = ranges - minimum -= .1 - maximum += .1 - - # prints current value - def rotary_callback(counter): - print("Counter value: ", round(counter, 2)) - - - # init rotary - my_rotary = Rotary( - clk_gpio=17, - dt_gpio=27, - sw_gpio=22 - ) - - # setup rotary - my_rotary.setup_rotary( - min=minimum, - max=maximum, - scale=scale, - debounce=300, - rotary_callback=rotary_callback - ) - - #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() +def main_loop(): + try: + counter = next_band() + while True: + clkState = GPIO.input(clk) + dtState = GPIO.input(dt) + swState = GPIO.input(sw) + if swState == GPIO.HIGH: + print("yeet") + if clkState != clkLastState: + if counter > bands[CURRENT_BAND][1]: + counter = next_band() + if counter < bands[CURRENT_BAND][0]: + counter = prev_band() + if dtState != clkState: + counter += step + else: + counter -= step + print(round(counter, 3)) + clkLastState = clkState + sleep(0.01) + except KeyboardInterrupt: + print("\nBye") + finally: + print("AAAAA") + GPIO.cleanup()