-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImpExp.lua
542 lines (469 loc) · 14.7 KB
/
ImpExp.lua
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
local toolName = "TNS|ImpExp|TNE"
local function init()
lcd.clear()
end
-- function dump_table(o, escSpec, indent)
-- if type(o) == 'table' then
-- local s = '{ '
-- for k,v in pairs(o) do
-- if type(k) ~= 'number' then k = '"'..k..'"' end
-- local vout = ""
-- if type(o) == 'table' then
-- vout = dump_table(v, escSpec)
-- end
-- s = s .. '['..k..'] = ' .. vout .. ','
-- end
-- return s .. '} '
-- else
-- return tostring(o)
-- end
-- end
local function get_model()
return model
end
function serialize_model_index_index(getter, model)
local input = 0
local r = {}
while true do
local rr = {}
local tab = true
local line = 0
while tab ~= nil do
tab = getter(input, line)
if tab ~= nil then
table_insert(rr, tab)
-- print("input="..input.."/".."line="..line.." "..dump_table(tab))
end
line = line + 1
end
if table_length(rr) == 0 then
return r
end
table_insert(r, rr)
input = input + 1
end
return r
end
function serialize_model_index(getter, model)
local rr = {}
local tab = true
local line = 0
while tab ~= nil do
tab = getter(line, 0)
if tab ~= nil then
-- print(dump_table(tab))
table_insert(rr, tab)
end
line = line + 1
end
return rr
end
function to_char(c, str)
local i
i = string.find(str, c, 1, true)
if i == nil then
return str, ""
end
return string.sub(str, 1, i-1), string.sub(str, i+1)
end
function skip_first_spaces(str)
local max = 1
local i
for i = 1, #str do
if string.sub(str,i,i) ~= " " then
return string.sub(str,i)
end
end
return str
end
function table_length(tab)
local c = 0
local _
for _ in pairs(tab) do c = c + 1 end
return c
end
function typeify(s)
if string.sub(s,1,1) == "n" then
return tonumber(string.sub(s,2))
end
if string.sub(s,1,1) == "s" then
return string.sub(s, 2)
end
if string.sub(s,1,1) == "b" then
local v = string.sub(s,2)
if (v == "T") or (v == "t") or (t == "1") then
return true
end
return false
end
end
function process_worker(str)
local tab = {}
local line
while #str > 0 do
line, str = to_char("\n", str)
local k, v = to_char("=", skip_first_spaces(line))
if skip_first_spaces(k) == "END" then
return tab, str
elseif skip_first_spaces(v) == "" then
v, str = process_worker(str)
end
if string.match(k, '^[0-9]+$') then
k = tonumber(k)
end
if type(v) == "table" then
tab[k] = v
else
tab[k] = typeify(v)
end
end
return tab, str
end
function process(str)
local tab, _ = process_worker(str)
return tab
end
function table_insert(tab, line)
local n = table_length(tab)
tab[n+1] = line
return tab
end
function serialize_table_worker(tab, indent)
local k, v
local lines = {}
for k, v in pairs(tab) do
if type(v) == 'table' then
local vv, _
lines = table_insert(lines, indent .. k)
for _, vv in pairs(serialize_table_worker(v, indent .. " ")) do
lines = table_insert(lines, vv)
end
lines = table_insert(lines, indent.."END")
else
local t = type(v)
if t == "boolean" then
if v then v = "bt" else v = "bf" end
end
if t == "string" then v = "s" .. v end
if t == "number" then v = "n" .. v end
-- lines.insert(indent .. k .. '=' .. v)
lines = table_insert(lines, indent .. k .. '=' .. v)
end
end
return lines
end
function serialize_table(tab)
return serialize_table_worker(tab, "")
end
-- function print_serialized(serial)
-- local _, v
-- for _,v in pairs(serial) do
-- print(v)
-- end
-- end
function array_to_string(serial)
local s = ""
local first = true
local _, v
for _,v in pairs(serial) do
if first ~= true then
s = s .. "\n"
end
s = s .. v
first = false
end
return s
end
function read_all(filename)
local f = io.open(filename, "")
local s = ""
local read_data = "SOMETHING"
while read_data ~= "" do
read_data = io.read(f, 99)
s = s .. read_data
end
io.close(f)
return s
end
function write_all(filename, lines)
local f = io.open(filename, "w")
io.write(f, array_to_string(lines))
io.close(f)
return true
end
function model_input_getter(index_1, index_2)
local model = get_model()
print("GETTER: INPUT: " .. index_1 .. "/" .. index_2)
return model.getInput(index_1, index_2)
end
function model_global_variable_values_getter(index_1, index_2)
local model = get_model()
print("GETTER: GLOBAL_VARIABLE_VALUES: " .. index_1 .. "/" .. index_2)
return model.getGlobalVariable(index_1, index_2)
end
function model_global_variable_values_getter(index_1, index_2)
local model = get_model()
print("GETTER: GLOBAL_VARIABLE_DETAILS" .. index_1 .. "/" .. index_2)
return model.getGlobalVariable(index_1, index_2)
end
function model_mix_getter(index_1, index_2)
local model = get_model()
print("GETTER: MIX: " .. index_1 .. "/" .. index_2)
return model.getMix(index_1, index_2)
end
function model_single_meta_getter(f, desc, index_1, zero_or_nil_return)
if zero_or_nil_return ~= 0 then return nil end
local model = get_model()
print("GETTER: " .. desc .. ": " .. index_1)
return f(index_1)
end
function model_output_getter(index_1, zero_or_nil_return)
return model_single_meta_getter(
function(index_1, zero_or_nil_return) return model.getOutput(index_1) end,
"GETTER: OUTPUT",
index_1,
zero_or_nil_return
)
end
function model_curve_getter(index_1, zero_or_nil_return)
return model_single_meta_getter(
function(index_1, zero_or_nil_return) return model.getCurve(index_1) end,
"GETTER: CURVE",
index_1,
zero_or_nil_return
)
end
function model_logical_switch_getter(index_1, zero_or_nil_return)
return model_single_meta_getter(
function(index_1, zero_or_nil_return) return model.getLogicalSwitch(index_1) end,
"GETTER: LOGICAL_SWITCH",
index_1,
zero_or_nil_return
)
end
function model_flight_mode_getter(index_1, zero_or_nil_return)
return model_single_meta_getter(
function(index_1, zero_or_nil_return) return model.getFlightMode(index_1) end,
"GETTER: LOGICAL_SWITCH",
index_1,
zero_or_nil_return
)
end
function model_custom_function_getter(index_1, zero_or_nil_return)
return model_single_meta_getter(
function(index_1, zero_or_nil_return) return model.getCustomFunction(index_1) end,
"GETTER: CUSTOM_FUNCTION",
index_1,
zero_or_nil_return
)
end
function model_global_variable_details_getter(index_1, zero_or_nil_return)
return model_single_meta_getter(
function(index_1, zero_or_nil_return) return model.getGlobalVariableDetails(index_1) end,
"GETTER: GLOBAL_VARIABLE_DETAILS",
index_1,
zero_or_nil_return
)
end
function dump_model_index_index(filename, getter)
write_all(filename, serialize_table(serialize_model_index_index(getter, "")))
end
function dump_model_index(filename, getter)
write_all(filename, serialize_table(serialize_model_index(getter, "")))
end
function load_inputs()
print("LOAD INPUTS")
local line, value, input_contents
local inputs = process(read_all("inputs.dat"))
local del_inputs = serialize_model_index_index(model_input_getter, "")
for input, input_contents in pairs(del_inputs) do
print("" .. input .. "=" .. table_length(input_contents))
local i = table_length(input_contents)
while i > 0 do
print("model.deleteInput(" .. input - 1 .. ", " .. i - 1 .. ")")
model.deleteInput(input - 1, i - 1)
i = i - 1
end
end
for input, input_contents in pairs(inputs) do
for line, value in pairs(input_contents) do
model.insertInput(input - 1, line - 1, value)
end
end
end
function load_global_variable_value()
print("LOAD GLOBAL_VARIABLE_VALUES")
local line, value, input_contents
local inputs = process(read_all("global_variable_values.dat"))
for input, input_contents in pairs(inputs) do
for line, value in pairs(input_contents) do
model.setGlobalVariable(input - 1, line - 1, value)
end
end
end
function load_mixes()
print("LOAD MIXES")
local line, value, mix_contents, mix
local mixes
mixes = process(read_all("mixes.dat"))
local del_mixes = serialize_model_index_index(model_mix_getter, "")
for mix, mix_contents in pairs(del_mixes) do
print("" .. mix .. "=" .. table_length(mix_contents))
local i = table_length(mix_contents)
while i > 0 do
print("model.deleteMix(" .. mix - 1 .. ", " .. i - 1 .. ")")
model.deleteMix(mix - 1, i - 1)
i = i - 1
end
end
for mix, mix_contents in pairs(mixes) do
for line, value in pairs(mix_contents) do
model.insertMix(mix - 1, line - 1, value)
end
end
end
function load_global_variable_details(f, filename)
print("LOAD " .. filename)
local line, value, input_contents, idx, tab
local inputs = process(read_all(filename))
-- print(array_to_string(serialize_table(process(txt))))
for idx, tab in pairs(inputs) do
f(idx, tab)
end
end
local sel_0 = 0
local sel_1 = 0
local mode = 0
local function maybe_to_upper(b, s)
if (b) then
return string.upper(s)
end
return s
end
local function run(event)
local write_funcs = {
function() return dump_model_index_index("inputs.dat", model_input_getter) end,
function() return dump_model_index_index("mixes.dat", model_mix_getter) end,
function() return dump_model_index("outputs.dat", model_output_getter) end,
function() return dump_model_index("curves.dat", model_curve_getter) end,
function() return dump_model_index_index("global_variable_values.dat", model_global_variable_values_getter) end,
function() return dump_model_index("custom_functions.dat", model_custom_function_getter) end,
function() return dump_model_index("logical_switches.dat", model_logical_switch_getter) end,
function() return dump_model_index("flight_modes.dat", model_flight_mode_getter) end,
function() return dump_model_index("global_variable_details.dat", model_global_variable_details_getter) end,
}
local read_funcs = {
load_inputs,
load_mixes,
function()
return load_global_variable_details(
function(idx, tab) model.setOutput(idx - 1, tab) end,
"outputs.dat"
)
end,
function()
load_global_variable_details(
function(idx, tab)
print("set curve " .. idx)
-- print(dump_table(tab))
local r = model.setCurve(idx - 1, tab)
print("R=" .. r)
end,
"curves.dat"
)
end,
load_global_variable_value,
function()
load_global_variable_details(
function(idx, tab) model.setCustomFunction(idx - 1, tab) end,
"custom_functions.dat"
)
end,
function()
load_global_variable_details(
function(idx, tab) model.setLogicalSwitch(idx - 1, tab) end,
"logical_switches.dat"
)
end,
function()
load_global_variable_details(
function(idx, tab) model.setFlightMode(idx - 1, tab) end,
"flight_modes.dat"
)
end,
function()
load_global_variable_details(
function(idx, tab) model.setGlobalVariableDetails(idx - 1, tab) end,
"global_variable_details.dat"
)
end,
}
local funcs = { write_funcs, read_funcs }
if event == EVT_ENTER_BREAK then
mode = mode + 1
end
if mode == 2 then
print(sel_1 .. "/" .. sel_0)
local f = funcs[sel_1 + 1][sel_0 + 1]
print("call f ++" .. sel_1 .. "/" .. sel_0)
print("t1=" .. table_length(funcs[1]))
print("t2=" .. table_length(funcs[2]))
f()
print("call f --")
return 1
end
if event == EVT_EXIT_BREAK then
if mode == 0 then
return 1
end
if mode == 1 then
mode = 0
end
end
if mode == 0 then
local max_menu = 8
if event == EVT_ROT_LEFT then
sel_0 = sel_0 - 1
end
if event == EVT_ROT_RIGHT then
sel_0 = sel_0 + 1
end
if (sel_0 < 0) then
sel_0 = max_menu
end
if (sel_0 > max_menu) then
sel_0 = 0
end
end
if mode == 1 then
local max_menu = 1
if event == EVT_ROT_LEFT then
sel_1 = sel_1 - 1
end
if event == EVT_ROT_RIGHT then
sel_1 = sel_1 + 1
end
if (sel_1 < 0) then
sel_1 = max_menu
end
if (sel_1 > max_menu) then
sel_1 = 0
end
end
lcd.clear()
lcd.drawText(1, 1, maybe_to_upper(sel_0 == 0, 'inputs'), SMLSIZE)
lcd.drawText(1, 9, maybe_to_upper(sel_0 == 1, 'mixes'), SMLSIZE)
lcd.drawText(1, 18, maybe_to_upper(sel_0 == 2, 'outputs'), SMLSIZE)
lcd.drawText(1, 27, maybe_to_upper(sel_0 == 3, 'curves'), SMLSIZE)
lcd.drawText(1, 36, maybe_to_upper(sel_0 == 4, 'gvar values'), SMLSIZE)
lcd.drawText(64, 1, maybe_to_upper(sel_0 == 5, 'functions'), SMLSIZE)
lcd.drawText(64, 9, maybe_to_upper(sel_0 == 6, 'logical sw'), SMLSIZE)
lcd.drawText(64, 18, maybe_to_upper(sel_0 == 7, 'flight modes'), SMLSIZE)
lcd.drawText(64, 27, maybe_to_upper(sel_0 == 8, 'gvar details'), SMLSIZE)
lcd.drawText(12, 55, maybe_to_upper((mode == 1) and (sel_1 == 0), '[export]'), SMLSIZE)
lcd.drawText(76, 55, maybe_to_upper((mode == 1) and (sel_1 == 1), '[import]'), SMLSIZE)
-- todo: timer
return 0
end
run()
return {init = init, run = run}