This commit is contained in:
kepler155c@gmail.com
2019-01-27 00:27:05 -05:00
parent 04815b2d09
commit e264d98739
8 changed files with 53 additions and 9 deletions

View File

@@ -1,254 +0,0 @@
local Event = require('event')
local UI = require('ui')
local colors = _G.colors
local device = _G.device
local turtle = _G.turtle
if not turtle then
error('This program can only be run on a turtle')
end
local radio = device.drive or error('No drive attached')
if radio.side ~= 'top' and radio.side ~= 'bottom' then
error('Disk drive must be above or below turtle')
end
UI:configure('Music', ...)
UI.Button.defaults.backgroundFocusColor = colors.gray
local page = UI.Page({
volume = 15,
stationName = UI.Text({
y = 2,
x = 2,
ex = -14,
height = 3,
backgroundColor = colors.black,
}),
play = UI.Button({
y = -4,
x = 2,
height = 3,
event = 'play',
text = '> / ll',
}),
seek = UI.Button({
y = -4,
x = 12,
height = 3,
event = 'seek',
text = ' >> ',
}),
quiet = UI.Button({
y = -4,
x = -19,
event = 'quiet',
width = 3,
height = 3,
text = '-',
}),
louder = UI.Button({
y = -4,
x = -14,
width = 3,
height = 3,
event = 'louder',
text = '+',
}),
volumeDisplay = UI.Text({
y = 3,
x = -9,
width = 4,
}),
volume1 = UI.Window({
y = -2,
x = -9,
height = 1,
width = 1,
color = colors.white
}),
volume2 = UI.Window({
y = -3,
x = -8,
height = 2,
width = 1,
color = colors.white
}),
volume3 = UI.Window({
y = -4,
x = -7,
height = 3,
width = 1,
color = colors.yellow
}),
volume4 = UI.Window({
y = -5,
x = -6,
height = 4,
width = 1,
color = colors.yellow,
}),
volume5 = UI.Window({
y = -6,
x = -5,
height = 5,
width = 1,
color = colors.orange,
}),
volume6 = UI.Window({
y = -7,
x = -4,
height = 6,
width = 1,
color = colors.orange,
}),
volume7 = UI.Window({
y = -8,
x = -3,
height = 7,
width = 1,
color = colors.red,
}),
volume8 = UI.Window({
y = -9,
x = -2,
height = 8,
width = 1,
color = colors.red,
})
})
page.volumeControls = {
page.volume1, page.volume2,
page.volume3, page.volume4,
page.volume5, page.volume6,
page.volume7, page.volume8,
}
function page:eventHandler(event)
if event.type == 'play' then
self:play(not self.playing)
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, 15)
volume = math.max(volume, 1)
self.volume = volume
volume = math.ceil(volume / 2)
for i = 1, volume do
self.volumeControls[i].backgroundColor =
self.volumeControls[i].color
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
local percent = math.ceil(self.volume / 15 * 100)
self.volumeDisplay.value = percent .. '%'
self.volumeDisplay:draw()
end
function page:seek()
local actions = {
top = {
suck = turtle.suckUp,
drop = turtle.dropUp,
},
bottom = {
suck = turtle.suckDown,
drop = turtle.dropDown,
},
}
local slot = turtle.selectOpenSlot()
actions[radio.side].suck()
repeat
slot = slot + 1
if (slot > 16) then
slot = 1
end
until turtle.getItemCount(slot) >= 1
turtle.select(slot)
actions[radio.side].drop()
self:updateStationName()
end
function page:play(onOff)
self.playing = onOff
if self.playing then
if not radio.hasAudio() then
self:seek()
end
self:updateStationName()
radio.playAudio()
Event.addNamedTimer('songTimer', 180, false, function()
if self.playing then
self:seek()
self:play(true)
self:sync()
end
end)
else
radio.stopAudio()
end
end
function page.stationName:draw()
self:clear()
self:centeredWrite(2, self.value)
end
function page:updateStationName()
local title = radio.getAudioTitle()
if title then
self.stationName.value = title
self.stationName:draw()
end
end
Event.onInterval(1, function()
if not page.playing then
if page.stationName.value == '' then
page:updateStationName()
else
page.stationName.value = ''
page.stationName:draw()
end
page:sync()
end
end)
page:play(true)
page:setVolume(page.volume, true)
UI:setPage(page)
turtle.setStatus('Jamming')
UI:pullEvents()
turtle.setStatus('idle')
page:play(false)
UI.term:reset()

