added the full veml5030 code and config
parent
1ab19a8bd1
commit
60e8bc0ab0
|
@ -1,4 +1,3 @@
|
||||||
# Used by "mix format"
|
|
||||||
[
|
[
|
||||||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
]
|
]
|
||||||
|
|
|
@ -22,5 +22,6 @@ erl_crash.dump
|
||||||
# Ignore package tarball (built via "mix hex.build").
|
# Ignore package tarball (built via "mix hex.build").
|
||||||
veml6030-*.tar
|
veml6030-*.tar
|
||||||
|
|
||||||
# Temporary files, for example, from tests.
|
|
||||||
/tmp/
|
# Temporary files for e.g. tests
|
||||||
|
/tmp
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
defmodule VEML6030.Config do
|
|
||||||
defstruct [
|
|
||||||
gain: :gain_1_4th,
|
|
||||||
int_time: :it_100_ms,
|
|
||||||
shutdown: false,
|
|
||||||
interrupt: false
|
|
||||||
]
|
|
||||||
|
|
||||||
def new, do: struct[__MODULE__]
|
|
||||||
def new(opts), do: struct[__MODULE__, opts]
|
|
||||||
|
|
||||||
def to_integer(config) do
|
|
||||||
reserved = 0
|
|
||||||
persistence_protect = 0
|
|
||||||
|
|
||||||
<<integer::16>> = <<
|
|
||||||
reserve::3,
|
|
||||||
gain(config.gain)::2,
|
|
||||||
reserved::1,
|
|
||||||
int_time(config.int_time)::4,
|
|
||||||
persistence_protect::2,
|
|
||||||
reserved::2
|
|
||||||
interrupt(config.interrupt)::1,
|
|
||||||
shutdown(config.shutdown)::disk_log_1>>
|
|
||||||
|
|
||||||
integer
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,18 +1,68 @@
|
||||||
defmodule Veml6030 do
|
defmodule VEML6030 do
|
||||||
@moduledoc """
|
use GenServer
|
||||||
Documentation for `Veml6030`.
|
|
||||||
"""
|
|
||||||
|
|
||||||
@doc """
|
require Logger
|
||||||
Hello world.
|
|
||||||
|
|
||||||
## Examples
|
alias VEML6030.{Comm, Config}
|
||||||
|
|
||||||
iex> Veml6030.hello()
|
def start_link(options \\ %{}) do
|
||||||
:world
|
GenServer.start_link(__MODULE__, options, name: __MODULE__)
|
||||||
|
end
|
||||||
|
|
||||||
"""
|
def get_measurement do
|
||||||
def hello do
|
GenServer.call(__MODULE__, :get_measurement)
|
||||||
:world
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def init(%{address: address, i2c_bus_name: bus_name} = args) do
|
||||||
|
i2c = Comm.open(bus_name)
|
||||||
|
|
||||||
|
config =
|
||||||
|
args
|
||||||
|
|> Map.take([:gain, :int_time, :shutdown, :interrupt])
|
||||||
|
|> Config.new()
|
||||||
|
|
||||||
|
Comm.write_config(config, i2c, address)
|
||||||
|
:timer.send_interval(1_000, :measure)
|
||||||
|
|
||||||
|
state = %{
|
||||||
|
i2c: i2c,
|
||||||
|
address: address,
|
||||||
|
config: config,
|
||||||
|
last_reading: :no_reading
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def init(args) do
|
||||||
|
{bus_name, address} = Comm.discover()
|
||||||
|
transport = "bus: #{bus_name}, address: #{address}"
|
||||||
|
|
||||||
|
Logger.info("Starting VEML6030. Please specify an address and a bus.")
|
||||||
|
Logger.info("Starting on " <> transport)
|
||||||
|
|
||||||
|
defaults =
|
||||||
|
args
|
||||||
|
|> Map.put(:address, address)
|
||||||
|
|> Map.put(:i2c_bus_name, bus_name)
|
||||||
|
|
||||||
|
init(defaults)
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_info(
|
||||||
|
:measure,
|
||||||
|
%{i2c: i2c, address: address, config: config} = state
|
||||||
|
) do
|
||||||
|
last_reading = Comm.read(i2c, address, config)
|
||||||
|
updated_with_reading = %{state | last_reading: last_reading}
|
||||||
|
|
||||||
|
{:noreply, updated_with_reading}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_call(:get_measurement, _from, state) do
|
||||||
|
{:reply, state.last_reading, state}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
defmodule VEML6030.Comm do
|
||||||
|
alias Circuits.I2C
|
||||||
|
alias VEML6030.Config
|
||||||
|
|
||||||
|
@light_register <<4>>
|
||||||
|
|
||||||
|
def discover(possible_addresses \\ [0x10, 0x48]) do
|
||||||
|
I2C.discover_one!(possible_addresses)
|
||||||
|
end
|
||||||
|
|
||||||
|
def open(bus_name) do
|
||||||
|
{:ok, i2c} = I2C.open(bus_name)
|
||||||
|
|
||||||
|
i2c
|
||||||
|
end
|
||||||
|
|
||||||
|
def write_config(configuration, i2c, sensor) do
|
||||||
|
command = Config.to_integer(configuration)
|
||||||
|
|
||||||
|
I2C.write(i2c, sensor, <<0, command::little-16>>)
|
||||||
|
end
|
||||||
|
|
||||||
|
def read(i2c, sensor, configuration) do
|
||||||
|
<<value::little-16>> = I2C.write_read!(i2c, sensor, @light_register, 2)
|
||||||
|
|
||||||
|
Config.to_lumens(configuration, value)
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,78 @@
|
||||||
|
defmodule VEML6030.Config do
|
||||||
|
defstruct gain: :gain_1_4th,
|
||||||
|
int_time: :it_100_ms,
|
||||||
|
shutdown: false,
|
||||||
|
interrupt: false
|
||||||
|
|
||||||
|
def new, do: struct(__MODULE__)
|
||||||
|
def new(opts), do: struct(__MODULE__, opts)
|
||||||
|
|
||||||
|
def to_integer(config) do
|
||||||
|
reserved = 0
|
||||||
|
persistence_protect = 0
|
||||||
|
|
||||||
|
<<integer::16>> = <<
|
||||||
|
reserved::3,
|
||||||
|
gain(config.gain)::2,
|
||||||
|
reserved::1,
|
||||||
|
int_time(config.int_time)::4,
|
||||||
|
persistence_protect::2,
|
||||||
|
reserved::2,
|
||||||
|
interrupt(config.interrupt)::1,
|
||||||
|
shutdown(config.shutdown)::1
|
||||||
|
>>
|
||||||
|
|
||||||
|
integer
|
||||||
|
end
|
||||||
|
|
||||||
|
defp gain(:gain_1x), do: 0b0
|
||||||
|
defp gain(:gain_2x), do: 0b01
|
||||||
|
defp gain(:gain_1_8th), do: 0b10
|
||||||
|
defp gain(:gain_1_4th), do: 0b11
|
||||||
|
defp gain(:gain_default), do: 0b11
|
||||||
|
|
||||||
|
defp int_time(:it_25_ms), do: 0b1100
|
||||||
|
defp int_time(:it_50_ms), do: 0b1000
|
||||||
|
defp int_time(:it_100_ms), do: 0b0000
|
||||||
|
defp int_time(:it_200_ms), do: 0b0001
|
||||||
|
defp int_time(:it_400_ms), do: 0b0010
|
||||||
|
defp int_time(:it_800_ms), do: 0b0011
|
||||||
|
defp int_time(:it_default), do: 0b0000
|
||||||
|
|
||||||
|
defp shutdown(true), do: 1
|
||||||
|
defp shutdown(_), do: 0
|
||||||
|
|
||||||
|
defp interrupt(true), do: 1
|
||||||
|
defp interrupt(_), do: 0
|
||||||
|
|
||||||
|
@to_lumens_factor %{
|
||||||
|
{:it_800_ms, :gain_2x} => 0.0036,
|
||||||
|
{:it_800_ms, :gain_1x} => 0.0072,
|
||||||
|
{:it_800_ms, :gain_1_4th} => 0.0288,
|
||||||
|
{:it_800_ms, :gain_1_8th} => 0.0576,
|
||||||
|
{:it_400_ms, :gain_2x} => 0.0072,
|
||||||
|
{:it_400_ms, :gain_1x} => 0.0144,
|
||||||
|
{:it_400_ms, :gain_1_4th} => 0.0576,
|
||||||
|
{:it_400_ms, :gain_1_8th} => 0.1152,
|
||||||
|
{:it_200_ms, :gain_2x} => 0.0144,
|
||||||
|
{:it_200_ms, :gain_1x} => 0.0288,
|
||||||
|
{:it_200_ms, :gain_1_4th} => 0.1152,
|
||||||
|
{:it_200_ms, :gain_1_8th} => 0.2304,
|
||||||
|
{:it_100_ms, :gain_2x} => 0.0288,
|
||||||
|
{:it_100_ms, :gain_1x} => 0.0576,
|
||||||
|
{:it_100_ms, :gain_1_4th} => 0.2304,
|
||||||
|
{:it_100_ms, :gain_1_8th} => 0.4608,
|
||||||
|
{:it_50_ms, :gain_2x} => 0.0576,
|
||||||
|
{:it_50_ms, :gain_1x} => 0.1152,
|
||||||
|
{:it_50_ms, :gain_1_4th} => 0.4608,
|
||||||
|
{:it_50_ms, :gain_1_8th} => 0.9216,
|
||||||
|
{:it_25_ms, :gain_2x} => 0.1152,
|
||||||
|
{:it_25_ms, :gain_1x} => 0.2304,
|
||||||
|
{:it_25_ms, :gain_1_4th} => 0.9216,
|
||||||
|
{:it_25_ms, :gain_1_8th} => 1.8432
|
||||||
|
}
|
||||||
|
|
||||||
|
def to_lumens(config, measurement) do
|
||||||
|
@to_lumens_factor[{config.int_time, config.gain}] * measurement
|
||||||
|
end
|
||||||
|
end
|
|
@ -21,7 +21,7 @@ defmodule Veml6030.MixProject do
|
||||||
# Run "mix help deps" to learn about dependencies.
|
# Run "mix help deps" to learn about dependencies.
|
||||||
defp deps do
|
defp deps do
|
||||||
[
|
[
|
||||||
{:circuits_i2c, "~> 1.0"}
|
{:circuits_i2c, "~> 1.0.1"}
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
%{
|
||||||
|
"circuits_i2c": {:hex, :circuits_i2c, "1.0.1", "3a820dfa04bc5a92d0ec3c572cacb1a665802b2913fbc25784ed5a2020f12626", [:make, :mix], [{:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "e11393b3f462154ebec5d100859bd5d616e958d9f701e4d62af5764ee84f5213"},
|
||||||
|
"elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"},
|
||||||
|
}
|
Loading…
Reference in New Issue