Difference between revisions of "Peripheral (API)"
(Apparently the __TOC__ and arrangement of this page got spaghettified.) |
(Rewrote API function table, using Template:API_table) |
||
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
− | The Peripheral API is for interacting with | + | The Peripheral API is for interacting with [[peripheral]]s connected to the computer, such as the [[Disk Drive]], the [[Advanced Monitor]] and [[Monitor]]. (More peripherals can be found under [[:Category:Peripherals|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. | |
− | + | ||
− | + | {{API table|peripheral|image=Grid disk.png|2= | |
− | + | ||
− | + | {{API table/row|[[peripheral.isPresent]]({{type|string}} side)|{{type|boolean}}| | |
− | + | Returns '''true''' if a peripheral is connected on ''side''. | |
+ | |odd}} | ||
− | + | {{API table/row|[[peripheral.getType]]({{type|string}} side)|{{type|string}}/{{type|nil}}| | |
− | + | Returns the type of peripheral connected on ''side'', as a string. | |
+ | If no peripheral is connected, returns nil. | ||
+ | }} | ||
− | + | {{API table/row|[[peripheral.getMethods]]({{type|string}} side)|{{type|table}}/{{type|nil}}| | |
− | + | Returns a list of the names of all the methods of the peripheral connected on ''side''. | |
+ | If no peripheral is connected, returns nil. | ||
+ | |odd}} | ||
− | + | {{API table/row|[[peripheral.call]]({{type|string}} side, {{type|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. | ||
+ | }} | ||
+ | |||
+ | {{API table/row|[[peripheral.wrap]]({{type|string}} side)|{{type|table}}/{{type|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. | ||
+ | |odd}} | ||
+ | |||
+ | }} | ||
==Remote Peripherals using Networking Cables== | ==Remote Peripherals using Networking Cables== |
Revision as of 02:51, 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.
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. |
Remote 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 too!
Finally, you need to wrap the wired modem on your computer using the methods above. As well as those outlined on the Modem API page, the following additional functions will then become available to you, where p is the variable the modem is wrapped to:
Method Name | Description |
p.getNamesRemote() | 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". |
p.isPresentRemote(peripheral name) | Returns true if the specified peripheral is connected; false otherwise. |
p.getTypeRemote(peripheral name) | Returns the type of the specified peripheral. |
p.getMethodsRemote(peripheral name) | Returns a table of all the methods for the specified peripheral. |
p.callRemote(peripheral name, peripheral method) | Calls the specified method for the specified peripheral. |
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.