Advertisement
samuelask

Tablet

Mar 8th, 2025 (edited)
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.92 KB | None | 0 0
  1. -- Tablet UI Script for Kino Control
  2. local component = require("component")
  3. local thread = require("thread")
  4. local event = require("event")
  5. local term = require("term")
  6. local serialization = require("serialization")
  7. local filesystem = require("filesystem")
  8. local modem = component.modem
  9. local gpu = component.gpu
  10. running = true
  11. -- Configuration
  12. local Version = "1.0.0"
  13. local kino_code = "kino.lua"
  14. local addressFile = "/home/modem_addresses.json"
  15. local kino_address, tablet_address, base_address
  16. local fine_motor_control = false -- Default: Disabled
  17. local scanning = false
  18. local kino_nopower = false
  19. local manual_scan = false
  20. local moving = false
  21. local confirmed = false
  22. modem.open(123)
  23.  
  24. -- Function to load addresses from JSON file
  25. local function load_addresses()
  26.     if filesystem.exists(addressFile) then
  27.         local file = io.open(addressFile, "r")
  28.         if file then
  29.             local content = file:read("*a")
  30.             file:close()
  31.             local data = serialization.unserialize(content)
  32.             if data then
  33.                 kino_address = data.kino_address
  34.                 tablet_address = data.tablet_address
  35.                 base_address = data.base_adress
  36.                 print("[✔] Loaded addresses: Kino = " .. kino_address .. ", Tablet = " .. tablet_address)
  37.             else
  38.                 print("[❌] ERROR: Failed to parse address file.")
  39.             end
  40.         else
  41.             print("[❌] ERROR: Could not open address file.")
  42.         end
  43.     else
  44.         print("[⚠] Address file not found. Communication may not work.")
  45.     end
  46. end
  47. -- UI Drawing Function
  48. local function draw_ui()
  49.     term.clear()
  50.     print("Kino Control Tablet")
  51.     print("[W] Forward  [S] Backward")
  52.     print("[A] Left     [D] Right")
  53.     print("[Q] Up       [E] Down")
  54.     print("[1] Toggle Normal/Manual Scan [" .. (manual_scan and "MANUAL" or "NORMAL") .. "]")
  55.     print("[SPACE] " .. (scanning and "Stop Scan [SCANNING]" or "Start Scan"))
  56.      print("[F] Toggle 4 Block Movement [" .. (fine_motor_control and "ON" or "OFF") .. "]")
  57.      print("[P] Shutdown")
  58.     if kino_nopower then
  59.         for i = 5, 1, -1 do
  60.             term.clear()
  61.             print("Kino Control Tablet")
  62.             print("[⚠] Kino ran out of power, shutting down in " .. i .. " seconds")
  63.             os.sleep(1)
  64.         end
  65.         running = false
  66.     end
  67. end
  68. local function move_ui()
  69.     term.clear()
  70.     print("Kino Control Tablet")
  71.     print("[W] Forward  [S] Backward")
  72.     print("[A] Left     [D] Right")
  73.     print("[Q] Up       [E] Down")
  74.     print("[P] Shutdown")
  75.     print("")
  76.     print(confirmed and "" or "Move the kino 1 block away from the event horizon")
  77.     print(confirmed and "Move the kino inside the gate to start scanning" or "When you are done confirm by pressing [SPACE]")
  78.     print(confirmed and "If you move it the wrong way, restart and try again. Otherwise just wait." or "")
  79.     print(confirmed and "The kino will reboot on the other side and start scanning, keep your tablet up until this message disappears." or "")
  80. end
  81. -- Function to handle incoming modem messages
  82. local function handle_modem_message(_, _, from, port, _, message)
  83.     if from == kino_address and port == 123 then
  84.         if message == "finish" then
  85.             confirmed = false
  86.             scanning = false
  87.             draw_ui()
  88.             print("[✔] Kino has completed its scan successfully.")
  89.         elseif message == "shutdownenergy" then
  90.             confirmed = false
  91.             scanning = false
  92.             kino_nopower = true
  93.             draw_ui()
  94.         elseif message == "abortedscan" then
  95.             confirmed = false
  96.             scanning = false
  97.             draw_ui()
  98.         elseif message == "moveme" then
  99.             moving = true
  100.             move_ui()
  101.         elseif message == "wake_ack" then
  102.             if confirmed then
  103.                 print("[🔄] Kino has restarted.")
  104.                 local file = io.open(kino_code, "r")
  105.                 local kino_script = file:read("*all")
  106.                 file:close()
  107.                 modem.send(kino_address, 123, "upload")
  108.                 for i = 1, #kino_script, 256 do
  109.                     modem.send(kino_address, 123, kino_script:sub(i, i + 255))
  110.                     os.sleep(0.1)
  111.                 end
  112.                 modem.send(kino_address, 123, "end")
  113.             end
  114.         elseif message == "kino_update_success" then
  115.             if confirmed then
  116.                 print("[✔] Kino has booted.")
  117.                 modem.send(kino_address, 123, "adresses", tablet_address, base_address)
  118.                 os.sleep(0.5)
  119.                 modem.send(kino_address, 123, "autoscan")
  120.             end
  121.         elseif message == "autoscanning" then
  122.             draw_ui()
  123.         end
  124.     end
  125. end
  126.  
  127. load_addresses()
  128.  
  129. if kino_address and base_address then
  130.     print("[📩] Sending tablet address to Kino...")
  131.     modem.send(kino_address, 123, "adresses", tablet_address, base_address)
  132.     print("[✔] Tablet and base address sent!")
  133.     draw_ui()
  134. else
  135.     print("[❌] ERROR: No Kino/Base address found. Cannot send tablet address.")
  136.     running = false
  137. end
  138.  
  139. -- Function to send commands to the Kino
  140. local function send_command(command, value)
  141.     modem.send(kino_address, 123, command, value)
  142. end
  143.  
  144. -- Keyboard Input Listener
  145. local function key_listener(_, _, char, code, _)
  146.     local key = string.char(char)
  147.     if key == "w" then
  148.         if confirmed then
  149.             send_command("move", "forward")
  150.             os.sleep(0.5)
  151.             modem.broadcast(123, "kino_wake")
  152.         else
  153.             send_command("move", "forward")
  154.         end
  155.     elseif key == "s" then
  156.         if confirmed then
  157.             send_command("move", "backward")
  158.             os.sleep(0.5)
  159.             modem.broadcast(123, "kino_wake")
  160.         else
  161.             send_command("move", "backward")
  162.         end    
  163.     elseif key == "a" then
  164.         if confirmed then
  165.             send_command("move", "left")
  166.             os.sleep(0.5)
  167.             modem.broadcast(123, "kino_wake")
  168.         else
  169.             send_command("move", "left")
  170.         end
  171.     elseif key == "d" then
  172.         if confirmed then
  173.             send_command("move", "right")
  174.             os.sleep(0.5)
  175.             modem.broadcast(123, "kino_wake")
  176.         else
  177.             send_command("move", "right")
  178.         end
  179.     elseif key == "q" then
  180.             send_command("move", "up")
  181.     elseif key == "e" then
  182.             send_command("move", "down")
  183.     elseif key == "1" then
  184.         manual_scan = not manual_scan
  185.         send_command("set_scan_mode", manual_scan and "manual" or "normal")
  186.         draw_ui()
  187.         print("[🔧] Scan mode toggled: " .. (manual_scan and "MANUAL" or "NORMAL"))
  188.     elseif code == 57 then
  189.         if fine_motor_control then
  190.             print("[❌] Scan BLOCKED: 4 Block Movement is ENABLED!")
  191.         elseif not scanning and not moving then
  192.             send_command("scanconfirm", "")
  193.             local startTime = os.time()
  194.             while os.time() - startTime < 5 do  -- Wait up to 5 seconds
  195.                 local _, _, from, port, _, message = event.pull(1, "modem_message")
  196.                 if from == kino_address and port == 123 and message == "newscan" then
  197.                     send_command("scan", "")
  198.                     scanning = true
  199.                     draw_ui()
  200.                 end
  201.                
  202.                 if not scanning then
  203.                     print("[❌] ERROR: No response from Kino!")
  204.                 end
  205.             end
  206.         elseif scanning and not moving then
  207.             print("[⚠] Aborting Scan!")
  208.             send_command("abortscan", "")
  209.         elseif moving then
  210.             send_command("confirmed_move", "")
  211.             moving = false
  212.             confirmed = true
  213.             move_ui()
  214.         end
  215.     elseif key == "f" then
  216.         fine_motor_control = not fine_motor_control
  217.         send_command("fine_control", fine_motor_control and "on" or "off")
  218.         draw_ui()
  219.         print("[🔧] 4 Block Movement: " .. (fine_motor_control and "ENABLED" or "DISABLED")) 
  220.     elseif key == "p" then
  221.         send_command("shutdown", "")
  222.         -- Wait for Kino to acknowledge the shutdown
  223.         local shutdownConfirmed = false
  224.         local startTime = os.time()
  225.         while os.time() - startTime < 5 do  -- Wait up to 5 seconds
  226.             local _, _, from, port, _, message = event.pull(1, "modem_message")
  227.             if from == kino_address and port == 123 and message == "shutok" then
  228.                 print("[✔] Kino has shut down successfully.")
  229.                 shutdownConfirmed = true
  230.                 running = false
  231.                 break
  232.             end
  233.         end
  234.  
  235.         if not shutdownConfirmed then
  236.             print("[❌] ERROR: No response from Kino! Shutdown may have failed.")
  237.         end
  238.     end
  239. end
  240.  
  241. messages = event.listen("modem_message", handle_modem_message)
  242. event.listen("key_down", key_listener)
  243.  
  244. while running do
  245.     os.sleep(0.5)
  246. end
  247.  
  248. os.sleep(1)
  249. os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement