-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigGenerator.py
318 lines (275 loc) · 11.3 KB
/
configGenerator.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
import json
import os
import sys
from ALookCom.anim import Anim
from ALookCom.img import Img
import ALookCom.imgFmt as imgFmt
from ALookCom.layout import Layout
from ALookCom.commandPub import CommandPub
from ALookCom.comBin import ComBin
from ALookCom.comFile import ComFile
import sensorParam
import setTimer
class ConfigGenerator:
def __init__(self, com):
self.cmd = CommandPub(com)
self.anim = Anim(com)
self.img = Img(com)
self.layout = Layout(com)
self.com = com
def parseJson(self, path):
# read file
f = open(path, 'r')
cfg = json.load(f)
f.close()
# image conversion format as string
# formats save image decompressed and save image compressed are added
imgFmtDict = {
"mono_4bpp": imgFmt.MONO_4BPP,
"mono_1bpp": imgFmt.MONO_1BPP,
"mono_4bpp_heatshrink": imgFmt.MONO_4BPP_HEATSHRINK,
"mono_4bpp_heatshrink_save_comp": imgFmt.MONO_4BPP_HEATSHRINK_SAVE_COMP,
"ryyg": imgFmt.RYYG,
'rryg': imgFmt.RRYG,
"mono_alpha_8bpp": imgFmt.MONOALPHA_8BPP
}
# write config, without version (in case of failure)
if 'config' in cfg:
for config in cfg['config']:
if config['deprecated']:
if not self.cmd.cfgWriteDeprecated(config['cfg'], 0, 0, 0, 0):
print("cmd cfgWriteDeprecated failed")
return False
else:
if not self.cmd.cfgWrite(config['cfg'], 0, config['cfgKey']):
print("cmd cfgWrite failed")
return False
# save fonts
if 'fonts' in cfg:
for font in cfg['fonts']:
baseline = 0.25
if 'baseline' in font:
baseline = font['baseline']
if not self.cmd.fontSave(font['id'], font['height'], font['path'], font['first'], font['last'], baseline):
print("failed to save font: {}".format(font['path']))
return False
# layoutDelete
if 'layoutDelete' in cfg:
for layoutDelete in cfg['layoutDelete']:
if not self.cmd.layoutDelete(layoutDelete['id']):
print("cmd layoutDelete failed")
return False
# save layouts
if 'layouts' in cfg:
for lay in cfg['layouts']:
cmds = []
for c in lay['cmds']:
if c['type'] == 'img':
cmds += self.cmd.layoutCmdImg(c['id'], c['x'], c['y'])
elif c['type'] == 'circle':
cmds += self.cmd.layoutCmdCircle(c['x'], c['y'], c['r'])
elif c['type'] == 'circleFull':
cmds += self.cmd.layoutCmdCircleFull(c['x'], c['y'], c['r'])
elif c['type'] == 'rect':
cmds += self.cmd.layoutCmdRect(c['x0'], c['y0'], c['x1'], c['y1'])
elif c['type'] == 'rectFull':
cmds += self.cmd.layoutCmdRectFull(c['x0'], c['y0'], c['x1'], c['y1'])
elif c['type'] == 'line':
cmds += self.cmd.layoutCmdLine(c['x0'], c['y0'], c['x1'], c['y1'])
elif c['type'] == 'point':
cmds += self.cmd.layoutCmdPoint(c['x'], c['y'])
elif c['type'] == 'color':
cmds += self.cmd.layoutCmdGreyLvl(c['val'])
elif c['type'] == 'font':
cmds += self.cmd.layoutCmdFont(c['id'])
elif c['type'] == 'text':
cmds += self.cmd.layoutCmdText(c['x'], c['y'], c['txt'])
elif c['type'] == 'anim':
cmds += self.cmd.layoutCmdAnimDisplay(c['handlerId'], c['id'], c['delay'], c['repeat'], c['x'], c['y'])
else:
print("Layout #{}: unknown type: {}".format(lay['id'], c['type']))
return False
ret = self.cmd.layoutSave(
id=lay['id'],
x0=lay['x0'],
y0=lay['y0'],
width=lay['width'],
height=lay['height'],
foreColor=lay['foreColor'],
backColor=lay['backColor'],
font=lay['font'],
txtX0=lay['txtX0'],
txtY0=lay['txtY0'],
txtRot=lay['txtRot'],
txtOpacity=lay['txtOpacity'],
usetxt=lay['usetxt'],
cmd=cmds)
if not ret:
print("failed to save layout {}#".format(lay['id']))
return False
# imgDelete
if 'imgDelete' in cfg:
for imgDelete in cfg['imgDelete']:
if imgDelete['deprecated']:
if not self.cmd.imgDeleteDeprecated(imgDelete['id']):
print("cmd imgDeleteDeprecated failed")
return False
else:
if not self.cmd.imgDelete(imgDelete['id']):
print("cmd imgDelete failed")
return False
# fontDelete
if 'fontDelete' in cfg:
for fontDelete in cfg['fontDelete']:
if not self.cmd.fontDelete(fontDelete['id']):
print("cmd fontDelete failed")
return False
# save images
if 'imgs' in cfg:
for img in cfg['imgs']:
fmt = imgFmt.MONO_4BPP_HEATSHRINK
if 'fmt' in img:
fmt = imgFmtDict[img['fmt']]
ret = self.img.saveImage(img['id'], img['path'], fmt)
if not ret:
print("failed to save image: {}".format(img['path']))
return False
# save pages
if 'pages' in cfg:
for p in cfg['pages']:
layouts = []
for lay in p['layouts']:
layouts += [[lay['id'], lay['x'], lay['y']]]
if not self.cmd.pageSave(p['id'], layouts):
print("failed to save page {}#".format(p['id']))
return False
# save animation
if 'anim' in cfg:
for a in cfg['anim']:
fmt = imgFmt.MONO_4BPP_HEATSHRINK
if 'fmt' in a:
fmt = imgFmtDict[a['fmt']]
if 'gif' in a:
if not self.anim.saveAnimationGif(a['id'], a['gif'], False, fmt):
print("failed to save animation: {}".format(a['gif']))
return False
else:
if not self.anim.saveAnimation(a['id'], a['folder'], fmt):
print("failed to save animation: {}".format(a['folder']))
return False
# Sensor
if not sensorParam.setSensorParam(self.com, cfg):
return False
if not setTimer.setTimer(self.com, cfg):
return False
# update config version
if 'config' in cfg:
for config in cfg['config']:
if config['deprecated']:
if not self.cmd.cfgWriteDeprecated(config['cfg'], config['cfgVersion'], config['nbBmp'], config['nbLayout'], config['nbFont']):
print("cmd cfgWriteDeprecated failed")
return False
if not self.cmd.cfgSetDeprecated(config['cfg']):
print("cmd cfgSetDeprecated failed")
return False
else:
if not self.cmd.cfgWrite(config['cfg'], config['cfgVersion'], config['cfgKey']):
print("cmd cfgWrite failed")
return False
if not self.cmd.cfgSet(config['cfg']):
print("cmd cfgSet failed")
return False
return True
def cyclePages(self):
ret, lst = self.cmd.pageList()
if not ret:
print('failed to list pagess')
return False
if len(lst) == 0:
print('no page to display')
return True
i = 0
while 1:
print("Display page {}".format(lst[i]))
strs = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
self.cmd.clear()
self.cmd.pageDisplay(lst[i], strs)
i += 1
if i >= len(lst):
i = 0
input('Press Enter to continue')
def cycleLayouts(self):
ret, lst = self.cmd.layoutList()
if not ret:
print('failed to list layouts')
return False
if len(lst) == 0:
print('no layout to display')
return True
i = 0
while 1:
print("Display layout {}".format(lst[i]))
self.cmd.clear()
self.cmd.layoutDisplay(lst[i], str(lst[i]))
i += 1
if i >= len(lst):
i = 0
input('Press Enter to continue')
def cycleImages(self):
ret, lst = self.cmd.imgList()
if not ret:
print('failed to list images')
return False
if len(lst) == 0:
print('no image to display')
return True
i = 0
while 1:
print("Display image {}".format(lst[i][0]))
self.cmd.clear()
self.cmd.imgDisplay(lst[i][0], 0, 0)
i += 1
if i >= len(lst):
i = 0
input('Press Enter to continue')
# --- main ---
if __name__ == '__main__':
folder = 'cfgDescriptor'
sub_folders = [name for name in os.listdir(folder) if os.path.isdir(os.path.join(folder, name))]
folderChoosen = ''
mode = input("mode:\n1 - Save in file\n2 - USB live test\n")
if mode == "1":
com = ComFile(True)
comName = input("Enter filename\n")
elif mode == "2":
com = ComBin(True)
comName = com.findDevice()
else:
sys.exit(1)
if not comName:
print("Activelook not connected")
sys.exit()
com.open(comName)
generator = ConfigGenerator(com)
temp = "Choose a file"
for f in range(len(sub_folders)):
temp += f"\n{f+1} - {sub_folders[f]}"
temp += "\n"
folderChoosen = sub_folders[int(input(temp))-1]
folderPath = f"cfgDescriptor/{folderChoosen}/config.json"
if not generator.parseJson(folderPath):
print("failed to generate config")
else:
if type(generator.com) is ComFile:
generator.com.close()
sys.exit(0)
mode = input("Select test mode:\n1 - Images\n2 - Layouts\n3 - Pages\n")
if mode == "1":
generator.cycleImages()
elif mode == "2":
generator.cycleLayouts()
elif mode == "3":
generator.cyclePages()
else:
print("unknown mode")
generator.com.close()