Advertisement
joebodo

radio.lua

Apr 25th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI('apis.lua')
  2.  
  3. args = { ... }
  4. if #args < 1 then
  5. --  error('radio <direction of turtle in relationship to radio>')
  6. direction = 'down'
  7. else
  8. direction = args[1]
  9. end
  10.  
  11. radio = Peripheral.wrap('openblocks_radio')
  12. rs.setAnalogOutput('top', 0)
  13.  
  14. local monitor = UI.Device({
  15.   deviceType = 'monitor',
  16.   textScale = 0.5,
  17. })
  18.  
  19. UI.setDefaultDevice(monitor)
  20.  
  21. page = UI.Page({
  22.   volume = 6,
  23.   stationBackground = UI.Window({
  24.     y = 2,
  25.     x = 2,
  26.     width = monitor.width - 14,
  27.     height = 3,
  28.     backgroundColor = colors.brown,
  29.   }),
  30.   stationName = UI.Text({
  31.     y = 3,
  32.     x = 3,
  33.     width = monitor.width-16,
  34.     backgroundColor = colors.brown,
  35.   }),
  36.   seek = UI.Button({
  37.     y = 7,
  38.     x = 13,
  39.     height = 3,
  40.     event = 'seek',
  41.     text = '  >>  ',
  42.   }),
  43.   play = UI.Button({
  44.     y = 7,
  45.     x = 2,
  46.     height = 3,
  47.     event = 'play',
  48.     text = '> / ll',
  49.   }),
  50.   louder = UI.Button({
  51.     y = 7,
  52.     x = monitor.width - 15,
  53.     width = 3,
  54.     height = 3,
  55.     event = 'louder',
  56.     text = '+',
  57.   }),
  58.   quiet = UI.Button({
  59.     y = 7,
  60.     x = monitor.width - 20,
  61.     event = 'quiet',
  62.     width = 3,
  63.     height = 3,
  64.     text = '-',
  65.   }),
  66.   volumeDisplay = UI.Text({
  67.     y = 3,
  68.     x = monitor.width - 9,
  69.     width = 4,
  70.   }),
  71.   volume1 = UI.Window({
  72.     y = monitor.height - 1,
  73.     x = monitor.width - 8,
  74.     height = 1,
  75.     width = 1,
  76.     color = colors.white
  77.   }),
  78.   volume2 = UI.Window({
  79.     y = monitor.height - 2,
  80.     x = monitor.width - 7,
  81.     height = 2,
  82.     width = 1,
  83.     color = colors.white
  84.   }),
  85.   volume3 = UI.Window({
  86.     y = monitor.height - 3,
  87.     x = monitor.width - 6,
  88.     height = 3,
  89.     width = 1,
  90.     color = colors.yellow
  91.   }),
  92.   volume4 = UI.Window({
  93.     y = monitor.height - 4,
  94.     x = monitor.width - 5,
  95.     height = 4,
  96.     width = 1,
  97.     color = colors.yellow,
  98.   }),
  99.   volume5 = UI.Window({
  100.     y = monitor.height - 5,
  101.     x = monitor.width - 4,
  102.     height = 5,
  103.     width = 1,
  104.     color = colors.orange,
  105.   }),
  106.   volume6 = UI.Window({
  107.     y = monitor.height - 6,
  108.     x = monitor.width - 3,
  109.     height = 6,
  110.     width = 1,
  111.     color = colors.orange,
  112.   }),
  113.   volume7 = UI.Window({
  114.     y = monitor.height - 7,
  115.     x = monitor.width - 2,
  116.     height = 7,
  117.     width = 1,
  118.     color = colors.red,
  119.   }),
  120.   volume8 = UI.Window({
  121.     y = monitor.height - 8,
  122.     x = monitor.width - 1,
  123.     height = 8,
  124.     width = 1,
  125.     color = colors.red,
  126.   })
  127. })
  128.  
  129. page.volumeControls = {
  130.   page.volume1, page.volume2,
  131.   page.volume3, page.volume4,
  132.   page.volume5, page.volume6,
  133.   page.volume7, page.volume8,
  134. }
  135.  
  136. function page:eventHandler(event)
  137.   if event.type == 'play' then
  138.     if self.playing then
  139.       self:play(false)
  140.     else
  141.       if not radio.getStackInSlot(1) then
  142.         self:seek()
  143.       end
  144.       self:play(true)
  145.     end
  146.   elseif event.type == 'seek' then
  147.     self:seek()
  148.     self:play(true)
  149.   elseif event.type == 'louder' then
  150.     if self.playing then
  151.       self:setVolume(self.volume + 1)
  152.     end
  153.   elseif event.type == 'quiet' then
  154.     if self.playing then
  155.       self:setVolume(self.volume - 1)
  156.     end
  157.   end
  158. end
  159.  
  160. function page:setVolume(volume, displayOnly)
  161.   volume = math.min(volume, 15)
  162.   volume = math.max(volume, 1)
  163.   self.volume = volume
  164.   volume = math.ceil(volume / 2)
  165.  
  166.   for i = 1, volume do
  167.     self.volumeControls[i].backgroundColor =
  168.       self.volumeControls[i].color
  169.   end
  170.   for i = volume + 1, #self.volumeControls do
  171.     self.volumeControls[i].backgroundColor = colors.black
  172.   end
  173.   for i = 1, #self.volumeControls do
  174.     self.volumeControls[i]:clear()
  175.   end
  176.   local percent = math.ceil(self.volume / 15 * 100)
  177.   self.volumeDisplay.value = percent .. '%'
  178.   self.volumeDisplay:draw()
  179.   if not displayOnly then
  180.     rs.setAnalogOutput('top', self.volume)
  181.   end
  182. end
  183.  
  184. function page:seek()
  185.   local slot = TL2.selectOpenSlot() or 0
  186.   repeat
  187.     slot = slot + 1
  188.     if (slot > 16) then
  189.       slot = 1
  190.     end
  191.   until turtle.getItemCount(slot) >= 1
  192.   radio.pushItem(direction, 1, 1)
  193.   radio.pullItem(direction, slot, 1)
  194.   self:updateStationName()
  195. end
  196.  
  197. function page:play(onOff)
  198.   self.playing = onOff
  199.   if self.playing then
  200.     self:setVolume(self.volume)
  201.     self:updateStationName()
  202.   else
  203. --    self.stationName.value = 'Paused'
  204.     self.stationName:draw()
  205.     rs.setAnalogOutput('top', 0)
  206.   end
  207. end
  208.  
  209. function page.stationName:draw()
  210.   self:clear()
  211.   self:centeredWrite(1, self.value)
  212. end
  213.  
  214. function page:updateStationName()
  215.   local stack = radio.getStackInSlot(1)
  216.  
  217.   if stack then
  218.     self.stationName.value = stack.name
  219.     self.stationName:draw()
  220.   end
  221. end
  222.  
  223. Event.addHandler('heartbeat', function()
  224.   if not page.playing then
  225.     if page.stationName.value == '' then
  226.       page:updateStationName()
  227.     else
  228.       page.stationName.value = ''
  229.       page.stationName:draw()
  230.     end
  231.   end
  232. end)
  233.  
  234. UI.pager:setPage(page)
  235.  
  236. page:updateStationName()
  237. page:play(false)
  238. page:setVolume(page.volume, true)
  239.  
  240. Logger.filter('event')
  241.  
  242. Event.heartbeat(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement