local t=require("ext") local i=t.redirect_print_nuconsole _G["nuconsole_info_block_symbol"]="NuConsole_InfoBlock" _G["nuconsole_info_block_addr"]=536870912 local e={buf_addr=0, size=0, head=0, tail=0, mode=0} function e:new(e) e=e or{} setmetatable(e,self) self.__index=self return e end function e:count() if self.head>=self.tail then return(self.head-self.tail) else return(self.size-self.tail+self.head) end end function e:capacity() if self.size>0 then return(self.size-1) else return 0 end end local o={magic_id="", tx_fifo=e:new(), rx_fifo=e:new()} function o:new(e) e=e or{} setmetatable(e,self) self.__index=self return e end function o:check_id() if self.magic_id=="NuConsole"then return true else return false end end function _bytes_to_int(o,i) local e=0 for a=0,3 do e=t.bor(e,t.lshift(o[i+a+1],a*8)) end return e end function _unserialize_to_fifo(a,t) local e=e:new() if(#a-t)<20 then return e,false else e.buf_addr=_bytes_to_int(a,t) e.size=_bytes_to_int(a,t+4) e.head=_bytes_to_int(a,t+8) e.tail=_bytes_to_int(a,t+12) e.mode=_bytes_to_int(a,t+16) return e,true end end function _unserialize_to_info_block(t,a) local e=o:new() if(#t-a)<52 then return e,false else local o="" for i=1,12 do local e=string.char(t[a+i]) if e=='\0'then break end o=o..e end e.magic_id=o e.tx_fifo=_unserialize_to_fifo(t,a+12) e.rx_fifo=_unserialize_to_fifo(t,a+32) return e,true end end function nuconsole_init(t) local e,t=pcall(function() local e=true if _G["SCRIPT_APP_HOST"]=="NuConsole"then ice:OpenPort() e=pcall(function() if t~=0 then ice:MCUConnect(ice.RESET_AUTO,ice.CONNECT_NORMAL) else ice:MCUConnect(ice.RESET_NONE,ice.CONNECT_NORMAL) end ice:MCUFreeRun() end) end if e then return ice:CheckLink() else return false end end) if not e then t:gsub("%w+[.]lua:%d+: (.*)$",i) i("\n") end return e and t or false end function nuconsole_uninit() local e,t=pcall(function() if _G["SCRIPT_APP_HOST"]=="NuConsole"then pcall(function() ice:MCUConnect(ice.RESET_NONE,ice.CONNECT_DISCONNECT) end) ice:ClosePort() end end) return e end function nuconsole_get_data() local t,e=pcall(function() local o=_G["nuconsole_info_block_addr"] local e=ice:ReadMem(o,52) local e,t=_unserialize_to_info_block(e,0) if t and e:check_id()then local e=e.tx_fifo if e:count()>0 then local a={} local t=0 if e.head>e.tail then t=e.head-e.tail a=ice:ReadMem(e.buf_addr+e.tail,t) e.tail=e.head else t=e.size-e.tail a=ice:ReadMem(e.buf_addr+e.tail,t) e.tail=0 end ice:WriteMem32(o+12+12,e.tail) local e="" for t=1,t do e=e..string.format("%c",a[t]) end i(e) end end return true end) return t and e or false end function nuconsole_set_data(a) local e,t=pcall(function() local t=_G["nuconsole_info_block_addr"] local e=ice:ReadMem(t,52) local e,o=_unserialize_to_info_block(e,0) if o and e:check_id()then local e=e.rx_fifo if(e:capacity()-e:count())>0 then ice:WriteMem(e.buf_addr+e.head,{string.byte(a)}) e.head=e.head+1 if e.head==e.size then e.head=0 end ice:WriteMem32(t+32+8,e.head) return true elseif e.mode==1 then return true end end return false end) return e and t or false end