From 1a0b03d991e9184fb4900a03ec5cd61d458047fe Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Thu, 19 Dec 2024 07:25:50 +0100 Subject: [PATCH] Switch to rotary encoder --- mpdlisten.py | 90 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/mpdlisten.py b/mpdlisten.py index ec5b287..e1ee1da 100755 --- a/mpdlisten.py +++ b/mpdlisten.py @@ -18,19 +18,18 @@ from luma.oled.device import ssd1306 from PIL import Image, ImageDraw, ImageFont # Pot_cap -import pigpio -import pot_cap -min_val = 8 -max_val = 298 -vol_mult = 100/(max_val-min_val) -volume = 0 -v_1 = 0 -v_2 = 0 -ctrlc_pressed = False -pot_cap_gpio = 23 -drain_ms = 0.8 -timeout_s = 1.0 - +# ~ import pigpio +# ~ import pot_cap +# ~ min_val = 8 +# ~ max_val = 298 +# ~ vol_mult = 100/(max_val-min_val) +# ~ volume = 0 +# ~ v_1 = 0 +# ~ v_2 = 0 +# ~ ctrlc_pressed = False +# ~ pot_cap_gpio = 23 +# ~ drain_ms = 0.8 +# ~ timeout_s = 1.0 jfont = ImageFont.truetype('DejaVuSansMono.ttf', 10) @@ -46,6 +45,14 @@ GPIO.setmode(GPIO.BCM) RELAIS_1_GPIO = 17 GPIO.setup(RELAIS_1_GPIO, GPIO.OUT) # GPIO Assign mode +# Rotary encoder +clk = 27 +dt = 22 +GPIO.setup(clk, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) +GPIO.setup(dt, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) +volume = 0 +clkLastState = GPIO.input(clk) + # Buttons GPIO BTNS = { 'BTN_1' : dict(GPIO=7, state=1), 'BTN_2' : dict(GPIO=8, state=1), @@ -359,9 +366,9 @@ def main(args): ui_idle_for = 0 # Pot_cap # Connect to Pi. - pi = pigpio.pi() + # ~ pi = pigpio.pi() # Instantiate Pot/Cap reader. - pc = pot_cap.reader(pi, pot_cap_gpio, drain_ms, timeout_s) + # ~ pc = pot_cap.reader(pi, pot_cap_gpio, drain_ms, timeout_s) start = time() previous_song_id = None @@ -398,22 +405,37 @@ def main(args): static_ui = generate_static_ui(current_mode) while ctrlc_pressed is False: # pot_cap - global v_1 - global v_2 - global volume - s, v, r = pc.read() - if s and r < 4: - volume = round(v*vol_mult) - if (abs(volume - v_1) > 1) and (abs(volume - v_2) > 2): - print("Volume: {}".format(volume)) - if volume < min_val: - volume = 0 - if volume > 100: - volume = 100 - client.setvol(100-volume) - ui_idle_for = 0 - v_2 = v_1 - v_1 = volume + # ~ global v_1 + # ~ global v_2 + global volume, clkLastState + # ~ s, v, r = pc.read() + # ~ if s and r < 4: + # ~ volume = round(v*vol_mult) + # ~ if (abs(volume - v_1) > 1) and (abs(volume - v_2) > 2): + # ~ print("Volume: {}".format(volume)) + # ~ if volume < min_val: + # ~ volume = 0 + # ~ if volume > 100: + # ~ volume = 100 + # ~ client.setvol(100-volume) + # ~ ui_idle_for = 0 + # ~ v_2 = v_1 + # ~ v_1 = volume + # Rotary encoder + clkState = GPIO.input(clk) + dtState = GPIO.input(dt) + if clkState != clkLastState: + if dtState != clkState: + if volume < 100: + volume += 1 + else: + if volume > 0: + volume -= 1 + print(volume) + client.setvol(volume) + ui_idle_for = 0 + clkLastState = clkState + # ~ sleep(0.01) # MPD mpd_status = client.status() if len(mpd_status): @@ -470,9 +492,9 @@ def main(args): # Avoid further execution ui_idle_for = 11 device.hide() - # pot_cap - pc.cancel() # Cancel the reader. - pi.stop() # Disconnect from Pi. + # ~ # pot_cap + # ~ pc.cancel() # Cancel the reader. + # ~ pi.stop() # Disconnect from Pi. device.cleanup() client.disconnect()