Wafer is an Elixir library to make writing drivers for i2c and SPI connected peripherals and interacting with GPIO pins easier.
Find a file
Renovate Bot acbe626f4e
All checks were successful
continuous-integration/drone/push Build is passing
chore: Configure Renovate (#40)
Welcome to [Renovate](https://github.com/renovatebot/renovate)! This is an onboarding PR to help you understand and configure settings before regular Pull Requests begin.

🚦 To activate Renovate, merge this Pull Request. To disable Renovate, simply close this Pull Request unmerged.

---
### Detected Package Files

 * `.tool-versions` (asdf)
 * `.drone.yml` (droneci)
 * `mix.exs` (mix)

### Configuration Summary

Based on the default config's presets, Renovate will:

  - Start dependency updates only once this onboarding PR is merged
  - Enable Renovate Dependency Dashboard creation.
  - Use semantic commit type `fix` for dependencies and `chore` for all others if semantic commits are in use.
  - Ignore `node_modules`, `bower_components`, `vendor` and various test/tests directories.
  - Group known monorepo packages together.
  - Use curated list of recommended non-monorepo package groupings.
  - Apply crowd-sourced package replacement rules.
  - Apply crowd-sourced workarounds for known problems with packages.
  - Use semantic prefixes for commit messages and PR titles.
  - Use `chore` as semantic commit type for commit messages and PR titles.
  - Removes rate limit for PR creation per hour.
  - Remove limit for open PRs at any time.
  - Automerge all upgrades (including `major`) if they pass tests.
  - Assign PRs to `james`.
  - Rebase existing PRs any time the base branch has been updated.
  - Raise PR when vulnerability alerts are detected.
  - Disable Renovate Dependency Dashboard creation.

🔡 Do you want to change how Renovate upgrades your dependencies? Add your custom config to `renovate.json` in this branch. Renovate will update the Pull Request description the next time it runs.

---

### What to Expect

With your current configuration, Renovate will create 1 Pull Request:

<details>
<summary>chore(deps): update dependency circuits_i2c to v2</summary>

  - Schedule: ["at any time"]
  - Branch name: `renovate/circuits_i2c-2.x`
  - Merge into: `main`
  - Upgrade circuits_i2c to `~> 2.0`

</details>

---

 Got questions? Check out Renovate's [Docs](https://docs.renovatebot.com/), particularly the Getting Started section.
If you need any further assistance then you can also [request help here](https://github.com/renovatebot/renovate/discussions).

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

<!--renovate-config-hash:758a0db051676a629cb8daada3244b6287c728a47d095002c4ae62062eae6bf3-->

Reviewed-on: https://code.harton.nz/james/wafer/pulls/40
Co-authored-by: Renovate Bot <bot@harton.nz>
Co-committed-by: Renovate Bot <bot@harton.nz>
2023-11-27 10:20:02 +13:00
config chore: fix tests with Elixir 1.13.0. 2021-12-09 20:10:07 +13:00
lib chore: Set up documentation linting with doctor. 2023-11-24 17:03:40 +13:00
test chore: install dialixyr and get it passing. 2023-11-24 17:03:39 +13:00
.check.exs chore: Add .tool-versions and ex_check. 2023-11-24 14:57:06 +13:00
.doctor.exs chore: Set up documentation linting with doctor. 2023-11-24 17:03:40 +13:00
.drone.yml chore: Add drone CI configuration. 2023-11-24 17:07:20 +13:00
.formatter.exs Empty mix application. 2019-12-20 10:24:51 +13:00
.gitignore Ignore Elixir language server data. 2020-05-10 08:55:23 +12:00
.tool-versions chore: Add .tool-versions and ex_check. 2023-11-24 14:57:06 +13:00
CHANGELOG.md chore: release version v1.0.3 2023-11-24 07:20:28 +00:00
LICENSE.md chore!: Relicense to HL3-FULL. 2023-01-17 11:25:39 +13:00
mix.exs chore: release version v1.0.3 2023-11-24 07:20:28 +00:00
mix.lock chore(deps): Update circuits_spi to 2.0.0. 2023-11-24 17:07:21 +13:00
README.md chore: release version v1.0.3 2023-11-24 07:20:28 +00:00
renovate.json chore: Configure Renovate (#40) 2023-11-27 10:20:02 +13:00

Wafer

Build Status Hex.pm Hippocratic License HL3-FULL

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.

Documentation for the main branch can always be found here.

Some examples of how to use this project:

  • Augie, a hexapod robot.
  • PCA9641, an example of how easy it is to write a driver with Wafer.

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

Working with GPIO

Wafer provides a simple way to drive specific GPIO functionality per device.

Here's a super simple "blinky" example:

defmodule WaferBlinky do
  @derive [Wafer.GPIO]
  defstruct ~w[conn]a
  @behaviour Wafer.Conn
  alias Wafer.Conn
  alias Wafer.GPIO

  @type t :: %WaferBlinky{conn: Conn.t()}
  @type acquire_options :: [acquire_option]
  @type acquire_option :: {:conn, Conn.t()}

  @impl Wafer.Conn
  def acquire(options) do
    with {:ok, conn} <- Keyword.fetch(options, :conn) do
      {:ok, %WaferBlinky{conn: conn}}
    else
      :error -> {:error, "`WaferBlinky.acquire/1` requires the `conn` option."}
      {:error, reason} -> {:error, reason}
    end
  end

  def turn_on(conn), do: GPIO.write(conn, 1)
  def turn_off(conn), do: GPIO.write(conn, 0)
end

And a simple mix task to drive it:

defmodule Mix.Tasks.Blink do
  use Mix.Task
  @shortdoc "GPIO LED Blink Example"

  alias Wafer.Driver.Circuits.GPIO

  def run(_args) do
    {:ok, led_pin_21} = GPIO.acquire(pin: 21, direction: :out)
    {:ok, conn} = WaferBlinky.acquire(conn: led_pin_21)

    Enum.each(1..10, fn _ ->
      WaferBlinky.turn_on(conn)
      :timer.sleep(500)
      WaferBlinky.turn_off(conn)
      :timer.sleep(500)
    end)
  end
end

Running the tests

I've included stub implementations of the parts of ElixirALE and Circuits that are interacted with by this project, so the tests should run and pass on machines without physical hardware interfaces. If you have a Raspberry Pi with a Pi Sense Hat connected you can run the tests with the SENSE_HAT_PRESENT=true environment variable set and it will perform integration tests with two of the sensors on this device.

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, "~> 1.0.3"}
  ]
end

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

Gitlab Mirror

This repository is mirrored on Gitlab from it's primary location on my Forgejo instance. Feel free to raise issues and open PRs on Gitlab.

License

This software is licensed under the terms of the HL3-FULL, see the LICENSE.md file included with this package for the terms.

This license actively proscribes this software being used by and for some industries, countries and activities. If your usage of this software doesn't comply with the terms of this license, then contact me with the details of your use-case to organise the purchase of a license - the cost of which may include a donation to a suitable charity or NGO.