commit e75063a0f3cd0e3f64edb0305c792c837b2b2e11 Author: Lukas Date: Mon Jul 5 10:50:11 2021 +0200 Initial commit diff --git a/amp_controller.pdf b/amp_controller.pdf new file mode 100644 index 0000000..618ecce Binary files /dev/null and b/amp_controller.pdf differ diff --git a/amp_controller.py b/amp_controller.py new file mode 100644 index 0000000..305d5f3 --- /dev/null +++ b/amp_controller.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 + +import RPi.GPIO as gpio +import time +import logging +import re +import subprocess + + +class amp_controller: + """Amp controller class to hold certain attributes and methods for automatically + turning on and off the amplifier. + """ + + SHUTOFF_TIMER_DURATION = 5 * 60 # in seconds + SHORT_TIMER_DURATION = 60 + CHECK_FREQUENCY = 1 # in seconds + MPD_LOG_FILE = "/var/log/mpd/mpd.log" + PULSE_CARD_PATH = "/proc/asound/card0/pcm0p/sub0/status" + GPIO_PINS = {"Relay": 2, "Phono": 4, "Tape": 14, "CD": 15} + + powered = False + last_mpd_output = "" + + gpio.setmode(gpio.BCM) + logging.basicConfig(level=logging.DEBUG) + + def setup_pins(self) -> None: + """Set all GPIO pins to output.""" + for pin in self.GPIO_PINS.values(): + gpio.setup(pin, gpio.OUT) + + def set_channel(self, channel_name) -> None: + """Set the channel on the amplifier.""" + pin = self.GPIO_PINS[channel_name] + gpio.output(pin, True) + time.sleep(0.5) + gpio.output(pin, False) + + def power(self, state) -> None: + """Turn the amplifier on or off via the GPIO pins.""" + if state == "on": + gpio.output(self.GPIO_PINS["Relay"], True) + time.sleep(0.5) + self.powered = True + else: + gpio.output(self.GPIO_PINS["Relay"], False) + self.powered = False + + def pulse_playing(self) -> bool: + """Return True if there is something playing on the specified Pulseaudio card. + Return False in every other case. + """ + output = subprocess.check_output( + "cat " + self.PULSE_CARD_PATH, shell=True, universal_newlines=True + ) + + state = output.split("\n")[0] + + if "RUNNING" in state: + logging.debug("Pulseaudio connected") + return True + + return False + + def mpd_client_connected(self) -> bool: + """Return True if an mpd client is connected and False if not. + Requires 'log_level="secure"' or "debug" to be set in mpd.conf. + """ + output = subprocess.check_output( + ["tail", "-1", self.MPD_LOG_FILE], universal_newlines=True + ) + + if output != self.last_mpd_output: + self.last_mpd_output = output + if re.match(".* opened from .*", output): + logging.debug("mpd client connected") + return True + + return False + + +if __name__ == "__main__": + ac = amp_controller() + ac.setup_pins() + ac.power("on") + ac.set_channel("CD") + + timer = ac.SHUTOFF_TIMER_DURATION + while True: + # Check for changes every second. + time.sleep(ac.CHECK_FREQUENCY) + + # Only connected, nothing playing. + if ac.mpd_client_connected(): + if not ac.powered: + ac.power("on") + ac.set_channel("CD") + + # (Re)set the timer to the short duration if a client connects. Don't + # overwrite a running timer currently greater than the short duration one. + if timer > 0 and timer <= ac.SHORT_TIMER_DURATION: + timer = ac.SHORT_TIMER_DURATION + elif timer < 0: + timer = ac.SHORT_TIMER_DURATION + + logging.debug("Timer set to {}".format(timer)) + + if timer == 0: + logging.debug("Timer ran out") + ac.power("off") + + if ac.pulse_playing() and not ac.powered: + ac.power("on") + ac.set_channel("CD") + + # Reset the timer. + timer = ac.SHUTOFF_TIMER_DURATION + + # Start the counter if nothing is currently playing. + if not ac.pulse_playing(): + timer -= ac.CHECK_FREQUENCY + logging.debug("Nothing playing.") + logging.debug("Timer: " + str(timer)) + continue + + if timer < -1000: # Just to be safe and avoid weird overflows. + timer = -1 + + # If there is something playing (no ifs matched) just reset the counter. + timer = ac.SHUTOFF_TIMER_DURATION + logging.debug("Something is playing.") + logging.debug("Timer: " + str(timer)) diff --git a/amp_controller.sch b/amp_controller.sch new file mode 100644 index 0000000..1db8e8c --- /dev/null +++ b/amp_controller.sch @@ -0,0 +1,378 @@ +EESchema Schematic File Version 4 +EELAYER 30 0 +EELAYER END +$Descr A4 11693 8268 +encoding utf-8 +Sheet 1 1 +Title "Automating an old HiFi receiver" +Date "2021-03-10" +Rev "" +Comp "" +Comment1 "" +Comment2 "" +Comment3 "" +Comment4 "" +$EndDescr +$Comp +L Connector:Raspberry_Pi_2_3 J1 +U 1 1 60DDC401 +P 5450 3800 +F 0 "J1" H 6000 5200 50 0000 C CNN +F 1 "Raspberry_Pi_3" H 6000 5100 50 0000 C CNN +F 2 "" H 5450 3800 50 0001 C CNN +F 3 "https://www.raspberrypi.org/documentation/hardware/raspberrypi/schematics/rpi_SCH_3bplus_1p0_reduced.pdf" H 5450 3800 50 0001 C CNN + 1 5450 3800 + 1 0 0 -1 +$EndComp +$Comp +L Isolator:LTV-847 U1 +U 3 1 60DE520A +P 7200 4300 +F 0 "U1" H 7200 4100 50 0000 C CNN +F 1 "LTV-847" H 7200 4000 50 0000 C CNN +F 2 "Package_DIP:DIP-16_W7.62mm" H 7000 4100 50 0001 L CIN +F 3 "http://optoelectronics.liteon.com/upload/download/DS-70-96-0016/LTV-8X7%20series.PDF" H 7200 4300 50 0001 L CNN + 3 7200 4300 + 1 0 0 -1 +$EndComp +$Comp +L Isolator:LTV-847 U1 +U 2 1 60DE73C5 +P 4050 3600 +F 0 "U1" H 4050 3925 50 0000 C CNN +F 1 "LTV-847" H 4050 3834 50 0000 C CNN +F 2 "Package_DIP:DIP-16_W7.62mm" H 3850 3400 50 0001 L CIN +F 3 "http://optoelectronics.liteon.com/upload/download/DS-70-96-0016/LTV-8X7%20series.PDF" H 4050 3600 50 0001 L CNN + 2 4050 3600 + 1 0 0 -1 +$EndComp +$Comp +L power:GND #PWR07 +U 1 1 60DEB291 +P 6800 4400 +F 0 "#PWR07" H 6800 4150 50 0001 C CNN +F 1 "GND" H 6805 4227 50 0000 C CNN +F 2 "" H 6800 4400 50 0001 C CNN +F 3 "" H 6800 4400 50 0001 C CNN + 1 6800 4400 + 1 0 0 -1 +$EndComp +$Comp +L power:GND1 #PWR09 +U 1 1 60DEC006 +P 7600 4400 +F 0 "#PWR09" H 7600 4150 50 0001 C CNN +F 1 "GND1" H 7605 4227 50 0000 C CNN +F 2 "" H 7600 4400 50 0001 C CNN +F 3 "" H 7600 4400 50 0001 C CNN + 1 7600 4400 + 1 0 0 -1 +$EndComp +Wire Wire Line + 7600 4400 7500 4400 +Wire Wire Line + 6900 4400 6800 4400 +$Comp +L Device:R_Small R3 +U 1 1 60DEFEFD +P 6700 4200 +F 0 "R3" V 6504 4200 50 0000 C CNN +F 1 "330R" V 6595 4200 50 0000 C CNN +F 2 "" H 6700 4200 50 0001 C CNN +F 3 "~" H 6700 4200 50 0001 C CNN + 1 6700 4200 + 0 1 1 0 +$EndComp +Text GLabel 3200 3500 2 50 UnSpc ~ 0 +Tuner +Text GLabel 7600 4200 2 50 UnSpc ~ 0 +Phono +Text GLabel 4400 3500 2 50 UnSpc ~ 0 +CD +$Comp +L power:GND #PWR05 +U 1 1 60DF21B1 +P 5750 5150 +F 0 "#PWR05" H 5750 4900 50 0001 C CNN +F 1 "GND" H 5755 4977 50 0000 C CNN +F 2 "" H 5750 5150 50 0001 C CNN +F 3 "" H 5750 5150 50 0001 C CNN + 1 5750 5150 + 1 0 0 -1 +$EndComp +Wire Wire Line + 5050 5100 5150 5100 +Connection ~ 5150 5100 +Wire Wire Line + 5150 5100 5250 5100 +Connection ~ 5250 5100 +Wire Wire Line + 5250 5100 5350 5100 +Connection ~ 5350 5100 +Wire Wire Line + 5350 5100 5450 5100 +Connection ~ 5450 5100 +Wire Wire Line + 5450 5100 5550 5100 +Connection ~ 5550 5100 +Wire Wire Line + 5550 5100 5650 5100 +Connection ~ 5650 5100 +Wire Wire Line + 5650 5100 5750 5100 +$Comp +L Device:Q_NPN_BCE Q1 +U 1 1 60E00D90 +P 7150 2800 +F 0 "Q1" H 7341 2846 50 0000 L CNN +F 1 "Q_NPN_BCE" H 7341 2755 50 0000 L CNN +F 2 "" H 7350 2900 50 0001 C CNN +F 3 "~" H 7150 2800 50 0001 C CNN + 1 7150 2800 + 1 0 0 -1 +$EndComp +$Comp +L Device:R_Small R4 +U 1 1 60E03B10 +P 6700 2800 +F 0 "R4" V 6504 2800 50 0000 C CNN +F 1 "1k" V 6595 2800 50 0000 C CNN +F 2 "" H 6700 2800 50 0001 C CNN +F 3 "~" H 6700 2800 50 0001 C CNN + 1 6700 2800 + 0 1 1 0 +$EndComp +Wire Wire Line + 7500 4200 7600 4200 +Wire Wire Line + 6800 4200 6900 4200 +$Comp +L Isolator:LTV-847 U1 +U 1 1 60DE5F9F +P 2850 3600 +F 0 "U1" H 2850 3925 50 0000 C CNN +F 1 "LTV-847" H 2850 3834 50 0000 C CNN +F 2 "Package_DIP:DIP-16_W7.62mm" H 2650 3400 50 0001 L CIN +F 3 "http://optoelectronics.liteon.com/upload/download/DS-70-96-0016/LTV-8X7%20series.PDF" H 2850 3600 50 0001 L CNN + 1 2850 3600 + 1 0 0 -1 +$EndComp +$Comp +L power:GND1 #PWR02 +U 1 1 60E260A0 +P 3250 3700 +F 0 "#PWR02" H 3250 3450 50 0001 C CNN +F 1 "GND1" H 3255 3527 50 0000 C CNN +F 2 "" H 3250 3700 50 0001 C CNN +F 3 "" H 3250 3700 50 0001 C CNN + 1 3250 3700 + 1 0 0 -1 +$EndComp +$Comp +L power:GND1 #PWR04 +U 1 1 60E262E7 +P 4450 3700 +F 0 "#PWR04" H 4450 3450 50 0001 C CNN +F 1 "GND1" H 4455 3527 50 0000 C CNN +F 2 "" H 4450 3700 50 0001 C CNN +F 3 "" H 4450 3700 50 0001 C CNN + 1 4450 3700 + 1 0 0 -1 +$EndComp +Wire Wire Line + 4350 3700 4450 3700 +Wire Wire Line + 3150 3700 3250 3700 +$Comp +L power:GND #PWR01 +U 1 1 60E27552 +P 2450 3700 +F 0 "#PWR01" H 2450 3450 50 0001 C CNN +F 1 "GND" H 2455 3527 50 0000 C CNN +F 2 "" H 2450 3700 50 0001 C CNN +F 3 "" H 2450 3700 50 0001 C CNN + 1 2450 3700 + 1 0 0 -1 +$EndComp +$Comp +L power:GND #PWR03 +U 1 1 60E27847 +P 3650 3700 +F 0 "#PWR03" H 3650 3450 50 0001 C CNN +F 1 "GND" H 3655 3527 50 0000 C CNN +F 2 "" H 3650 3700 50 0001 C CNN +F 3 "" H 3650 3700 50 0001 C CNN + 1 3650 3700 + 1 0 0 -1 +$EndComp +Wire Wire Line + 2450 3700 2550 3700 +Wire Wire Line + 3650 3700 3750 3700 +Wire Wire Line + 2450 3500 2550 3500 +Wire Wire Line + 4650 3000 3650 3000 +Wire Wire Line + 3650 3500 3750 3500 +Wire Wire Line + 3150 3500 3200 3500 +Wire Wire Line + 4350 3500 4400 3500 +$Comp +L Device:R_Small R1 +U 1 1 60E39D0E +P 2450 3200 +F 0 "R1" V 2254 3200 50 0000 C CNN +F 1 "330R" V 2345 3200 50 0000 C CNN +F 2 "" H 2450 3200 50 0001 C CNN +F 3 "~" H 2450 3200 50 0001 C CNN + 1 2450 3200 + 1 0 0 -1 +$EndComp +$Comp +L Device:R_Small R2 +U 1 1 60E3A62A +P 3650 3200 +F 0 "R2" V 3454 3200 50 0000 C CNN +F 1 "330R" V 3545 3200 50 0000 C CNN +F 2 "" H 3650 3200 50 0001 C CNN +F 3 "~" H 3650 3200 50 0001 C CNN + 1 3650 3200 + 1 0 0 -1 +$EndComp +Wire Wire Line + 2450 2900 2450 3100 +Wire Wire Line + 3650 3000 3650 3100 +Wire Wire Line + 3650 3300 3650 3500 +Wire Wire Line + 2450 3300 2450 3500 +Wire Wire Line + 6800 2800 6950 2800 +Wire Wire Line + 6250 3300 6400 3300 +$Comp +L power:GND #PWR08 +U 1 1 60E02F01 +P 7250 3050 +F 0 "#PWR08" H 7250 2800 50 0001 C CNN +F 1 "GND" H 7255 2877 50 0000 C CNN +F 2 "" H 7250 3050 50 0001 C CNN +F 3 "" H 7250 3050 50 0001 C CNN + 1 7250 3050 + 1 0 0 -1 +$EndComp +Wire Wire Line + 7250 3000 7250 3050 +Connection ~ 8900 2250 +Wire Wire Line + 8900 2550 8900 2250 +Wire Wire Line + 8850 2550 8900 2550 +Wire Wire Line + 8650 2250 8900 2250 +$Comp +L power:+5V #PWR010 +U 1 1 60DFDF95 +P 8900 2250 +F 0 "#PWR010" H 8900 2100 50 0001 C CNN +F 1 "+5V" H 8915 2423 50 0000 C CNN +F 2 "" H 8900 2250 50 0001 C CNN +F 3 "" H 8900 2250 50 0001 C CNN + 1 8900 2250 + 1 0 0 -1 +$EndComp +$Comp +L Device:D_Small D1 +U 1 1 60DF89B5 +P 8550 2250 +F 0 "D1" H 8550 2150 50 0000 C CNN +F 1 "D_Small" H 8550 2134 50 0001 C CNN +F 2 "" V 8550 2250 50 0001 C CNN +F 3 "~" V 8550 2250 50 0001 C CNN + 1 8550 2250 + -1 0 0 1 +$EndComp +$Comp +L Relay:DIPxx-1Cxx-51x RL1 +U 1 1 60DF6245 +P 8550 2750 +F 0 "RL1" V 9000 2750 50 0000 C CNN +F 1 "AZ8-1CH-50A" V 9100 2750 50 0000 C CNN +F 2 "Relay_THT:Relay_StandexMeder_DIP_LowProfile" H 9000 2700 50 0001 L CNN +F 3 "https://standexelectronics.com/wp-content/uploads/datasheet_reed_relay_DIP.pdf" H 8550 2750 50 0001 C CNN + 1 8550 2750 + 0 1 1 0 +$EndComp +Wire Wire Line + 7250 2550 7250 2600 +Wire Wire Line + 2450 2900 4650 2900 +Wire Wire Line + 6600 2800 6400 2800 +Wire Wire Line + 6400 2800 6400 3200 +Wire Wire Line + 6400 3200 6250 3200 +Connection ~ 5750 5100 +Wire Wire Line + 7250 2550 8200 2550 +Wire Wire Line + 8200 2250 8200 2550 +Wire Wire Line + 5250 2500 5350 2500 +Wire Wire Line + 5250 2450 5250 2500 +Connection ~ 5250 2500 +$Comp +L power:+5V #PWR06 +U 1 1 60DF360B +P 5250 2450 +F 0 "#PWR06" H 5250 2300 50 0001 C CNN +F 1 "+5V" H 5265 2623 50 0000 C CNN +F 2 "" H 5250 2450 50 0001 C CNN +F 3 "" H 5250 2450 50 0001 C CNN + 1 5250 2450 + 1 0 0 -1 +$EndComp +Wire Wire Line + 5750 5100 5750 5150 +$Comp +L Switch:SW_SPST SW1 +U 1 1 60E85113 +P 8550 3650 +F 0 "SW1" H 8550 3550 50 0000 C CNN +F 1 "SW_SPST" H 8550 3794 50 0001 C CNN +F 2 "" H 8550 3650 50 0001 C CNN +F 3 "~" H 8550 3650 50 0001 C CNN + 1 8550 3650 + 1 0 0 -1 +$EndComp +Wire Wire Line + 6400 4200 6600 4200 +Wire Wire Line + 6400 3300 6400 4200 +Wire Wire Line + 8750 3650 8950 3650 +Wire Wire Line + 8950 3650 8950 3050 +Wire Wire Line + 8950 3050 8850 3050 +Wire Wire Line + 8200 2250 8450 2250 +Wire Wire Line + 8200 2550 8250 2550 +Connection ~ 8200 2550 +Wire Wire Line + 8150 2950 8150 3650 +Wire Wire Line + 8150 3650 8350 3650 +Wire Wire Line + 8150 2950 8250 2950 +Text GLabel 8150 3350 0 50 Input ~ 0 +AMP_Power +Text GLabel 8950 3350 2 50 Output ~ 0 +AMP_Power +$EndSCHEMATC