Skip to content

Commit 09f29e7

Browse files
committed
Doc updates and some code consistency updates
1 parent 32707ec commit 09f29e7

File tree

11 files changed

+46
-39
lines changed

11 files changed

+46
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* `t:gpio_info/0` now only contains static information so that it's easier to
77
cache. Caching reduces open time from 2-3ms to 100us on an RPi Zero, so it's
88
a decent improvement for apps that set a lot of GPIOs on boot.
9-
* Add `t:gpio_status/0` and `gpio_status/1` to get runtime information about
9+
* Add `t:status/0` and `status/1` to get runtime information about
1010
GPIOs. It's currently populated with GPIO consumer info, direction and pull
1111
mode. It could certainly have more added in the future.
1212
* Add `write_one/3` and `read_one/2` convenience methods to simplify one-off

c_src/gpio_nif.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static ERL_NIF_TERM set_pull_mode(ErlNifEnv *env, int argc, const ERL_NIF_TERM a
327327
return atom_ok;
328328
}
329329

330-
static ERL_NIF_TERM get_gpio_status(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
330+
static ERL_NIF_TERM get_status(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
331331
{
332332
struct gpio_priv *priv = enif_priv_data(env);
333333
char gpiochip_path[MAX_GPIOCHIP_PATH_LEN];
@@ -337,7 +337,7 @@ static ERL_NIF_TERM get_gpio_status(ErlNifEnv *env, int argc, const ERL_NIF_TERM
337337
return enif_make_badarg(env);
338338

339339
ERL_NIF_TERM result;
340-
int rc = hal_get_gpio_status(priv->hal_priv, env, gpiochip_path, offset, &result);
340+
int rc = hal_get_status(priv->hal_priv, env, gpiochip_path, offset, &result);
341341
if (rc >= 0)
342342
return make_ok_tuple(env, result);
343343
else
@@ -434,7 +434,7 @@ static ErlNifFunc nif_funcs[] = {
434434
{"set_interrupts", 4, set_interrupts, 0},
435435
{"set_direction", 2, set_direction, 0},
436436
{"set_pull_mode", 2, set_pull_mode, 0},
437-
{"status", 1, get_gpio_status, 0},
437+
{"status", 1, get_status, 0},
438438
{"backend_info", 0, backend_info, 0},
439439
{"enumerate", 0, gpio_enumerate, 0}
440440
};

c_src/gpio_nif.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ ERL_NIF_TERM hal_info(ErlNifEnv *env, void *hal_priv, ERL_NIF_TERM info);
9696
/**
9797
* Enumerate all GPIO pins
9898
*
99-
* Returns a list of Circuits.GPIO.gpio_info maps
99+
* Returns a list of Circuits.GPIO.identifiers maps
100100
*/
101101
ERL_NIF_TERM hal_enumerate(ErlNifEnv *env, void *hal_priv);
102102

@@ -195,7 +195,7 @@ int hal_apply_pull_mode(struct gpio_pin *pin);
195195
* @param result where to store the result when successful
196196
* @return 0 on success, -errno on failure
197197
*/
198-
int hal_get_gpio_status(void *hal_priv, ErlNifEnv *env, const char *gpiochip, int offset, ERL_NIF_TERM *result);
198+
int hal_get_status(void *hal_priv, ErlNifEnv *env, const char *gpiochip, int offset, ERL_NIF_TERM *result);
199199

200200
// nif_utils.c
201201
ERL_NIF_TERM make_ok_tuple(ErlNifEnv *env, ERL_NIF_TERM value);

c_src/hal_cdev_gpio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ ERL_NIF_TERM hal_enumerate(ErlNifEnv *env, void *hal_priv)
345345
return gpio_list;
346346
}
347347

348-
int hal_get_gpio_status(void *hal_priv, ErlNifEnv *env, const char *gpiochip, int offset, ERL_NIF_TERM *result)
348+
int hal_get_status(void *hal_priv, ErlNifEnv *env, const char *gpiochip, int offset, ERL_NIF_TERM *result)
349349
{
350350
int gpiochip_fd = open(gpiochip, O_RDWR|O_CLOEXEC);
351351
if (gpiochip_fd < 0)

c_src/hal_stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ ERL_NIF_TERM hal_enumerate(ErlNifEnv *env, void *hal_priv)
225225
return gpio_list;
226226
}
227227

228-
int hal_get_gpio_status(void *hal_priv, ErlNifEnv *env, const char *gpiochip, int offset, ERL_NIF_TERM *result)
228+
int hal_get_status(void *hal_priv, ErlNifEnv *env, const char *gpiochip, int offset, ERL_NIF_TERM *result)
229229
{
230230
struct stub_priv *stub_priv = hal_priv;
231231
int pin_base;

lib/gpio.ex

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,20 @@ defmodule Circuits.GPIO do
102102
@type pull_mode() :: :not_set | :none | :pullup | :pulldown
103103

104104
@typedoc """
105-
Static GPIO information
105+
Ways of referring to a GPIO
106106
107-
Location and other static information about a GPIO that is returned by
108-
`enumerate/1`. Backends must provide `:location` which is an unambiguous
109-
`t:gpio_spec/0` for use with `open/3`. As described in `open/3`, there are
110-
many ways to refer to GPIOs and those may be exposed here as well. Since GPIO
111-
information can be cached, this map does not contain mode settings or current
112-
values. See other functions for getting that information.
107+
It's possible to refer to a GPIOs in many ways and this map contains
108+
information for doing that. See `enumerate/1` and `identifiers/1` for
109+
querying `Circuits.GPIO` for these maps.
110+
111+
The information in this map is backend specific. At a minimum, all backends
112+
provide the `:location` field which is an unambiguous `t:gpio_spec/0` for use
113+
with `open/3`.
114+
115+
When provided, the `:label` field is a string name for the GPIO that should
116+
be unique to the system but this isn't guaranteed. A common convention is to
117+
label GPIOs by their pin names in documentation or net names in schematics.
118+
The Linux cdev backend uses labels from the device tree file.
113119
114120
Fields:
115121
@@ -181,7 +187,7 @@ defmodule Circuits.GPIO do
181187
def gpio_spec?(x), do: is_gpio_spec(x)
182188

183189
@doc """
184-
Return static information about a GPIO
190+
Return indentifying information about a GPIO
185191
186192
See `t:gpio_spec/0` for the ways of referring to GPIOs. If the GPIO is found,
187193
this function returns information about the GPIO.
@@ -190,7 +196,7 @@ defmodule Circuits.GPIO do
190196
def identifiers(gpio_spec) do
191197
{backend, backend_defaults} = default_backend()
192198

193-
backend.gpio_identifiers(gpio_spec, backend_defaults)
199+
backend.identifiers(gpio_spec, backend_defaults)
194200
end
195201

196202
@doc """
@@ -203,7 +209,7 @@ defmodule Circuits.GPIO do
203209
def status(gpio_spec) do
204210
{backend, backend_defaults} = default_backend()
205211

206-
backend.gpio_status(gpio_spec, backend_defaults)
212+
backend.status(gpio_spec, backend_defaults)
207213
end
208214

209215
@doc """
@@ -391,12 +397,12 @@ defmodule Circuits.GPIO do
391397
def backend_info(backend \\ nil)
392398

393399
def backend_info(nil), do: backend_info(default_backend())
394-
def backend_info({backend, _options}), do: backend.info()
400+
def backend_info({backend, _options}), do: backend.backend_info()
395401

396402
@doc """
397403
Return a list of accessible GPIOs
398404
399-
Each GPIO is described in a `t:gpio_info/0` map. Some fields in the map like
405+
Each GPIO is described in a `t:identifiers/0` map. Some fields in the map like
400406
`:location` and `:label` may be passed to `open/3` to use the GPIO. The map
401407
itself can also be passed to `open/3` and the function will figure out how to
402408
access the GPIO.

lib/gpio/backend.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ defmodule Circuits.GPIO.Backend do
2323
See `t:gpio_spec/0` for the ways of referring to GPIOs. The `options` contain
2424
backend-specific options to help enumerating GPIOs.
2525
26-
If the GPIO is found, this function returns information about the GPIO.
26+
If the GPIO is found, this function returns identifying information about the GPIO.
2727
"""
28-
@callback gpio_identifiers(
28+
@callback identifiers(
2929
gpio_spec :: GPIO.gpio_spec(),
3030
options :: GPIO.open_options()
3131
) :: {:ok, GPIO.identifiers()} | {:error, atom()}
@@ -43,7 +43,7 @@ defmodule Circuits.GPIO.Backend do
4343
4444
If the GPIO is found, this function returns its status.
4545
"""
46-
@callback gpio_status(
46+
@callback status(
4747
gpio_spec :: GPIO.gpio_spec(),
4848
options :: GPIO.open_options()
4949
) :: {:ok, GPIO.status()} | {:error, atom()}
@@ -74,5 +74,5 @@ defmodule Circuits.GPIO.Backend do
7474
@doc """
7575
Return information about this backend
7676
"""
77-
@callback info() :: map()
77+
@callback backend_info() :: map()
7878
end

lib/gpio/cdev.ex

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,25 +70,25 @@ defmodule Circuits.GPIO.CDev do
7070
end
7171

7272
@impl Backend
73-
def gpio_identifiers(number, options) when is_integer(number) and number >= 0 do
73+
def identifiers(number, options) when is_integer(number) and number >= 0 do
7474
retry_find(options, &find_by_index(&1, number))
7575
end
7676

77-
def gpio_identifiers(line_label, options) when is_binary(line_label) do
77+
def identifiers(line_label, options) when is_binary(line_label) do
7878
retry_find(options, &find_by_label(&1, line_label))
7979
end
8080

81-
def gpio_identifiers(tuple_spec, options)
81+
def identifiers(tuple_spec, options)
8282
when is_tuple(tuple_spec) and tuple_size(tuple_spec) == 2 do
8383
retry_find(options, &find_by_tuple(&1, tuple_spec))
8484
end
8585

86-
def gpio_identifiers(_gpio_spec, _options) do
86+
def identifiers(_gpio_spec, _options) do
8787
{:error, :not_found}
8888
end
8989

9090
@impl Backend
91-
def gpio_status(gpio_spec, options \\ []) do
91+
def status(gpio_spec, options \\ []) do
9292
with {:ok, location} <- find_location(gpio_spec, options) do
9393
resolved_location = resolve_gpiochip(location)
9494
Nif.status(resolved_location)
@@ -101,8 +101,8 @@ defmodule Circuits.GPIO.CDev do
101101
end
102102

103103
defp find_location(gpio_spec, options) do
104-
with {:ok, identifiers} <- gpio_identifiers(gpio_spec, options) do
105-
{:ok, identifiers.location}
104+
with {:ok, ids} <- identifiers(gpio_spec, options) do
105+
{:ok, ids.location}
106106
end
107107
end
108108

@@ -127,7 +127,7 @@ defmodule Circuits.GPIO.CDev do
127127
end
128128

129129
@impl Backend
130-
def info() do
130+
def backend_info() do
131131
Nif.backend_info() |> Map.put(:name, __MODULE__)
132132
end
133133

lib/gpio/diagnostics.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ defmodule Circuits.GPIO.Diagnostics do
3737
"""
3838
@spec report(GPIO.gpio_spec(), GPIO.gpio_spec()) :: boolean
3939
def report(out_gpio_spec, in_gpio_spec) do
40-
{:ok, out_gpio_identifiers} = GPIO.identifiers(out_gpio_spec)
41-
{:ok, in_gpio_identifiers} = GPIO.identifiers(in_gpio_spec)
40+
{:ok, out_identifiers} = GPIO.identifiers(out_gpio_spec)
41+
{:ok, in_identifiers} = GPIO.identifiers(in_gpio_spec)
4242
results = run(out_gpio_spec, in_gpio_spec)
4343
passed = Enum.all?(results, fn {_, result} -> result == :ok end)
4444
check_connections? = hd(results) != {"Simple writes and reads work", :ok}
@@ -51,8 +51,8 @@ defmodule Circuits.GPIO.Diagnostics do
5151
Output GPIO: #{inspect(out_gpio_spec)}
5252
Input GPIO: #{inspect(in_gpio_spec)}
5353
54-
Output info: #{inspect(out_gpio_identifiers)}
55-
Input info: #{inspect(in_gpio_identifiers)}
54+
Output ids: #{inspect(out_identifiers)}
55+
Input ids: #{inspect(in_identifiers)}
5656
Backend: #{inspect(Circuits.GPIO.backend_info()[:name])}
5757
5858
""",

lib/gpio/nil_backend.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ defmodule Circuits.GPIO.NilBackend do
1616
end
1717

1818
@impl Backend
19-
def gpio_identifiers(_gpio_spec, _options) do
19+
def identifiers(_gpio_spec, _options) do
2020
{:error, :not_found}
2121
end
2222

2323
@impl Backend
24-
def gpio_status(_gpio_spec, _options) do
24+
def status(_gpio_spec, _options) do
2525
{:error, :not_found}
2626
end
2727

@@ -31,7 +31,7 @@ defmodule Circuits.GPIO.NilBackend do
3131
end
3232

3333
@impl Backend
34-
def info() do
34+
def backend_info() do
3535
%{name: __MODULE__}
3636
end
3737
end

0 commit comments

Comments
 (0)