Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/fix bt sink toggle led #2468

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions components/bluetooth-sink-switch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,13 @@ $ ./install-bt-sink-switch.sh

#### Status LED

An optional status LED will be turned on if the audio sink is set to bluetooth. If a toggle command is issued, but no bluetooth device is connected, the LED will blink three times. Looks very neat, if you have a button with integrated LED. Add these lines to your `RPi-Jukebox-RFID/settings/gpio_settings.ini`, to use GPIO 13 as LED signal. It is `led_pin` the BCM number of the GPIO pin (i.e. 'led_pin = 13' means GPIO13) and defaults to None. Create the file, if it does not exist.
An optional status LED will be turned on if the audio sink is set to bluetooth. If a toggle command is issued, but no bluetooth device is connected, the LED will blink three times. Looks very neat, if you have a button with integrated LED. Add these lines to your `RPi-Jukebox-RFID/settings/gpio_settings.ini`, to use GPIO 13 as LED signal. It is `led_pin` the gpiofind name of the GPIO pin (i.e. 'led_pin = GPIO13' means GPIO13) and defaults to None. Create the file, if it does not exist.

**Important note**: Correct capitalization of [BluetoothToggleLed] is important!

~~~bash
[BluetoothToggleLed]
enabled: True
led_pin: 13
led_pin: GPIO27
~~~

#### GPIO control
Expand Down
51 changes: 22 additions & 29 deletions components/bluetooth-sink-switch/bt-sink-switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging
import os
import configparser
import time


# Create logger
Expand Down Expand Up @@ -110,24 +111,16 @@

# 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:
# detect GPIO
proc = subprocess.run(["gpiofind", led_pin],

Check warning on line 115 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.9)

W291 trailing whitespace

Check warning on line 115 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.10)

W291 trailing whitespace

Check warning on line 115 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.11)

W291 trailing whitespace

Check warning on line 115 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.12)

W291 trailing whitespace
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc = subprocess.run(["gpiofind", led_pin],
proc = subprocess.run(["gpiofind", led_pin],

shell=False, check=False,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
logger.debug(proc.stdout)
if proc.returncode != 0:
logger.error("GPIO for LED not found")
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_pin = proc.stdout.decode("utf-8").strip().split(" ")

# 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 @@ -157,7 +150,7 @@
# 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,
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1]+"=1"], shell=False,

Check failure on line 153 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.9)

E226 missing whitespace around arithmetic operator

Check failure on line 153 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.10)

E226 missing whitespace around arithmetic operator

Check failure on line 153 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.11)

E226 missing whitespace around arithmetic operator

Check failure on line 153 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.12)

E226 missing whitespace around arithmetic operator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1]+"=1"], shell=False,
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1] + "=1"], shell=False,

check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
logger.debug(b'LED on: ' + proc.stdout)
return
Expand All @@ -166,9 +159,12 @@
if led_pin:
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)
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1]+"=1"], shell=False,

Check failure on line 162 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.9)

E226 missing whitespace around arithmetic operator

Check failure on line 162 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.10)

E226 missing whitespace around arithmetic operator

Check failure on line 162 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.11)

E226 missing whitespace around arithmetic operator

Check failure on line 162 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.12)

E226 missing whitespace around arithmetic operator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1]+"=1"], shell=False,
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1] + "=1"], shell=False,

check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
time.sleep(sleeptime)
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1]+"=0"], shell=False,

Check failure on line 165 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.9)

E226 missing whitespace around arithmetic operator

Check failure on line 165 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.10)

E226 missing whitespace around arithmetic operator

Check failure on line 165 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.11)

E226 missing whitespace around arithmetic operator

Check failure on line 165 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.12)

E226 missing whitespace around arithmetic operator
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1]+"=0"], shell=False,
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1] + "=0"], shell=False,

check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
time.sleep(sleeptime)

# Default: Switch to Speakers
print("Switched audio sink to \"Output 1\"")
Expand All @@ -178,9 +174,9 @@
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_pin is not None:
proc = subprocess.run(["gpioset", led_pin[0], led_pin[1]+"=0"], shell=False,

Check failure on line 178 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.9)

E226 missing whitespace around arithmetic operator

Check failure on line 178 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.10)

E226 missing whitespace around arithmetic operator

Check failure on line 178 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.11)

E226 missing whitespace around arithmetic operator

Check failure on line 178 in components/bluetooth-sink-switch/bt-sink-switch.py

View workflow job for this annotation

GitHub Actions / build (3.12)

E226 missing whitespace around arithmetic operator
check=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
logger.debug(b'LED off: ' + proc.stdout)


Expand All @@ -192,8 +188,7 @@
The file must contain the entry

[BluetoothToggleLed]
enabled = True
led_pin = 6
led_pin: GPIO27

where
- led_pin is the BCM number of the GPIO pin (i.e. 'led_pin = 6' means GPIO6) and defaults to None
Expand All @@ -214,11 +209,9 @@
led_pin = None
if section_name in cfg:
if cfg[section_name].getboolean('enabled', fallback=False):
led_pin = cfg[section_name].getint('led_pin', fallback=None)
led_pin = cfg[section_name].get('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
Loading