Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- **************************************************************** --
- -- ***** Touchscreen Control for 3x2 or 3x4 advanced monitors ***** --
- -- ***** By Jharakn ***** --
- -- **************************************************************** --
- --create a few global variables for names.
- messageOn = {long = " on ", short = " on "}
- messageOff = {long = " off ", short = " off "}
- messageAuto = {long = " auto ", short = " auto "}
- messageBlank = {long = " ", short = " "}
- --build the matrix for holding all the button info
- local button = {}
- for i = 1, 8 do
- button[i] = {}
- end
- button[1].name = " "
- button[1].status = messageOff.short
- button[1].colour = colors.red
- button[1].visible = true
- button[1].toggle = true
- button[1].wireColour = colours.white
- button[1].xPos =2
- button[1].yPos =8
- button[2].name = " "
- button[2].status = messageOff.short
- button[2].colour = colors.red
- button[2].visible = true
- button[2].toggle = true
- button[2].wireColour = colours.orange
- button[2].xPos =2
- button[2].yPos =11
- button[3].name = " "
- button[3].status = messageOff.short
- button[3].colour = colors.red
- button[3].visible = true
- button[3].toggle = true
- button[3].wireColour = colours.magenta
- button[3].xPos =2
- button[3].yPos =14
- button[4].name = " "
- button[4].status = messageOff.long
- button[4].colour = colors.red
- button[4].visible = false
- button[4].toggle = true
- button[4].wireColour = colors.lightBlue
- button[4].xPos =22
- button[4].yPos =2
- button[5].name = " "
- button[5].status = messageOff.long
- button[5].colour = colors.red
- button[5].visible = false
- button[5].toggle = true
- button[5].wireColour = colors.yellow
- button[5].xPos =22
- button[5].yPos =5
- button[6].name = " "
- button[6].status = messageOff.long
- button[6].colour = colors.red
- button[6].visible = false
- button[6].toggle = true
- button[6].wireColour = colors.lime
- button[6].xPos =22
- button[6].yPos =8
- button[7].name = " "
- button[7].status = messageOff.long
- button[7].colour = colors.red
- button[7].visible = false
- button[7].toggle = true
- button[7].wireColour = colors.pink
- button[7].xPos =22
- button[7].yPos =11
- button[8].name = " "
- button[8].status = messageOff.long
- button[8].colour = colors.red
- button[8].visible = false
- button[8].toggle = true
- button[8].wireColour = colors.gray
- button[8].xPos =22
- button[8].yPos =14
- button.redstoneValue = 0
- button.monitorSide = "right"
- button.redstoneSide = "left"
- button.modemSide = nil
- button.title = nil
- button.title2 = nil
- button.title3 = nil
- button.widescreen = false
- --Clear the monitor and reset the curser in preperation for building the GUI
- local monitor = peripheral.wrap(button.monitorSide)
- monitor.setBackgroundColor(colors.black)
- monitor.setCursorPos(1, 1)
- monitor.clear()
- --Create / refresh the numbered button
- function createButton(number)
- monitor.setCursorPos(button[number].xPos, button[number].yPos)
- monitor.setBackgroundColor(button[number].colour)
- monitor.write(button[number].name)
- monitor.setCursorPos(button[number].xPos, button[number].yPos+1)
- monitor.write(button[number].status)
- monitor.setBackgroundColor(colors.black)
- end
- --start the program reading from the saveState and refreshing buttons
- function startup()
- -- read the savestate and update the buttons array
- if fs.exists("saveState") then
- local file = fs.open("saveState","r")
- local data = file.readAll()
- file.close()
- local array = {}
- array = textutils.unserialize(data)
- for i = 1,8 do
- button[i].name = array[i].name
- button[i].status = array[i].status
- button[i].colour = array[i].colour
- button[i].visible = array[i].visible
- button[i].toggle = array[i].toggle
- button[i].wireColour = array[i].wireColour
- button[i].xPos = array[i].xPos
- button[i].yPos = array[i].yPos
- end
- button.redstoneValue = array.redstoneValue
- button.monitorSide = array.monitorSide
- button.redstoneSide = array.redstoneSide
- button.modemSide = array.modemSide
- button.title = array.title
- button.title2 = array.title2
- button.title3 = array.title3
- button.widescreen = array.widescreen
- end
- -- set up the buttons
- for i = 1,8 do
- if button[i].visible then
- createButton(i)
- end
- end
- --set the redstone output
- redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
- --set the titles
- if button.title ~= nil then
- monitor.setCursorPos(2,2)
- monitor.write(button.title)
- end
- if button.title2 ~= nil then
- monitor.setCursorPos(2,4)
- monitor.write(button.title2)
- end
- if button.title3 ~= nil then
- monitor.setCursorPos(2,5)
- monitor.write(button.title3)
- end
- pcall(writeState)
- end
- --save the current button information to a file called saveState
- function writeState()
- local file = fs.open("saveState","w")
- file.write(textutils.serialize(button))
- file.close()
- end
- -- ************ --
- -- Main Program --
- -- ************ --
- startup()
- writeState()
- while true do
- -- wait for a monitor click event to occur and output the results
- local event, side, xPos, yPos = os.pullEvent("monitor_touch")
- local row = nil; local column = nil; local buttonPressed = nil
- --check what row the button press was on
- if xPos > 1 and xPos < 20 then row = 1 end
- if xPos > 21 and xPos < 40 then row = 2 end
- --check what column the button press was on
- if yPos > 1 and yPos < 4 then column = 1 end
- if yPos > 4 and yPos < 7 then column = 2 end
- if yPos > 7 and yPos < 10 then column = 3 end
- if yPos > 10 and yPos < 13 then column = 4 end
- if yPos > 13 and yPos < 16 then column = 5 end
- -- calculate what button was pressed from the row and column
- if row == 1 and column == 3 then buttonPressed = 1 end
- if row == 1 and column == 4 then buttonPressed = 2 end
- if row == 1 and column == 5 then buttonPressed = 3 end
- if row == 2 and column == 1 then buttonPressed = 4 end
- if row == 2 and column == 2 then buttonPressed = 5 end
- if row == 2 and column == 3 then buttonPressed = 6 end
- if row == 2 and column == 4 then buttonPressed = 7 end
- if row == 2 and column == 5 then buttonPressed = 8 end
- --check that a button was pressed not an empty patch of monitor and that button is supposed to be visible
- if buttonPressed ~= nil and button[buttonPressed].visible then
- --process for toggle buttons
- if button[buttonPressed].toggle then
- if button[buttonPressed].colour == colours.red then
- button[buttonPressed].colour = colours.blue
- if button.widescreen then
- button[buttonPressed].status = messageAuto.long
- else
- button[buttonPressed].status = messageAuto.short
- end
- elseif button[buttonPressed].colour == colours.blue then
- button[buttonPressed].colour = colours.green
- if button.widescreen then
- button[buttonPressed].status = messageOn.long
- else
- button[buttonPressed].status = messageOn.short
- end
- button.redstoneValue = button.redstoneValue + button[buttonPressed].wireColour
- elseif button[buttonPressed].colour == colours.green then
- button[buttonPressed].colour = colours.red
- if button.widescreen then
- button[buttonPressed].status = messageOff.long
- else
- button[buttonPressed].status = messageOff.short
- end
- button.redstoneValue = button.redstoneValue - button[buttonPressed].wireColour
- end
- pcall(writeState)
- createButton(buttonPressed)
- redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
- --process for non-toggle buttons
- else
- button[buttonPressed].colour = colors.purple
- button.redstoneValue = button.redstoneValue + button[buttonPressed].wireColour
- createButton(buttonPressed)
- redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
- sleep(1)
- button[buttonPressed].colour = colors.gray
- button.redstoneValue = button.redstoneValue - button[buttonPressed].wireColour
- createButton(buttonPressed)
- redstone.setBundledOutput(button.redstoneSide, button.redstoneValue)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement