Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Logic Gates by Croswat & VikeStep
- local gatevars = nil --variable for gates
- local inputs = nil
- local outputs = nil
- function change(event)
- print("Press x to change gates")
- parallel.waitForAll(waitforkey,event)
- end
- function run()
- term.clear()
- term.setCursorPos(1,1)
- print("running "..gatevars[1])
- if gatevars[1] == "timer" then
- change(timer)
- elseif gatevars[1] == "and" then
- change(andgate)
- elseif gatevars[1] == "or" then
- change(orgate)
- elseif gatevars[1] == "nor" then
- change(norgate)
- elseif gatevars[1] == "nand" then
- change(nandgate)
- elseif gatevars[1] == "repeater" then
- change(repeater)
- elseif gatevars[1] == "xor" then
- change(xorgate)
- elseif gatevars[1] == "xnor" then
- change(xnorgate)
- end
- end
- function waitforkey()
- while true do
- local sEvent, param = os.pullEvent("key")
- if sEvent == "key" then
- if param == 45 then
- print("Are you sure? (y/n)")
- local sEvent, param2 = os.pullEvent("key")
- if sEvent == "key" then
- if param2 == 21 then
- fs.delete("lgvariables")
- os.reboot()
- else
- os.reboot()
- end
- end
- end
- end
- end
- end
- function loadVariables() -- get fom file
- local file = fs.open("lgvariables","r")
- gatevars = split(file.readLine(), "@")
- file.close()
- end
- function saveVariables(variables)
- local file = fs.open("lgvariables","w")
- file.writeLine(variables)
- file.close()
- end
- function split(pString, pPattern)
- numchanged = 0
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- numchanged = numchanged +1
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- numchanged = numchanged +1
- end
- return Table
- end
- function readgate()
- local loopflag = true
- local gatetype = nil
- while loopflag do
- gatetype = read()
- gatetype = string.lower(gatetype)
- if gatetype == "list" then
- print("AND, OR, NOT, NOR, NAND, XOR, XNOR, Timer, Repeater")
- elseif gatetype == "timer" or gatetype == "and" or gatetype == "or" or gatetype == "not" or gatetype == "nor" or gatetype == "inverter" or gatetype == "nand" or gatetype == "xor" or gatetype == "xnor" or gatetype == "buffer" or gatetype == "repeater" then -- check for gate types here
- loopflag = false
- else
- print("gate not recognised, try again")
- end
- end
- return gatetype
- end
- function printCube() -- The info for whether to type in inputs or not will be displayed after this
- term.clear()
- term.setCursorPos(1,1)
- print [[
- Back Please type in the
- | the side(s) in
- +---+---+ the following format
- /| V /|
- / | / | "front, left, right"
- / |Top / |
- +-------+ |<-Right
- | | | |
- | +---|---+
- Left->| Front | /
- | / | /
- |/ ^ |/
- +-------+
- |
- Bottom
- ]]
- end
- --menu and setting up gates
- function setup()
- term.clear()
- term.setCursorPos(1,1)
- print("To get started, write down what gate you wish to use")
- print("type \"list\" to see what gates are available")
- local gatetype = readgate()
- --work out what gate, then get the other inputs, and save to file. then run()
- if gatetype == "timer" then
- setuptimer()
- elseif gatetype == "and" then
- setupand()
- elseif gatetype == "or" then
- setupor()
- elseif gatetype == "nor" or gatetype == "not" or gatetype== "inverter" then
- setupnor()
- elseif gatetype == "nand" then
- setupnand()
- elseif gatetype == "xor" then
- setupxor()
- elseif gatetype == "xnor" then
- setupxnor()
- elseif gatetype == "buffer" or gatetype == "repeater" then
- setuprepeater()
- end
- end
- function ioToArray()
- inputs = split(gatevars[2],", ")
- outputs = split(gatevars[3],", ")
- end
- function getNumberInputs()
- local number = table.getn(inputs)
- return number
- end
- function getNumberOutputs()
- local number = table.getn(outputs)
- return number
- end
- function round(x)
- floor = tonumber(x)-math.floor(tonumber(x))
- ceil = math.ceil(tonumber(x))-tonumber(x)
- if floor==ceil then
- return math.ceil(tonumber(x))
- elseif floor<ceil then
- return math.floor(tonumber(x))
- elseif floor>ceil then
- return math.ceil(tonumber(x))
- end
- end
- function getside(printstring)
- valid=false
- tempsidesvalid=0
- printCube()
- print(printstring)
- while not valid do
- text = read()
- sides = split(text,", ")
- amountsides = table.getn(sides)
- for i=1, amountsides do
- if sides[i] == "front" or sides[i] == "left" or sides[i] == "right" or sides[i] == "back" or sides[i] == "bottom" or sides[i] == "top" then
- tempsidesvalid = tempsidesvalid + 1
- else
- printCube()
- print(sides[1].." is not a valid side")
- print(printstring)
- break
- end
- end
- if tempsidesvalid==amountsides then
- valid=true
- else
- tempsidesvalid=0
- end
- end
- return text
- end
- --Timer
- function setuptimer()
- local timerinput = getside("Type in the inputs (disables the timer)")
- local timeroutput = getside("Type in the outputs (side pulsed to)")
- term.clear()
- term.setCursorPos(1,1)
- print("enter how many seconds you would like the timer to cycle")
- local loopflag = true
- while loopflag do
- timerseconds = read()
- integer=tonumber(timerseconds)
- if integer == nil or integer <= 0 then
- term.clear()
- term.setCursorPos(1,1)
- print(timerseconds.." is not a valid positive number")
- print("enter how many seconds you would like the timer to cycle (can enter decimal)")
- else
- loopflag = false
- end
- end
- saveVariables("timer".."@"..tostring(timerinput).."@"..tostring(timeroutput).."@"..tostring(timerseconds))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(timer)
- end
- function timer()
- input = false
- pulseLength = 1 -- In ticks
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- clockSpeed = round(20*tonumber(gatevars[4])) -- In ticks, remember 20 ticks a second
- while true do
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- input=true
- else
- end
- end
- while not input do
- for i=1, numberOutputs do
- rs.setOutput(outputs[i], true)
- end
- sleep(pulseLength / 20)
- for i=1, numberOutputs do
- rs.setOutput(outputs[i], false)
- end
- sleep((clockSpeed - pulseLength) / 20)
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- input=true
- else
- end
- end
- end
- event = os.pullEvent("redstone")
- input=false
- end
- end
- --AND Gate
- function setupand()
- local andinput = getside("Type in the inputs (when all are inputs are on, output is on)")
- local andoutput = getside("Type in the outputs (when all inputs are on, output is on)")
- saveVariables("and".."@"..tostring(andinput).."@"..tostring(andoutput))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(andgate)
- end
- function andgate()
- input = false
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- amount = 0
- while true do
- amount=0
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- amount = amount + 1
- else
- end
- end
- if amount == numberInputs then
- input=true
- else
- amount = 0
- input=false
- end
- if input == true then
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],true)
- end
- else
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],false)
- end
- end
- event = os.pullEvent("redstone")
- end
- end
- -- OR Gate
- function setupor()
- local orinput = getside("Type in the inputs (when one of the inputs are on, output is on)")
- local oroutput = getside("Type in the outputs (when one of the inputs are on, output is on)")
- saveVariables("or".."@"..tostring(orinput).."@"..tostring(oroutput))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(orgate)
- end
- function orgate()
- input = false
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- while true do
- input=false
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- input=true
- else
- end
- end
- if input == true then
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],true)
- end
- else
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],false)
- end
- end
- event = os.pullEvent("redstone")
- end
- end
- -- Repeater
- function setuprepeater()
- local delay
- local repeaterinput = getside("Type in the inputs (when one of the inputs are on, output is on)")
- local repeateroutput = getside("Type in the outputs (when one of the inputs are on, output is on)")
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the delay (seconds) for the signal, type 0 for no delay (can be decimal)")
- local loopflag = true
- while loopflag do
- delay = read()
- integer=tonumber(delay)
- if integer == nil or integer < 0 then
- term.clear()
- term.setCursorPos(1,1)
- print(delay.." is not a valid positive number")
- print("Enter the delay for the signal, type 0 for no delay")
- else
- loopflag = false
- end
- end
- saveVariables("repeater".."@"..tostring(repeaterinput).."@"..tostring(repeateroutput).."@"..tostring(delay))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(orgate)
- end
- function repeater()
- local delay = round(20*tonumber(gatevars[4]))
- input = false
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- while true do
- input=false
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- input=true
- else
- end
- end
- if input == true then
- sleep(delay)
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],true)
- end
- else
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],false)
- end
- end
- event = os.pullEvent("redstone")
- end
- end
- -- NOT/NOR Gate
- function setupnor()
- local norinput = getside("Type in the inputs (if 1 input is on, output is off)")
- local noroutput = getside("Type in the outputs (if 1 input is on, output is off)")
- saveVariables("nor".."@"..tostring(norinput).."@"..tostring(noroutput))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(norgate)
- end
- function norgate()
- input = false
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- while true do
- input=false
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- input=true
- else
- end
- end
- if input == false then
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],true)
- end
- else
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],false)
- end
- end
- event = os.pullEvent("redstone")
- end
- end
- --NAND Gate
- function setupnand()
- local nandinput = getside("Type in the inputs (when at least 1 input is off, output is on)")
- local nandoutput = getside("Type in the outputs (when at least 1 input is off, output is on)")
- saveVariables("nand".."@"..tostring(nandinput).."@"..tostring(nandoutput))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(nandgate)
- end
- function nandgate()
- input = false
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- amount = 0
- while true do
- amount=0
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- amount = amount + 1
- else
- end
- end
- if amount == numberInputs then
- input=true
- else
- amount = 0
- input=false
- end
- if input == false then
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],true)
- end
- else
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],false)
- end
- end
- event = os.pullEvent("redstone")
- end
- end
- -- XOR Gate
- function setupxor()
- local xorinput = getside("Type in the inputs (when an odd number of inputs are on, output is on)")
- local xoroutput = getside("Type in the outputs (when an odd number of inputs are on, output is on)")
- saveVariables("xor".."@"..tostring(xorinput).."@"..tostring(xoroutput))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(xorgate)
- end
- function xorgate()
- input = false
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- amount = 0
- while true do
- amount=0
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- amount = amount + 1
- else
- end
- end
- if amount % 2 == 1 then
- input=true
- else
- amount = 0
- input=false
- end
- if input == true then
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],true)
- end
- else
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],false)
- end
- end
- event = os.pullEvent("redstone")
- end
- end
- -- XNOR Gate
- function setupxnor()
- local xnorinput = getside("Type in the inputs (when an even number of inputs are on, output is on)")
- local xnoroutput = getside("Type in the outputs (when an even number of inputs are on, output is on)")
- saveVariables("xnor".."@"..tostring(xnorinput).."@"..tostring(xnoroutput))
- loadVariables()
- term.clear()
- term.setCursorPos(1,1)
- change(xnorgate)
- end
- function xnorgate()
- input = false
- ioToArray()
- numberInputs=tonumber(getNumberInputs())
- numberOutputs=tonumber(getNumberOutputs())
- amount = 0
- while true do
- amount=0
- for i=1, numberInputs do
- if rs.getInput(inputs[i])==true then
- amount = amount + 1
- end
- end
- if amount % 2 == 1 then
- input=true
- else
- amount = 0
- input=false
- end
- if input == false then
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],true)
- end
- else
- for i=1,numberOutputs do
- rs.setOutput(outputs[i],false)
- end
- end
- event = os.pullEvent("redstone")
- end
- end
- -- functions go above here
- term.clear()
- term.setCursorPos(1,1)
- --start
- if fs.exists("lgvariables") then -- variables saved
- loadVariables()
- run()
- else
- setup()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement