Drone library update

This commit is contained in:
Kirigaya Kazuto 2018-11-24 00:45:58 +08:00
parent 8d7835d385
commit eb55fc867c
2 changed files with 110 additions and 36 deletions

View File

@ -1,6 +1,7 @@
drone=component.proxy(component.list("drone")())
drone.setStatusText("Drone v3.0")
modem=component.proxy(component.list("modem")())
drone_version="Drone v3.1"
drone.setStatusText(drone_version .. '\n' .. modem.address)
modem.open(98)
handlers={}
handlers["modem_message"]={}
@ -26,19 +27,22 @@ end)
handle_event=function(raw_event)
if(handlers[raw_event[1]]~=nil) then
for idx,callback in ipairs(handlers[raw_event[1]]) do
pcall(function()
local ok,result=pcall(function()
callback(raw_event)
end)
if(ok and result) then break end
end
end
end
sleep=function(sec)
local deadline=computer.uptime() + sec
local current=computer.uptime()
local deadline=current + sec
while true do
local raw_event=table.pack(computer.pullSignal(deadline-computer.uptime()))
local raw_event=table.pack(computer.pullSignal(deadline-current))
if(raw_event[1]==nil) then break
else handle_event(raw_event)
end
else handle_event(raw_event) end
current=computer.uptime()
if(current<dealine) then break end
end
end

View File

@ -17,9 +17,22 @@ modem.broadcast(98,'modem=component.proxy(component.list("modem")())')
print('Done')
term.clear()
print('Drone Debug Console v1.4.2')
print('Drone Debug Console v1.4.3-beta')
print('Command Prompt (Ctrl+D to exit)')
local function SetResponse(msg)
local a,b=term.getCursor()
local w,h=term.gpu().getResolution()
term.gpu().fill(1,h/2,w,h/2-3,' ')
term.setCursor(1,h/2)
print("Response")
print(msg)
term.setCursor(a,b)
end
local last_response=computer.uptime()
local listener=AddEventListener("modem_message",function(e)
@ -30,7 +43,7 @@ local listener=AddEventListener("modem_message",function(e)
local w,h=term.gpu().getResolution()
term.gpu().fill(1,h,w,1,' ')
local text="distance: " .. e.distance .. " offset: " .. e.data[2] .. " energy:"
local text="device: " .. string.sub(e.senderAddress,1,8) .. " distance: " .. string.format("%.2f",e.distance) .. " offset: " .. string.format("%.2f",e.data[2]) .. " energy:"
term.gpu().set(1,h,text)
if(e.data[3]>3000) then
local temp=term.gpu().setForeground(0x00FF00)
@ -47,21 +60,13 @@ local listener=AddEventListener("modem_message",function(e)
end
term.setCursor(a,b)
elseif(e.data[1]~=nil) then
local a,b=term.getCursor()
local w,h=term.gpu().getResolution()
term.gpu().fill(1,h/2,w,h/2-3,' ')
term.setCursor(1,h/2)
print("Remote Response")
print(e.data[1])
term.setCursor(a,b)
end -- nil response are not rendered.
elseif(e.data[1]~=nil and e.data[1]~='hide_msg') then
SetResponse("[Remote] " .. e.data[1])
end -- nil,'hide_msg' response are not rendered.
end)
local timer=AddTimer(5,function()
local function add_ping_timer()
return AddTimer(5,function()
if(computer.uptime()-last_response>10) then
local a,b=term.getCursor()
@ -76,10 +81,65 @@ local timer=AddTimer(5,function()
end
modem.broadcast(98,"execute_command","modem.send('" .. modem.address .. "',99,'console_info',drone.getOffset(),computer.energy())")
end,-1)
end,-1)
end
local timer=add_ping_timer()
-- Global helper for Console User, will be cleaned if program exit normally.
helper={}
function helper.setRadar(enable)
if(enable) then
if(timer>0) then
return "Radar is already on."
else
timer=add_ping_timer()
return "Radar switched on."
end
else
if(timer>0) then
RemoveTimer(timer)
timer=0
return "Radar switched off."
else
return "Radar is already off."
end
end
end
helper.target_drone=''
function helper.install_step1()
modem.broadcast(98,"modem.send('" .. modem.address .. "',99,'hide_msg','prepare_for_install')")
while true do
local e=WaitEvent(5,"modem_message")
if(e==nil) then
break
elseif(e.event=="modem_message" and
e.port==99 and
e.data[1]~=nil and e.data[1]=='hide_msg' and
e.data[2]~=nil and e.data[2]=='prepare_for_install') then
helper.target_drone=e.senderAddress
SetResponse("[Local] Install step 1: target drone found as: " .. helper.target_drone)
return true
end
end
SetResponse("[Local] Install step 1: no drone response in 5 seconds.")
return false
end
helper.drone_lib=[==[
drone_lib_version='DroneLib v0.1'
return drone_lib_version .. " successfully installed."
]==]
function helper.install_step2()
if(helper.target_drone~=nil and string.len(helper.target_drone)>0) then
SetResponse("[Local] Install step 2: About to perform installation on " .. helper.target_drone)
os.sleep(1)
SetResponse("[Local] Install step 2: Sending data to " .. helper.target_drone)
modem.send(helper.target_drone,98,helper.drone_lib)
end
end
local history={}
while true do
local w,h=term.gpu().getResolution()
term.gpu().fill(1,3,w,h/2-3,' ')
@ -87,8 +147,16 @@ while true do
local str=term.read(history) -- read a line
if(str==nil) then break end
if(string.sub(str,1,1)=='>') then -- Execute it on this machine
local ok,err=pcall(load(string.sub(str,2)))
if(not ok or err~=nil) then
SetResponse("[Local] " .. err)
end
else
modem.broadcast(98,'execute_command',str)
if(#history>25) then table.remove(history,1) end
end
end
term.clear()
@ -97,4 +165,6 @@ modem.close(99)
print("Removing listeners...")
RemoveEventListener(listener)
print("Removing timers...")
RemoveTimer(timer)
if(timer>0) then RemoveTimer(timer) end
print("Cleaning up...")
helper=nil