Difference between revisions of "Peripheral (API)"

From ComputerCraft Wiki
Jump to: navigation, search
m (Forgot capitalization)
(You don't need to do this!)
Line 36: Line 36:
 
}}
 
}}
  
==Remote Peripherals using Networking Cables==
+
==Connecting peripherals using networking cables==
Using Networking Cables, you can attach and use peripherals without having them directly touching the computer. To connect a peripheral in this way, place a Wired Modem on any side of your computer, as you would a [[Wireless Modem]], as well as all the peripherals you wish to connect, connecting all of them with Networking Cables. Then right click each of the wired modems. You should receive messages in your chat bar informing you of a successful connection. Don't forget to connect the [[Computer|computer]] too!
+
Using Networking Cables, you can attach and use peripherals without having them directly touching the computer. To connect a peripheral in this way, place a Wired Modem on any side of your computer, as you would a [[Wireless Modem]]. Place a Wired Modem on each peripheral you wish to connect, and then connect all of them with Networking Cables. Then right click each of the wired modems.
  
Finally, you need to wrap the wired modem on your [[Computer|computer]] using the methods above. As well as those outlined on the [[Modem_(API)| Modem API]] page, the following additional functions will then become available to you, where ''p'' is the variable the modem is wrapped to:
+
You should receive a message in your chat bar for each peripheral informing you of a successful connection, and the peripheral side.
 
+
<table style="width: 100%; border: solid 1px black; margin: 2px; border-spacing: 0px;">
+
<tr><td style="width: 350px; background: #E0E0E0; padding: .4em; font-weight:bold;">Method Name</td><td style="background: #E0E0E0; padding: .4em; font-weight:bold;">Description</td></tr>
+
 
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">p.getNamesRemote()</td>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the names of all connected peripherals. Peripheral names are the type of peripheral followed by an underscore and a unique number, e.g. ''"disk_0"''.</td></tr>
+
 
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">p.isPresentRemote(peripheral name)</td>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns '''true''' if the specified peripheral is connected; '''false''' otherwise. </td></tr>
+
 
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">p.getTypeRemote(peripheral name)</td>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns the type of the specified peripheral.</td></tr>
+
 
+
<tr style="background-color: #E8E8E8;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">p.getMethodsRemote(peripheral name)</td>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Returns a table of all the methods for the specified peripheral. </td></tr>
+
 
+
<tr style="background-color: #FFFFFF;"><td style="border-top: solid #C9C9C9 1px; padding: .4em;">p.callRemote(peripheral name, peripheral method)</td>
+
<td style="border-top: solid #C9C9C9 1px; padding: .4em;">Calls the specified method for the specified peripheral.</td></tr>
+
</table>
+
  
 
==Monitor==
 
==Monitor==

Revision as of 02:55, 25 August 2013

The Peripheral API is for interacting with peripherals connected to the computer, such as the Disk Drive, the Advanced Monitor and Monitor. (More peripherals can be found under here).

Each peripheral block has a name, or side. Although peripherals can now be attached to a computer using networking cables, the word side is a holdover from when peripherals needed to be directly next to the computer.

If the peripheral is next to the computer, its side is either "front", "back", "left", "right", "top" or "bottom". If the peripheral is attached by a cable, its side will follow the format "type_id", for example "printer_0".

Peripheral functions are called methods, a term borrowed from Java.

Grid disk.png  Peripheral (API)
Function Return values Description
peripheral.isPresent(string side) boolean

Returns true if a peripheral is connected on side.

peripheral.getType(string side) string/nil

Returns the type of peripheral connected on side, as a string. If no peripheral is connected, returns nil.

peripheral.getMethods(string side) table/nil

Returns a list of the names of all the methods of the peripheral connected on side. If no peripheral is connected, returns nil.

peripheral.call(string side, string method, ...) any

Calls a method on a peripheral. The arguments (apart from side and method) and the return values depend on the method being called. If no peripheral is connected, returns nil.

peripheral.wrap(string side) table/nil

Returns a table of functions, allowing you to call peripheral methods as if they were normal Lua functions. If no peripheral is connected, returns nil.

Connecting peripherals using networking cables

Using Networking Cables, you can attach and use peripherals without having them directly touching the computer. To connect a peripheral in this way, place a Wired Modem on any side of your computer, as you would a Wireless Modem. Place a Wired Modem on each peripheral you wish to connect, and then connect all of them with Networking Cables. Then right click each of the wired modems.

You should receive a message in your chat bar for each peripheral informing you of a successful connection, and the peripheral side.

Monitor

Monitor Peripheral functions have been moved to the Monitor page. In addition, the Advanced Monitor Peripheral functions have been moved to the Advanced Monitor page.

Printer

Printer Peripheral functions have been moved to the Printer page.

Computers

Computer Peripheral functions have been moved to the Computer page. In addition, the Advanced Computer Peripheral functions have been moved to the Advanced Computer page.