This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Arrowny's profile picture

[Question] Printer

Started by Arrowny, 25 January 2013 - 06:00 PM
Arrowny #1
Posted 25 January 2013 - 07:00 PM
I'm doing a printer program that let's you choose the amount of lines and then you write what you want. I know I have 25 Chars per line and 21 lines.
The problem is that it only prints one page at a time. So, I wanted some help to make it print multiple copies (determined by the user).

Basically this is the program.

Spoiler

term.clear()
term.setCursorPos(1,1)
local printer
for _,side in pairs(redstone.getSides()) do
if peripheral.getType(side)=="printer" then
printer=peripheral.wrap(side)
break
end
end
if printer==nil then
error("Connect a printer.")
end

local ink=printer.getInkLevel()
local paper=printer.getPaperLevel()
print("Ink levels: ")
term.setCursorPos(19,1)
print(ink)
print("Paper levels: ")
term.setCursorPos(19,2)
print(paper)

if ink==0 then
print("Needs Ink.")
end

if paper==0 then
print("Put some Paper.")
end

while printer.getInkLevel() == 0 do
os.sleep(.5)
end

while printer.getPaperLevel() == 0 do
os.sleep(.5)
end

printer.newPage()

print("Insert Title.")
local title = read()
printer.setPageTitle(title)

print("Amount of Lines. (1 to 21)")
local lines = read()
local temp = lines
lines = tonumber(lines)

while true do
if (lines < 1) or (lines > 21) then
print("Choose between 1 a 21")
else
break
end
end

print("Write your Document.")
print("NOTA: Max 25 characters per line.")
local h = 0

while h < lines do
h = h + 1
local doc = read()
printer.setCursorPos(1,h)
printer.write(doc)
end

printer.endPage()
term.clear()
term.setCursorPos(1,1)

LBPHacker #2
Posted 25 January 2013 - 10:21 PM
Put lines 39 to 69 into a for loop

term.clear()
term.setCursorPos(1,1)
local printer
for _,side in pairs(redstone.getSides()) do
if peripheral.getType(side)=="printer" then
printer=peripheral.wrap(side)
break
end
end
if printer==nil then
error("Connect a printer.")
end

print("Amount of Copies.")
local copies = read()

for i = 1, copies do
local ink=printer.getInkLevel()
local paper=printer.getPaperLevel()
print("Ink levels: ")
term.setCursorPos(19,1)
print(ink)
print("Paper levels: ")
term.setCursorPos(19,2)
print(paper)

if ink==0 then
print("Needs Ink.")
end

if paper==0 then
print("Put some Paper.")
end

while printer.getInkLevel() == 0 do
coroutine.yield() -- looks better
end

while printer.getPaperLevel() == 0 do
coroutine.yield()
end

printer.newPage()

print("Insert Title.")
local title = read()
printer.setPageTitle(title)

print("Amount of Lines. (1 to 21)")
local lines = read()
local temp = lines
lines = tonumber(lines)

while true do
if (lines < 1) or (lines > 21) then
print("Choose between 1 a 21")
else
break
end
end

print("Write your Document.")
print("NOTA: Max 25 characters per line.")
local h = 0

while h < lines do
h = h + 1
local doc = read()
printer.setCursorPos(1,h)
printer.write(doc)
end

printer.endPage()
end
term.clear()
term.setCursorPos(1,1)

Something like this will do it.
GopherAtl #3
Posted 26 January 2013 - 02:07 AM
that would let the user enter multiple pages, but I think he wanted multiple copies of the same page, presumably without making the user type it all again.

You'll want to read the lines into a table, so you can then loop over the table and print multiple times.

where you read and print, change it to something like this:


--make a table to store lines in
local doc={}
--loop reading into doc
for i=1,lines do
  doc[i]=read()
end

--now loop over doc printing copies
for j=1,copies do
  printer.newPage()
  for i=1,lines do
	printer.setCursorPos(1,i)
	write(doc[i])
  end
  printer.endPage()
end



You've also got a mistake before that, where you read in the number of lines and then check if it's greater than 21. If it is, it goes into a loop waiting for it to be valid, but it never calls read() in that loop, just prints "must be between 1 and 21" over and over again!

You'll read the number of copies basically the same way you read the number of lines, though you'll probably want to move the check for ink and paper to after, so you can check that each is not 0, but not less than the number of copies the player wants to make.