fremnet

Monitor

Feb 24th, 2013
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.43 KB | None | 0 0
  1. -- Configure these variabls to point to the side your monitor and radio are on
  2. monitorSide = "top"
  3. radioSide   = "back"
  4.  
  5. -- Don't touch these ones
  6. startTime = 0
  7. jbRunTime = 0
  8. turtleId  = 0
  9. discInfo  = nil
  10. state     = "startup"
  11. monitor   = nil
  12.  
  13. function checkPeripheral(side, type)
  14.     if not(peripheral.isPresent(side) and peripheral.getType(side) == type) then
  15.         print("Please attach a " .. type .. " to the " .. side .. " side")
  16.         exit()
  17.     end
  18. end
  19.  
  20. function renderButton(text, active, state)
  21.     if state == active then
  22.         monitor.setTextColor(colors.green)
  23.         monitor.write(text)
  24.     else
  25.         monitor.setTextColor(colors.gray)
  26.         monitor.write(text)
  27.     end
  28. end
  29.  
  30. function renderFooter(state)
  31.     monitor.setCursorPos(1,5)
  32.     monitor.clearLine()
  33.  
  34.     renderButton("[]", "stopped", state)
  35.     renderButton("<<", "rewind", state)
  36.     renderButton(">>", "forward", state)
  37.     renderButton("[>", "playing", state)
  38.    
  39. end
  40.  
  41. function transmit(action)
  42.     rednet.send(turtleId, textutils.serialize({type="jukebox", action=action}))
  43. end
  44.  
  45. function checkButton(x, y)
  46.     if y == 5 then
  47.         if x == 1 or x == 2 then
  48.             transmit("stop")
  49.         elseif x == 3 or x == 4 then
  50.             transmit("prev")
  51.             state = "rewind"
  52.         elseif x == 5 or x == 6 then
  53.             transmit("next")
  54.             state = "forward"
  55.         elseif x == 7 or x == 8 then
  56.             transmit("play")
  57.         end
  58.         renderFooter(state)
  59.     end
  60. end
  61.  
  62. function renderDuration()
  63.     monitor.setCursorPos(2,3)
  64.     monitor.clearLine()
  65.     local timeLeft = math.floor(discInfo['duration'] - (jbRunTime + (os.clock() - startTime)))
  66.     if timeLeft < 0 then
  67.         timeLeft = 0
  68.     end
  69.     monitor.write(discInfo['duration'] .. "/" .. timeLeft)
  70. end
  71.  
  72. checkPeripheral(monitorSide, "monitor")
  73. checkPeripheral(radioSide, "modem")
  74.  
  75. monitor = peripheral.wrap(monitorSide)
  76. monitor.clear()
  77. monitor.setCursorPos(1,1)
  78. monitor.write("Startup....")
  79. monitor.setCursorPos(2,2)
  80. monitor.write("Looking for jukebox...")
  81.  
  82. rednet.open(radioSide)
  83. rednet.broadcast(textutils.serialize({action="announce", type="jukebox", device="monitor"}))
  84.  
  85. countDownTimer = os.startTimer(1)
  86.  
  87. while true do
  88.     ev,p1,p2,p3 = os.pullEvent()
  89.     if ev == "rednet_message" then
  90.         data = textutils.unserialize(p2)
  91.         if data["type"] == "jukebox" then
  92.             if data["action"] == "acknowledge" or (data["action"] == "announce" and data["device"] == "jukebox") then
  93.                 turtleId = p1
  94.                 if data["action"] == "announce" then
  95.                     -- say hi back
  96.                     rednet.send(computerId, textutils.serialize({type="jukebox", action="acknowledge"}))
  97.                 end
  98.             elseif data["action"] == "playing" then
  99.                 if not(state == "playing") then
  100.                     monitor.clear()
  101.                     monitor.setCursorPos(1,1)
  102.                     monitor.setTextColor(colors.gray)
  103.                     monitor.write("Now playing:")
  104.                     state="playing"
  105.                     renderFooter(state)
  106.                 end
  107.                 monitor.setCursorPos(2,2)
  108.                 monitor.clearLine()
  109.                 monitor.setTextColor(colors.white)
  110.                 discInfo = data["discInfo"]
  111.                 monitor.write(data["discInfo"]["title"])
  112.                 jbRunTime = data["runTime"]
  113.                 startTime = os.clock()
  114.                 renderDuration()
  115.             elseif data["action"] == "stopped" then
  116.                 if not(state == "stopped") then
  117.                     monitor.clear()
  118.                     monitor.setCursorPos(1,1)
  119.                     monitor.setTextColor(colors.gray)
  120.                     monitor.write("Stopped...")
  121.                     state="stopped"
  122.                     renderFooter(state)
  123.                 end
  124.             end
  125.         end
  126.     elseif ev == "timer" then
  127.         currentTimer = os.startTimer(1)
  128.         if state == "playing" then
  129.             renderDuration()
  130.         end
  131.     elseif ev == "monitor_touch" then
  132.         checkButton(p2, p3)
  133.     end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment