Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Configure these variabls to point to the side your monitor and radio are on
- monitorSide = "top"
- radioSide = "back"
- -- Don't touch these ones
- startTime = 0
- jbRunTime = 0
- turtleId = 0
- discInfo = nil
- state = "startup"
- monitor = nil
- function checkPeripheral(side, type)
- if not(peripheral.isPresent(side) and peripheral.getType(side) == type) then
- print("Please attach a " .. type .. " to the " .. side .. " side")
- exit()
- end
- end
- function renderButton(text, active, state)
- if state == active then
- monitor.setTextColor(colors.green)
- monitor.write(text)
- else
- monitor.setTextColor(colors.gray)
- monitor.write(text)
- end
- end
- function renderFooter(state)
- monitor.setCursorPos(1,5)
- monitor.clearLine()
- renderButton("[]", "stopped", state)
- renderButton("<<", "rewind", state)
- renderButton(">>", "forward", state)
- renderButton("[>", "playing", state)
- end
- function transmit(action)
- rednet.send(turtleId, textutils.serialize({type="jukebox", action=action}))
- end
- function checkButton(x, y)
- if y == 5 then
- if x == 1 or x == 2 then
- transmit("stop")
- elseif x == 3 or x == 4 then
- transmit("prev")
- state = "rewind"
- elseif x == 5 or x == 6 then
- transmit("next")
- state = "forward"
- elseif x == 7 or x == 8 then
- transmit("play")
- end
- renderFooter(state)
- end
- end
- function renderDuration()
- monitor.setCursorPos(2,3)
- monitor.clearLine()
- local timeLeft = math.floor(discInfo['duration'] - (jbRunTime + (os.clock() - startTime)))
- if timeLeft < 0 then
- timeLeft = 0
- end
- monitor.write(discInfo['duration'] .. "/" .. timeLeft)
- end
- checkPeripheral(monitorSide, "monitor")
- checkPeripheral(radioSide, "modem")
- monitor = peripheral.wrap(monitorSide)
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.write("Startup....")
- monitor.setCursorPos(2,2)
- monitor.write("Looking for jukebox...")
- rednet.open(radioSide)
- rednet.broadcast(textutils.serialize({action="announce", type="jukebox", device="monitor"}))
- countDownTimer = os.startTimer(1)
- while true do
- ev,p1,p2,p3 = os.pullEvent()
- if ev == "rednet_message" then
- data = textutils.unserialize(p2)
- if data["type"] == "jukebox" then
- if data["action"] == "acknowledge" or (data["action"] == "announce" and data["device"] == "jukebox") then
- turtleId = p1
- if data["action"] == "announce" then
- -- say hi back
- rednet.send(computerId, textutils.serialize({type="jukebox", action="acknowledge"}))
- end
- elseif data["action"] == "playing" then
- if not(state == "playing") then
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.setTextColor(colors.gray)
- monitor.write("Now playing:")
- state="playing"
- renderFooter(state)
- end
- monitor.setCursorPos(2,2)
- monitor.clearLine()
- monitor.setTextColor(colors.white)
- discInfo = data["discInfo"]
- monitor.write(data["discInfo"]["title"])
- jbRunTime = data["runTime"]
- startTime = os.clock()
- renderDuration()
- elseif data["action"] == "stopped" then
- if not(state == "stopped") then
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.setTextColor(colors.gray)
- monitor.write("Stopped...")
- state="stopped"
- renderFooter(state)
- end
- end
- end
- elseif ev == "timer" then
- currentTimer = os.startTimer(1)
- if state == "playing" then
- renderDuration()
- end
- elseif ev == "monitor_touch" then
- checkButton(p2, p3)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment