Skip to content

Commit 1adc9c0

Browse files
committed
Add grisp example
1 parent 2144f87 commit 1adc9c0

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

.credo.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
%{
55
name: "default",
66
files: %{
7-
included: ["lib/"],
7+
included: ["lib/", "examples/"],
88
excluded: ["lib/i2c/i2c_nif.ex"]
99
},
1010
strict: true,

examples/backends/grisp.ex

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
defmodule Circuits.I2C.GRiSP do
2+
@moduledoc """
3+
Circuits.I2C backend for GRiSP
4+
5+
This is completely untested.
6+
"""
7+
@behaviour Circuits.I2C.Backend
8+
9+
alias Circuits.I2C.Backend
10+
alias Circuits.I2C.Bus
11+
12+
defstruct [:ref]
13+
14+
@doc """
15+
Return the I2C bus names on this system
16+
17+
No supported options
18+
"""
19+
@impl Backend
20+
def bus_names(_options) do
21+
:grisp_i2c.buses() |> Map.keys() |> Enum.map(&Atom.to_string/1)
22+
end
23+
24+
@doc """
25+
Open an I2C bus
26+
"""
27+
@impl Backend
28+
def open(bus_name, _options) do
29+
ref = :grisp_i2c.open(String.to_atom(bus_name))
30+
{:ok, %__MODULE__{ref: ref}}
31+
end
32+
33+
@doc """
34+
Return information about this backend
35+
"""
36+
@impl Backend
37+
def info() do
38+
%{backend: __MODULE__}
39+
end
40+
41+
defimpl Bus do
42+
@impl Bus
43+
def read(%Circuits.I2C.GRiSP{ref: ref}, address, count, _options) do
44+
with [result] <- :grisp_i2c.transfer(ref, [{:read, address, 0, count}]) do
45+
{:ok, result}
46+
end
47+
end
48+
49+
@impl Bus
50+
def write(%Circuits.I2C.GRiSP{ref: ref}, address, data, _options) do
51+
with [:ok] <- :grisp_i2c.transfer(ref, [{:write, address, 0, data}]) do
52+
:ok
53+
end
54+
end
55+
56+
@impl Bus
57+
def write_read(
58+
%Circuits.I2C.GRiSP{ref: ref},
59+
address,
60+
write_data,
61+
read_count,
62+
options
63+
) do
64+
with [:ok, result] <-
65+
:grisp_i2c.transfer(ref, [
66+
{:write, address, 0, write_data},
67+
{:read, address, 0, count}
68+
]) do
69+
{:ok, result}
70+
end
71+
end
72+
73+
@impl Bus
74+
def close(%Circuits.I2C.GRiSP{ref: ref}) do
75+
:ok
76+
end
77+
end
78+
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ defmodule Circuits.I2C.MixProject do
3232
]
3333
end
3434

35-
defp elixirc_paths(env) when env in [:test, :dev], do: ["lib", "examples"]
35+
defp elixirc_paths(env) when env in [:test, :dev], do: ["lib", "examples/circuits_sim"]
3636
defp elixirc_paths(_env), do: ["lib"]
3737

3838
def application do

0 commit comments

Comments
 (0)