-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Giovanni Visciano
committed
Sep 8, 2017
1 parent
7d9be70
commit 211e7a1
Showing
11 changed files
with
400 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
defmodule NervesDHT.Device do | ||
@moduledoc false | ||
|
||
defmodule Runner do | ||
@moduledoc false | ||
|
||
use GenServer | ||
|
||
defmodule State do | ||
@moduledoc false | ||
|
||
defstruct [:father, :fun, :timeout] | ||
end | ||
|
||
def start_link(father, fun, timeout) do | ||
state = %State{father: father, fun: fun, timeout: timeout} | ||
GenServer.start_link(__MODULE__, state) | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast(:read, %State{father: father, fun: fun, timeout: timeout}=state) do | ||
task = Task.async(fun) | ||
case Task.yield(task, timeout) do | ||
{:ok, result} -> | ||
GenServer.cast(father, {:result, result}) | ||
nil -> | ||
Task.shutdown(task) | ||
GenServer.cast(father, {:result, {:error, :timeout}}) | ||
end | ||
{:noreply, state} | ||
end | ||
end | ||
|
||
use GenServer | ||
|
||
defmodule State do | ||
@moduledoc false | ||
defstruct [:runner, :callers_queue] | ||
end | ||
|
||
def start_link(name, fun, timeout \\ 4000) do | ||
GenServer.start_link(__MODULE__, [fun, timeout], name: name) | ||
end | ||
|
||
def read(ref, timeout \\ 5000) do | ||
GenServer.call(ref, :read, timeout) | ||
end | ||
|
||
@impl GenServer | ||
def init([fun, timeout]) do | ||
{:ok, runner} = Runner.start_link(self(), fun, timeout) | ||
{:ok, %State{runner: runner, callers_queue: []}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_call(:read, from, %State{runner: runner, callers_queue: []}=state) do | ||
GenServer.cast(runner, :read) | ||
{:noreply, %State{state | callers_queue: [from]}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_call(:read, from, %State{callers_queue: callers_queue}=state) do | ||
{:noreply, %State{state | callers_queue: [from | callers_queue]}} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:result, result}, %State{callers_queue: callers_queue}=state) | ||
when callers_queue != [] do | ||
notify(callers_queue, result) | ||
{:noreply, %State{state | callers_queue: []}} | ||
end | ||
|
||
defp notify(callers_queue, reply) do | ||
Enum.each(callers_queue, &(GenServer.reply(&1, reply))) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule NervesDHT.Driver do | ||
@moduledoc false | ||
|
||
# executable name injection (tests use "dht_exe.sh" fake) | ||
@dht_exe Application.get_env(:nerves_dht, :dht_exe, "dht") | ||
|
||
def dht_read(sensor, pin) do | ||
cmd = Application.app_dir(:nerves_dht, Path.join("priv", @dht_exe)) | ||
args = dht_cmd_args(sensor, pin) | ||
|
||
{result, exit_status} = System.cmd(cmd, args) | ||
|
||
if exit_status == 0 do | ||
[humidity_str, temperature_str] = String.split(result) | ||
|
||
{humidity, ""} = Float.parse(humidity_str) | ||
{temperature, ""} = Float.parse(temperature_str) | ||
|
||
{:ok, humidity, temperature} | ||
else | ||
{:error, dht_error_code_to_reason(exit_status)} | ||
end | ||
end | ||
|
||
defp dht_cmd_args(sensor, pin) do | ||
[ | ||
case sensor do | ||
:dht11 -> "11" | ||
:dht22 -> "22" | ||
:am2302 -> "2302" | ||
end, | ||
to_string(pin) | ||
] | ||
end | ||
|
||
# -1, -2, -3, -4 | ||
defp dht_error_code_to_reason(255), do: :timeout | ||
defp dht_error_code_to_reason(254), do: :checksum | ||
defp dht_error_code_to_reason(253), do: :argument | ||
defp dht_error_code_to_reason(252), do: :gpio | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.