Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --USAGE: 1:circuit type (p or s) 2:voltage total 3..:resistor values
- --Example: p 12 300 300 39000423 35184
- local tArgs = {...}
- --Results table colors. Change if you want
- local columbC = colors.green
- local rowC = colors.green
- local tableC = colors.white
- local tableSpace = 11
- term.clear()
- term.setCursorPos(1,1)
- local rCount = #tArgs-2
- --Input error checking
- local inputError = "Usage: 1:circuit type (p or s) 2:voltage total 3..:resistor values"
- if rCount <= 0 then
- error("rCount less than 0. "..inputError)
- elseif tArgs[1] ~= "p" and tArgs[1] ~= "s" then
- error("p/s missing. "..inputError)
- elseif not tonumber(tArgs[2]) then
- error("voltage value not a number: "..tArgs[2]..", "..type(tArgs[2])..". "..inputError)
- elseif rCount >= 1 then
- for i = 3, #tArgs do
- if not tonumber(tArgs[i]) then
- error("resistor #"..i.." not a number: "..tArgs[i]..", "..type(tArgs[i])..". "..inputError)
- end
- end
- end
- --E = volts, R = resistors, I = current, P = power
- --t = total
- local E = { t = tArgs[2] }
- local R = { t = 0 }
- local I = { t = 0 }
- local P = { t = 0 }
- local function sci(x)
- return 1*10^(x)
- end
- local function formatE(x)
- if not tonumber(x) then error("Attempted to format non-number: "..x..", "..type(x)) end
- x = tonumber(x)
- local num
- local suffix = ""
- if x >= sci(9) then --giga
- num = x / sci(9)
- suffix = "g"
- elseif x >= sci(6) then --mega
- num = x / sci(6)
- suffix = "M"
- elseif x >= sci(3) then --kilo
- num = x / sci(3)
- suffix = "k"
- elseif x >= 1 and x < 1000 then
- num = x
- elseif x <= 1*10^-9 then --nano
- num = x / sci(-9)
- suffix = "n"
- elseif x <= 1*10^-6 then --micro
- num = x / sci(-6)
- suffix = "u"
- elseif x < 1 then --milli
- num = x / sci(-3)
- suffix = "m"
- else error(x.." wasn't caught in formatting")
- end
- local mult = 10^(4)
- num = math.modf(num*mult)/mult
- return (num.." "..suffix)
- end
- --solve a parallel circuit given resistors and voltage total
- local function solveP()
- --Fill in ohms and voltage row
- local rBuffer = 0
- for i = 3, #tArgs do
- R[i-2] = tArgs[i]
- E[i-2] = E.t
- --Add up tArgs in buffer
- rBuffer = rBuffer + 1/tArgs[i]
- end
- R.t = 1/rBuffer
- --Get remaining totals
- I.t = E.t / R.t
- P.t = E.t * I.t
- --Fill in remaining rows
- for i = 1, rCount do
- I[i] = E[i] / R[i]
- P[i] = E[i] * I[i]
- end
- end
- --solve a series circuit given resistors and voltage total
- local function solveS()
- --Fill in ohms row
- for i = 3, #tArgs do
- R[i-2] = tArgs[i]
- --Add up tArgs to get resistor total
- R.t = R.t + tArgs[i]
- end
- --Get remaining totals
- I.t = E.t / R.t
- P.t = E.t * I.t
- --Fill in remaining rows
- for i = 1, rCount do
- I[i] = I.t
- E[i] = I[i] * R[i]
- P[i] = E[i] * I[i]
- end
- end
- local function solveCircuit() end
- solveCircuit = solveS
- --Switch circuit type
- if tArgs[1] == "p" then
- solveCircuit = solveP
- end
- --Solve the selected circuit type
- solveCircuit()
- --Print results table
- term.setTextColor(columbC)
- term.write(" Totals")
- for i = 1, #tArgs-2 do
- term.setCursorPos((i*tableSpace)+4,1)
- term.write("R"..i)
- end
- print()
- term.setTextColor(rowC)
- term.write("E = ")
- term.setTextColor(tableC)
- print(formatE(E.t))
- term.setTextColor(rowC)
- term.write("R = ")
- term.setTextColor(tableC)
- print(formatE(R.t))
- term.setTextColor(rowC)
- term.write("I = ")
- term.setTextColor(tableC)
- print(formatE(I.t))
- term.setTextColor(rowC)
- term.write("P = ")
- term.setTextColor(tableC)
- print(formatE(P.t))
- for i = 1, #tArgs-2 do
- local x = (i*tableSpace)+4
- term.setCursorPos(x,2)
- term.write(formatE(E[i]))
- term.setCursorPos(x,3)
- term.write(formatE(R[i]))
- term.setCursorPos(x,4)
- term.write(formatE(I[i]))
- term.setCursorPos(x,5)
- term.write(formatE(P[i]))
- end
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement