Skip to content
Snippets Groups Projects
Commit a61eadd7 authored by Jess Ward's avatar Jess Ward
Browse files

Final changes:

Added async loop to fix issue where it hung after muting
Final clean-up and comments
parent b782759a
No related branches found
No related tags found
No related merge requests found
import RPi.GPIO as GPIO import asyncio
import time import time
import snapcast.control import RPi.GPIO as GPIO
from snapcast import get_snapcast_client from snap import get_snapcast_client
from snapcast import add_server
SENSOR_PIN = 7 SENSOR_PIN = 7
MUTE_BRIGHTNESS = 0.1 MUTE_BRIGHTNESS = 0.1
def dark_mute(): def dark_mute():
'''Runs measure_brightness and prints output''' '''Uses brighness level to mute or unmute device'''
server = snapcast.control.Snapserver(add_server(), # Snapcast library only supports asynchronous so need to wrap in async loop
snapcast.control.CONTROL_PORT) loop = asyncio.new_event_loop()
client = get_snapcast_client(server) client = get_snapcast_client(loop)
GPIO.setmode(GPIO.BOARD) GPIO.setmode(GPIO.BOARD)
try: try:
while True: while True:
# TODO: track current state, don't send constant updates
brightness = measure_brightness(SENSOR_PIN) brightness = measure_brightness(SENSOR_PIN)
if brightness < MUTE_BRIGHTNESS: if brightness < MUTE_BRIGHTNESS:
client.muted = True # client.muted did not work so need client.set_muted
# loop used here and also passed into snap.py to get server
loop.run_until_complete(client.set_muted(True))
else: else:
client.muted = False loop.run_until_complete(client.set_muted(False))
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
GPIO.cleanup() GPIO.cleanup()
loop.close()
def measure_brightness(pin_to_circuit): def measure_brightness(pin_to_circuit):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment