-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabusliconf.py
341 lines (293 loc) · 15.3 KB
/
abusliconf.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import configparser
outputList=[]
sectionList=[]
for x in range(0,16):
sectionList.append("Input"+str(x))
outputList.append("Output"+str(x))
#rint(sectionList)
config = configparser.ConfigParser()
buttonConf=[False]*16
ioConf=[False]*1024
oConf=[False]*32
timerOConf=[False]*32
timervals=[0]*16
longPushThr=0
timeoutThr=0
brownoutThr=0
description="none"
outDefaults=[False]*16
patternSavingShort=[False]*16
patternSavingLong=[False]*16
fastPwmEnable = [False]*16
ultraSlowPwmEnable = [False]*16
debouncetime=0
def readConfFromFile():
global filename
global timeoutThr
global longPushThr
global brownoutThr
global description
global debouncetime
global fastPwmEnable
global ultraSlowPwmEnable
config.read(filename)
try:
#global longPushThr
longPushThr = int(config.get('Global', 'long-push-threshold'))
#print("Long-push-thr: "+str(longPushThr))
except:
#global longPushThr
longPushThr = 9999
#print("no long push threshold in config file")
try:
#global longPushThr
debouncetime = int(config.get('Global', 'debounce-time'))
#print("Long-push-thr: "+str(longPushThr))
except:
#global longPushThr
debouncetime = 9999
#print("no long push threshold in config file")
try:
# global timeoutThr
timeoutThr = int(config.get('Global', 'timeout'))
#print("Timeout: "+str(timeoutThr))
except:
# global timeoutThr
timeoutThr = 0
#print("no timeout thresold in config file")
try:
brownoutThr = int(config.get('Global', 'brownout-threshold'))
except:
brownoutThr = 0
try:
description = str(config.get('Global', 'description')).rstrip(" ")
except:
description = "none"
for sections in sectionList:
try:
shortOn = list(map(int,config.get(sections, 'short-on').split(',')))
# print("Section: "+str(sectionList.index(sections))+", shortOn: "+str(shortOn))
except:
shortOn=[]
try:
shortOff = list(map(int,config.get(sections, 'short-off').split(',')))
# print("Section: "+str(sectionList.index(sections))+", shortOff: "+str(shortOff))
except:
shortOff=[]
try:
longOn = list(map(int,config.get(sections, 'long-on').split(',')))
# print("Section: "+str(sectionList.index(sections))+", longOn: "+str(longOn))
except:
longOn=[]
try:
longOff = list(map(int,config.get(sections, 'long-off').split(',')))
# print("Section: "+str(sectionList.index(sections))+", longOff: "+str(longOff))
except:
longOff=[]
try:
switchType = config.get(sections, 'switch-type')
except:
switchType = "push-button"
try:
if(config.get(sections, 'pattern-saving-shortpush')=='on'):
patternSavingShort[sectionList.index(sections)]=True
except:
patternSavingShort[sectionList.index(sections)]=False
try:
if(config.get(sections, 'pattern-saving-longpush')=='on'):
patternSavingLong[sectionList.index(sections)]=True
except:
patternSavingLong[sectionList.index(sections)]=False
########################### Arrange data, copy into boolean list ###############################
if sectionList.index(sections)>7:
skip = 512
multiplier=(sectionList.index(sections)-8)*8
else:
skip = 0
multiplier=(sectionList.index(sections))*8
for x in range(0,len(shortOn)):
if (shortOn[x] < 8):
bitLocation=shortOn[x]+multiplier+skip
ioConf[bitLocation]=True
else:
if(shortOn[x] > 15):
raise Exception("Output doesn't exist")
bitLocation=shortOn[x]+multiplier+skip+128-8
ioConf[bitLocation]=True
# print(str(shortOn[x])+": Bit"+str(bitLocation))
for x in range(0,len(shortOff)):
if (shortOff[x] < 8):
bitLocation=shortOff[x]+multiplier+64+skip
ioConf[bitLocation]=True
else:
bitLocation=shortOff[x]+multiplier+64+128+skip-8
ioConf[bitLocation]=True
# print(str(shortOff[x])+": Bit"+str(bitLocation))
for x in range(0,len(longOn)):
if (longOn[x] < 8):
bitLocation=longOn[x]+256+multiplier+skip
ioConf[bitLocation]=True
else:
if(longOn[x] > 15):
raise Exception("Output doesn't exist")
bitLocation=longOn[x]+256+multiplier+skip+128-8
ioConf[bitLocation]=True
# print(str(longOn[x])+": Bit"+str(bitLocation))
for x in range(0,len(longOff)):
if (longOff[x] < 8):
bitLocation=longOff[x]+256+64+multiplier+skip
ioConf[bitLocation]=True
else:
if(longOff[x] > 15):
raise Exception("Output doesn't exist")
bitLocation=longOff[x]+256+multiplier+64+skip+128-8
ioConf[bitLocation]=True
# print(str(longOff[x])+": Bit"+str(bitLocation))
if switchType == "toggle-switch":
# print("Input"+str(sectionList.index(sections))+": Schalter")
buttonConf[sectionList.index(sections)]=True
#else:
# print("Input"+str(sectionList.index(sections))+": Taster")
for outputsection in outputList:
try:
oConfStr = config.get(outputsection, 'input-controlled')
except:
oConfStr=[]
if oConfStr == 'timeout-only':
oConf[outputList.index(outputsection)+16]=True
elif oConfStr == 'always':
oConf[outputList.index(outputsection)]=True
oConf[outputList.index(outputsection)+16]=True
try:
oConfStr = config.get(outputsection, 'timer-controlled')
except:
oConfStr=[]
if oConfStr == 'timeout-only':
timerOConf[outputList.index(outputsection)+16]=True
elif oConfStr == 'always':
timerOConf[outputList.index(outputsection)]=True
timerOConf[outputList.index(outputsection)+16]=True
try:
timervals[outputList.index(outputsection)] = int(config.get(outputsection, 'timeout-value'))
except:
timervals[outputList.index(outputsection)] = 0
if timervals[outputList.index(outputsection)] > 65535:
timervals[outputList.index(outputsection)] = 0
if timervals[outputList.index(outputsection)] < 0:
timervals[outputList.index(outputsection)] = 0
try:
if(config.get(outputsection, 'default-state')=='on'):
outDefaults[outputList.index(outputsection)]=True
else:
outDefaults[outputList.index(outputsection)]=False
except:
outDefaults[outputList.index(outputsection)]=False
try:
pwmStr = config.get(outputsection, 'pwm')
except:
pwmStr=""
if pwmStr == 'fast':
fastPwmEnable[outputList.index(outputsection)]=True
elif pwmStr == 'ultra-slow':
ultraSlowPwmEnable[outputList.index(outputsection)]=True
########################################################################################
#@brief: writeConfTofile(), write configuration data to file
def writeConfToFile():
global filename
configwriter = configparser.ConfigParser()
configwriter.add_section('Global')
configwriter.set('Global', 'timeout', str(timeoutThr))
configwriter.set('Global', 'long-push-threshold', str(longPushThr))
configwriter.set('Global', 'debounce-time', str(debouncetime))
configwriter.set('Global', 'brownout-threshold', str(brownoutThr))
configwriter.set('Global', 'description', str(description))
for section in sectionList:
try:
configwriter.add_section(section)
except:
print("Section "+section+" already exists, overwriting")
####################### make dummy config file ##########################################
configwriter.set(section, 'short-on', '')
configwriter.set(section, 'short-off','')
configwriter.set(section, 'long-on','')
configwriter.set(section, 'long-off','')
configwriter.set(section, 'switch-type', 'push-button')
configwriter.set(section, 'pattern-saving-shortpush', 'off')
configwriter.set(section, 'pattern-saving-longpush', 'off')
for outputsection in outputList:
try:
configwriter.add_section(outputsection)
except:
print("Section "+outputsection+" already exists, overwriting")
configwriter.set(outputsection, 'input-controlled','never')
configwriter.set(outputsection, 'timer-controlled','never')
configwriter.set(outputsection, 'timeout-value','0')
configwriter.set(outputsection, 'default-state', 'off')
configwriter.set(outputsection, 'pwm', 'off')
####################### prepare lists for config file #####################################
for x in range(0,16):
if(buttonConf[x]==True):
configwriter.set(sectionList[x], 'switch-type', 'toggle-switch')
for x in range(0,16):
if(patternSavingShort[x]==True):
configwriter.set(sectionList[x], 'pattern-saving-shortpush', 'on')
for x in range(0,16):
if(patternSavingLong[x]==True):
configwriter.set(sectionList[x], 'pattern-saving-longpush', 'on')
for section in sectionList:
shortOn = []
shortOff = []
longOn = []
longOff = []
if sectionList.index(section)>7:
skip = 512
multiplier=(sectionList.index(section)-8)*8
else:
skip = 0
multiplier=(sectionList.index(section))*8
#shorton:
for x in range(0,16):
if x<8 and ioConf[x+multiplier+skip]==True:
shortOn.append(x)
if x>7 and ioConf[x-8+multiplier+skip+128]==True:
shortOn.append(x)
if x<8 and ioConf[x+64+multiplier+skip]==True:
shortOff.append(x)
if x>7 and ioConf[x-8+64+128+multiplier+skip]==True:
shortOff.append(x)
if x<8 and ioConf[x+256+multiplier+skip]==True:
longOn.append(x)
if x>7 and ioConf[x-8+256+128+multiplier+skip]==True:
longOn.append(x)
if x<8 and ioConf[x+256+64+multiplier+skip]==True:
longOff.append(x)
if x>7 and ioConf[x-8+256+64+128+multiplier+skip]==True:
longOff.append(x)
######################### finally, write to file ##################################
configwriter.set(section, 'short-on', ','.join(map(str,shortOn)))
configwriter.set(section, 'short-off', ','.join(map(str,shortOff)))
configwriter.set(section, 'long-on', ','.join(map(str,longOn)))
configwriter.set(section, 'long-off', ','.join(map(str,longOff)))
for outputsection in outputList:
if(oConf[outputList.index(outputsection)+16]):
configwriter.set(outputsection, 'input-controlled','timeout-only')
if(oConf[outputList.index(outputsection)]):
configwriter.set(outputsection, 'input-controlled','always')
for outputsection in outputList:
if(fastPwmEnable[outputList.index(outputsection)]):
configwriter.set(outputsection, 'pwm','fast')
if(ultraSlowPwmEnable[outputList.index(outputsection)]):
configwriter.set(outputsection, 'pwm','ultra-slow')
for outputsection in outputList:
if(timerOConf[outputList.index(outputsection)+16]):
configwriter.set(outputsection, 'timer-controlled','timeout-only')
if(timerOConf[outputList.index(outputsection)]):
configwriter.set(outputsection, 'timer-controlled','always')
configwriter.set(outputsection,'timeout-value',str(timervals[outputList.index(outputsection)]))
for outputsection in outputList:
if(outDefaults[outputList.index(outputsection)]):
configwriter.set(outputsection, 'default-state', 'on')
else:
configwriter.set(outputsection, 'default-state', 'off')
with open(filename, 'w') as configfile:
configwriter.write(configfile)