|
1 | 1 | defmodule Circuits.I2C.Nif do
|
2 | 2 | @moduledoc false
|
3 | 3 |
|
4 |
| - defp load_nif_and_apply(fun, args) do |
5 |
| - nif_binary = Application.app_dir(:circuits_i2c, "priv/i2c_nif") |
| 4 | + @on_load {:load_nif, 0} |
| 5 | + @compile {:autoload, false} |
6 | 6 |
|
7 |
| - # Optimistically load the NIF. Handle the possible race. |
8 |
| - case :erlang.load_nif(to_charlist(nif_binary), 0) do |
9 |
| - :ok -> apply(__MODULE__, fun, args) |
10 |
| - {:error, {:reload, _}} -> apply(__MODULE__, fun, args) |
11 |
| - error -> error |
12 |
| - end |
13 |
| - end |
14 |
| - |
15 |
| - def open(device) do |
16 |
| - load_nif_and_apply(:open, [device]) |
| 7 | + def load_nif() do |
| 8 | + :erlang.load_nif(:code.priv_dir(:circuits_i2c) ++ ~c"/i2c_nif", 0) |
17 | 9 | end
|
18 | 10 |
|
| 11 | + def open(_device), do: :erlang.nif_error(:nif_not_loaded) |
19 | 12 | def read(_ref, _address, _count, _retries), do: :erlang.nif_error(:nif_not_loaded)
|
20 | 13 | def write(_ref, _address, _data, _retries), do: :erlang.nif_error(:nif_not_loaded)
|
21 | 14 |
|
22 | 15 | def write_read(_ref, _address, _write_data, _read_count, _retries),
|
23 | 16 | do: :erlang.nif_error(:nif_not_loaded)
|
24 | 17 |
|
25 | 18 | def close(_ref), do: :erlang.nif_error(:nif_not_loaded)
|
26 |
| - |
27 |
| - def info() do |
28 |
| - load_nif_and_apply(:info, []) |
29 |
| - end |
| 19 | + def info(), do: :erlang.nif_error(:nif_not_loaded) |
30 | 20 | end
|
0 commit comments