Skip to content
This repository was archived by the owner on Jan 11, 2024. It is now read-only.

Commit b1a8fad

Browse files
committed
Update docs
1 parent 3ec4346 commit b1a8fad

1 file changed

Lines changed: 106 additions & 7 deletions

File tree

lib/chip.ex

Lines changed: 106 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,39 @@ defmodule Circuits.GPIO.Chip do
2929
}
3030

3131
@typedoc """
32-
Explain offset
32+
The offset of the pin
33+
34+
An offset is the pin number provided. Normally these are labeled `GPIO N` or
35+
`GPIO_N` where `N` is the pin number. For example, if you wanted to use to
36+
use `GPIO 17` on a Raspberry PI the offset value would be `17`.
37+
38+
More resources:
39+
40+
Raspberry PI: https://pinout.xyz/
41+
Beaglebone: https://beagleboard.org/Support/bone101
3342
"""
3443
@type offset() :: non_neg_integer()
3544

3645
@typedoc """
37-
Explain offset value
46+
The value of the offset
47+
48+
This is either 0 for low or off, or 1 for high or on.
3849
"""
3950
@type offset_value() :: 0 | 1
4051

4152
@typedoc """
42-
Explain line direction
53+
The direction of the line
54+
55+
With the character device you drive a line with configured offsets. These
56+
offsets all share a direction, either `:output` or `:input`, which is called
57+
the line direction.
58+
59+
The `:output` direction means you control the GPIOs by setting the value of
60+
the GPIOs to 1 or 0. See `Circuits.GPIO.Chip.set_value/2` for more
61+
information.
62+
63+
The `:input` direction means you can only read the current value of the GPIOs
64+
on the line. See `Circuits.GPIO.Chip.read_value/1` for more information.
4365
"""
4466
@type line_direction() :: :input | :output
4567

@@ -67,6 +89,20 @@ defmodule Circuits.GPIO.Chip do
6789
end
6890

