Advertisement
neonerz

rmpv2

Feb 22nd, 2014
3,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.46 KB | None | 0 0
  1. -- RAC Media Player v2.0 by neonerZ (computer script)
  2. -- More Info: https://www.youtube.com/watch?v=tfSSdY0jQQk
  3.  
  4. --set variables
  5. modem="right" -- modem position
  6. monitor = "left" -- monitor position
  7. turtle = 8 -- ID of turtle DJ
  8. radioCrystal="15254" -- item ID of radio crystal
  9.  
  10. -- Feel free to do whatever you want with this.
  11. -- Edit it, incorporate it into your own project, eat it, I don't care.
  12. -- I only ask if you redistribute this or edit it/incorporate it into
  13. -- your own project you give me a shout out (In the case of including
  14. -- this into something you release. Send me a message, I'd love to
  15. -- check it out). This is only a friendly suggestion, you are
  16. -- welcome to be a jerk and claim this as your own
  17.  
  18. -- don't edit below here
  19. m = peripheral.wrap(monitor)
  20. rednet.open(modem)
  21. local button={}
  22.  
  23. --Function to create button table
  24. function setTable(name, text, xmin, ymin, width, height, onFreq, offFreq, bcolor, tcolor, extra)
  25.    button[name] = {}
  26.    button[name]["text"] = text
  27.    button[name]["active"] = false
  28.    button[name]["xmin"] = xmin
  29.    button[name]["ymin"] = ymin
  30.    button[name]["width"] = width
  31.    button[name]["height"] = height
  32.    button[name]["onFreq"] = onFreq
  33.    button[name]["offFreq"] = offFreq
  34.    button[name]["bcolor"] = bcolor
  35.    button[name]["tcolor"] = tcolor
  36.    button[name]["extra"] = extra
  37. end
  38.  
  39. --Toggle button on and off
  40. function toggleButton(name, state)
  41.         if state=="on" then
  42.                 button[name]["active"] = true
  43.         else
  44.                 button[name]["active"] = false
  45.         end
  46. end    
  47.  
  48. --Draw button on screen
  49. function drawButton(x, y, width, height, bcolor, tcolor, text)
  50.     m.setBackgroundColor(bcolor)
  51.     --Draw the background
  52.     for i=1,height do
  53.         m.setCursorPos(x,y+i-1)
  54.         m.write(string.rep(" ", width))
  55.     end
  56. --      m.setBackgroundColor(colors.black)
  57.     --Write the text
  58.          m.setTextColor(tcolor)
  59.     local textX = x + math.floor(width/2 - string.len(text)/2)
  60.     local textY = y + math.floor(height/2)
  61.     m.setCursorPos(textX, textY)
  62.     m.write(text)
  63. end
  64.  
  65. --Check if a button within the table has been clicked
  66. --(returns button name if matches, or false if not)
  67. function checkxy(x, y)
  68.     returnCheckxy=false
  69.     for name, data in pairs(button) do
  70.         if data["height"] == 1 then
  71.             ymax = data["ymin"]
  72.         else
  73.             ymax = data["height"] + data["ymin"]
  74.         end
  75.                
  76.         if data["width"] == 1 then
  77.             xmax = data["xmin"]
  78.         else
  79.             xmax = data["width"] + data["xmin"]
  80.         end
  81.         if y>=data["ymin"] and  y <= ymax then
  82.             if x>=data["xmin"] and x<= xmax-1 then
  83.                 returnCheckxy = {name = name,
  84.                 text = data["text"],
  85.                 onFreq = data["onFreq"],
  86.                 offFreq = data["offFreq"],
  87.                 bcolor = data["bcolor"],
  88.                 tcolor = data["tcolor"],
  89.                 active = data["active"],
  90.                 extra = data["extra"]}
  91.             end
  92.         end
  93.     end
  94. end
  95.  
  96.  
  97. --Draw out GUI using the buttons registered in the regButtons function
  98. function displayButtons()
  99.     m.setBackgroundColor(colors.black)
  100.     for name,data in pairs(button) do
  101.         text=data["text"]
  102.         xmin=data["xmin"]
  103.         ymin=data["ymin"]
  104.         width=data["width"]
  105.         height=data["height"]
  106.         tcolor=data["tcolor"]
  107.         active=data["active"]
  108.         if active == true then
  109.                 bcolor = colors.lime
  110.         else
  111.                 bcolor=data["bcolor"]
  112.         end
  113.         drawButton(xmin, ymin, width, height, bcolor, tcolor, text)
  114.     end
  115.     m.setBackgroundColor(colors.black)
  116. end
  117.  
  118. function checkMonitorSize()
  119.  
  120. local w, h = m.getSize()
  121. if w < 39 then
  122.     printError("Error: monitor width too small, please make at least 4 monitors wide")
  123.     return false
  124. end
  125. if h < 19 then
  126.     printError("Error: monitor height too small, please make at least 3 monitors tall")
  127.     return false
  128. end
  129. mWidth = w
  130. mHeight = h
  131. return true
  132. end
  133.  
  134. function drawGUI()
  135.     local startPos
  136.     local volDown
  137.     button={}
  138.     m.clear()
  139.     checkMonitorSize()
  140.     m.setCursorPos(1,1)
  141.     m.setBackgroundColor(colors.black)
  142.     for i = 1,mWidth do
  143.         m.write(" ")
  144.     end
  145.     startPos=(mWidth-21)/2+1
  146.     local playButton=(mWidth-26)/2+1
  147.     local stopButton=playButton+5
  148.     local ejectButton=stopButton+5
  149.     m.setCursorPos(startPos,1)
  150.     m.setTextColor(colors.white)
  151.     if (mWidth % 2 == 0) then
  152.         m.write("RAC Media Player  v2.0")
  153.         volDown=ejectButton+6
  154.     else
  155.         m.write("RAC Media Player v2.0")
  156.         volDown=ejectButton+7
  157.     end
  158.     local volume=volDown+1
  159.     local volUp=volume+3
  160.     local scanButton=volUp+2
  161.     m.setBackgroundColor(colors.gray)
  162.     m.setCursorPos(1,2)
  163.     for i = 1,mWidth do
  164.         m.write(" ")
  165.     end
  166.     m.setBackgroundColor(colors.lightGray)
  167.     m.setCursorPos(1,3)
  168.     for i = 1,mWidth do
  169.         m.write(" ")
  170.     end
  171.     setTable("play","play",math.floor(playButton),2,4,1,0,0,colors.gray,colors.white,0)
  172.     setTable("stop","stop",math.floor(stopButton),2,4,1,0,0,colors.gray,colors.white,0)
  173.     setTable("eject","eject",math.floor(ejectButton),2,5,1,0,0,colors.gray,colors.white,0)
  174.     setTable("voldown"," -",math.floor(volDown)-1,2,2,1,0,0,colors.gray,colors.white,0)
  175.     setTable("volup","+ ",math.floor(volUp),2,2,1,0,0,colors.gray,colors.white,0)
  176.     setTable("scan","scan",math.floor(scanButton),2,4,1,0,0,colors.gray,colors.white,0)
  177.     m.setCursorPos(volDown+1,2)
  178.     m.setBackgroundColor(colors.gray)
  179.     m.setTextColor(colors.white)
  180.     m.write("vol")
  181.     m.setBackgroundColor(colors.black)
  182.  
  183. end
  184.  
  185. function scan(id)
  186.     data = {}
  187.     track={}
  188.     drawGUI()
  189.     local trackCount = 0
  190.     local w, h = m.getSize()
  191.     rednet.send(id, "scan:no")
  192.     local id, msg, dis = rednet.receive()
  193.     data = textutils.unserialize(msg)
  194.     count = 0
  195.     for name, line in pairsByKeys(data) do
  196.         data[name]=line
  197.     end
  198.     for name, line in pairsByKeys(data) do
  199.         count=count+1
  200.         local title=name
  201.         for k,v in string.gmatch(line, "(.+):(.+)") do
  202.             if v == radioCrystal then
  203.                 track[count]=title..":radio"
  204.             else
  205.                 for k,v in string.gmatch(title, "(.+)-(.+)") do
  206.                     name=string.gsub(v, " ", "", 1)
  207.                     track[count]=name..":"..k
  208.                 end
  209.             end
  210.         end
  211.     end
  212.     for _ in pairs(track) do trackCount = trackCount + 1 end
  213.     local buttonAmmount = math.floor((w-2)/11)
  214.     local buttonLeftover = ((w-(buttonAmmount*11))-(buttonAmmount-1))/2
  215.    
  216.     local rows = math.ceil(count/buttonAmmount)
  217.     local num = 1
  218.     local row = 5
  219.     for i = 1, rows do
  220.         if math.floor(buttonLeftover) == buttonLeftover then
  221.             buttonPos = buttonLeftover
  222.         else
  223.             buttonPos = math.floor(buttonLeftover)
  224.             extraSpace=true
  225.         end
  226.         local totalSpace = ((buttonAmmount-1)*2)+(buttonAmmount*11)
  227.         if buttonPos < 0 then
  228.             buttonPos = 0
  229.         end
  230.         for perRow = 1,buttonAmmount do
  231.             local skip = 0
  232.             if track[num] == nil then
  233.                 skip = 1
  234.             else
  235.                 local title, artist = string.match(track[num], "(.+):(.+)")
  236.                 if extraSpace and perRow ~= 1 and totalSpace <= w then
  237.                     setTable(title,string.sub(title,1,11),buttonPos+1,row,11,1,"play",0,colors.blue,colors.white,artist)
  238.                     buttonPos=buttonPos+13
  239.                 elseif perRow == 1 and buttonPos == 1 and totalSpace > w then
  240.                     setTable(title,string.sub(title,1,11),2,row,11,1,"play",0,colors.blue,colors.white,artist)
  241.                     buttonPos=buttonPos+13
  242.                 else
  243.                     setTable(title,string.sub(title,1,11),buttonPos,row,11,1,"play",0,colors.blue,colors.white,artist)
  244.                     buttonPos=buttonPos+12
  245.                 end
  246.                 num=num+1
  247.             end
  248.         end
  249.         row=row+2
  250.        
  251.     end
  252.        
  253. --  setTable("test2","test2",15,4,11,1,0,0,colors.blue,colors.white,0) 
  254. --  setTable("test3","test3",28,4,11,1,0,0,colors.blue,colors.white,0) 
  255. end
  256.  
  257. function alive(id2)
  258.     m.clear()
  259.     m.setCursorPos(1,1)
  260.     m.setTextColor(colors.white)
  261.     m.setBackgroundColor(colors.black)
  262.     m.write("Searching for turtle...")
  263.     m.setCursorBlink(true)
  264.     msg = nil
  265.     print("connect try #1")
  266.     rednet.send(id2,"alive:none")
  267.     local id, msg, dis = rednet.receive(5)
  268.     if msg == nil then
  269.         print("connect try #2")
  270.         rednet.send(id2,"alive:none")
  271.         id, msg, dis = rednet.receive(5)
  272.     end
  273.     if msg == nil then
  274.         print("connect try #3")
  275.         rednet.send(id2,"alive:none")
  276.         id, msg, dis = rednet.receive(5)
  277.     end
  278.     m.setCursorBlink(false)
  279.     if msg ~= nil and id == id2 then
  280.         print("connected to turtle #"..id2)
  281.         result=true
  282.     else
  283.         printError("Turtle DJ not responding, are you sure it's alive?")
  284.         m.clear()
  285.         m.setCursorPos(1,1)
  286.         m.write("Turtle Timeout: Is turtle alive?")
  287.         result=false
  288.     end
  289.  
  290. end
  291.  
  292. function pairsByKeys (t, f)
  293.     local a = {}
  294.     for n in pairs(t) do table.insert(a, n) end
  295.         table.sort(a, f)
  296.         local i = 0      -- iterator variable
  297.         local iter = function ()   -- iterator function
  298.         i = i + 1
  299.         if a[i] == nil then return nil
  300.         else return a[i], t[a[i]]
  301.         end
  302.     end
  303.     return iter
  304. end
  305.  
  306. function checkProgress(preMessage,successMessage,failureMessage)
  307.     msg = nil
  308.     messageBox(preMessage)
  309.     m.setCursorBlink(true)
  310.     local id, msg, dis = rednet.receive(5)
  311.     if msg == nil then
  312.         messageBox(failureMessage)
  313.         result = false
  314.         print("FAILED!")
  315.     elseif id == turtle then
  316.         print("SUCCESS")
  317.         messageBox(successMessage)
  318.         result = true
  319.     end
  320.     m.setCursorBlink(false)
  321. end
  322.  
  323. function clearMessageBox()
  324.     m.setBackgroundColor(colors.lightGray)
  325.     m.setCursorPos(1,3)
  326.     for i = 1,mWidth do
  327.         m.write(" ")
  328.     end
  329. end
  330.  
  331. function messageBox(message)
  332.     local w, h = m.getSize()
  333.     clearMessageBox()
  334.     message=string.sub(message,1,w-3)
  335.     stringLength = string.len(message)
  336.     startWidth = ((w-stringLength)/2)
  337.     m.setCursorPos(startWidth+1,3)
  338.     m.setTextColor(colors.black)
  339.     m.setBackgroundColor(colors.lightGray)
  340.     m.write(message)
  341.     m.setBackgroundColor(colors.black)
  342. end
  343.  
  344. function currentlyPlaying(id,load)
  345.     messageBox("Checking track...")
  346.     m.setCursorBlink(true)
  347.     rednet.send(id,"track:none")
  348.     local id, msg, dis = rednet.receive()
  349.     if (msg == "none") and id == turtle then
  350.         messageBox("No track loaded")
  351.     else
  352.             messageBox("Currently Loaded: "..msg)
  353.     end
  354.     m.setCursorBlink(false)
  355. end
  356.  
  357.  
  358. if checkMonitorSize() then
  359.     print("Starting up RAC Media Player 2.0")
  360.     alive(turtle)
  361.     if not result then
  362.         return
  363.     end
  364.     print("SUCCESS")
  365.     scan(turtle)
  366.     currentlyPlaying(turtle)
  367.     displayButtons()
  368.     while true do
  369.         local e, side, x,y = os.pullEvent("monitor_touch")
  370.         checkxy(x,y)
  371.         if returnCheckxy ~= false then
  372.             print(">you pressed "..returnCheckxy["name"])
  373.             if returnCheckxy["name"]=="play" then
  374.                 rednet.send(turtle,"play:none")
  375.                 checkProgress("Playing track...","Track loaded","Turtle Timeout: Is turtle alive?")
  376.                 if result then currentlyPlaying(turtle) end
  377.             end
  378.             if returnCheckxy["name"]=="stop" then
  379.                 rednet.send(turtle,"stop:none")
  380.                 checkProgress("Stopping Track...","Track Stopped","Turtle Timeout: Is turtle alive?")
  381.                 if result then currentlyPlaying(turtle) end
  382.             end
  383.             if returnCheckxy["name"]=="eject" then
  384.                 rednet.send(turtle,"stopeject:none")
  385.                 checkProgress("Ejecting track...","Track ejected","Turtle Timeout: Is turtle alive?")
  386.                 scan(turtle)
  387.                 if result then currentlyPlaying(turtle) end
  388.                 displayButtons()
  389.             end
  390.             if returnCheckxy["name"]=="scan" then
  391.                 alive(turtle)
  392.                 if not result then
  393.                     return
  394.                 end
  395.                 print("SUCCESS")
  396.                 scan(turtle)
  397.                 currentlyPlaying(turtle)
  398.                 displayButtons()
  399.             end
  400.             if returnCheckxy["name"]=="voldown" then
  401.                 rednet.send(turtle,"volume:0")
  402.                 local id, msg, dis = rednet.receive(2)
  403.                 if msg ~= "SUCCESS" and id == turtle then printError("No response from turtle, is it alive?") end
  404.             end
  405.             if returnCheckxy["name"]=="volup" then
  406.                 rednet.send(turtle,"volume:1")
  407.                 local id, msg, dis = rednet.receive(2)
  408.                 if msg ~= "SUCCESS" and id == turtle then printError("No response from turtle, is it alive?")  end
  409.             end
  410.             if returnCheckxy["onFreq"]=="play" then
  411.                 if returnCheckxy["extra"] == "radio" then
  412.                     print("playt:"..returnCheckxy["extra"].."- "..returnCheckxy["name"])
  413.                     rednet.send(turtle,"playt:"..returnCheckxy["name"])
  414.                     checkProgress("Loading Radio Station...","Radio loaded","Turtle Timeout: Is turtle alive?")
  415.                     if result then
  416.                         scan(turtle)
  417.                         displayButtons()
  418.                         currentlyPlaying(turtle)
  419.                     end
  420.                 else
  421.                     print("playt:"..returnCheckxy["extra"].."- "..returnCheckxy["name"])
  422.                     rednet.send(turtle,"playt:"..returnCheckxy["extra"].."- "..returnCheckxy["name"])
  423.                     checkProgress("Playing track...","Track loaded","Turtle Timeout: Is turtle alive?")
  424.                     print(result)
  425.                     if result then
  426.                         scan(turtle)
  427.                         displayButtons()
  428.                         currentlyPlaying(turtle)
  429.                     end
  430.                 end
  431.             end
  432.         end
  433.     end
  434. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement