From eb55fc867ced82cd886a8f95352075c05a4b6c9b Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Sat, 24 Nov 2018 00:45:58 +0800 Subject: [PATCH] Drone library update --- drone_bios.lua | 16 +++--- drone_console.lua | 130 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 110 insertions(+), 36 deletions(-) diff --git a/drone_bios.lua b/drone_bios.lua index 0a0eafe..00b4797 100644 --- a/drone_bios.lua +++ b/drone_bios.lua @@ -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(current3000) then local temp=term.gpu().setForeground(0x00FF00) @@ -47,39 +60,86 @@ 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() - if(computer.uptime()-last_response>10) then - local a,b=term.getCursor() +local function add_ping_timer() + return AddTimer(5,function() + if(computer.uptime()-last_response>10) then + local a,b=term.getCursor() - local w,h=term.gpu().getResolution() - term.gpu().fill(1,h,w,1,' ') + local w,h=term.gpu().getResolution() + term.gpu().fill(1,h,w,1,' ') - local temp=term.gpu().setForeground(0xFF0000) - term.gpu().set(1,h,"No drone in range.") - term.gpu().setForeground(temp) + local temp=term.gpu().setForeground(0xFF0000) + term.gpu().set(1,h,"No drone in range.") + term.gpu().setForeground(temp) - term.setCursor(a,b) + term.setCursor(a,b) + end + + modem.broadcast(98,"execute_command","modem.send('" .. modem.address .. "',99,'console_info',drone.getOffset(),computer.energy())") + 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 - - modem.broadcast(98,"execute_command","modem.send('" .. modem.address .. "',99,'console_info',drone.getOffset(),computer.energy())") -end,-1) +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 - modem.broadcast(98,'execute_command',str) - if(#history>25) then table.remove(history,1) 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 \ No newline at end of file