6991
@doc """
92+
Listen to line events on the line offset
93+
94+
```elixir
95+
Circuits.GPIO.Chip.listen_event(mygpio_chip, 24)
96+
# cause the offset to change value
97+
flush
98+
{:circuits_cdev, 24, timestamp, new_value}
99+
```
100+
101+
The timestamp will be in nanoseconds so as you do time calculations and
102+
conversions be sure to take that into account.
103+
104+
The `new_value` will be the value the offset value changed to either `1` or
105+
`0`.
70106
"""
71107
@spec listen_event(t() | String.t(), offset()) :: :ok
72108
def listen_event(%__MODULE__{} = chip, offset) do
@@ -83,6 +119,7 @@ defmodule Circuits.GPIO.Chip do
83119
Open a GPIO Chip
84120
85121
```elixir
122+
{:ok, chip} = Circuits.GPIO.Chip.open(gpiochip_device)
86123
```
87124
"""
88125
@spec open(String.t()) :: {:ok, t()}
@@ -102,6 +139,17 @@ defmodule Circuits.GPIO.Chip do
102139

103140
@doc """
104141
Read value from a line handle
142+
143+
This is useful when you have a line handle that contains only one GPIO
144+
offset.
145+
146+
If you want to read multiple GPIOs at once see
147+
`Circuits.GPIO.Chip.read_values/1`.
148+
149+
```elixir
150+
{:ok, line_handle} = Circuits.GPIO.Chip.request_line("gpiochip0", 17)
151+
{:ok, 0} = Circuits.GPIO.Chip.read_value(line_handle)
152+
```
105153
"""
106154
@spec read_value(LineHandle.t()) :: {:ok, offset_value()} | {:error, atom()}
107155
def read_value(line_handle) do
@@ -116,6 +164,19 @@ defmodule Circuits.GPIO.Chip do
116164

117165
@doc """
118166
Read values for a line handle
167+
168+
This is useful when you a line handle that contains multiple GPIO offsets.
169+
170+
```elixir
171+
{:ok, line_handle} = Circuits.GPIO.Chip.request_lines("gpiochip0", [17, 22, 23, 24])
172+
{:ok, [0, 0, 0, 0]} = Circuits.GPIO.Chip.read_values(line_handle)
173+
```
174+
175+
Note that the values in the list match the index order of how the offsets were
176+
requested.
177+
178+
Note that the order of the values returned return the order that the offsets
179+
were requested.
119180
"""
120181
@spec read_values(LineHandle.t()) :: {:ok, [offset_value()]} | {:error, atom()}
121182
def read_values(line_handle) do
@@ -125,7 +186,15 @@ defmodule Circuits.GPIO.Chip do
125186
end
126187

127188
@doc """
128-
Request a line handle for a single line
189+
Request a line handle for a single GPIO offset
190+
191+
192+
```elixir
193+
{:ok, line_handle} = Circuits.GPIO.Chip.request_line(my_gpio_chip, 17, :output)
194+
```
195+
196+
See `Circuits.GPIO.Chip.request_lines/3` and `Circuits.GPIO.LineHandle` for
197+
more details about line handles.
129198
"""
130199
@spec request_line(t() | String.t(), offset(), line_direction()) :: {:ok, LineHandle.t()}
131200
def request_line(%__MODULE__{} = chip, offset, direction) do
@@ -140,7 +209,19 @@ defmodule Circuits.GPIO.Chip do
140209
end
141210

142211
@doc """
143-
Request a line handle for many lines
212+
Request a line handle for multiple GPIO offsets
213+
214+
```elixir
215+
{:ok, line_handle} = Circuits.GPIO.Chip.request_lines(my_gpio_chip, [17, 24], :output)
216+
```
217+
218+
For the GPIO character device driver you drive GPIOs by requesting for a line
219+
handle what contains one or more GPIO offsets. The line handle is mechanism
220+
by which you can read and set the values of the GPIO(s). The line handle is
221+
attached to the calling process and kernel will not allow others to control
222+
the GPIO(s) that are part of that the line handle. Moreover, one the process
223+
that requested the line handle goes away the kernel will be able to
224+
automatically free the system resources that were tied to that line handle.
144225
"""
145226
@spec request_lines(t() | String.t(), [offset()], line_direction()) :: {:ok, LineHandle.t()}
146227
def request_lines(%__MODULE__{} = chip, offsets, direction) do
@@ -157,15 +238,33 @@ defmodule Circuits.GPIO.Chip do
157238
end
158239

159240
@doc """
160-
ASDF
241+
Set the value of the GPIO
242+
243+
```elixir
244+
{:ok, line_handle} = Circuits.GPIO.Chip.request_lines(my_gpio_chip, 17)
245+
{:ok, 0} = Circuits.GPIO.Chip.read_value(line_handle)
246+
:ok = Circuits.GPIO.Chip.set_value(line_handle, 1)
247+
{:ok, 1} = Circuits.GPIO.Chip.read_value(line_handle)
248+
```
161249
"""
162250
@spec set_value(LineHandle.t(), offset_value()) :: :ok | {:error, atom()}
163251
def set_value(handle, value) do
164252
set_values(handle, [value])
165253
end
166254

167255
@doc """
168-
Set values of the GPIO lines
256+
Set values of the GPIOs
257+
258+
```elixir
259+
{:ok, line_handle} = Circuits.GPIO.Chip.request_lines(my_gpio_chip, [17, 24, 22])
260+
{:ok, [0, 0, 0]} = Circuits.GPIO.Chip.read_value(line_handle)
261+
:ok = Circuits.GPIO.Chip.set_value(line_handle, [1, 0, 1])
262+
{:ok, [1, 0, 1]} = Circuits.GPIO.Chip.read_value(line_handle)
263+
```
264+
265+
Note that the order of the values that were sent matches the order by which
266+
the GPIO offsets where requested. In the example above offset 17 was set to
267+
1, offset 24 was stayed at 0, offset 22 was set to 1.
169268
"""
170269
@spec set_values(LineHandle.t(), [offset_value()]) :: :ok | {:error, atom()}
171270
def set_values(line_handle, values) do

0 commit comments

Comments
 (0)