-
Notifications
You must be signed in to change notification settings - Fork 2
/
AudioPinSwitch.py
71 lines (49 loc) · 1.8 KB
/
AudioPinSwitch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from PyQt4 import QtCore
from PyQt4.QtCore import QObject
from Helpers.perpetualTimer import perpetualTimer
from threading import Timer,Thread,Event
import time
hasIOLibraries = False
try:
import pigpio
hasIOLibraries = True
except ImportError:
print('PIGPIO library not found')
class AudioPinSwitch(QObject):
"""Controls the dual-pole relay that switches between the external auxiliary input and the audio output from the raspberry pi"""
Enabled = True
ListenForPinEvent = True
audioPin1 = None
audioPin2 = None
audioPinsInitialized = False
def __init__(self):
super(AudioPinSwitch, self).__init__()
def setPin(self, pinConfig):
self.audioPin1 = pinConfig["AUDIO_ONE_SWITCH"]
self.audioPin2 = pinConfig["AUDIO_TWO_SWITCH"]
self.initialize()
def initialize(self):
if(hasIOLibraries):
self.pi = pigpio.pi()
self.pi.set_mode(self.audioPin1, pigpio.OUTPUT)
self.pi.set_mode(self.audioPin2, pigpio.OUTPUT)
self.doDisablePins()
self.audioPinsInitialized = True
def processPinEvent(self, pinNum):
if(self.audioPinsInitialized):
if(self.audioPin1 == pinNum):
self.pi.write(self.audioPin2,0)
self.pi.write(self.audioPin1,1)
elif(self.audioPin2 == pinNum):
self.pi.write(self.audioPin1,0)
self.pi.write(self.audioPin2,1)
self.doDisablePins()
def doDisablePins(self):
t = Thread(target=self._disablePins, args=(self,))
t.start()
def _disablePins(self, parent):
time.sleep(0.5)
self.pi.write(self.audioPin1,0)
self.pi.write(self.audioPin2,0)
def dispose(self):
print("disposed of audio relay switch")