Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI('apis.lua')
- args = { ... }
- if #args < 1 then
- -- error('radio <direction of turtle in relationship to radio>')
- direction = 'down'
- else
- direction = args[1]
- end
- radio = Peripheral.wrap('openblocks_radio')
- rs.setAnalogOutput('top', 0)
- local monitor = UI.Device({
- deviceType = 'monitor',
- textScale = 0.5,
- })
- UI.setDefaultDevice(monitor)
- page = UI.Page({
- volume = 1,
- stationName = UI.Text({
- y = 2,
- width = monitor.width,
- }),
- seek = UI.Button({
- y = 6,
- x = 13,
- height = 3,
- event = 'seek',
- text = ' > ',
- }),
- play = UI.Button({
- y = 6,
- x = 2,
- height = 3,
- event = 'play',
- text = '> / ll',
- }),
- louder = UI.Button({
- y = 8,
- x = 30,
- width = 3,
- event = 'louder',
- text = ' + ',
- }),
- quiet = UI.Button({
- y = 8,
- x = 24,
- event = 'quiet',
- width = 3,
- text = ' - ',
- }),
- volume1 = UI.Window({
- y = monitor.height - 4,
- x = 24,
- height = 1,
- width = 1,
- }),
- volume2 = UI.Window({
- y = monitor.height - 5,
- x = 25,
- height = 2,
- width = 1,
- }),
- volume3 = UI.Window({
- y = monitor.height - 6,
- x = 26,
- height = 3,
- width = 1,
- }),
- volume4 = UI.Window({
- y = monitor.height - 7,
- x = 27,
- height = 4,
- width = 1
- })
- })
- page.volumeControls = {
- page.volume1, page.volume2,
- page.volume4, page.volume5
- }
- function page:eventHandler(event)
- if event.type == 'play' then
- if self.playing then
- self:play(false)
- else
- if not radio.getStackInSlot(1) then
- self:seek()
- end
- self:play(true)
- end
- elseif event.type == 'seek' then
- self:seek()
- self:play(true)
- elseif event.type == 'louder' then
- if self.playing then
- self:setVolume(self.volume + 1)
- end
- elseif event.type == 'quiet' then
- if self.playing then
- self:setVolume(self.volume - 1)
- end
- end
- end
- function page:setVolume(volume)
- volume = math.min(volume, #self.volumeControls)
- volume = math.max(volume, 1)
- self.volume = volume
- print(self.volume)
- for i = 1, volume do
- self.volumeControls[i].backgroundColor = colors.red
- end
- for i = volume + 1, #self.volumeControls do
- self.volumeControls[i].backgroundColor = colors.black
- end
- for i = 1, #self.volumeControls do
- self.volumeControls[i]:clear()
- end
- rs.setAnalogOutput('top', volume)
- end
- function page:seek()
- local slot = TL2.selectOpenSlot() or 0
- repeat
- slot = slot + 1
- if (slot > 16) then
- slot = 1
- end
- until turtle.getItemCount(slot) >= 1
- radio.pushItem(direction, 1, 1)
- radio.pullItem(direction, slot, 1)
- self:updateStationName()
- end
- function page:play(onOff)
- self.playing = onOff
- if self.playing then
- self:setVolume(self.volume)
- self:updateStationName()
- else
- self.stationName.value = 'Paused'
- self.stationName:draw()
- rs.setAnalogOutput('top', 0)
- end
- end
- function page.stationName:draw()
- self:clear()
- self:centeredWrite(1, self.value)
- end
- function page:updateStationName()
- local stack = radio.getStackInSlot(1)
- if stack then
- self.stationName.value = stack.name
- self.stationName:draw()
- end
- end
- UI.pager:setPage(page)
- page:updateStationName()
- page:play(false)
- Event.pullEvents()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement