Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.55 KB | None | 0 0
  1.  
  2. if warpdriveCommons then os.unloadAPI("warpdriveCommons") end
  3. if not os.loadAPI("warpdrive/warpdriveCommons") then error("missing warpdriveCommons") end
  4. local w = warpdriveCommons.w
  5.  
  6. local data
  7.  
  8. ----------- Force field support
  9.  
  10. local ffield_projectorAddresses = {}
  11. local ffield_projectors = {}
  12. local ffield_projector_indexSelected = 1
  13. local ffield_projector_indexFirstLine = 1
  14. local ffield_projector_lines = 10
  15.  
  16. local ffield_relayAddresses = {}
  17. local ffield_relays = {}
  18. local ffield_relay_indexSelected = 1
  19. local ffield_relay_indexFirstLine = 1
  20. local ffield_relay_lines = 10
  21.  
  22. function ffield_boot(isDetailed)
  23. if #ffield_projectorAddresses == 0 and #ffield_relayAddresses == 0 then
  24. return
  25. end
  26.  
  27. if isDetailed == nil then
  28. isDetailed = true
  29. end
  30.  
  31. if isDetailed then
  32. w.write("Booting Force field projectors and relays")
  33.  
  34. w.writeLn("...")
  35. w.sleep(0.1)
  36. end
  37.  
  38. -- getting projectors parameters
  39. ffield_projectors = {}
  40. for key, address in pairs(ffield_projectorAddresses) do
  41. local device = w.device_get(address)
  42. local x, y, z = device.getLocalPosition()
  43. local beamFrequency = device.beamFrequency()
  44. -- local isEnabled = device.enable()
  45. local status, isEnabled, isConnected, isPowered, shape, energy = device.state()
  46. -- @TODO add tier reporting
  47. local projector = {
  48. address = address,
  49. device = device,
  50. position = { x = x, y = y, z = z },
  51. name = "-not defined-",
  52. beamFrequency = beamFrequency,
  53. shape = shape,
  54. isEnabled = isEnabled }
  55. if isDetailed then
  56. w.writeLn(ffield_projector_getDescription(projector))
  57. end
  58. table.insert(ffield_projectors, projector)
  59. end
  60.  
  61. -- getting relays parameters
  62. ffield_relays = {}
  63. for key, address in pairs(ffield_relayAddresses) do
  64. local device = w.device_get(address)
  65. local x, y, z = device.getLocalPosition()
  66. local beamFrequency = device.beamFrequency()
  67. local isEnabled = device.enable()
  68. local relay = {
  69. address = address,
  70. device = device,
  71. position = { x = x, y = y, z = z },
  72. name = "-not defined-",
  73. beamFrequency = beamFrequency,
  74. isEnabled = isEnabled }
  75. if isDetailed then
  76. w.writeLn(ffield_relay_getDescription(relay))
  77. end
  78. table.insert(ffield_relays, relay)
  79. end
  80. end
  81.  
  82. function ffield_save()
  83. -- nothing
  84. end
  85.  
  86. function ffield_read(parData)
  87. data = parData
  88. end
  89.  
  90. function ffield_projector_getDescription(projector)
  91. if projector == nil or projector.device == nil then
  92. return "~invalid~"
  93. end
  94. local description = "#" .. w.format_integer(projector.beamFrequency, 5)
  95. .. " @ (" .. w.format_integer(projector.position.x, 7) .. " " .. w.format_integer(projector.position.y, 3) .. " " .. w.format_integer(projector.position.z, 7) .. ") "
  96. .. w.format_string(projector.shape, 10)
  97. .. " "
  98. if projector.isEnabled then
  99. description = description .. "Enabled"
  100. else
  101. description = description .. "Disabled"
  102. end
  103. return description
  104. end
  105.  
  106. function ffield_projector_getIndexes()
  107. if ffield_projectors ~= nil then
  108. if ffield_projector_indexSelected > #ffield_projectors then
  109. ffield_projector_indexSelected = 1
  110. elseif ffield_projector_indexSelected < 1 then
  111. ffield_projector_indexSelected = #ffield_projectors
  112. end
  113. if ffield_projector_indexFirstLine > ffield_projector_indexSelected then
  114. ffield_projector_indexFirstLine = ffield_projector_indexSelected
  115. elseif ffield_projector_indexFirstLine + ffield_projector_lines < ffield_projector_indexSelected then
  116. ffield_projector_indexFirstLine = ffield_projector_indexSelected - ffield_projector_lines
  117. end
  118. return ffield_projector_indexFirstLine, ffield_projector_indexSelected
  119. else
  120. return 1, 1
  121. end
  122. end
  123.  
  124. function ffield_projector_get(index)
  125. local indexToUse = index
  126. local projector
  127.  
  128. if ffield_projectors ~= nil then
  129. if indexToUse > #ffield_projectors then
  130. indexToUse = 1
  131. elseif indexToUse < 1 then
  132. indexToUse = #ffield_projectors
  133. end
  134. projector = ffield_projectors[indexToUse]
  135. end
  136.  
  137. if projector == nil then
  138. ffield_boot(false)
  139. w.status_showWarning("Invalid projector index " .. index)
  140. projector = {
  141. address = "-",
  142. device = nil,
  143. position = { x = 0, y = 0, z = 0 },
  144. name = "-",
  145. beamFrequency = -1,
  146. shape = "NONE",
  147. isEnabled = false }
  148. end
  149.  
  150. return projector
  151. end
  152.  
  153. function ffield_projector_getSelected()
  154. return ffield_projector_get(ffield_projector_indexSelected)
  155. end
  156.  
  157. function ffield_enable(projectorOrRelay, enable)
  158. if projectorOrRelay == nil or projectorOrRelay.device == nil then
  159. return
  160. end
  161. local enableToApply = enable
  162. if enableToApply == nil then
  163. enableToApply = not projectorOrRelay.device.enable()
  164. end
  165. projectorOrRelay.isEnabled = projectorOrRelay.device.enable(enableToApply)
  166. return projectorOrRelay.isEnabled
  167. end
  168.  
  169. function ffield_projector_key(character, keycode)
  170. if character == 's' or character == 'S' then
  171. for key, projector in pairs(ffield_projectors) do
  172. ffield_enable(projector, true)
  173. end
  174. return true
  175. elseif character == 'p' or character == 'P' then
  176. for key, projector in pairs(ffield_projectors) do
  177. ffield_enable(projector, false)
  178. end
  179. return true
  180. elseif character == 'e' or character == 'E' then
  181. local projector = ffield_projector_getSelected()
  182. if projector ~= nil and projector.device ~= nil then
  183. ffield_enable(projector)
  184. end
  185. return true
  186. elseif character == 'c' or character == 'C' then -- C or keycode == 46
  187. ffield_projector_config()
  188. w.data_save()
  189. return true
  190. elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or -
  191. ffield_projector_indexSelected = ffield_projector_indexSelected - 1
  192. return true
  193. elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or +
  194. ffield_projector_indexSelected = ffield_projector_indexSelected + 1
  195. return true
  196. end
  197. return false
  198. end
  199.  
  200. function ffield_projector_page()
  201. w.page_begin(w.data_getName() .. " - Force field projectors")
  202.  
  203. -- w.setCursorPos(1, 2)
  204. if #ffield_projectors == 0 then
  205. w.setColorDisabled()
  206. w.writeCentered(2, "No force field projector defined, connect one and reboot!")
  207. else
  208. w.setColorNormal()
  209. local indexFirstLine, indexSelected = ffield_projector_getIndexes()
  210. w.writeCentered(2, "Force field projector " .. indexSelected .. " of " .. #ffield_projectors .. " is selected")
  211. local indexLastLine = math.min(indexFirstLine + ffield_projector_lines, #ffield_projectors)
  212. for indexCurrent = indexFirstLine, indexLastLine do
  213. if indexCurrent == indexSelected then
  214. w.setColorSelected()
  215. w.clearLine()
  216. w.write(">")
  217. else
  218. w.setColorNormal()
  219. w.write(" ")
  220. end
  221. local projector = ffield_projector_get(indexCurrent)
  222. local description = ffield_projector_getDescription(projector)
  223. w.write(description)
  224. w.writeLn("")
  225. end
  226. end
  227.  
  228. w.setCursorPos(1, 14)
  229. w.setColorNormal()
  230. w.write(" -----------------------------------------------")
  231.  
  232. w.setCursorPos(1, 15)
  233. w.setColorControl()
  234. w.writeFullLine(" Start/stoP all force field projectors (S/P)")
  235. w.writeFullLine(" Configure (C) or togglE (E) selected projector")
  236. w.writeFullLine(" select force field projector (Up, Down)")
  237. end
  238.  
  239. function ffield_projector_config()
  240. local projector = ffield_projector_getSelected()
  241. if projector == nil then
  242. return
  243. end
  244. w.page_begin(w.data_getName() .. " - Projector configuration")
  245.  
  246. w.setCursorPos(1, 2)
  247. w.setColorNormal()
  248. projector.position.x, projector.position.y, projector.position.z = projector.device.getLocalPosition()
  249. w.write("Projector @ " .. w.format_integer(projector.position.x, 7) .. " " .. w.format_integer(projector.position.y, 3) .. " " .. w.format_integer(projector.position.z, 7))
  250.  
  251. -- name
  252. w.setCursorPos(1, 4)
  253. w.setColorHelp()
  254. w.writeFullLine(" Press enter to validate.")
  255. w.setCursorPos(1, 3)
  256. w.setColorNormal()
  257. w.writeLn("Name (" .. projector.name .. "):")
  258. projector.name = w.input_readText(projector.name)
  259. w.setCursorPos(1, 3)
  260. w.clearLine()
  261. w.writeLn("Name set to " .. projector.name)
  262. w.clearLine()
  263.  
  264. w.setColorDisabled()
  265.  
  266. -- beam frequency
  267. projector.beamFrequency = projector.device.beamFrequency()
  268. w.setCursorPos(1, 6)
  269. w.setColorHelp()
  270. w.writeFullLine(" Enter a number between 0 and 65000.")
  271. local frequency
  272. repeat
  273. w.setCursorPos(1, 5)
  274. w.setColorNormal()
  275. w.clearLine()
  276. w.write("Beam frequency (" .. w.format_integer(projector.beamFrequency, 5) .. "): ")
  277. frequency = w.input_readInteger(projector.beamFrequency)
  278. if frequency ~= 0 and (frequency < 0 or frequency > 65000) then
  279. w.status_showWarning("This is not a valid beam frequency. Try again.")
  280. end
  281. until frequency > 0 and frequency <= 65000
  282. w.setCursorPos(1, 4)
  283. w.clearLine()
  284. projector.beamFrequency = projector.device.beamFrequency(frequency)
  285. w.write("Beam frequency set to " .. projector.beamFrequency)
  286. w.setCursorPos(1, 5)
  287. w.clearLine()
  288. w.setCursorPos(1, 6)
  289. w.clearLine()
  290.  
  291. -- translation
  292. ffield_config_xyz(5, "Translation", "%", "translation where 0 is centered", projector.device.translation, 100)
  293.  
  294. -- rotation
  295. ffield_config_xyz(6, "Rotation in deg", "", "rotation where 0 is centered", projector.device.rotation, 1)
  296.  
  297. -- scale
  298. ffield_config_xyz(7, "Min scale", "%", "min scale where -100 is full scale", projector.device.min, 100)
  299. ffield_config_xyz(8, "Max scale", "%", "max scale where 100 is full scale", projector.device.max, 100)
  300. end
  301.  
  302. function ffield_config_xyz(yCursor, title, unit, help, method, factor)
  303. w.setCursorPos(1, yCursor + 1)
  304. local x, y, z = method()
  305. x = factor * x
  306. y = factor * y
  307. z = factor * z
  308. w.write(title .. " is currently set to " .. w.format_integer(x, 4) .. unit .. " " .. w.format_integer(y, 4) .. unit .. " " .. w.format_integer(z, 4) .. unit)
  309.  
  310. w.setCursorPos(1, 16)
  311. w.setColorHelp()
  312. w.writeFullLine(" Enter X " .. help)
  313.  
  314. w.setCursorPos(1, yCursor + 3)
  315. w.setColorNormal()
  316. w.write(title .. " along X axis (" .. w.format_integer(x) .. "): ")
  317. x = w.input_readInteger(x)
  318.  
  319. w.setCursorPos(1, 16)
  320. w.setColorHelp()
  321. w.writeFullLine(" Enter Y " .. help)
  322.  
  323. w.setCursorPos(1, yCursor + 4)
  324. w.setColorNormal()
  325. w.write(title .. " along Y axis (" .. w.format_integer(y) .. "): ")
  326. y = w.input_readInteger(y)
  327.  
  328. w.setCursorPos(1, 16)
  329. w.setColorHelp()
  330. w.writeFullLine(" Enter Z " .. help)
  331.  
  332. w.setCursorPos(1, yCursor + 5)
  333. w.setColorNormal()
  334. w.write(title .. " along Z axis (" .. w.format_integer(z) .. "): ")
  335. z = w.input_readInteger(z)
  336.  
  337. w.setCursorPos(1, 16)
  338. w.clearLine()
  339.  
  340. x, y, z = method(x / factor, y / factor, z / factor)
  341. x = factor * x
  342. y = factor * y
  343. z = factor * z
  344. w.setCursorPos(1, yCursor)
  345. w.setColorNormal()
  346. w.write(title .. " set to " .. w.format_integer(x, 4) .. unit .. " " .. w.format_integer(y, 4) .. unit .. " " .. w.format_integer(z, 4) .. unit)
  347. w.setCursorPos(1, yCursor + 1)
  348. w.clearLine()
  349. w.setCursorPos(1, yCursor + 3)
  350. w.clearLine()
  351. w.setCursorPos(1, yCursor + 4)
  352. w.clearLine()
  353. w.setCursorPos(1, yCursor + 5)
  354. w.clearLine()
  355. end
  356.  
  357. function ffield_relay_getDescription(relay)
  358. if relay == nil or relay.device == nil then
  359. return "~invalid~"
  360. end
  361. local description = "#" .. w.format_integer(relay.beamFrequency, 5)
  362. .. " @ (" .. w.format_integer(relay.position.x, 7) .. " " .. w.format_integer(relay.position.y, 3) .. " " .. w.format_integer(relay.position.z, 7) .. ") "
  363. if relay.isEnabled then
  364. description = description .. "Enabled"
  365. else
  366. description = description .. "Disabled"
  367. end
  368. return description
  369. end
  370.  
  371. function ffield_relay_getIndexes()
  372. if ffield_relays ~= nil then
  373. if ffield_relay_indexSelected > #ffield_relays then
  374. ffield_relay_indexSelected = 1
  375. elseif ffield_relay_indexSelected < 1 then
  376. ffield_relay_indexSelected = #ffield_relays
  377. end
  378. if ffield_relay_indexFirstLine > ffield_relay_indexSelected then
  379. ffield_relay_indexFirstLine = ffield_relay_indexSelected
  380. elseif ffield_relay_indexFirstLine + ffield_relay_lines < ffield_relay_indexSelected then
  381. ffield_relay_indexFirstLine = ffield_relay_indexSelected - ffield_relay_lines
  382. end
  383. return ffield_relay_indexFirstLine, ffield_relay_indexSelected
  384. else
  385. return 1, 1
  386. end
  387. end
  388.  
  389. function ffield_relay_get(index)
  390. local indexToUse = index
  391. local relay
  392.  
  393. if ffield_relays ~= nil then
  394. if indexToUse > #ffield_relays then
  395. indexToUse = 1
  396. elseif indexToUse < 1 then
  397. indexToUse = #ffield_relays
  398. end
  399. relay = ffield_relays[indexToUse]
  400. end
  401.  
  402. if relay == nil then
  403. ffield_boot(false)
  404. w.status_showWarning("Invalid relay index " .. index)
  405. relay = {
  406. address = "-",
  407. device = nil,
  408. position = { x = 0, y = 0, z = 0 },
  409. name = "-",
  410. beamFrequency = -1,
  411. isEnabled = false }
  412. end
  413.  
  414. return relay
  415. end
  416.  
  417. function ffield_relay_getSelected()
  418. return ffield_relay_get(ffield_relay_indexSelected)
  419. end
  420.  
  421. function ffield_relay_key(character, keycode)
  422. if character == 's' or character == 'S' then
  423. for key, relay in pairs(ffield_relays) do
  424. ffield_enable(relay, true)
  425. end
  426. return true
  427. elseif character == 'p' or character == 'P' then
  428. for key, relay in pairs(ffield_relays) do
  429. ffield_enable(relay, false)
  430. end
  431. return true
  432. elseif character == 'e' or character == 'E' then
  433. local relay = ffield_relay_getSelected()
  434. ffield_enable(relay)
  435. return true
  436. elseif character == 'c' or character == 'C' then -- C or keycode == 46
  437. ffield_relay_config()
  438. w.data_save()
  439. return true
  440. elseif keycode == 200 or keycode == 203 or character == '-' then -- Up or Left or -
  441. ffield_relay_indexSelected = ffield_relay_indexSelected - 1
  442. return true
  443. elseif keycode == 208 or keycode == 205 or character == '+' then -- Down or Right or +
  444. ffield_relay_indexSelected = ffield_relay_indexSelected + 1
  445. return true
  446. end
  447. return false
  448. end
  449.  
  450. function ffield_relay_page()
  451. w.page_begin(w.data_getName() .. " - Force field relays")
  452.  
  453. -- w.setCursorPos(1, 2)
  454. if #ffield_relays == 0 then
  455. w.setColorDisabled()
  456. w.writeCentered(2, "No force field relay defined, connect one and reboot!")
  457. else
  458. w.setColorNormal()
  459. local indexFirstLine, indexSelected = ffield_relay_getIndexes()
  460. w.writeCentered(2, "Force field relay " .. indexSelected .. " of " .. #ffield_relays .. " is selected")
  461. local indexLastLine = math.min(indexFirstLine + ffield_relay_lines, #ffield_relays)
  462. for indexCurrent = indexFirstLine, indexLastLine do
  463. if indexCurrent == indexSelected then
  464. w.setColorSelected()
  465. w.clearLine()
  466. w.write(">")
  467. else
  468. w.setColorNormal()
  469. w.write(" ")
  470. end
  471. local relay = ffield_relay_get(indexCurrent)
  472. local description = ffield_relay_getDescription(relay)
  473. w.write(description)
  474. w.writeLn("")
  475. end
  476. end
  477.  
  478. w.setCursorPos(1, 14)
  479. w.setColorNormal()
  480. w.write(" -----------------------------------------------")
  481.  
  482. w.setCursorPos(1, 15)
  483. w.setColorControl()
  484. w.writeFullLine(" Start/stoP all force field relays (S/P)")
  485. w.writeFullLine(" Configure (C) or togglE (E) selected relay")
  486. w.writeFullLine(" select force field relay (Up, Down)")
  487. end
  488.  
  489. function ffield_relay_config()
  490. local relay = ffield_relay_getSelected()
  491. if relay == nil then
  492. return
  493. end
  494. w.page_begin(w.data_getName() .. " - Relay configuration")
  495.  
  496. w.setCursorPos(1, 2)
  497. w.setColorNormal()
  498. relay.position.x, relay.position.y, relay.position.z = relay.device.getLocalPosition()
  499. w.write("Relay @ " .. w.format_integer(relay.position.x, 7) .. " " .. w.format_integer(relay.position.y, 3) .. " " .. w.format_integer(relay.position.z, 7))
  500.  
  501. -- name
  502. w.setCursorPos(1, 4)
  503. w.setColorHelp()
  504. w.writeFullLine(" Press enter to validate.")
  505. w.setCursorPos(1, 3)
  506. w.setColorNormal()
  507. w.writeLn("Name (" .. relay.name .. "):")
  508. relay.name = w.input_readText(relay.name)
  509. w.setCursorPos(1, 3)
  510. w.clearLine()
  511. w.writeLn("Name set to " .. relay.name)
  512. w.clearLine()
  513.  
  514. w.setColorDisabled()
  515.  
  516. -- beam frequency
  517. relay.beamFrequency = relay.device.beamFrequency()
  518. w.setCursorPos(1, 6)
  519. w.setColorHelp()
  520. w.writeFullLine(" Enter a number between 0 and 65000.")
  521. local frequency
  522. repeat
  523. w.setCursorPos(1, 5)
  524. w.setColorNormal()
  525. w.clearLine()
  526. w.write("Beam frequency (" .. w.format_integer(relay.beamFrequency, 5) .. "): ")
  527. frequency = w.input_readInteger(relay.beamFrequency)
  528. if frequency ~= 0 and (frequency < 0 or frequency > 65000) then
  529. w.status_showWarning("This is not a valid beam frequency. Try again.")
  530. end
  531. until frequency > 0 and frequency <= 65000
  532. w.setCursorPos(1, 4)
  533. w.clearLine()
  534. relay.beamFrequency = relay.device.beamFrequency(frequency)
  535. w.write("Beam frequency set to " .. relay.beamFrequency)
  536. w.setCursorPos(1, 5)
  537. w.clearLine()
  538. w.setCursorPos(1, 6)
  539. w.clearLine()
  540. end
  541.  
  542. function ffield_register()
  543. w.device_register("warpdriveForceFieldProjector",
  544. function(deviceType, address, wrap) table.insert(ffield_projectorAddresses, address) end,
  545. function() end)
  546. w.device_register("warpdriveForceFieldRelay",
  547. function(deviceType, address, wrap) table.insert(ffield_relayAddresses, address) end,
  548. function() end)
  549. w.data_register("ffield", ffield_read, ffield_save, nil)
  550. end
  551.  
  552. ----------- connections status
  553.  
  554. function connections_page(isBooting)
  555. w.page_begin(w.data_getName() .. " - Connections")
  556.  
  557. w.writeLn("")
  558.  
  559. local monitors = w.device_getMonitors()
  560. if #monitors == 0 then
  561. w.setColorDisabled()
  562. w.writeLn("No Monitor detected")
  563. elseif #monitors == 1 then
  564. w.setColorSuccess()
  565. w.writeLn("1 monitor detected")
  566. else
  567. w.setColorSuccess()
  568. w.writeLn(#monitors .. " Monitors detected")
  569. end
  570.  
  571. if #ffield_projectorAddresses == 0 then
  572. w.setColorDisabled()
  573. w.writeLn("No force field projector detected")
  574. elseif #ffield_projectorAddresses == 1 then
  575. w.setColorSuccess()
  576. w.writeLn("1 force field projector detected")
  577. else
  578. w.setColorSuccess()
  579. w.writeLn(#ffield_projectorAddresses .. " force field projectors detected")
  580. end
  581.  
  582. if #ffield_relayAddresses == 0 then
  583. w.setColorDisabled()
  584. w.writeLn("No force field relay detected")
  585. elseif #ffield_relayAddresses == 1 then
  586. w.setColorSuccess()
  587. w.writeLn("1 force field relay detected")
  588. else
  589. w.setColorSuccess()
  590. w.writeLn(#ffield_relayAddresses .. " force field relays detected")
  591. end
  592.  
  593. if isBooting then
  594. ffield_boot()
  595. end
  596.  
  597. w.writeLn("")
  598. w.setColorNormal()
  599. w.writeLn("This is a keyboard controlled user interface.")
  600. w.write("Key controls are written like so: ")
  601. w.setColorControl()
  602. w.write("Action (key)")
  603. w.setColorNormal()
  604. w.writeLn(".")
  605. w.write("For example, typing ")
  606. w.setColorControl()
  607. w.write(" 1 ")
  608. w.setColorNormal()
  609. w.writeLn(" will open Force field projectors.")
  610. end
  611.  
  612. ----------- Boot sequence
  613.  
  614. w.page_setEndText(" Home (0), Projectors (1), Relays (2)")
  615. w.page_register('0', connections_page, nil)
  616. w.page_register('1', ffield_projector_page, ffield_projector_key)
  617. w.page_register('2', ffield_relay_page, ffield_relay_key)
  618. ffield_register()
  619.  
  620. w.boot()
  621. w.run()
  622. w.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement