Guest User

alarm

a guest
May 10th, 2021
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | None | 0 0
  1. ---------------    Author: Dewrot    --------------
  2.                                            
  3. -- This is my first touchscreen so sorry if the  --
  4. --     code is a little bit rough around the     --
  5. --     edges. Prehaps I may improve this in the  --
  6. --     future and add more features...           --
  7. --                                               --
  8. -- This program is supposed to run an alarm with --
  9. --     on & off inputs as well as having an exit --
  10. --     button to shut down the program.          --
  11. --                                               --
  12. -- Please for the love of GOD, do NOT change any --
  13. --     of this code without my supervision. This --
  14. --     is my example code to write better and    --
  15. --     more complex programs with, so it's best  --
  16. --     left alone anyway. Mind the gap.  :)      --
  17.  
  18. ---------------------------------------------------
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  ----------    Setup & Configuration    ----------
  36.  
  37.  
  38.  
  39.  
  40. -- Boot Sequence --
  41.  
  42.     speaker = peripheral.find("speaker")
  43.      --Find a speaker to use
  44.    
  45.     monitor = peripheral.wrap("top")
  46.      --Declare where the monitor is
  47.      
  48.     monitor.clear()
  49.      --Clear the monitor
  50.      
  51.     monitor.setCursorPos(1, 1)
  52.      --Set the cursor position to the top left
  53.  
  54.  
  55.  
  56.  
  57. -- Variable Creation --
  58.  
  59.     w,h = monitor.getSize()
  60.      --Grab the monitor dimensions
  61.  
  62.     mouseWidth = 0
  63.     mouseHeight = 0
  64.      --Mouse position on screen
  65.  
  66.     monitorPosX, monitorPosY = monitor.getCursorPos()
  67.      --Grab the current monitor text position
  68.    
  69.     on = false
  70.      --Is the alarm currently on?
  71.    
  72.     quit = false
  73.      --A variable used to exit the program
  74.  
  75.  
  76.  
  77. -- Status Message --
  78.    
  79.     term.setCursorPos(1, 1)
  80.     term.clear()
  81.    
  82.     print("")
  83.     print("Program Running: Alarm")
  84.     print("")
  85.     print("Monitor Width:", w)
  86.     print("Monitor Height:", h)
  87.     print("")
  88.     print("Monitor Position:", "("..monitorPosX..","..monitorPosY..")")
  89.     print("")
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. --------------    Draw the Screen    --------------
  98.  
  99.  
  100.  
  101.  
  102.  
  103. -- Button Creation --
  104.  
  105.     monitor.setBackgroundColor((colors.red))
  106.      --Sets background of text to red
  107.     monitor.setCursorPos(7,1)
  108.     monitor.write("*")
  109.      --The "EXIT" button
  110.  
  111.     monitor.setBackgroundColor((colors.gray))
  112.     monitor.setCursorPos(2,3)
  113.     monitor.write(" ON  ")
  114.      --The "ON" button
  115.    
  116.     monitor.setBackgroundColor((colors.lime))
  117.     monitor.setCursorPos(2,5)
  118.     monitor.write(" OFF ")
  119.      --The "OFF" button
  120.  
  121.     monitor.setBackgroundColor((colors.black))
  122.      --Makes sure we dont keep writing in green
  123.     monitor.setCursorPos(1,1)
  124.     monitor.write("Alarm")
  125.      --Name of the program
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. ---------------    Button  Logic    ---------------
  133.  
  134.  
  135.  
  136.  
  137. -- Button Functions --
  138.  
  139.     function checkClickPosition()
  140.       if mouseWidth == 7 and mouseHeight == 1 then
  141.        --Red X button was pressed
  142.       quit = true
  143.        --Set the quit variable to true
  144.      
  145.       elseif mouseWidth > 1 and mouseWidth < 7 and mouseHeight == 3 then
  146.        --On button was pressed
  147.       on = true
  148.       term.write("on")
  149.      
  150.       elseif mouseWidth > 1 and mouseWidth < 7 and mouseHeight == 5 then
  151.        --Off button was pressed
  152.       on = false
  153.       term.write("off")
  154.          
  155.       end
  156.     end
  157.    
  158.    
  159.     function quitProgram()
  160.       local event, p1 = os.pullEvent("char")
  161.         if p1 == "x" then
  162.           quit = true
  163.       end
  164.     end
  165.    
  166.    
  167.     repeat
  168.       event,p1,p2,p3 = os.pullEvent()
  169.        
  170.         if event=="monitor_touch" then
  171.           mouseWidth = p2
  172.           mouseHeight = p3
  173.           checkClickPosition()
  174.          
  175.         elseif event=="char" then
  176.           quitProgram()
  177.            
  178.         elseif quit then
  179.         break
  180.        
  181.         elseif on then
  182.         speaker.playSound("indrev:laser",3.0,1.75)
  183.        
  184.         --elseif event=="monitor_touch" then
  185.          -- mouseWidth = p2
  186.          -- mouseHeight = p3
  187.          -- checkClickPosition()
  188.         end
  189.         os.startTimer(1.05)
  190.     until quit==true
  191.  
  192. --[[
  193. while on == true do
  194.  
  195. speaker.playSound("indrev:laser", 3.0, 1.75)
  196. sleep(1.05)
  197.  
  198. end
  199. ]]
  200.  
Advertisement
Add Comment
Please, Sign In to add comment