Wafer is an Elixir library to make writing drivers for i2c and SPI connected peripherals and interacting with GPIO pins easier.
Find a file
2019-12-30 20:37:34 +13:00
lib Tested and working on a Raspberry Pi 4B with a Pi Sense Hat connected. 2019-12-30 20:37:34 +13:00
test Tested and working on a Raspberry Pi 4B with a Pi Sense Hat connected. 2019-12-30 20:37:34 +13:00
.credo.exs Tested and working on a Raspberry Pi 4B with a Pi Sense Hat connected. 2019-12-30 20:37:34 +13:00
.formatter.exs Empty mix application. 2019-12-20 10:24:51 +13:00
.gitignore First pass. 2019-12-29 17:12:36 +13:00
.gitlab-ci.yml I believe that everything I wanted now works. Yowza! 2019-12-30 13:12:42 +13:00
mix.exs Tested and working on a Raspberry Pi 4B with a Pi Sense Hat connected. 2019-12-30 20:37:34 +13:00
mix.lock I believe that everything I wanted now works. Yowza! 2019-12-30 13:12:42 +13:00
README.md Tested and working on a Raspberry Pi 4B with a Pi Sense Hat connected. 2019-12-30 20:37:34 +13:00

Wafer

Wafer is an OTP application that assists with writing drivers for peripherals using I2C, SPI and GPIO pins.

Wafer provides Elixir protocols for interacting with device registers and dealing with GPIO, so that you can use directly connected hardware GPIO pins or GPIO expanders such as the MCP23008 or the CD74HC595 SPI shift register.

Wafer implements the GPIO and Chip protocols for ElixirALE's GPIO and I2C drivers, Circuits.GPIO and Circuits.I2C. Implementing it for SPI should also be trivial, I just don't have any SPI devices to test with at the moment.

Working with registers

Wafer provides the very helpful Registers macros which allow you to quickly and easily define your registers for your device:

Here's a very simple example:

defmodule HTS221.Registers do
  use Wafer.Registers

  defregister(:ctrl_reg1, 0x20, :rw, 1)
  defregister(:humidity_out_l, 0x28, :ro, 1)
  defregister(:humidity_out_h, 0x29, :ro, 1)
end

defmodule HTS221 do
  import HTS221.Registers
  use Bitwise

  def humidity(conn) do
    with {:ok, <<msb>>} <- read_humidity_out_h(conn),
         {:ok, <<lsb>} <- read_humidity_out_l(conn),
         do: {:ok, msb <<< 8 + lsb}
  end

  def on?(conn) do
    case read_ctrl_reg1(conn) do
      {:ok, <<1::integer-size(1), _::bits>>} -> true
      _ -> false
    end
  end

  def turn_on(conn), do: write_ctrl_reg1(conn, <<1::integer-size(1), 0::integer-size(7)>>)
  def turn_off(conn), do: write_ctrl_reg1(conn, <<0>>)
end

Installation

If available in Hex, the package can be installed by adding wafer to your list of dependencies in mix.exs:

def deps do
  [
    {:wafer, "~> 0.1"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/wafer.