Skip to content

Commit

Permalink
fix: use led device for bluetooth sink switch led
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvinSchiller committed Jan 6, 2025
1 parent 088ea3a commit 176b8ef
Showing 1 changed file with 14 additions and 28 deletions.
42 changes: 14 additions & 28 deletions components/bluetooth-sink-switch/bt-sink-switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import logging
import os
import configparser
import time

from gpio_control.GPIODevices import LED

# Create logger
logger = logging.getLogger('bt-sink-switch.py')
Expand Down Expand Up @@ -108,26 +110,14 @@ def bt_switch(cmd, led_pin=None): # noqa C901
logger.error("Invalid command. Doing nothing.")
return

led_device = None
# Rudimentary check if LED pin request is valid GPIO pin number
if led_pin is not None:
if led_pin < 2 or led_pin > 27:
led_pin = None
logger.error("Invalid led_pin. Ignoring led_pin = " + str(led_pin))

if led_pin is not None:
# Set-up GPIO LED pin if not already configured. If it already exists, sanity check direction of pin before use
try:
with open("/sys/class/gpio/gpio" + str(led_pin) + "/direction") as f:
if f.readline(3) != "out":
logger.error("LED pin already in use with direction 'in'. Ignoring led_pin = " + str(led_pin))
led_pin = None
except FileNotFoundError:
# GPIO direction file does not exist -> pin is not configured. Set it up (sleep is necessary!)
proc = subprocess.run("echo " + str(led_pin) + " > /sys/class/gpio/export; \
sleep 0.1; \
echo out > /sys/class/gpio/gpio" + str(led_pin) + "/direction", shell=True, check=False,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
logger.debug(proc.stdout)
else:
led_device = LED(led_pin, name="BluetoothToggleLed", initial_value=False)

# Figure out if output 1 (speakers) is enabled
isSpeakerOn_console = subprocess.run("mpc outputs", shell=True, check=False, stdout=subprocess.PIPE,
Expand Down Expand Up @@ -156,19 +146,19 @@ def bt_switch(cmd, led_pin=None): # noqa C901
logger.debug(proc.stdout)
# Yet, in some cases, a stream error still occurs: check and recover
bt_check_mpc_err()
if led_pin is not None:
proc = subprocess.run("echo 1 > /sys/class/gpio/gpio" + str(led_pin) + "/value", shell=True,
check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if led_device is not None:
led_device.on()
logger.debug(b'LED on: ' + proc.stdout)
return
else:
print("No bluetooth device connected. Defaulting to \"Output 1\".")
if led_pin:
if led_device is not None:
sleeptime = 0.25
for i in range(0, 3):
subprocess.run("echo 1 > /sys/class/gpio/gpio" + str(led_pin) + "/value; sleep " + str(
sleeptime) + "; echo 0 > /sys/class/gpio/gpio" + str(led_pin) + "/value; sleep " + str(
sleeptime), shell=True, check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
led_device.on()
time.sleep(sleeptime)
led_device.off()
time.sleep(sleeptime)

# Default: Switch to Speakers
print("Switched audio sink to \"Output 1\"")
Expand All @@ -178,9 +168,8 @@ def bt_switch(cmd, led_pin=None): # noqa C901
logger.debug(proc.stdout)
# Yet, in some cases, a stream error still occurs: check and recover
bt_check_mpc_err()
if led_pin:
proc = subprocess.run("echo 0 > /sys/class/gpio/gpio" + str(led_pin) + "/value", shell=True, check=False,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
if led_device is not None:
led_device.off()
logger.debug(b'LED off: ' + proc.stdout)


Expand Down Expand Up @@ -217,9 +206,6 @@ def get_led_pin_config(cfg_file):
led_pin = cfg[section_name].getint('led_pin', fallback=None)
if not led_pin:
logger.warning("Could not find 'led_pin' or could not read integer value")
elif not 1 <= led_pin <= 27:
logger.warning(f"Ignoring out of range pin number: {led_pin}.")
led_pin = None
else:
logger.debug(f"No section {section_name} found. Defaulting to led_pin = None")

Expand Down

0 comments on commit 176b8ef

Please sign in to comment.