monitorSide = "front"
radioSide = "back"
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 renderShuffleButton(text, status)
if status == "Shuffling" then
monitor.setTextColor(colors.green)
monitor.write(text)
sleep(3)
status = "status"
renderFooter(state)
else
monitor.setTextColor(colors.gray)
monitor.write(text)
end
end
function renderFooter(state)
monitor.clearLine()
monitor.setCursorPos(22,5)
renderButton("[]", "stopped", state)
renderButton("<<", "rewind", state)
renderButton(">>", "forward", state)
renderButton("[>", "playing", state)
monitor.setCursorPos(35,5)
renderShuffleButton("Shuffle", status)
status = "status"
end
function transmit(action)
rednet.send(turtleId, textutils.serialize({type="jukebox", action=action}))
end
function checkButton(x, y)
if y == 5 then
if x == 22 or x == 23 then
transmit("stop")
elseif x == 24 or x == 25 then
transmit("prev")
state = "rewind"
elseif x == 26 or x == 27 then
transmit("next")
state = "forward"
elseif x == 28 or x == 29 then
transmit("play")
elseif x == 35 or x == 36 then
transmit("shuffle")
status = "Shuffling"
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,1)
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
This is what I tried