Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- RAC Media Player v2.0 by neonerZ (computer script)
- -- More Info: https://www.youtube.com/watch?v=tfSSdY0jQQk
- --set variables
- modem="right" -- modem position
- monitor = "left" -- monitor position
- turtle = 8 -- ID of turtle DJ
- radioCrystal="15254" -- item ID of radio crystal
- -- Feel free to do whatever you want with this.
- -- Edit it, incorporate it into your own project, eat it, I don't care.
- -- I only ask if you redistribute this or edit it/incorporate it into
- -- your own project you give me a shout out (In the case of including
- -- this into something you release. Send me a message, I'd love to
- -- check it out). This is only a friendly suggestion, you are
- -- welcome to be a jerk and claim this as your own
- -- don't edit below here
- m = peripheral.wrap(monitor)
- rednet.open(modem)
- local button={}
- --Function to create button table
- function setTable(name, text, xmin, ymin, width, height, onFreq, offFreq, bcolor, tcolor, extra)
- button[name] = {}
- button[name]["text"] = text
- button[name]["active"] = false
- button[name]["xmin"] = xmin
- button[name]["ymin"] = ymin
- button[name]["width"] = width
- button[name]["height"] = height
- button[name]["onFreq"] = onFreq
- button[name]["offFreq"] = offFreq
- button[name]["bcolor"] = bcolor
- button[name]["tcolor"] = tcolor
- button[name]["extra"] = extra
- end
- --Toggle button on and off
- function toggleButton(name, state)
- if state=="on" then
- button[name]["active"] = true
- else
- button[name]["active"] = false
- end
- end
- --Draw button on screen
- function drawButton(x, y, width, height, bcolor, tcolor, text)
- m.setBackgroundColor(bcolor)
- --Draw the background
- for i=1,height do
- m.setCursorPos(x,y+i-1)
- m.write(string.rep(" ", width))
- end
- -- m.setBackgroundColor(colors.black)
- --Write the text
- m.setTextColor(tcolor)
- local textX = x + math.floor(width/2 - string.len(text)/2)
- local textY = y + math.floor(height/2)
- m.setCursorPos(textX, textY)
- m.write(text)
- end
- --Check if a button within the table has been clicked
- --(returns button name if matches, or false if not)
- function checkxy(x, y)
- returnCheckxy=false
- for name, data in pairs(button) do
- if data["height"] == 1 then
- ymax = data["ymin"]
- else
- ymax = data["height"] + data["ymin"]
- end
- if data["width"] == 1 then
- xmax = data["xmin"]
- else
- xmax = data["width"] + data["xmin"]
- end
- if y>=data["ymin"] and y <= ymax then
- if x>=data["xmin"] and x<= xmax-1 then
- returnCheckxy = {name = name,
- text = data["text"],
- onFreq = data["onFreq"],
- offFreq = data["offFreq"],
- bcolor = data["bcolor"],
- tcolor = data["tcolor"],
- active = data["active"],
- extra = data["extra"]}
- end
- end
- end
- end
- --Draw out GUI using the buttons registered in the regButtons function
- function displayButtons()
- m.setBackgroundColor(colors.black)
- for name,data in pairs(button) do
- text=data["text"]
- xmin=data["xmin"]
- ymin=data["ymin"]
- width=data["width"]
- height=data["height"]
- tcolor=data["tcolor"]
- active=data["active"]
- if active == true then
- bcolor = colors.lime
- else
- bcolor=data["bcolor"]
- end
- drawButton(xmin, ymin, width, height, bcolor, tcolor, text)
- end
- m.setBackgroundColor(colors.black)
- end
- function checkMonitorSize()
- local w, h = m.getSize()
- if w < 39 then
- printError("Error: monitor width too small, please make at least 4 monitors wide")
- return false
- end
- if h < 19 then
- printError("Error: monitor height too small, please make at least 3 monitors tall")
- return false
- end
- mWidth = w
- mHeight = h
- return true
- end
- function drawGUI()
- local startPos
- local volDown
- button={}
- m.clear()
- checkMonitorSize()
- m.setCursorPos(1,1)
- m.setBackgroundColor(colors.black)
- for i = 1,mWidth do
- m.write(" ")
- end
- startPos=(mWidth-21)/2+1
- local playButton=(mWidth-26)/2+1
- local stopButton=playButton+5
- local ejectButton=stopButton+5
- m.setCursorPos(startPos,1)
- m.setTextColor(colors.white)
- if (mWidth % 2 == 0) then
- m.write("RAC Media Player v2.0")
- volDown=ejectButton+6
- else
- m.write("RAC Media Player v2.0")
- volDown=ejectButton+7
- end
- local volume=volDown+1
- local volUp=volume+3
- local scanButton=volUp+2
- m.setBackgroundColor(colors.gray)
- m.setCursorPos(1,2)
- for i = 1,mWidth do
- m.write(" ")
- end
- m.setBackgroundColor(colors.lightGray)
- m.setCursorPos(1,3)
- for i = 1,mWidth do
- m.write(" ")
- end
- setTable("play","play",math.floor(playButton),2,4,1,0,0,colors.gray,colors.white,0)
- setTable("stop","stop",math.floor(stopButton),2,4,1,0,0,colors.gray,colors.white,0)
- setTable("eject","eject",math.floor(ejectButton),2,5,1,0,0,colors.gray,colors.white,0)
- setTable("voldown"," -",math.floor(volDown)-1,2,2,1,0,0,colors.gray,colors.white,0)
- setTable("volup","+ ",math.floor(volUp),2,2,1,0,0,colors.gray,colors.white,0)
- setTable("scan","scan",math.floor(scanButton),2,4,1,0,0,colors.gray,colors.white,0)
- m.setCursorPos(volDown+1,2)
- m.setBackgroundColor(colors.gray)
- m.setTextColor(colors.white)
- m.write("vol")
- m.setBackgroundColor(colors.black)
- end
- function scan(id)
- data = {}
- track={}
- drawGUI()
- local trackCount = 0
- local w, h = m.getSize()
- rednet.send(id, "scan:no")
- local id, msg, dis = rednet.receive()
- data = textutils.unserialize(msg)
- count = 0
- for name, line in pairsByKeys(data) do
- data[name]=line
- end
- for name, line in pairsByKeys(data) do
- count=count+1
- local title=name
- for k,v in string.gmatch(line, "(.+):(.+)") do
- if v == radioCrystal then
- track[count]=title..":radio"
- else
- for k,v in string.gmatch(title, "(.+)-(.+)") do
- name=string.gsub(v, " ", "", 1)
- track[count]=name..":"..k
- end
- end
- end
- end
- for _ in pairs(track) do trackCount = trackCount + 1 end
- local buttonAmmount = math.floor((w-2)/11)
- local buttonLeftover = ((w-(buttonAmmount*11))-(buttonAmmount-1))/2
- local rows = math.ceil(count/buttonAmmount)
- local num = 1
- local row = 5
- for i = 1, rows do
- if math.floor(buttonLeftover) == buttonLeftover then
- buttonPos = buttonLeftover
- else
- buttonPos = math.floor(buttonLeftover)
- extraSpace=true
- end
- local totalSpace = ((buttonAmmount-1)*2)+(buttonAmmount*11)
- if buttonPos < 0 then
- buttonPos = 0
- end
- for perRow = 1,buttonAmmount do
- local skip = 0
- if track[num] == nil then
- skip = 1
- else
- local title, artist = string.match(track[num], "(.+):(.+)")
- if extraSpace and perRow ~= 1 and totalSpace <= w then
- setTable(title,string.sub(title,1,11),buttonPos+1,row,11,1,"play",0,colors.blue,colors.white,artist)
- buttonPos=buttonPos+13
- elseif perRow == 1 and buttonPos == 1 and totalSpace > w then
- setTable(title,string.sub(title,1,11),2,row,11,1,"play",0,colors.blue,colors.white,artist)
- buttonPos=buttonPos+13
- else
- setTable(title,string.sub(title,1,11),buttonPos,row,11,1,"play",0,colors.blue,colors.white,artist)
- buttonPos=buttonPos+12
- end
- num=num+1
- end
- end
- row=row+2
- end
- -- setTable("test2","test2",15,4,11,1,0,0,colors.blue,colors.white,0)
- -- setTable("test3","test3",28,4,11,1,0,0,colors.blue,colors.white,0)
- end
- function alive(id2)
- m.clear()
- m.setCursorPos(1,1)
- m.setTextColor(colors.white)
- m.setBackgroundColor(colors.black)
- m.write("Searching for turtle...")
- m.setCursorBlink(true)
- msg = nil
- print("connect try #1")
- rednet.send(id2,"alive:none")
- local id, msg, dis = rednet.receive(5)
- if msg == nil then
- print("connect try #2")
- rednet.send(id2,"alive:none")
- id, msg, dis = rednet.receive(5)
- end
- if msg == nil then
- print("connect try #3")
- rednet.send(id2,"alive:none")
- id, msg, dis = rednet.receive(5)
- end
- m.setCursorBlink(false)
- if msg ~= nil and id == id2 then
- print("connected to turtle #"..id2)
- result=true
- else
- printError("Turtle DJ not responding, are you sure it's alive?")
- m.clear()
- m.setCursorPos(1,1)
- m.write("Turtle Timeout: Is turtle alive?")
- result=false
- end
- end
- function pairsByKeys (t, f)
- local a = {}
- for n in pairs(t) do table.insert(a, n) end
- table.sort(a, f)
- local i = 0 -- iterator variable
- local iter = function () -- iterator function
- i = i + 1
- if a[i] == nil then return nil
- else return a[i], t[a[i]]
- end
- end
- return iter
- end
- function checkProgress(preMessage,successMessage,failureMessage)
- msg = nil
- messageBox(preMessage)
- m.setCursorBlink(true)
- local id, msg, dis = rednet.receive(5)
- if msg == nil then
- messageBox(failureMessage)
- result = false
- print("FAILED!")
- elseif id == turtle then
- print("SUCCESS")
- messageBox(successMessage)
- result = true
- end
- m.setCursorBlink(false)
- end
- function clearMessageBox()
- m.setBackgroundColor(colors.lightGray)
- m.setCursorPos(1,3)
- for i = 1,mWidth do
- m.write(" ")
- end
- end
- function messageBox(message)
- local w, h = m.getSize()
- clearMessageBox()
- message=string.sub(message,1,w-3)
- stringLength = string.len(message)
- startWidth = ((w-stringLength)/2)
- m.setCursorPos(startWidth+1,3)
- m.setTextColor(colors.black)
- m.setBackgroundColor(colors.lightGray)
- m.write(message)
- m.setBackgroundColor(colors.black)
- end
- function currentlyPlaying(id,load)
- messageBox("Checking track...")
- m.setCursorBlink(true)
- rednet.send(id,"track:none")
- local id, msg, dis = rednet.receive()
- if (msg == "none") and id == turtle then
- messageBox("No track loaded")
- else
- messageBox("Currently Loaded: "..msg)
- end
- m.setCursorBlink(false)
- end
- if checkMonitorSize() then
- print("Starting up RAC Media Player 2.0")
- alive(turtle)
- if not result then
- return
- end
- print("SUCCESS")
- scan(turtle)
- currentlyPlaying(turtle)
- displayButtons()
- while true do
- local e, side, x,y = os.pullEvent("monitor_touch")
- checkxy(x,y)
- if returnCheckxy ~= false then
- print(">you pressed "..returnCheckxy["name"])
- if returnCheckxy["name"]=="play" then
- rednet.send(turtle,"play:none")
- checkProgress("Playing track...","Track loaded","Turtle Timeout: Is turtle alive?")
- if result then currentlyPlaying(turtle) end
- end
- if returnCheckxy["name"]=="stop" then
- rednet.send(turtle,"stop:none")
- checkProgress("Stopping Track...","Track Stopped","Turtle Timeout: Is turtle alive?")
- if result then currentlyPlaying(turtle) end
- end
- if returnCheckxy["name"]=="eject" then
- rednet.send(turtle,"stopeject:none")
- checkProgress("Ejecting track...","Track ejected","Turtle Timeout: Is turtle alive?")
- scan(turtle)
- if result then currentlyPlaying(turtle) end
- displayButtons()
- end
- if returnCheckxy["name"]=="scan" then
- alive(turtle)
- if not result then
- return
- end
- print("SUCCESS")
- scan(turtle)
- currentlyPlaying(turtle)
- displayButtons()
- end
- if returnCheckxy["name"]=="voldown" then
- rednet.send(turtle,"volume:0")
- local id, msg, dis = rednet.receive(2)
- if msg ~= "SUCCESS" and id == turtle then printError("No response from turtle, is it alive?") end
- end
- if returnCheckxy["name"]=="volup" then
- rednet.send(turtle,"volume:1")
- local id, msg, dis = rednet.receive(2)
- if msg ~= "SUCCESS" and id == turtle then printError("No response from turtle, is it alive?") end
- end
- if returnCheckxy["onFreq"]=="play" then
- if returnCheckxy["extra"] == "radio" then
- print("playt:"..returnCheckxy["extra"].."- "..returnCheckxy["name"])
- rednet.send(turtle,"playt:"..returnCheckxy["name"])
- checkProgress("Loading Radio Station...","Radio loaded","Turtle Timeout: Is turtle alive?")
- if result then
- scan(turtle)
- displayButtons()
- currentlyPlaying(turtle)
- end
- else
- print("playt:"..returnCheckxy["extra"].."- "..returnCheckxy["name"])
- rednet.send(turtle,"playt:"..returnCheckxy["extra"].."- "..returnCheckxy["name"])
- checkProgress("Playing track...","Track loaded","Turtle Timeout: Is turtle alive?")
- print(result)
- if result then
- scan(turtle)
- displayButtons()
- currentlyPlaying(turtle)
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement