xref: /netbsd-src/share/man/man3lua/gpio.3lua (revision 16ffcab87863f2b1d7d13c8fea81e0433324c4a5)
1.\"	$NetBSD: gpio.3lua,v 1.1 2013/10/26 10:48:19 mbalmer Exp $
2.\"
3.\" Copyright (c) 2013 Marc Balmer <mbalmer@NetBSD.org>. All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\" 3. Neither the name of the University nor the names of its contributors
14.\"    may be used to endorse or promote products derived from this software
15.\"    without specific prior written permission.
16.\"
17.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27.\" SUCH DAMAGE.
28.\"
29.\"
30.Dd October 26, 2013
31.Dt GPIO 3lua
32.Os
33.Sh NAME
34.Nm gpio
35.Nd access
36.Xr gpio 4
37pins from Lua
38.Sh SYNOPSIS
39.Cd "require 'gpio'"
40.Pp
41.Bl -tag -width XXXX -compact
42.It Dv gpiodev = gpio.open(path)
43.It Dv pins = gpio.info(gpiodev)
44.It Dv gpio.close(gpiodev)
45.It Dv gpio.set(gpiodev, pin, flags)
46.It Dv gpio.unset(gpiodev, pin)
47.It Dv state = gpio.read(gpiodev, pin)
48.It Dv oldstate = gpio.write(gpiodev, pin, state)
49.It Dv gpio.toggle(gpiodev, pin)
50.It Dv gpio.attach(gpiodev, driver, offset, mask [, flags])
51.El
52.Sh DESCRIPTION
53The
54.Nm
55Lua provides access to a
56.Xr gpio 4
57device using the
58.Xr ioctl 2
59interface.
60.Pp
61.Bl -tag -width XXXX -compact
62.Pp
63.It Dv gpiodev = gpio.open(path)
64Open the gpio device and return an object to access its pins.
65.Pp
66.It Dv pins = gpio.info(gpiodev)
67Returns the number of pins.
68As with all remaining functions, this can also be called using the :
69notation, i.e. as
70.Em gpiodev:info() .
71.Pp
72.It Dv gpio.close(gpiodev)
73Close the gpio device.
74.Pp
75.It Dv gpio.set(gpiodev, pin, flags)
76Set gpio pin flags.
77Note that the pin number in this and all remaining functions is zero based and
78not one based, this to avoid confusion with tools like
79.Xr gpioctl 8
80which also number pins starting at zero.
81The following flags are defined:
82.Pp
83.Bl -tag -width XXXX -compact
84.It Dv gpio.PIN_INPUT
85Pin is an input.
86.Pp
87.It Dv gpio.PIN_OUTPUT
88Pin is an output.
89.Pp
90.It Dv gpio.PIN_INOUT
91Pin is birectional.
92.Pp
93.It Dv gpio.PIN_OPENDRAIN
94Pin is an open-drain output.
95.Pp
96.It Dv gpio.PIN_PUSHPULL
97Pin is a push-pull output.
98.Pp
99.It Dv gpio.PIN_TRISTATE
100Pin is tri-state (output disabled).
101.Pp
102.It Dv gpio.PIN_PULLUP
103Pin has an internal pull-up enabled.
104.Pp
105.It Dv gpio.PIN_PULLDOWN
106Pin has an internal pull-down enabled.
107.Pp
108.It Dv gpio.PIN_INVIN
109Invert input.
110.Pp
111.It Dv gpio.PIN_INVOUT
112Invert output.
113.Pp
114.It Dv gpio.PIN_USER
115Pin accessible by users.
116.Pp
117.It Dv gpio.PIN_PULSATE
118Pulsate pin at a hardware set frequency.
119.Pp
120.It Dv gpio.PIN_SET
121Pin is set.
122.El
123.Pp
124.It Dv gpio.unset(gpiodev, pin)
125Unset gpio pin.
126.Pp
127.It Dv stat = gpio.read(gpiodev, pin)
128Read the current pin state.
129.Pp
130.It Dv oldstate = gpio.write(gpiodev, pin, state)
131Write the pin state returning the old state.
132The following states are defined:
133.Pp
134.Bl -tag -width XXXX -compact
135.It Dv gpio.PIN_LOW
136Pin is in the low state.
137.Pp
138.It Dv gpio.PIN_HIGH
139Pin is in the high state.
140.El
141.Pp
142.It Dv gpio.toggle(gpiodev, pin)
143Toggle pin state.
144.Pp
145.It Dv gpio.attach(gpiodev, driver, offset, mask [, flags])
146Attach a device driver with offset, mask, and optional flags at a pin.
147.El
148.Sh EXAMPLES
149The following example code opens
150.Pa /dev/gpio0
151and prints all pin values:
152.Bd -literal
153local gpio = require 'gpio'
154
155gpiodev = gpio.open('/dev/gpio0')
156
157local npins = gpiodev:info()
158
159for n = 1, npins do
160	print('pin ' .. n .. ': ' .. gpiodev:read(n - 1))
161end
162.Ed
163.Sh SEE ALSO
164.Xr lua 1 ,
165.Xr luac 1 ,
166.Xr intro 3lua ,
167.Xr gpio 4
168.Sh HISTORY
169An
170.Nm
171manual appeared in
172.Nx 7.0 .
173.Sh AUTHORS
174.An -nosplit
175The
176.Nm
177Lua binding was written by
178.An Marc Balmer Aq Mt mbalmer@NetBSD.org .
179