-
Notifications
You must be signed in to change notification settings - Fork 9
/
input_selector.py
135 lines (120 loc) Β· 3.66 KB
/
input_selector.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
import digitalio
import time
from adafruit_bus_device import spi_device
class InputSelector(object):
'''
Manages an input selector via SPI
'''
def __init__(self, spi, cs, baudrate=1000000, input_current=1,
debug=False, inputs=6, loop=True):
self.input_current = input_current
self.inputs = inputs
self.input_loop = loop
self.mute_enabled = True
self.debug = debug
self._device = spi_device.SPIDevice(spi, cs, baudrate=baudrate, polarity=0, phase=0)
self._print('starting init')
self._set_register(0, 0)
self._set_register(1, 0)
def _print(self, message):
'''
Small function for printing information is debug option is enabled
'''
if not self.debug:
return
else:
print('input-selector: {0}'.format(message))
def _set_register(self, register, value):
'''
Sets a both registers on the MCP23S17
'''
with self._device as device:
device.write(bytearray([64]))
device.write(bytearray([register]))
device.write(bytearray([value]))
def _set_gpio(self, gio_a, gio_b):
'''
Sets a both registers on the MCP23S17
'''
with self._device as device:
device.write(bytearray([64]))
device.write(bytearray([18]))
device.write(bytearray([gio_a]))
device.write(bytearray([gio_b]))
def toggle_mute(self):
'''
Toggles mute relay on/off
'''
if self.mute_enabled:
self._set_gpio(64, 0)
self.mute_enabled = False
self._print('mute disabled')
else:
self._set_gpio(0, 0)
self.mute_enabled = True
self._print('mute enabled')
def _disable_all_relays(self, with_delay=True):
'''
Turns off all relays with optional delay
'''
if with_delay:
time.sleep(0.01)
self._set_gpio(64, 0)
def _set_all_relays_to_off(self):
'''
Switches all relays to off position where no input is selected
'''
if self.mute_enabled:
self._set_gpio(42, 84)
else:
self._set_gpio(106, 84)
def select_input(self, input):
'''
Selects a specific input
'''
if input > self.inputs:
if self.input_loop:
input = 1
else:
return
elif input < 1:
if self.input_loop:
input = self.inputs
else:
return
if input == 1:
self._set_gpio(106, 82)
self._disable_all_relays()
elif input == 2:
self._set_gpio(106, 76)
self._disable_all_relays()
elif input == 3:
self._set_gpio(106, 52)
self._disable_all_relays()
elif input == 4:
self._set_gpio(105, 84)
self._disable_all_relays()
elif input == 5:
self._set_gpio(102, 84)
self._disable_all_relays()
elif input == 6:
self._set_gpio(90, 84)
self._disable_all_relays()
self._print('input {0} selected'.format(input))
self.input_current = input
def next_input(self, increment=1):
'''
Selects the next input
'''
self.select_input(input=self.input_current + increment)
def up(self):
'''
Switches up to next input
'''
self.next_input(1)
def down(self):
'''
Switches down to next input
'''
self.next_input(-1)