Skip to content

Commit 861dc77

Browse files
committed
Simplify SensorWorker; support both with and without genserver
1 parent 9af9a1b commit 861dc77

File tree

4 files changed

+88
-35
lines changed

4 files changed

+88
-35
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Devices on I2C bus "i2c-1":
1919
1 devices detected on 1 I2C buses
2020

2121
# Connect to the sensor.
22-
iex> {:ok, aht20} = AHT20.start(bus_name: "i2c-1", bus_address: 0x38)
23-
{:ok, AHT20.Sensor{}}
22+
iex> {:ok, aht20} = AHT20.start_link(bus_name: "i2c-1", bus_address: 0x38)
23+
{:ok, #PID<0.1407.0>}
2424

2525
# Read the humidity and temperature from the sensor.
2626
iex> AHT20.read_data(aht20)
@@ -35,4 +35,4 @@ iex> AHT20.read_data(aht20)
3535
```
3636

3737
Depending on your hardware configuration, you may need to modify the call to
38-
`AHT20.start/1`. See `t:AHT20.Sensor.config/0` for parameters.
38+
`AHT20.start_link/1`. See `t:AHT20.Sensor.config/0` for parameters.

lib/aht20.ex

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,37 @@
11
defmodule AHT20 do
2-
@moduledoc false
2+
@moduledoc """
3+
A collection of convenient functions to use this library.
4+
"""
5+
6+
@spec start(AHT20.Sensor.config()) :: {:ok, AHT20.Sensor.t()} | {:error, any}
7+
def start(config) do
8+
AHT20.Sensor.start(config)
9+
end
310

411
@spec start_link(AHT20.Sensor.config()) :: {:ok, pid} | {:error, any}
512
def start_link(config) do
613
AHT20.SensorWorker.start_link(config)
714
end
815

9-
@spec read_data() :: {:ok, AHT20.Measurement.t()} | {:error, any}
10-
def read_data() do
11-
AHT20.SensorWorker.read_data()
16+
@spec read_data(pid) :: {:ok, AHT20.Measurement.t()} | {:error, any}
17+
def read_data(pid) when is_pid(pid) do
18+
AHT20.SensorWorker.read_data(pid)
19+
end
20+
21+
@spec read_data(AHT20.Sensor.t()) :: {:ok, AHT20.Measurement.t()} | {:error, any}
22+
def read_data(%AHT20.Sensor{} = sensor) when is_struct(sensor) do
23+
{:ok, sensor_output} = AHT20.Sensor.read_data(sensor)
24+
{:ok, AHT20.Measurement.from_sensor_output(sensor_output)}
25+
end
26+
27+
@spec read_state(pid) :: {:ok, AHT20.State.t()} | {:error, any}
28+
def read_state(pid) when is_pid(pid) do
29+
AHT20.SensorWorker.read_state(pid)
1230
end
1331

14-
@spec read_state() :: {:ok, AHT20.State.t()} | {:error, any}
15-
def read_state() do
16-
AHT20.SensorWorker.read_state()
32+
@spec read_state(AHT20.Sensor.t()) :: {:ok, AHT20.State.t()} | {:error, any}
33+
def read_state(%AHT20.Sensor{} = sensor) when is_struct(sensor) do
34+
{:ok, <<sensor_state_byte>>} = AHT20.Sensor.read_state(sensor)
35+
{:ok, AHT20.State.from_byte(sensor_state_byte)}
1736
end
1837
end

lib/aht20/sensor_worker.ex

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ defmodule AHT20.SensorWorker do
99

1010
@spec start_link(AHT20.Sensor.config()) :: {:ok, pid} | {:error, any}
1111
def start_link(config) do
12-
GenServer.start_link(__MODULE__, config, name: __MODULE__)
12+
GenServer.start_link(__MODULE__, config)
1313
end
1414

15-
def read_data, do: GenServer.call(__MODULE__, :read_data)
15+
def read_data(pid), do: GenServer.call(pid, :read_data)
1616

17-
def read_state, do: GenServer.call(__MODULE__, :read_state)
17+
def read_state(pid), do: GenServer.call(pid, :read_state)
1818

1919
@impl GenServer
2020
def init(config) do

test/aht20_test.exs

+56-22
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,67 @@ defmodule AHT20Test do
1515
:ok
1616
end
1717

18-
test "start_link" do
19-
config = [i2c_bus: "i2c-1", i2c_address: 0x38]
20-
assert {:ok, _} = AHT20.start_link(config)
21-
end
18+
describe "without worker" do
19+
test "start" do
20+
config = [i2c_bus: "i2c-1", i2c_address: 0x38]
21+
assert {:ok, _} = AHT20.start(config)
22+
end
23+
24+
test "read_data" do
25+
AHT20.MockI2C
26+
|> Mox.expect(:read, 1, fn _ref, _address, _data, [] -> {:ok, <<28, 38, 154, 118, 66, 231, 118>>} end)
2227

23-
test "read_data" do
24-
AHT20.MockI2C
25-
|> Mox.expect(:read, 1, fn _ref, _address, _data, [] -> {:ok, <<28, 38, 154, 118, 66, 231, 118>>} end)
28+
sensor = %AHT20.Sensor{i2c_address: 0x38, i2c_bus: "i2c-1", i2c_ref: make_ref()}
29+
assert {:ok, data} = AHT20.read_data(sensor)
2630

27-
assert {:ok, _pid} = AHT20.start_link(i2c_bus: "i2c-1", i2c_address: 0x38)
28-
assert {:ok, data} = AHT20.read_data()
31+
assert data == %AHT20.Measurement{
32+
raw_humidity: 158_119,
33+
raw_temperature: 410_343,
34+
relative_humidity: 15.079402923583984,
35+
temperature_c: 28.26671600341797,
36+
temperature_f: 82.88008880615234
37+
}
38+
end
2939

30-
assert data == %AHT20.Measurement{
31-
raw_humidity: 158_119,
32-
raw_temperature: 410_343,
33-
relative_humidity: 15.079402923583984,
34-
temperature_c: 28.26671600341797,
35-
temperature_f: 82.88008880615234
36-
}
40+
test "read_state" do
41+
AHT20.MockI2C
42+
|> Mox.expect(:write_read, 1, fn _ref, _address, _data, _bytes_to_read, [] -> {:ok, <<0b00011100>>} end)
43+
44+
sensor = %AHT20.Sensor{i2c_address: 0x38, i2c_bus: "i2c-1", i2c_ref: make_ref()}
45+
assert {:ok, state} = AHT20.read_state(sensor)
46+
assert state == %AHT20.State{busy: false, calibrated: true, mode: :nor}
47+
end
3748
end
3849

39-
test "read_state" do
40-
AHT20.MockI2C
41-
|> Mox.expect(:write_read, 1, fn _ref, _address, _data, _bytes_to_read, [] -> {:ok, <<0b00011100>>} end)
50+
describe "with worker" do
51+
test "start_link" do
52+
config = [i2c_bus: "i2c-1", i2c_address: 0x38]
53+
assert {:ok, _} = AHT20.start_link(config)
54+
end
55+
56+
test "read_data" do
57+
AHT20.MockI2C
58+
|> Mox.expect(:read, 1, fn _ref, _address, _data, [] -> {:ok, <<28, 38, 154, 118, 66, 231, 118>>} end)
59+
60+
assert {:ok, pid} = AHT20.start_link(i2c_bus: "i2c-1", i2c_address: 0x38)
61+
assert {:ok, data} = AHT20.read_data(pid)
62+
63+
assert data == %AHT20.Measurement{
64+
raw_humidity: 158_119,
65+
raw_temperature: 410_343,
66+
relative_humidity: 15.079402923583984,
67+
temperature_c: 28.26671600341797,
68+
temperature_f: 82.88008880615234
69+
}
70+
end
71+
72+
test "read_state" do
73+
AHT20.MockI2C
74+
|> Mox.expect(:write_read, 1, fn _ref, _address, _data, _bytes_to_read, [] -> {:ok, <<0b00011100>>} end)
4275

43-
AHT20.start_link(i2c_bus: "i2c-1", i2c_address: 0x38)
44-
assert {:ok, state} = AHT20.read_state()
45-
assert state == %AHT20.State{busy: false, calibrated: true, mode: :nor}
76+
assert {:ok, pid} = AHT20.start_link(i2c_bus: "i2c-1", i2c_address: 0x38)
77+
assert {:ok, state} = AHT20.read_state(pid)
78+
assert state == %AHT20.State{busy: false, calibrated: true, mode: :nor}
79+
end
4680
end
4781
end

0 commit comments

Comments
 (0)