Advertisement
Guest User

radio.lua

a guest
Apr 25th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  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 = 1,
  23.   stationName = UI.Text({
  24.     y = 2,
  25.     width = monitor.width,
  26.   }),
  27.   seek = UI.Button({
  28.     y = 6,
  29.     x = 13,
  30.     height = 3,
  31.     event = 'seek',
  32.     text = '   >  ',
  33.   }),
  34.   play = UI.Button({
  35.     y = 6,
  36.     x = 2,
  37.     height = 3,
  38.     event = 'play',
  39.     text = '> / ll',
  40.   }),
  41.   louder = UI.Button({
  42.     y = 8,
  43.     x = 30,
  44.     width = 3,
  45.     event = 'louder',
  46.     text = ' + ',
  47.   }),
  48.   quiet = UI.Button({
  49.     y = 8,
  50.     x = 24,
  51.     event = 'quiet',
  52.     width = 3,
  53.     text = ' - ',
  54.   }),
  55.   volume1 = UI.Window({
  56.     y = monitor.height - 4,
  57.     x = 24,
  58.     height = 1,
  59.     width = 1,
  60.   }),
  61.   volume2 = UI.Window({
  62.     y = monitor.height - 5,
  63.     x = 25,
  64.     height = 2,
  65.     width = 1,
  66.   }),
  67.   volume3 = UI.Window({
  68.     y = monitor.height - 6,
  69.     x = 26,
  70.     height = 3,
  71.     width = 1,
  72.   }),
  73.   volume4 = UI.Window({
  74.     y = monitor.height - 7,
  75.     x = 27,
  76.     height = 4,
  77.     width = 1
  78.   })
  79. })
  80.  
  81. page.volumeControls = {
  82.   page.volume1, page.volume2,
  83.   page.volume4, page.volume5
  84. }
  85.  
  86. function page:eventHandler(event)
  87.   if event.type == 'play' then
  88.     if self.playing then
  89.       self:play(false)
  90.     else
  91.       if not radio.getStackInSlot(1) then
  92.         self:seek()
  93.       end
  94.       self:play(true)
  95.     end
  96.   elseif event.type == 'seek' then
  97.     self:seek()
  98.     self:play(true)
  99.   elseif event.type == 'louder' then
  100.     if self.playing then
  101.       self:setVolume(self.volume + 1)
  102.     end
  103.   elseif event.type == 'quiet' then
  104.     if self.playing then
  105.       self:setVolume(self.volume - 1)
  106.     end
  107.   end
  108. end
  109.  
  110. function page:setVolume(volume)
  111.   volume = math.min(volume, #self.volumeControls)
  112.   volume = math.max(volume, 1)
  113.   self.volume = volume
  114.   print(self.volume)
  115.   for i = 1, volume do
  116.     self.volumeControls[i].backgroundColor = colors.red
  117.   end
  118.   for i = volume + 1, #self.volumeControls do
  119.     self.volumeControls[i].backgroundColor = colors.black
  120.   end
  121.   for i = 1, #self.volumeControls do
  122.     self.volumeControls[i]:clear()
  123.   end
  124.   rs.setAnalogOutput('top', volume)
  125. end
  126.  
  127. function page:seek()
  128.   local slot = TL2.selectOpenSlot() or 0
  129.   repeat
  130.     slot = slot + 1
  131.     if (slot > 16) then
  132.       slot = 1
  133.     end
  134.   until turtle.getItemCount(slot) >= 1
  135.   radio.pushItem(direction, 1, 1)
  136.   radio.pullItem(direction, slot, 1)
  137.   self:updateStationName()
  138. end
  139.  
  140. function page:play(onOff)
  141.   self.playing = onOff
  142.   if self.playing then
  143.     self:setVolume(self.volume)
  144.     self:updateStationName()
  145.   else
  146.     self.stationName.value = 'Paused'
  147.     self.stationName:draw()
  148.     rs.setAnalogOutput('top', 0)
  149.   end
  150. end
  151.  
  152. function page.stationName:draw()
  153.   self:clear()
  154.   self:centeredWrite(1, self.value)
  155. end
  156.  
  157. function page:updateStationName()
  158.   local stack = radio.getStackInSlot(1)
  159.   if stack then
  160.     self.stationName.value = stack.name
  161.     self.stationName:draw()
  162.   end
  163. end
  164.  
  165. UI.pager:setPage(page)
  166.  
  167. page:updateStationName()
  168. page:play(false)
  169.  
  170. Event.pullEvents()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement