Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from smartrent/digit/raw_exposed
Browse files Browse the repository at this point in the history
Expose raw readings in Measurement struct, allow passing compensation functions.
  • Loading branch information
doawoo authored Feb 24, 2023
2 parents 4c7749a + 0cb17b0 commit b2fc3ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/sht4x.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ defmodule SHT4X do
@impl GenServer
def handle_call({:measure, opts}, _from, state) do
{:ok, data} = SHT4X.Comm.measure(state.transport, opts)
{:reply, {:ok, SHT4X.Measurement.from_raw(data)}, state}
{:reply, {:ok, SHT4X.Measurement.from_raw(data, opts)}, state}
end
end
11 changes: 9 additions & 2 deletions lib/sht4x/measurement.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ defmodule SHT4X.Measurement do
field(:dew_point_c, number)
field(:humidity_rh, number, enforce: true)
field(:temperature_c, number, enforce: true)
field(:raw_reading_temperature, integer, enforce: true)
field(:raw_reading_humidity, integer, enforce: true)
field(:timestamp_ms, integer, enforce: true)
end

@spec from_raw(<<_::48>>) :: t()
def from_raw(<<raw_t::16, _crc1, raw_rh::16, _crc2>>) do
@spec from_raw(<<_::48>>, Keyword.t()) :: t()
def from_raw(<<raw_t::16, _crc1, raw_rh::16, _crc2>>, opts) do
compensation_func = Keyword.get(opts, :compensation_func, &Function.identity/1)

__struct__(
humidity_rh: humidity_rh_from_raw(raw_rh),
temperature_c: temperature_c_from_raw(raw_t),
raw_reading_temperature: raw_t,
raw_reading_humidity: raw_rh,
timestamp_ms: System.monotonic_time(:millisecond)
)
|> compensation_func.()
|> put_dew_point_c()
end

Expand Down

0 comments on commit b2fc3ac

Please sign in to comment.