-- redstonecontrol.lua -- Remote redstone control via ender modem -- Requires a remote computer running redstoneremote.lua ------------- Config ------------- local MONITOR_SIDE = "top" -- side where the monitor is attached local CHANNEL = "redstone_1" -- communication channel (change this to run multiple instances) local REMOTE_COMPUTER = 15 -- ID of remote computer with redstone (find via "list" command) --------------------------------- local mon = peripheral.wrap(MONITOR_SIDE) if not mon then error("No monitor found on side: " .. MONITOR_SIDE) end -- Open modem for _, side in ipairs(rs.getSides()) do if peripheral.getType(side) == "modem" then rednet.open(side) end end -- Auto-scale monitor local function setupMonitor() local scales = {5, 3, 2, 1.5, 1, 0.5} for _, scale in ipairs(scales) do mon.setTextScale(scale) local w, h = mon.getSize() if w >= 10 and h >= 6 then return scale, w, h end end return 0.5, mon.getSize() end local scale, width, height = setupMonitor() -- Draw button function local function drawButton(x, y, w, h, text, bgColor, textColor) mon.setBackgroundColor(bgColor) mon.setTextColor(textColor) for row = y, y + h - 1 do mon.setCursorPos(x, row) mon.write(string.rep(" ", w)) end local textX = x + math.floor((w - #text) / 2) local textY = y + math.floor(h / 2) mon.setCursorPos(textX, textY) mon.write(text) end -- Draw the interface local function drawInterface(redstoneOn) mon.setBackgroundColor(colors.black) mon.clear() -- Title mon.setTextColor(colors.white) mon.setCursorPos(2, 2) mon.write("Redstone Control") -- Status mon.setCursorPos(2, 3) if redstoneOn then mon.setTextColor(colors.lime) mon.write("Status: ON") else mon.setTextColor(colors.red) mon.write("Status: OFF") end -- Green ON button local onY = math.floor(height / 2) - 1 drawButton(2, onY, 8, 3, "ON", colors.lime, colors.white) -- Red OFF button local offX = width - 9 drawButton(offX, onY, 8, 3, "OFF", colors.red, colors.white) end -- Check if click is within button bounds local function isInButton(x, y, btnX, btnY, btnW, btnH) return x >= btnX and x < btnX + btnW and y >= btnY and y < btnY + btnH end -- Main program local redstoneActive = false -- Send command to remote computer local function sendCommand(state) rednet.send(REMOTE_COMPUTER, {action = state}, CHANNEL) end drawInterface(redstoneActive) print("Redstone Control active") print("Remote computer ID: " .. REMOTE_COMPUTER) print("Touch monitor to control") while true do local event, side, x, y = os.pullEvent("monitor_touch") if side == MONITOR_SIDE then local onY = math.floor(height / 2) - 1 local onX = 2 local offX = width - 9 -- Check ON button (green) if isInButton(x, y, onX, onY, 8, 3) then redstoneActive = true sendCommand("on") drawInterface(redstoneActive) print("Redstone ON") end -- Check OFF button (red) if isInButton(x, y, offX, onY, 8, 3) then redstoneActive = false sendCommand("off") drawInterface(redstoneActive) print("Redstone OFF") end end end