-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_base.py
156 lines (128 loc) · 3.74 KB
/
platform_base.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
*****************************************
Speaker Select Platform Class (BASE)
*****************************************
Description: This library supports
controlling the outputs and observing
inputs on the hardware platform
*****************************************
"""
"""
*****************************************
Imported Libraries
*****************************************
"""
import redis
import json
"""
*****************************************
Parent Class Definition
*****************************************
"""
class SpeakerPlatform:
def __init__(self, settings):
self.cmdsts = redis.Redis()
self.settings = settings
self.spkr_state = {}
self._init_relays()
self._init_ir()
self._init_buttons()
def _init_relays(self):
"""
Setup relays (and LEDS) in default state, or reconfigure
"""
if(self.spkr_state == {}):
self.spkr_state = {
'spkr_00' : 'off',
'spkr_01' : 'off',
'spkr_02' : 'off',
'spkr_03' : 'off',
'spkr_pro' : 'off'
}
for key, value in self.spkr_state.items():
if key != 'spkr_pro':
if(self.settings['spkr_config'][key]['default'] == 'nc'):
self.spkr_state[key] = 'on' # For normally connected
else:
self.spkr_state[key] = 'off' # For normally open
self.set_relays()
def _init_ir(self):
pass
def _init_buttons(self):
self.buttons = {}
for key, value in self.spkr_state.items():
if(key != 'spkr_pro'):
self.buttons[key] = False
self.cmdsts.set('buttons', json.dumps(self.buttons))
def _read_states(self):
return(json.loads(self.cmdsts.get('speakers')))
def _write_states(self):
self.cmdsts.set('speakers', json.dumps(self.spkr_state))
def update_settings(self, settings):
self.settings = settings
self._init_relays()
self._init_ir()
self._init_buttons()
def set_relays(self):
"""
*****************************************
Simple function to set relay and LED outputs
*****************************************
"""
# In base, only check protection (set protection as needed)
self.check_protection()
# Commit changes to database
self._write_states()
def check_appinput(self):
current_state = self._read_states()
if(current_state != self.spkr_state):
self.spkr_state = current_state.copy()
self.set_relays()
def check_buttons(self):
"""
*****************************************
Check for input from front panel buttons
*****************************************
"""
self.buttons = json.loads(self.cmdsts.get('buttons'))
for key, value in self.spkr_state.items():
if (key != 'spkr_pro'):
if (self.buttons[key]):
self.button_pressed(key)
self.buttons[key] = False
self.cmdsts.set('buttons', json.dumps(self.buttons))
def button_pressed(self, spkr_key):
if(self.spkr_state[spkr_key] == 'on'):
self.spkr_state[spkr_key] = 'off'
else:
self.spkr_state[spkr_key] = 'on'
self.set_relays()
def check_ir_input(self):
"""
*****************************************
Check for input from IR remote control
*****************************************
"""
pass
def check_protection(self):
"""
*****************************************
Function: SetProtection
Input: spkr_state dict
Description: Set Protection On/Off as needed
*****************************************
"""
spkr_ohms = 0
for key, value in self.spkr_state.items():
if(key != 'spkr_pro'):
if(value == 'on'):
spkr_ohms += (1 / self.settings['spkr_config'][key]['impedance'])
# Calculate Impedance R = 1 / (1/R1) + (1/R2) ... + (1/RN)
if spkr_ohms > 0:
spkr_ohms = (1 / spkr_ohms)
if (spkr_ohms < 4):
self.spkr_state['spkr_pro'] = 'on'
else:
self.spkr_state['spkr_pro'] = 'off'
else:
self.spkr_state['spkr_pro'] = 'off'