Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Touch pad control panel
- local SIDES = {"left", "right", "top", "bottom", "front", "back"}
- local OUTPUT_SIDE = "bottom"
- local INPUT_CODE = "1973"
- local BUTTON_PUSH = 9
- local COMBO_SUCCESS = {11,13}
- local COMBO_FAIL = {4,5}
- local codeDoorOpen = false
- local CODE_DOOR = {}
- CODE_DOOR.CLOSE = colours.black
- CODE_DOOR.OPEN = colours.white
- CODE_DOOR.PULSES = 4
- local passDoorOpen = false
- local PASS_DOOR = {}
- PASS_DOOR.CLOSE = colours.red
- PASS_DOOR.OPEN = colours.green
- PASS_DOOR.PULSES = 4
- local drawList = {} --Stores references to everything that needs to be drawn
- drawList.invalid = false;
- local clickList = {} --Stores references to draw objects at each point in its 2D array
- local outputs = {}
- local count = 0
- local combo = ""
- local defaultColour = colours.black
- local currentColour = colours.black
- -------------------------------------------------Classes--
- local function newBox(width, height, colour, label)
- local function draw(self)
- print("in updraw")
- if not self.visible then
- return 0
- end
- print("setBG colour")
- for i, output in ipairs(outputs) do
- output.setBackgroundColour(self.colour)
- local x
- local y
- print("local x and y set")
- --print(self.x)
- --print(self.width)
- --xMax = self.x + (self.width - 1)
- print("pre loop")
- for x = self.x, self.x + (self.width - 1) do
- for y = self.y, self.y + (self.height - 1) do
- print("in loop at "..x.." by "..y)
- output.setCursorPos(x,y)
- output.write(" ")
- end
- end
- print("after box draw")
- x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
- y = self.y + math.floor(self.height * 0.5)
- output.setCursorPos(x,y)
- output.write(self.label)
- output.setBackgroundColour(currentColour)
- end
- end
- local function invalidate(self)
- if self.parent then
- self.parent.invalid = true
- end
- end
- box = {}
- box.x = 0
- box.y = 0
- box.width = width or 1
- box.height = height or 1
- box.visible = true
- box.colour = colour or colours.white
- box.draw = draw
- box.invalidate = invalidate
- box.label = label
- box.type = "Display Object"
- return box
- end
- local function newButton(width, height, colour, downColour, label, value)
- box = newBox(width, height, colour, label)
- local function draw(self)
- if self.downCounter > 0 then
- self:downDraw()
- else
- print("upDraw")
- self:upDraw()
- end
- end
- local function downDraw(self)
- for i, output in ipairs(outputs) do
- output.setBackgroundColour(self.downColour)
- for x = self.x, self.x + (self.width - 1) do
- for y = self.y, self.y + (self.height - 1) do
- output.setCursorPos(x,y)
- output.write(" ")
- end
- end
- x = self.x + math.floor((self.width * 0.5) - (string.len(self.label) * 0.5))
- y = self.y + math.floor(self.height * 0.5)
- output.setCursorPos(x,y)
- output.write(self.label)
- output.setBackgroundColour(currentColour)
- end
- self.downCounter = self.downCounter - 1
- self:invalidate()
- end
- local function click(self)
- self.downCounter = 1
- return value
- end
- box.downColour = downColour or colours.blue
- box.upDraw = box.draw
- box.downDraw = downDraw
- box.draw = draw
- box.type = "Clickable"
- box.downCounter = 0
- box.value = value
- box.click = click
- return box
- end
- local function getNoteBlock()
- for i, v in ipairs(SIDES) do
- if peripheral.getType(v) == "note" then
- return peripheral.wrap(v)
- end
- end
- end
- local function getMonitors()
- local monitors = {}
- for i, v in ipairs(SIDES) do
- if peripheral.getType(v) == "monitor" then
- table.insert(monitors, peripheral.wrap(v))
- end
- end
- return monitors
- end
- local function clearEvents()
- os.queueEvent("clear")
- while true do
- e = os.pullEvent()
- if e == "clear" then
- break
- end
- end
- end
- local function pulseRS(side, n, colour)
- if colour then
- for i = 1, n do
- rs.setBundledOutput(side, colours.combine(rs.getBundledOutput(side), colour))
- sleep(0.45)
- rs.setBundledOutput(side, colours.subtract(rs.getBundledOutput(side), colour))
- sleep(0.45)
- end
- else
- for i = 1, n do
- rs.setOutput(side, true)
- sleep(0.45)
- rs.setOutput(side, false)
- sleep(0.45)
- end
- end
- end
- local function playNote(notes)
- for i = 1, table.getn(notes) do
- note.playNote(0,notes[i])
- sleep(0.2)
- end
- end
- local function addToClickList(object)
- for x = object.x, object.x + (object.width - 1) do
- clickList[x] = clickList[x] or {}
- for y = object.y, object.y + (object.height - 1) do
- table.insert(clickList[x], y, object)
- end
- end
- end
- local function clearScreen(colour, text)
- print("num of monitors: "..table.getn(outputs))
- for i, v in ipairs(outputs) do
- v.setBackgroundColour(colour)
- v.clear()
- print("monitor"..i)
- if text then
- local x = 3 - math.floor((string.len(text) * 0.5))
- local y = 3
- x = x > 0 and x or 1
- v.setCursorPos(x,y)
- print("set pos")
- v.write(text)
- print("write String")
- end
- v.setBackgroundColour(defaultColour)
- print("colour back to default")
- end
- end
- local function drawScreen()
- if drawList.invalid then
- print("in draw")
- drawList.invalid = true
- clearScreen(defaultColour)
- for i, v in ipairs(drawList) do
- print("indraw list loop")
- v:draw()
- print("objectDone")
- end
- end
- end
- local function getObjectAt(x,y)
- if clickList[x] then
- if clickList[x][y] then
- return clickList[x][y]
- end
- end
- return false
- end
- local function handleClick(x, y)
- print(x)
- print(y)
- object = getObjectAt(x, y)
- print("object")
- if object and object.type == "Clickable" then
- combo = combo..object:click()
- count = count + 1
- note.playNote(0, BUTTON_PUSH)
- end
- end
- local function moveDoor(door, open)
- if open then
- pulseRS(OUTPUT_SIDE, door.PULSES, door.OPEN)
- else
- pulseRS(OUTPUT_SIDE, door.PULSES, door.CLOSE)
- end
- end
- local function addToDrawList(object, i)
- i = i or table.getn(drawList) + 1
- table.insert(drawList, i, object)
- object.parent = drawList
- print(object)
- if object.type == "Clickable" then
- addToClickList(object)
- end
- drawList.invalid = true
- end
- local function startingUpScreen()
- clearScreen(colours.yellow, "Booting")
- end
- local function successScreen()
- clearScreen(colours.green, "Welcome")
- end
- local function failScreen()
- clearScreen(colours.red, "Wrong")
- end
- local function main()
- outputs = getMonitors()
- note = getNoteBlock()
- startingUpScreen()
- --reset system just in case
- moveDoor(PASS_DOOR)
- moveDoor(CODE_DOOR)
- sleep(1)
- clearScreen(defaultColour)
- --(width, height, colour, downColour, label, value)
- button1 = newButton(1,1,colours.blue, colours.red, "1", "1")
- button1.x = 2
- button1.y = 1
- button2 = newButton(1,1,colours.blue, colours.red, "2", "2")
- button2.x = 4
- button2.y = 1
- button3 = newButton(1,1,colours.blue, colours.red, "3", "3")
- button3.x = 6
- button3.y = 1
- button4 = newButton(1,1,colours.blue, colours.red, "4", "4")
- button4.x = 2
- button4.y = 3
- button5 = newButton(1,1,colours.blue, colours.red, "5", "5")
- button5.x = 4
- button5.y = 3
- button6 = newButton(1,1,colours.blue, colours.red, "6", "6")
- button6.x = 6
- button6.y = 3
- button7 = newButton(1,1,colours.blue, colours.red, "7", "7")
- button7.x = 2
- button7.y = 5
- button8 = newButton(1,1,colours.blue, colours.red, "8", "8")
- button8.x = 4
- button8.y = 5
- button9 = newButton(1,1,colours.blue, colours.red, "9", "9")
- button9.x = 6
- button9.y = 5
- --button0 = newButton(1,1,colours.blue, colours.red, "0", "0")
- addToDrawList(button1)
- addToDrawList(button2)
- addToDrawList(button3)
- addToDrawList(button4)
- addToDrawList(button5)
- addToDrawList(button6)
- addToDrawList(button7)
- addToDrawList(button8)
- addToDrawList(button9)
- local doorTimer
- local eventTimer
- while true do
- print("tick")
- drawScreen()
- print("post draw")
- clearEvents()
- print("post clear Events")
- while true do
- print("timerActivate")
- eventTimer = os.startTimer(6)
- e, side, x, y = os.pullEvent()
- if e == "monitor_touch" then
- handleClick(x, y)
- print("click activated")
- print("count is "..count)
- print("combo is "..combo)
- if count == 4 then
- if combo == INPUT_CODE then
- sleep(0.5)
- playNote(COMBO_SUCCESS)
- successScreen()
- moveDoor(CODE_DOOR, true)
- sleep(3)
- else
- sleep(0.5)
- playNote(COMBO_FAIL)
- failScreen()
- count = 0
- combo = ""
- sleep(5)
- end
- os.reboot()
- end
- break
- end
- drawScreen()
- print("timeOut")
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement