Advertisement
Guest User

[CC_1.41] BENCH v1.3 multi porous testing utility

a guest
Aug 2nd, 2012
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.96 KB | None | 0 0
  1. --[[
  2.         Basic Testing and dignostic tool
  3.         by BigShinyToys
  4.         OPEN SOURCE CODE (no rights reserved)
  5. ]]--
  6.  
  7. -- varibles
  8. local BENCHver = 1.3
  9. local bRunning = true
  10. local tSideList = rs.getSides()
  11. local iTerminalID = os.getComputerID()
  12. local iPosq = 1
  13. -- functions
  14. local function menu(...) -- ver 0.1
  15.     local sel = 1
  16.     local list = {...}
  17.     local offX,offY = term.getCursorPos()
  18.     local curX,curY = term.getCursorPos()
  19.     while true do
  20.         if sel > #list then sel = 1 end
  21.         if sel < 1 then sel = #list end
  22.         for i = 1,#list do
  23.             term.setCursorPos(offX,offY+i-1)
  24.             if sel == i then
  25.                 print("["..list[i].."]")
  26.             else
  27.                 print(" "..list[i].." ")
  28.             end
  29.         end
  30.         while true do
  31.             local e,e1,e2,e3,e4,e5 = os.pullEvent()
  32.             if e == "key" then
  33.                 if e1 == 200 then -- up key
  34.                     sel = sel-1
  35.                     break
  36.                 end
  37.                 if e1 == 208 then -- down key
  38.                     sel = sel+1
  39.                     break
  40.                 end
  41.                 if e1 == 28 then
  42.                     term.setCursorPos(curX,curY)
  43.                     return list[sel],sel
  44.                 end
  45.             end
  46.         end
  47.     end
  48. end
  49. local function openRednet()
  50.     local listOfSides = rs.getSides()
  51.     for i = 1,6 do
  52.         if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then
  53.             rednet.open(listOfSides[i])
  54.             return listOfSides[i]
  55.         end
  56.     end
  57. end
  58. -- apps
  59. local function RedstoneControl()
  60.     local e,e1,e2,e3,e4,e5
  61.     local function expand(iInput)
  62.         local tOutput = {}
  63.         local check = 32768
  64.         for i = 1,16 do
  65.             if iInput >= check then
  66.                 tOutput[i] = 1
  67.                 iInput = iInput - check
  68.             else
  69.                 tOutput[i] = 0
  70.             end
  71.             check = check/2
  72.         end
  73.         return tOutput
  74.     end
  75.     local function compact(tInput)
  76.         local iOutput = 0
  77.         local check = 1
  78.         for i = 16,1,-1 do
  79.             if tInput[i] == 1 then
  80.                 iOutput = iOutput + check
  81.             end
  82.             check = check*2
  83.         end
  84.         return iOutput
  85.     end
  86.     function test(sInput,offX,offY,curPos)
  87.         term.setCursorPos(offX,offY)
  88.         write(sInput)
  89.         offX = offX + 7
  90.         term.setCursorPos(offX,offY)
  91.         local iStatusB = rs.getBundledInput(sInput)
  92.         if peripheral.isPresent(sInput) then
  93.             write("                     ")-- blank's out the space for the name
  94.             term.setCursorPos(offX,offY)
  95.             write(peripheral.getType(sInput))
  96.         else
  97.             local invar = expand(iStatusB)
  98.             local text = ""
  99.             for i = 1,#invar do
  100.                 text = text..invar[i]
  101.             end
  102.             write(text)
  103.         end
  104.         local iStatusA = rs.getBundledOutput(sInput)
  105.         local invar = expand(iStatusA)
  106.         term.setCursorPos(offX+17,offY)
  107.         write(" "..tostring(rs.getInput(sInput)).." "..iStatusB.."        ")
  108.         term.setCursorPos(offX+17,offY+1)
  109.         write(" "..tostring(rs.getOutput(sInput)).." "..iStatusA.."        ")
  110.         term.setCursorPos(offX,offY+1)
  111.        
  112.         text = ""
  113.         for i = 1,#invar do
  114.             text = text..invar[i]
  115.         end
  116.         write(text)
  117.         term.setCursorPos(offX,offY+2)
  118.         write("                     ")
  119.         if curPos then
  120.             if curPos > 16 then
  121.                 spacer = 4
  122.             else
  123.                 spacer = 0
  124.             end
  125.             term.setCursorPos(offX+curPos-1+spacer,offY+2)
  126.             write("^")
  127.         end
  128.     end
  129.  
  130.     local tSideList = rs.getSides()
  131.     local curX,curY = 1,1
  132.     local spacer = 0
  133.     term.clear()
  134.     term.setCursorPos(1,1)
  135.    
  136.     while true do
  137.         if e == "key" then
  138.             if e1 == 14 then -- Backspace
  139.                 return
  140.             end
  141.             if e1 == 200 then -- up key
  142.                 curY = curY -1
  143.             end
  144.             if e1 == 208 then -- down key
  145.                 curY = curY +1
  146.             end
  147.             if e1 == 203 then -- left key
  148.                 curX = curX -1
  149.             end
  150.             if e1 == 205 then -- right key
  151.                 curX = curX +1
  152.             end
  153.             if e1 == 28 then
  154.                 if curX == 17 then
  155.                     if rs.getOutput(tSideList[curY]) then
  156.                         rs.setOutput(tSideList[curY],false)
  157.                     else
  158.                         rs.setOutput(tSideList[curY],true)
  159.                     end
  160.                 else
  161.                     local total = expand(rs.getBundledOutput(tSideList[curY]))
  162.                     if total[curX] == 1 then
  163.                         total[curX] = 0
  164.                     else
  165.                         total[curX] = 1
  166.                     end
  167.                     rs.setBundledOutput(tSideList[curY],compact(total))
  168.                 end
  169.             end
  170.         end
  171.         if curY > 6 then curY = 1 end
  172.         if curY < 1 then curY = 6 end
  173.         if curX > 17 then curX = 1 end
  174.         if curX < 1 then curX = 17 end
  175.         for o = 1,6 do
  176.             if o == curY then
  177.                 test(tSideList[o],1,o*3-2,curX)
  178.             else
  179.                 test(tSideList[o],1,o*3-2)
  180.             end
  181.         end
  182.         e,e1,e2,e3,e4,e5 = os.pullEvent()
  183.     end
  184. end
  185. local function Hardware()
  186.     term.clear()
  187.     term.setCursorPos(1,1)
  188.     print("Under Construction\nPress any key to return to menu.")
  189.     os.pullEvent("key")
  190.     return
  191. end
  192. local function wifi()
  193.     local bWiFiRun = true
  194.     local message
  195.     term.clear()
  196.     term.setCursorPos(1,1)
  197.     function readADV() -- slightly modified read function credit to dan200 for original
  198.         term.setCursorBlink( true )
  199.  
  200.         local sLine = ""
  201.         local nPos = 0
  202.  
  203.         local w, h = term.getSize()
  204.         local sx, sy = term.getCursorPos() 
  205.         local function redraw()
  206.             local nScroll = 0
  207.             if sx + nPos >= w then
  208.                 nScroll = (sx + nPos) - w
  209.             end
  210.                
  211.             term.setCursorPos( sx, sy )
  212.             term.write( string.rep(" ", w - sx + 1) )
  213.             term.setCursorPos( sx, sy )
  214.             term.write( string.sub( sLine, nScroll + 1 ) )
  215.             term.setCursorPos( sx + nPos - nScroll, sy )
  216.         end
  217.        
  218.         while true do
  219.             local sEvent, param = os.pullEvent()
  220.             if sEvent == "char" then
  221.                 sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  222.                 nPos = nPos + 1
  223.                 redraw()
  224.                
  225.             elseif sEvent == "key" then
  226.                 if param == 28 then -- Enter
  227.                     break
  228.                    
  229.                 elseif param == 203 then -- Left
  230.                     if nPos > 0 then
  231.                         nPos = nPos - 1
  232.                         redraw()
  233.                     end
  234.                    
  235.                 elseif param == 205 then -- Right
  236.                     if nPos < string.len(sLine) then
  237.                         nPos = nPos + 1
  238.                         redraw()
  239.                     end
  240.                    
  241.                 elseif param == 14 then
  242.                     -- Backspace
  243.                     if nPos > 0 then
  244.                         sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  245.                         nPos = nPos - 1                
  246.                         redraw()
  247.                     end
  248.                 end
  249.             else
  250.                 redraw()
  251.             end
  252.         end
  253.         term.setCursorBlink( false )
  254.         term.setCursorPos( w + 1, sy )
  255.         return sLine
  256.     end
  257.  
  258.     local function writer()
  259.         while true do
  260.             coroutine.yield()
  261.             writer2()
  262.         end
  263.     end
  264.  
  265.     function writer2()
  266.         term.setCursorPos(1,1)
  267.         term.clearLine()
  268.         if stat == "to" then
  269.             write("To  :")
  270.         elseif stat == "mes" then
  271.             write("Mes :")
  272.         elseif stat == "fail" then
  273.             write("No target specified press enter to continue.")
  274.         end
  275.     end
  276.  
  277.     local function send()
  278.         while true do
  279.             local sizX,sizY = term.getSize()
  280.             term.setCursorPos(6,1)
  281.             stat = "to"
  282.             writer2()
  283.             local id = readADV()
  284.             if id == "exit" then
  285.                 bWiFiRun = false
  286.                 rednet.close(modemOn)
  287.                 return
  288.             elseif id == "all" then
  289.                 id = nil
  290.                 term.setCursorPos(6,1)
  291.                 stat = "mes"
  292.                 writer2()
  293.                 message = readADV()
  294.                 rednet.send(id,message)
  295.             elseif tonumber(id) then
  296.                 id = tonumber(id)
  297.                 term.setCursorPos(6,1)
  298.                 stat = "mes"
  299.                 writer2()
  300.                 message = readADV()
  301.                 rednet.send(id,message)
  302.             else
  303.                 stat = "fail"
  304.                 writer2()
  305.                 os.pullEvent("key")
  306.             end
  307.         end
  308.     end
  309.  
  310.     local function recive()
  311.         local lastX,lastY = 1,2
  312.         while true do
  313.             term.setCursorBlink( true )
  314.             local event = {coroutine.yield()}
  315.             term.setCursorBlink( false )
  316.             if event[1] == "rednet_message" then
  317.                 local sizX,sizY = term.getSize()
  318.                 term.setCursorPos(1,lastY)
  319.                 print("Frm: "..event[2].." Dist: "..event[4].."M Mes: "..event[3])
  320.                 lastX,lastY = term.getCursorPos()
  321.             end
  322.         end
  323.     end
  324.  
  325.     -- moved openRednet from here
  326.     modemOn = openRednet()
  327.     if not modemOn then
  328.         print("No WIFI Modem\nPress any key to return to menu.")
  329.         os.pullEvent("key")
  330.         return
  331.     else
  332.         print("Opened wifi on "..modemOn.." side")
  333.     end
  334.  
  335.     term.clear()
  336.     term.setCursorPos(1,1)
  337.     local stat = nil
  338.  
  339.     local reciveHandel = coroutine.create(recive)
  340.     local writerHandel = coroutine.create(writer)
  341.     local sendHandel = coroutine.create(send)
  342.    
  343.     coroutine.resume(reciveHandel,e,e1,e2,e3,e4,e5)
  344.     coroutine.resume(writerHandel)
  345.     coroutine.resume(sendHandel,e,e1,e2,e3,e4,e5)
  346.    
  347.     while bWiFiRun do -- start a loop
  348.         local e,e1,e2,e3,e4,e5 = os.pullEvent()
  349.         coroutine.resume(reciveHandel,e,e1,e2,e3,e4,e5)
  350.         coroutine.resume(writerHandel)
  351.         coroutine.resume(sendHandel,e,e1,e2,e3,e4,e5)
  352.     end
  353. end
  354. local function EventMonitor()
  355.     term.clear()
  356.     term.setCursorPos(1,1)
  357.     print("press BACKSPACE key 14 to exit")
  358.     print("Wating For Event...")
  359.     local tEvents
  360.     while true do
  361.         tEvents = {os.pullEvent()}
  362.         if tEvents[1] == "key" and tEvents[2] == 14 then
  363.             return
  364.         end
  365.         for i = 1,#tEvents do
  366.             write(tostring(tEvents[i]).." ")
  367.         end
  368.         write("\n")
  369.     end
  370. end
  371. local function TurtleDriver()
  372.     term.clear()
  373.     term.setCursorPos(1,1)
  374.     if not turtle then
  375.         print("This is Not a Turtle \nPress any key to return")
  376.         os.pullEvent("key")
  377.         return
  378.     end
  379.     local compas = {"n","e","s","w"}
  380.     local turX,turY,turZ = 0,0,0
  381.     local gpsX,gpsY,gpsZ = nil , nil , nil
  382.     local face = 1
  383.     local slotSelX = 1
  384.     local slotSelY = 1
  385.     function move(ins,rep) -- low levle functions
  386.         if not ins and not rep then
  387.             return false,"error no move specified"
  388.         elseif not rep then
  389.             rep = 1
  390.         end
  391.         for i=1,rep do
  392.             if ins == "U" then -- up move
  393.                 if turtle.up() then
  394.                     turZ = turZ+1
  395.                 else
  396.                     return false
  397.                 end
  398.             end
  399.             if ins == "D" then -- down move
  400.                 if turtle.down() then
  401.                     turZ = turZ-1
  402.                 else
  403.                     return false
  404.                 end
  405.             end
  406.             if ins == "L" then -- left turn
  407.                 if turtle.turnLeft() then
  408.                     face = face - 1
  409.                     if face < 1 then
  410.                         face = 4   
  411.                     end
  412.                 else
  413.                     return false
  414.                 end
  415.             end
  416.             if ins == "R" then -- right turn
  417.                 if turtle.turnRight() then
  418.                     face = face + 1
  419.                     if face > 4 then
  420.                         face = 1   
  421.                     end
  422.                 else
  423.                     return false
  424.                 end
  425.             end
  426.             if ins == "F" then -- forward move
  427.                 if turtle.forward() then
  428.                     if face == 1 then
  429.                         turY = turY+1
  430.                     end
  431.                     if face == 2 then
  432.                         turX = turX+1
  433.                     end
  434.                     if face == 3 then
  435.                         turY = turY-1
  436.                     end
  437.                     if face == 4 then
  438.                         turX = turX-1
  439.                     end
  440.                 else
  441.                     return false
  442.                 end
  443.             end
  444.             if ins == "B" then -- back move
  445.                 if turtle.back() then
  446.                     if face == 1 then
  447.                         turY = turY-1
  448.                     end
  449.                     if face == 2 then
  450.                         turX = turX-1
  451.                     end
  452.                     if face == 3 then
  453.                         turY = turY+1
  454.                     end
  455.                     if face == 4 then
  456.                         turX = turX+1
  457.                     end
  458.                 else
  459.                     return false
  460.                 end
  461.             end
  462.         end
  463.         return true
  464.     end
  465.     local function reDraw()
  466.         term.clear()
  467.         term.setCursorPos(1,1)
  468.         print("Compus : "..compas[face].."  Loc : X "..turX.." Y "..turY.." Z "..turZ)
  469.         if gpsX then
  470.             print("last GPS ping   : X "..gpsX.." Y "..gpsY.." Z "..gpsZ)
  471.         else
  472.             print("GPS position unknown")
  473.         end
  474.         term.setCursorPos(1,3)
  475.         print("Remaning Fuel : "..turtle.getFuelLevel())
  476.         term.setCursorPos(1,5)
  477.         print([[Use "up down left right" keys to select slot then press "r" to refuel from slot.
  478. Press "g" locate GPS position.
  479. Press "b" to set Loc as GPS.
  480. Press "h" to ajust Heading]])
  481.     end
  482.     reDraw()
  483.     while true do
  484.         local e,e1,e2,e3,e4,e5 = os.pullEvent()
  485.         -- print(tostring(e).."-"..tostring(e1))
  486.         if e == "key" then
  487.             if e1 == 17 then
  488.                 move("F")
  489.             elseif e1 == 31 then
  490.                 move("B")
  491.             elseif e1 == 30 then
  492.                 move("L")
  493.             elseif e1 == 32 then
  494.                 move("R")
  495.             elseif e1 == 16 then
  496.                 move("U")
  497.             elseif e1 == 18 then
  498.                 move("D")
  499.             elseif e1 == 14 then -- backspace
  500.                 return
  501.             elseif e1 == 19 then -- r
  502.                 turtle.refuel(1)
  503.             elseif e1 == 34 then -- g
  504.                 local rednetSide = openRednet()
  505.                 if rednetSide then
  506.                     gpsX,gpsY,gpsZ = gps.locate( 2, false)
  507.                     rednet.close(rednetSide)
  508.                 else
  509.                     print("no WIFI modem connected")
  510.                 end
  511.             elseif e1 == 35 then -- h
  512.                 face = face +1
  513.                 if face > 4 then
  514.                     face = 1   
  515.                 end
  516.             elseif e1 == 200 then -- up turtle.select(e1-1)     local slotSelX = 1  local slotSelY = 1
  517.                 slotSelY = slotSelY -1
  518.                 if slotSelY < 1 then
  519.                     slotSelY = 4
  520.                 end
  521.             elseif e1 == 208 then -- down
  522.                 slotSelY = slotSelY +1
  523.                 if slotSelY > 4 then
  524.                     slotSelY = 1
  525.                 end
  526.             elseif e1 == 203 then -- left
  527.                 slotSelX = slotSelX -1
  528.                 if slotSelX < 1 then
  529.                     slotSelX = 4
  530.                 end
  531.             elseif e1 == 205 then -- right
  532.                 slotSelX = slotSelX +1
  533.                 if slotSelX > 4 then
  534.                     slotSelX = 1
  535.                 end
  536.             elseif e1 == 48 then -- b
  537.                 if gpsX then
  538.                     turX,turY,turZ = gpsX,gpsY,gpsZ
  539.                 end
  540.             end
  541.             turtle.select(slotSelX+(slotSelY*4)-4)
  542.         end
  543.         reDraw()
  544.     end
  545. end
  546. local function help() -- 203 left 205 right
  547. local tHelp = {
  548. [[This program is designed for use while testing other programs or redstone systems.
  549.  
  550. It allows you the user to change hardware settings quickly and read input from Redstone, Bundled Cable and WiFi.
  551.  
  552. Event Monitor will show what events happen. This is usefull for finding the number of a pressed key for example BackSpace is key 14.
  553.  
  554. OPEN SOURCE CODE (no rights reserved) 2012
  555. By Big Shiny Toys ver ]]..BENCHver.."\n\nPress Backspace to return to menu.",
  556. "section 2",
  557. "section 3",
  558. }
  559.     local iPage = 1
  560.     while true do
  561.     term.clear()
  562.     term.setCursorPos(1,1)
  563.     print(tHelp[iPage])
  564.     term.setCursorPos(10,18)
  565.     write("- Page "..iPage.." of "..#tHelp.." -")
  566.     local e,e1,e2 = os.pullEvent("key")
  567.         if e == "key" then
  568.             if e1 == 203 then -- left
  569.                 iPage = iPage - 1
  570.             elseif e1 == 205 then -- right
  571.                 iPage = iPage + 1
  572.             elseif e1 == 14 then -- Backspace
  573.                 return
  574.             end
  575.         end
  576.         if iPage < 1 then iPage = 1 end
  577.         if iPage > #tHelp then iPage = #tHelp end
  578.     end
  579. end
  580. -- Top Loop
  581. while bRunning do
  582.     term.clear()
  583.     term.setCursorPos(1,1)
  584.     print("Welcome to BENCH ver "..BENCHver.." terminal "..iTerminalID.."\nBy Big Shiny Toys")
  585.     term.setCursorPos(2,4)
  586.     term.setCursorBlink(false)
  587.     local selection = menu("Redstone","Hardware","WiFi","Event Monitor","Turtle Driver","Infomation/Help","Exit")
  588.     if selection == "Redstone" then
  589.         RedstoneControl()
  590.     elseif selection == "Hardware" then
  591.         Hardware()
  592.     elseif selection == "WiFi" then
  593.         wifi()
  594.     elseif selection == "Event Monitor" then
  595.         EventMonitor()
  596.     elseif selection == "Turtle Driver" then
  597.         TurtleDriver()
  598.     elseif selection == "Infomation/Help" then
  599.         help()
  600.     elseif selection == "Exit" then
  601.         bRunning = false
  602.     end
  603. end
  604. term.clear()
  605. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement