Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tablet UI Script for Kino Control
- local component = require("component")
- local thread = require("thread")
- local event = require("event")
- local term = require("term")
- local serialization = require("serialization")
- local filesystem = require("filesystem")
- local modem = component.modem
- local gpu = component.gpu
- running = true
- -- Configuration
- local Version = "1.0.0"
- local kino_code = "kino.lua"
- local addressFile = "/home/modem_addresses.json"
- local kino_address, tablet_address, base_address
- local fine_motor_control = false -- Default: Disabled
- local scanning = false
- local kino_nopower = false
- local manual_scan = false
- local moving = false
- local confirmed = false
- modem.open(123)
- -- Function to load addresses from JSON file
- local function load_addresses()
- if filesystem.exists(addressFile) then
- local file = io.open(addressFile, "r")
- if file then
- local content = file:read("*a")
- file:close()
- local data = serialization.unserialize(content)
- if data then
- kino_address = data.kino_address
- tablet_address = data.tablet_address
- base_address = data.base_adress
- print("[✔] Loaded addresses: Kino = " .. kino_address .. ", Tablet = " .. tablet_address)
- else
- print("[❌] ERROR: Failed to parse address file.")
- end
- else
- print("[❌] ERROR: Could not open address file.")
- end
- else
- print("[⚠] Address file not found. Communication may not work.")
- end
- end
- -- UI Drawing Function
- local function draw_ui()
- term.clear()
- print("Kino Control Tablet")
- print("[W] Forward [S] Backward")
- print("[A] Left [D] Right")
- print("[Q] Up [E] Down")
- print("[1] Toggle Normal/Manual Scan [" .. (manual_scan and "MANUAL" or "NORMAL") .. "]")
- print("[SPACE] " .. (scanning and "Stop Scan [SCANNING]" or "Start Scan"))
- print("[F] Toggle 4 Block Movement [" .. (fine_motor_control and "ON" or "OFF") .. "]")
- print("[P] Shutdown")
- if kino_nopower then
- for i = 5, 1, -1 do
- term.clear()
- print("Kino Control Tablet")
- print("[⚠] Kino ran out of power, shutting down in " .. i .. " seconds")
- os.sleep(1)
- end
- running = false
- end
- end
- local function move_ui()
- term.clear()
- print("Kino Control Tablet")
- print("[W] Forward [S] Backward")
- print("[A] Left [D] Right")
- print("[Q] Up [E] Down")
- print("[P] Shutdown")
- print("")
- print(confirmed and "" or "Move the kino 1 block away from the event horizon")
- print(confirmed and "Move the kino inside the gate to start scanning" or "When you are done confirm by pressing [SPACE]")
- print(confirmed and "If you move it the wrong way, restart and try again. Otherwise just wait." or "")
- print(confirmed and "The kino will reboot on the other side and start scanning, keep your tablet up until this message disappears." or "")
- end
- -- Function to handle incoming modem messages
- local function handle_modem_message(_, _, from, port, _, message)
- if from == kino_address and port == 123 then
- if message == "finish" then
- confirmed = false
- scanning = false
- draw_ui()
- print("[✔] Kino has completed its scan successfully.")
- elseif message == "shutdownenergy" then
- confirmed = false
- scanning = false
- kino_nopower = true
- draw_ui()
- elseif message == "abortedscan" then
- confirmed = false
- scanning = false
- draw_ui()
- elseif message == "moveme" then
- moving = true
- move_ui()
- elseif message == "wake_ack" then
- if confirmed then
- print("[🔄] Kino has restarted.")
- local file = io.open(kino_code, "r")
- local kino_script = file:read("*all")
- file:close()
- modem.send(kino_address, 123, "upload")
- for i = 1, #kino_script, 256 do
- modem.send(kino_address, 123, kino_script:sub(i, i + 255))
- os.sleep(0.1)
- end
- modem.send(kino_address, 123, "end")
- end
- elseif message == "kino_update_success" then
- if confirmed then
- print("[✔] Kino has booted.")
- modem.send(kino_address, 123, "adresses", tablet_address, base_address)
- os.sleep(0.5)
- modem.send(kino_address, 123, "autoscan")
- end
- elseif message == "autoscanning" then
- draw_ui()
- end
- end
- end
- load_addresses()
- if kino_address and base_address then
- print("[📩] Sending tablet address to Kino...")
- modem.send(kino_address, 123, "adresses", tablet_address, base_address)
- print("[✔] Tablet and base address sent!")
- draw_ui()
- else
- print("[❌] ERROR: No Kino/Base address found. Cannot send tablet address.")
- running = false
- end
- -- Function to send commands to the Kino
- local function send_command(command, value)
- modem.send(kino_address, 123, command, value)
- end
- -- Keyboard Input Listener
- local function key_listener(_, _, char, code, _)
- local key = string.char(char)
- if key == "w" then
- if confirmed then
- send_command("move", "forward")
- os.sleep(0.5)
- modem.broadcast(123, "kino_wake")
- else
- send_command("move", "forward")
- end
- elseif key == "s" then
- if confirmed then
- send_command("move", "backward")
- os.sleep(0.5)
- modem.broadcast(123, "kino_wake")
- else
- send_command("move", "backward")
- end
- elseif key == "a" then
- if confirmed then
- send_command("move", "left")
- os.sleep(0.5)
- modem.broadcast(123, "kino_wake")
- else
- send_command("move", "left")
- end
- elseif key == "d" then
- if confirmed then
- send_command("move", "right")
- os.sleep(0.5)
- modem.broadcast(123, "kino_wake")
- else
- send_command("move", "right")
- end
- elseif key == "q" then
- send_command("move", "up")
- elseif key == "e" then
- send_command("move", "down")
- elseif key == "1" then
- manual_scan = not manual_scan
- send_command("set_scan_mode", manual_scan and "manual" or "normal")
- draw_ui()
- print("[🔧] Scan mode toggled: " .. (manual_scan and "MANUAL" or "NORMAL"))
- elseif code == 57 then
- if fine_motor_control then
- print("[❌] Scan BLOCKED: 4 Block Movement is ENABLED!")
- elseif not scanning and not moving then
- send_command("scanconfirm", "")
- local startTime = os.time()
- while os.time() - startTime < 5 do -- Wait up to 5 seconds
- local _, _, from, port, _, message = event.pull(1, "modem_message")
- if from == kino_address and port == 123 and message == "newscan" then
- send_command("scan", "")
- scanning = true
- draw_ui()
- end
- if not scanning then
- print("[❌] ERROR: No response from Kino!")
- end
- end
- elseif scanning and not moving then
- print("[⚠] Aborting Scan!")
- send_command("abortscan", "")
- elseif moving then
- send_command("confirmed_move", "")
- moving = false
- confirmed = true
- move_ui()
- end
- elseif key == "f" then
- fine_motor_control = not fine_motor_control
- send_command("fine_control", fine_motor_control and "on" or "off")
- draw_ui()
- print("[🔧] 4 Block Movement: " .. (fine_motor_control and "ENABLED" or "DISABLED"))
- elseif key == "p" then
- send_command("shutdown", "")
- -- Wait for Kino to acknowledge the shutdown
- local shutdownConfirmed = false
- local startTime = os.time()
- while os.time() - startTime < 5 do -- Wait up to 5 seconds
- local _, _, from, port, _, message = event.pull(1, "modem_message")
- if from == kino_address and port == 123 and message == "shutok" then
- print("[✔] Kino has shut down successfully.")
- shutdownConfirmed = true
- running = false
- break
- end
- end
- if not shutdownConfirmed then
- print("[❌] ERROR: No response from Kino! Shutdown may have failed.")
- end
- end
- end
- messages = event.listen("modem_message", handle_modem_message)
- event.listen("key_down", key_listener)
- while running do
- os.sleep(0.5)
- end
- os.sleep(1)
- os.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement