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

How to use printers from software side

Started by kornichen, 23 January 2015 - 02:41 PM
kornichen #1
Posted 23 January 2015 - 03:41 PM
This tutorial will show you, how to use from the software side. As you will see, it is quite simple.

Connect the printer and start a new Page

The first thing, we are going to after putting a printer on side of a computer is to connect the printer. You do it like this:


local printer = peripheral.wrap("left")

This line of code defines a new local variable called printer which we will use to communicate with our printer in the program. Change "left" with the side that you have connected your printer to.
Next is to start a new page. You have to do this once before you print something or after you ended a page (more about that later).

Attention: To get more information about how to wrap a peripheral, take a look at this tutorial.


local success = printer.newPage()

The local variable success is either set to true if a new page was created successfully or false if that was not the case. False might be the output if there is no paper left in the printer. I would recommend to use the following commands in an if-statement as you can see in this tutorial. If you don't do this the program might cancel because of an error that occurs while for example trying to print on a non-existing piece of paper.

Print something

Now, let's print something. It is as easy as it could be:


if success then
  printer.write("Hello world!")
end


Important is the second line. That is the line where the magic happens. The line that we use to print out something. If you want to print out more text, just put more of these commands between line 2 and line 3. That means, you can extend it like this:


if success then
  printer.write("Hello world!")

printer.write("Hallo Welt!")
printer.write("¡Hola mundo!")
end


Attention: You do not have to put the write lines into an if-statement. It is just to prevent the program from running into an error.

Ending a page

If you are done with printing something on a page and you want to release it from the printer to make it possible for the user to take the paper out of the printer, use this command:


printer.endPage()


Now the page will be put to the output slot of the printer ready to take it out.

Attention: If you want to print more, you firstly have to start a new page as you learned earlier in this tutorial.

The cursor - move the text around

It is boring and quite useless to put text always into the first line. You have to move the cursor around to print text elsewhere.


printer.setCursorPos(1, 2)

This line will set the printer's cursor to character 1 of line 2.
There is also the possibility to get the position of the cursor. For example for control purposes:


local x, y = printer.getCursorPos()

The cursor position will in this case be stored in the local variables x and y.

The page's title

To find the page you are looking for faster, you can set a page title. As always, it is done with one line of code:


printer.setPageTitle("Hello World!")

Supervise the hardware

There are to commands making it possible for you to supervise both the ink status and the paper status.
To let the program know how much ink there is left in the printer use this command:


local inkLevel = printer.getInkLevel()

The current ink level will be in this case stored in the local variable inkLevel.
To get the current paper level, so the amount of paper in the printer, use this command:


local paperLevel = printer.getPaperLevel()

The current paper level will be in this case stored in the local variable paperLevel.
EmptyPlayerObject #2
Posted 07 May 2015 - 04:18 PM
I'm going to bookmark this and use it as a cheat sheet. Very informative and concise.
flaghacker #3
Posted 08 May 2015 - 11:42 AM
Nice tutorial, but you might want to fix the 4th code block :P/>