-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanix.lua
349 lines (298 loc) · 11.5 KB
/
nanix.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
local nano = require("nano")
local gui = require("guix").create()
local serialization = require("serialization")
local su = require("superUtiles")
local fs = require("filesystem")
local unicode = require("unicode")
--------------------------------------------
local stateScene = gui.createScene(gui.selectColor(0x222222, nil, false), gui.maxX, gui.maxY)
local main = gui.createScene(gui.selectColor(0x222222, nil, false), gui.maxX, gui.maxY)
local noConnectScene = gui.createScene(gui.selectColor(0x222222, nil, false), gui.maxX, gui.maxY)
local connection
local label = noConnectScene.createLabel((noConnectScene.sizeX // 2) - (24 // 2), noConnectScene.sizeY // 3, 24, 1, "нету подключения к нанитам")
label.backColor = 0xAAAAAA
label.foreColor = 0
local refreshButton = noConnectScene.createButton((noConnectScene.sizeX // 2) - 8, (noConnectScene.sizeY // 3) * 2, 16, 1, "refresh", function()
connection(true)
end)
refreshButton.backColor = gui.selectColor(0xAAAAAA, nil, true)
refreshButton.foreColor = gui.selectColor(0, nil, false)
refreshButton.invertBackColor = refreshButton.foreColor
refreshButton.invertForeColor = refreshButton.backColor
local stateLabel = stateScene.createLabel(1, stateScene.sizeY // 2, stateScene.sizeX, 1, "")
local oldStatusScene
local function status(text)
stateLabel.text = text
if gui.scene ~= stateScene then
oldStatusScene = gui.scene
gui.select(stateScene)
else
stateLabel.draw()
end
end
local function statusOff()
if oldStatusScene then
gui.select(oldStatusScene)
oldStatusScene = nil
end
end
local rx, ry = gui.gpu.getResolution()
local listsStart = rx - 45
--------------------------------------------
local connectOldScene, inputsCount, isConnection
local function noConnection()
isConnection = false
if gui.scene ~= noConnectScene then
connectOldScene = gui.scene
gui.select(noConnectScene)
while not isConnection do
os.sleep(0.5)
end
end
end
local function nanoCall(...)
::start::
local dat = {pcall(...)}
if not dat[1] then
if type(dat[2]) == "table" and dat[2].reason == "terminated" then
gui.exit()
end
noConnection()
goto start
end
return table.unpack(dat, 2)
end
function connection(back)
status("подключения")
local ok, inputs2 = pcall(nano.getTotalInputCount)
--statusOff()
if ok then
inputsCount = math.floor(inputs2)
isConnection = true
if back then gui.select(connectOldScene or main) end
else
noConnection()
end
end
gui.start()
connection()
--------------------------------------------
local cfg = {pins = {}, notes = {}}
for i = 1, inputsCount do
table.insert(cfg.pins, false)
table.insert(cfg.notes, false)
end
local cfgPath = "/etc/nano4312.cfg"
local function saveCfg()
su.saveFile(cfgPath, assert(serialization.serialize(cfg)))
end
if fs.exists(cfgPath) then
cfg = assert(serialization.unserialize(assert(su.getFile(cfgPath))))
else
saveCfg()
end
--------------------------------------------
local windowCount = 0
local function winAdd()
windowCount = windowCount + 1
gui.redrawAll = true
end
local function winRemove()
windowCount = windowCount - 1
if windowCount < 0 then windowCount = 0 end
if windowCount == 0 then gui.redrawAll = false end
end
--------------------------------------------
local updateLabels
local buttons = {}
for i = 1, inputsCount do
local b
b = main.createButton(1, i + 2, listsStart - 2, 1, "", function(state, oldState, button)
if button == 1 then
winAdd()
b.state = oldState
b.draw()
local window = main.createWindow(math.floor(gui.lastTouch[3]), math.floor(gui.lastTouch[4]), 40, 10)
window.userMove = true
window.color = 0xAAAAAA
local closeButton = main.createButton(1, 1, 8, 1, "close", function()
winRemove()
window.remove()
gui.redraw()
end)
closeButton.backColor = 0xFF0000
closeButton.foreColor = 0xFFFFFF
window.attachObj(1, 1, closeButton)
local noteLabel = main.createLabel(1, 1, window.sizeX, 1)
noteLabel.foreColor = 0xFFFFFF
noteLabel.backColor = 0x222222
window.attachObj(1, 5, noteLabel)
local mainNoteLabel = main.createLabel(1, 1, window.sizeX, 1, "управления заметкой к пину " .. tostring(i))
mainNoteLabel.foreColor = 0xFFFFFF
mainNoteLabel.backColor = 0x222222
window.attachObj(1, 3, mainNoteLabel)
local function updateText()
noteLabel.text = cfg.notes[i] or "отсутствует"
noteLabel.draw()
end
updateText()
local input = main.createInputbox(1, 1, 18, 1, "новая заметка", function(read)
cfg.notes[i] = read
saveCfg()
updateText()
updateLabels()
gui.redraw()
end)
input.backColor = 0xFFFFFF
input.foreColor = 0x222222
input.viewData = false
window.attachObj(2, 9, input)
local deleteNote = main.createButton(15, 1, 18, 1, "удалить заметку", function()
cfg.notes[i] = false
saveCfg()
updateText()
updateLabels()
gui.redraw()
end)
deleteNote.backColor = 0xFFFFFF
deleteNote.foreColor = 0x222222
window.attachObj(window.sizeX - 19, 9, deleteNote)
gui.redraw()
else
status("sending information")
nanoCall(nano.setInput, i, state)
cfg.pins[i] = state
saveCfg()
gui.select(main)
end
end, 1, cfg.pins[i])
table.insert(buttons, b)
b.backColor = gui.selectColor(0xAAAAAA, nil, true)
b.foreColor = gui.selectColor(0, nil, false)
b.invertBackColor = b.foreColor
b.invertForeColor = b.backColor
end
function updateLabels()
for i = 1, #buttons do
buttons[i].text = "pin: " .. tostring(i) .. ((cfg.notes[i] and (", note: " .. cfg.notes[i])) or "")
end
end
updateLabels()
local function createStr(str)
str = unicode.sub(str, 2, unicode.len(str) - 1)
local strs = {}
for _, dat in ipairs(su.split(str, ",")) do
local _, d = table.unpack(su.split(dat, "."))
table.insert(strs, d)
end
str = table.concat(strs, ", ")
str = unicode.sub(str, 1, buttons[1].sizeX - 17)
if str == "" then return false end
return str
end
main.createButton(1, 1, 16, 1, "full upload", function()
for i = 1, inputsCount do
status("sending information (pin " .. tostring(i) .. ")")
nanoCall(nano.setInput, i, cfg.pins[i])
end
gui.select(main)
end)
main.createButton(18, 1, 16, 1, "download data", function()
for i = 1, inputsCount do
status("getting information (pin " .. tostring(i) .. "/" .. tostring(inputsCount) .. ")")
cfg.pins[i] = nanoCall(nano.getInput, i)
buttons[i].state = cfg.pins[i]
end
saveCfg()
gui.select(main)
end)
main.createButton(18 + 17, 1, 16, 1, "nscan", function()
for i = 1, inputsCount do
status("sending information (pin " .. tostring(i) .. "/" .. tostring(inputsCount) .. ")")
if cfg.pins[i] then nanoCall(nano.setInput, i, false) end
end
for i = 1, inputsCount do
status("getting information (pin " .. tostring(i) .. "/" .. tostring(inputsCount) .. ")")
nanoCall(nano.setInput, i, true)
cfg.notes[i] = createStr(nanoCall(nano.getActiveEffects))
nanoCall(nano.setInput, i, false)
end
for i = 1, inputsCount do
status("sending information (pin " .. tostring(i) .. "/" .. tostring(inputsCount) .. ")")
if cfg.pins[i] then nanoCall(nano.setInput, i, true) end
end
saveCfg()
updateLabels()
gui.select(main)
end)
local list = main.createList(listsStart, 3, main.sizeX - listsStart, main.sizeY - 3 - 12)
list.autoScroll = false
list.autoRedraw = false
list.autoRemove = false
list.setSoundOn(false)
list.addStr("ИНСТРУКЦИЯ(обязательна к прочтению)")
list.addStr("если вам кажиться что произошел рассинхрон -")
list.addStr("вам не кажеться")
list.addStr(string.rep("-", list.sizeX))
list.addStr("в случаи рассинхрона необходи нажать")
list.addStr("на кнопку download data")
list.addStr("чтобы скачать все данные,")
list.addStr("или нажать full upload чтобы")
list.addStr("отправить все")
list.addStr(string.rep("-", list.sizeX))
list.addStr("все пины сохраняються на диск")
list.addStr("для изменения состояния нажимайте на кнопки")
list.addStr(string.rep("-", list.sizeX))
list.addStr("если программа не будет открываться удалите")
list.addStr("конфигурационный файл")
list.addStr("(" .. cfgPath .. ") и нажмите download data")
list.addStr(string.rep("-", list.sizeX))
list.addStr("во время передачи данных нельзя ходить,")
list.addStr("иначе программа")
list.addStr("может зависнуть, если это произошло, ")
list.addStr("закройте ее сочетаниям клавиш")
list.addStr("ctrl + alt + c и повторите действия")
list.addStr(string.rep("-", list.sizeX))
list.addStr("для упровления заметкой на пин")
list.addStr("нажмите на его кнопку правой")
list.addStr("кнопкой мыши")
local dataList = main.createList(listsStart, list.posY + list.sizeY + 1, list.sizeX, 9)
dataList.autoRedraw = false
dataList.autoRemove = false
dataList.setSoundOn(false)
local function refreshData()
status("пожалуйста подождите")
dataList.clear()
local name = tostring(nanoCall(nano.getName))
if name then
dataList.addStr("nikname: " .. name)
end
local power = tostring(nanoCall(nano.getPowerState))
if power then
dataList.addStr("power: " .. power)
end
local health = tostring(nanoCall(nano.getHealth))
if health then
dataList.addStr("health: " .. health)
end
local effects = nanoCall(nano.getActiveEffects)
if effects then
if type(effects) == "table" then effects = serialization.serialize(effects) end
dataList.addStr("effects: " .. (createStr(effects) or "none"))
end
local selfInputs = tostring(nanoCall(nano.getSafeActiveInputs))
if selfInputs then
dataList.addStr("self inputs count: " .. selfInputs)
end
local maxInputs = tostring(nanoCall(nano.getMaxActiveInputs))
if maxInputs then
dataList.addStr("max inputs count: " .. maxInputs)
end
statusOff()
dataList.draw()
end
main.createButton(listsStart, main.sizeY - 1, list.sizeX, 1, "refresh", refreshData)
refreshData()
--------------------------------------------
gui.select(main)
gui.run()