Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------- Author: Dewrot --------------
- -- This is my first touchscreen so sorry if the --
- -- code is a little bit rough around the --
- -- edges. Prehaps I may improve this in the --
- -- future and add more features... --
- -- --
- -- This program is supposed to run an alarm with --
- -- on & off inputs as well as having an exit --
- -- button to shut down the program. --
- -- --
- -- Please for the love of GOD, do NOT change any --
- -- of this code without my supervision. This --
- -- is my example code to write better and --
- -- more complex programs with, so it's best --
- -- left alone anyway. Mind the gap. :) --
- ---------------------------------------------------
- ---------- Setup & Configuration ----------
- -- Boot Sequence --
- speaker = peripheral.find("speaker")
- --Find a speaker to use
- monitor = peripheral.wrap("top")
- --Declare where the monitor is
- monitor.clear()
- --Clear the monitor
- monitor.setCursorPos(1, 1)
- --Set the cursor position to the top left
- -- Variable Creation --
- w,h = monitor.getSize()
- --Grab the monitor dimensions
- mouseWidth = 0
- mouseHeight = 0
- --Mouse position on screen
- monitorPosX, monitorPosY = monitor.getCursorPos()
- --Grab the current monitor text position
- on = false
- --Is the alarm currently on?
- quit = false
- --A variable used to exit the program
- -- Status Message --
- term.setCursorPos(1, 1)
- term.clear()
- print("")
- print("Program Running: Alarm")
- print("")
- print("Monitor Width:", w)
- print("Monitor Height:", h)
- print("")
- print("Monitor Position:", "("..monitorPosX..","..monitorPosY..")")
- print("")
- -------------- Draw the Screen --------------
- -- Button Creation --
- monitor.setBackgroundColor((colors.red))
- --Sets background of text to red
- monitor.setCursorPos(7,1)
- monitor.write("*")
- --The "EXIT" button
- monitor.setBackgroundColor((colors.gray))
- monitor.setCursorPos(2,3)
- monitor.write(" ON ")
- --The "ON" button
- monitor.setBackgroundColor((colors.lime))
- monitor.setCursorPos(2,5)
- monitor.write(" OFF ")
- --The "OFF" button
- monitor.setBackgroundColor((colors.black))
- --Makes sure we dont keep writing in green
- monitor.setCursorPos(1,1)
- monitor.write("Alarm")
- --Name of the program
- --------------- Button Logic ---------------
- -- Button Functions --
- function checkClickPosition()
- if mouseWidth == 7 and mouseHeight == 1 then
- --Red X button was pressed
- quit = true
- --Set the quit variable to true
- elseif mouseWidth > 1 and mouseWidth < 7 and mouseHeight == 3 then
- --On button was pressed
- on = true
- term.write("on")
- elseif mouseWidth > 1 and mouseWidth < 7 and mouseHeight == 5 then
- --Off button was pressed
- on = false
- term.write("off")
- end
- end
- function quitProgram()
- local event, p1 = os.pullEvent("char")
- if p1 == "x" then
- quit = true
- end
- end
- repeat
- event,p1,p2,p3 = os.pullEvent()
- if event=="monitor_touch" then
- mouseWidth = p2
- mouseHeight = p3
- checkClickPosition()
- elseif event=="char" then
- quitProgram()
- elseif quit then
- break
- elseif on then
- speaker.playSound("indrev:laser",3.0,1.75)
- --elseif event=="monitor_touch" then
- -- mouseWidth = p2
- -- mouseHeight = p3
- -- checkClickPosition()
- end
- os.startTimer(1.05)
- until quit==true
- --[[
- while on == true do
- speaker.playSound("indrev:laser", 3.0, 1.75)
- sleep(1.05)
- end
- ]]
Advertisement
Add Comment
Please, Sign In to add comment