-
Notifications
You must be signed in to change notification settings - Fork 12
/
i2c_dev.ex
106 lines (85 loc) · 2.41 KB
/
i2c_dev.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
defmodule Circuits.I2C.I2CDev do
@moduledoc """
Circuits.I2C backend for the Linux i2c-dev interface
This backend works on Nerves, embedded Linux, and desktop Linux.
"""
@behaviour Circuits.I2C.Backend
alias Circuits.I2C.Backend
alias Circuits.I2C.Bus
alias Circuits.I2C.Nif
defstruct [:ref, :retries, :flags]
@doc """
Return the I2C bus names on this system
No supported options
"""
case System.get_env("CIRCUITS_I2C_I2CDEV") do
"test" ->
@impl Backend
def bus_names(_options), do: ["i2c-test-0", "i2c-test-1", "i2c-flaky"]
"normal" ->
@impl Backend
def bus_names(_options) do
Path.wildcard("/dev/i2c-*")
|> Enum.map(fn p -> String.replace_prefix(p, "/dev/", "") end)
end
_ ->
@impl Backend
def bus_names(_options) do
[]
end
end
@doc """
Open an I2C bus
Bus names are typically of the form `"i2c-n"` and available buses may be
found by calling `Circuits.I2C.bus_names/0`.
Options:
* `:retries` - Specify a nonnegative integer for how many times to retry
a failed I2C operation.
"""
@impl Backend
def open(bus_name, options) do
retries = Keyword.get(options, :retries, 0)
with {:ok, ref, flags} <- Nif.open(bus_name) do
{:ok, %__MODULE__{ref: ref, flags: flags, retries: retries}}
end
end
@doc """
Return information about this backend
"""
@impl Backend
def info() do
Nif.info()
|> Map.put(:backend, __MODULE__)
end
defimpl Bus do
@impl Bus
def flags(%Circuits.I2C.I2CDev{flags: flags}) do
flags
end
@impl Bus
def read(%Circuits.I2C.I2CDev{ref: ref, retries: retries}, address, count, options) do
retries = Keyword.get(options, :retries, retries)
Nif.read(ref, address, count, retries)
end
@impl Bus
def write(%Circuits.I2C.I2CDev{ref: ref, retries: retries}, address, data, options) do
retries = Keyword.get(options, :retries, retries)
Nif.write(ref, address, data, retries)
end
@impl Bus
def write_read(
%Circuits.I2C.I2CDev{ref: ref, retries: retries},
address,
write_data,
read_count,
options
) do
retries = Keyword.get(options, :retries, retries)
Nif.write_read(ref, address, write_data, read_count, retries)
end
@impl Bus
def close(%Circuits.I2C.I2CDev{ref: ref}) do
Nif.close(ref)
end
end
end