View File

@@ -1,5 +1,3 @@
_G.requireInjector(_ENV)
local Point = require('point')
local device = _G.device

View File

@@ -1,151 +0,0 @@
local GPS = require('gps')
local Socket = require('socket')
local UI = require('ui')
local Util = require('util')
local multishell = _ENV.multishell
local textutils = _G.textutils
multishell.setTitle(multishell.getCurrent(), 'Shapes')
local args = { ... }
local turtleId = args[1] or error('Supply turtle ID')
turtleId = tonumber(turtleId)
local levelScript = [[
requireInjector(getfenv(1))
local Util = require('util')
local s, m = turtle.run(function()
turtle.addFeatures('level')
turtle.setStatus('Leveling')
if turtle.enableGPS() then
local pt = Util.shallowCopy(turtle.point)
local s, m = pcall(function()
turtle.level(data.startPt, data.endPt, data.firstPt)
end)
turtle.pathfind(pt)
if not s and m then
error(m)
end
end
end)
if not s then
error(m)
end
]]
local data = Util.readTable('/usr/config/shapes') or { }
local page = UI.Page {
titleBar = UI.TitleBar { title = 'Shapes' },
info = UI.Window { x = 5, y = 3, height = 1 },
startCoord = UI.Button { x = 2, y = 6, text = 'Start ', event = 'startCoord' },
endCoord = UI.Button { x = 2, y = 8, text = 'End ', event = 'endCoord' },
supplies = UI.Button { x = 2, y = 10, text = 'Supplies', event = 'supplies' },
first = UI.Button { x = 2, y = 11, text = 'First', event = 'firstCoord' },
cancel = UI.Button { x = 2, y = -3, text = 'Abort', event = 'cancel' },
begin = UI.Button { x = -8, y = -3, text = 'Begin', event = 'begin' },
accelerators = { q = 'quit' },
notification = UI.Notification(),
statusBar = UI.StatusBar(),
}
function page.info:draw()
local function size(a, b)
return (math.abs(a.x - b.x) + 1) *
(math.abs(a.y - b.y) + 1) *
(math.abs(a.z - b.z) + 1)
end
self:clear()
if not data.startPt then
self:write(1, 1, 'Set starting corner')
elseif not data.endPt then
self:write(1, 1, 'Set ending corner')
else
self:write(1, 1, 'Blocks: ' .. size(data.startPt, data.endPt))
end
end
function page:getPoint()
local pt = GPS.getPoint()
if not pt then
self.notification:error('GPS not available')
end
return pt
end
function page:runFunction(id, script)
--Util.writeFile('script.tmp', script)
self.notification:info('Connecting')
local fn, msg = loadstring(script, 'script')
if not fn then
self.notification:error('Error in script')
--debug(msg)
return
end
local socket = Socket.connect(id, 161)
if not socket then
self.notification:error('Unable to connect')
return
end
socket:write({ type = 'script', args = script })
socket:close()
self.notification:success('Sent')
end
function page:eventHandler(event)
if event.type == 'startCoord' then
data.startPt = self:getPoint()
if data.startPt then
self.statusBar:setStatus('starting corner set')
Util.writeTable('/usr/config/shapes', data)
end
self:draw()
elseif event.type == 'endCoord' then
data.endPt = self:getPoint()
if data.endPt then
self.statusBar:setStatus('ending corner set')
Util.writeTable('/usr/config/shapes', data)
end
self:draw()
elseif event.type == 'firstCoord' then
data.firstPt = self:getPoint()
if data.firstPt then
self.statusBar:setStatus('first point set')
Util.writeTable('/usr/config/shapes', data)
end
self:draw()
elseif event.type == 'supplies' then
data.suppliesPt = self:getPoint()
if data.suppliesPt then
self.statusBar:setStatus('supplies location set')
Util.writeTable('/usr/config/shapes', data)
end
elseif event.type == 'begin' then
if data.startPt and data.endPt then
local s = 'local data = ' .. textutils.serialize(data) .. levelScript
self:runFunction(turtleId, s)
else
self.notification:error('Corners not set')
end
self.statusBar:setStatus('')
elseif event.type == 'cancel' then
self:runFunction(turtleId, 'turtle.abort(true)')
self.statusBar:setStatus('')
else
return UI.Page.eventHandler(self, event)
end
return true
end
UI:setPage(page)
UI:pullEvents()