Skip to content

Instantly share code, notes, and snippets.

@TattdCodeMonkey
Created February 2, 2015 00:21
Show Gist options
  • Save TattdCodeMonkey/f63c04ea93a73f6efab4 to your computer and use it in GitHub Desktop.
Save TattdCodeMonkey/f63c04ea93a73f6efab4 to your computer and use it in GitHub Desktop.
Config map update issue
defmodule HMC5883L.Configuration do
defstruct averaging: 8, data_rate: 15, bias: :normal, gain: 1.3, scale: 0.0, mode: :continuous, i2c_channel: "i2c-1", i2c_devid: 0x1E
alias __MODULE__
alias HMC5883L.Utilities
@type t :: %Configuration{averaging: number, data_rate: number, bias: atom, gain: number, scale: number, mode: atom, i2c_channel: String.t, i2c_devid: byte}
import MultiDef
def new(), do: %Configuration{}
@doc """
Load a new %Configuration{} from the current enviroment variables.
"""
def load_from_env() do
new
|> load_compass_config
|> load_i2c_config
end
def test_load() do
config = new
config = load_compass_config(config)
IO.inspect config
config = config |> set_scale
IO.inspect config
config = load_i2c_config(config)
IO.inspect config
end
def load_compass_config(config) do
Application.get_env(:hmc5883l, :compass)
|> load_config_from_array config
|> set_scale
end
def load_i2c_config(config) do
Application.get_env(:hmc5883l, :i2c)
|> load_config_from_array config
end
def load_config_from_array(values, config), do: values |> Enum.reduce config, &load_config_val/2
mdef load_config_val do
{:i2c_channel, chan}, config -> %{config| i2c_channel: chan}
{:i2c_devid, id}, config -> %{config| i2c_devid: id}
{:gain, gain}, config -> %{config| gain: gain}
{:mode, mode}, config -> %{config| mode: mode}
{:bias, bias}, config -> %{config| bias: bias}
{:data_rate, data_rate}, config -> %{config| data_rate: data_rate}
{:averaging, avg}, config -> %{config| averaging: avg}
end
def set_scale(config) do
IO.inspect config
scale = config.gain |> Utilities.get_scale
IO.inspect scale
result = %{config| scale: scale}
end
def set_scale(%Configuration{} = config), do: %{config| scale: config.gain |> Utilities.get_scale}
end
@chrismccord
Copy link

defmodule HMC5883L.Configuration do
  defstruct averaging: 8, data_rate: 15, bias: :normal, gain: 1.3, scale: 0.0, mode: :continuous, i2c_channel: "i2c-1", i2c_devid: 0x1E 
  alias __MODULE__
  alias HMC5883L.Utilities
  @type t :: %Configuration{averaging: number, data_rate: number, bias: atom, gain: number, scale: number, mode: atom, i2c_channel: String.t, i2c_devid: byte} 
  import MultiDef

  def new(), do: %Configuration{}

  @doc """
  Load a new %Configuration{} from the current enviroment variables.
  """
  def load_from_env() do
    new
    |> load_compass_config
    |> load_i2c_config  
  end
  def test_load() do
    config = new
    config = load_compass_config(config)
    IO.inspect config
    config = config |> set_scale
    IO.inspect config
    config = load_i2c_config(config)
    IO.inspect config
  end 
  def load_compass_config(config) do
    Application.get_env(:hmc5883l, :compass)
    |> load_config_from_array(config)
    |> set_scale
  end

  def load_i2c_config(config) do
    Application.get_env(:hmc5883l, :i2c)
    |> load_config_from_array config
  end


  def load_config_from_array(values, config), do: values |> Enum.reduce(config, &load_config_val/2)

  mdef load_config_val do
    {:i2c_channel, chan}, config -> %{config| i2c_channel: chan}
    {:i2c_devid, id}, config     -> %{config| i2c_devid: id}
    {:gain, gain}, config -> %{config| gain: gain}
    {:mode, mode}, config -> %{config| mode: mode}
    {:bias, bias}, config -> %{config| bias: bias}
    {:data_rate, data_rate}, config -> %{config| data_rate: data_rate}
    {:averaging, avg}, config -> %{config| averaging: avg}
  end

  def set_scale(config) do
    IO.inspect config
    scale = config.gain |> Utilities.get_scale
    IO.inspect scale
    result = %{config| scale: scale}
  end
  def set_scale(%Configuration{} = config), do: %{config| scale: config.gain |> Utilities.get_scale} 

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment