From d9d141678ffbee06f58c7b31593c02a84698c16a Mon Sep 17 00:00:00 2001 From: ABelliqueux Date: Wed, 27 Mar 2024 11:35:28 +0100 Subject: [PATCH] Add pot volume --- mpdlisten.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) mode change 100644 => 100755 mpdlisten.py diff --git a/mpdlisten.py b/mpdlisten.py old mode 100644 new mode 100755 index 5aead48..483b3bd --- a/mpdlisten.py +++ b/mpdlisten.py @@ -8,7 +8,7 @@ from math import floor from os import environ import signal import sys -from time import sleep +from time import sleep, time # Relay import RPi.GPIO as GPIO # OLED SSD1306 @@ -17,6 +17,21 @@ from luma.core.render import canvas 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 + + jfont = ImageFont.truetype('DejaVuSansMono.ttf', 10) # MPD config @@ -42,6 +57,7 @@ for BTN in BTNS: GPIO.setup(BTNS[BTN]['GPIO'], GPIO.IN, pull_up_down=GPIO.PUD_UP) # SSD1306 setup - bouding box is (0, 0, 127, 63), 128x64 +# GPIOS 2, 3 serial = i2c(port=1, address=0x3C) device = ssd1306(serial) @@ -334,6 +350,13 @@ def send_mpd_cmd(client, cmd:str, ui_state:dict): def main(args): + # Pot_cap + # Connect to Pi. + pi = pigpio.pi() + # Instantiate Pot/Cap reader. + pc = pot_cap.reader(pi, pot_cap_gpio, drain_ms, timeout_s) + start = time() + previous_song_id = None previous_state = None paused_since_seconds = 0 @@ -367,11 +390,29 @@ def main(args): global static_ui 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) + # ~ print("{} {}".format(v_1, v_2)) + if (abs(volume - v_1) > 1) and (abs(volume - v_2) > 2): + client.setvol(volume) + print("Volume: {}".format(volume)) + if volume < min_val: + volume = 0 + if volume > 100: + volume = 100 + v_2 = v_1 + v_1 = volume # MPD mpd_status = client.status() if len(mpd_status): mpd_client_status = mpd_status mpd_client_currentsong = client.currentsong() + print(mpd_client_status['volume']) if 'state' in mpd_client_status: play_state = mpd_client_status['state'] if 'songid' in mpd_client_status: @@ -412,6 +453,10 @@ def main(args): # Save previous state BTNS[BTN]['state'] = GPIO.input(BTNS[BTN]['GPIO']) update_display(device, mpd_client_currentsong, mpd_client_status, current_mode, ui_state['cursor_pos']) + # pot_cap + pc.cancel() # Cancel the reader. + pi.stop() # Disconnect from Pi. + device.cleanup() client.disconnect() return 0