Pure Elixir library to interact with the sysfs interface to hardware PWM on Linux.
state : public draft, do not use yet
If you wish to just write and read to the sysfs Pwm interface, you can find path helpers in the Pwmx.Paths module.
File.read(Pwmx.Paths.duty_cycle_path("pwmchip0", "1"))If you want an Elixir interface instead of calling File.write/2 or File.read/1 yourself, the module Pwmx.Backend.Sysfs.Ops has a stateless interface that actually performs the reads and writes.
Pwmx.Backend.Sysfs.Ops.get_duty_cycle("pwmchip0", "1")The Pwmx.Output module can be used to spin up a genserver representing your output, managing its state.
{:ok, pid} = Pwmx.Output.start_link({"pwmchip0", 1})
Pwmx.Output.enable(pid) |> Pwmx.Output.set_period(1_000_000, :us) In a test or non-linux environment, the Pwmx.Backend.Sysfs module is not used, and calls are instead dispatched to Pwmx.Backend.Virtual, which keeps track of your operations on PWM outputs with the Pwmx.State struct. This struct isn't meant to be used directly as it wouldn't be of particular help.
An helper function can help you. Note that this function actually exports then closes each output as it seems that after a bit of testing, it cannot be taken for granted that every output reported by /sys/class/pwm/chip/npwm is able to be opened. Note that already exported outputs by something else in your system will not show up in this list as the export will fail.
Pwmx.list_available_outputs()
[
{"virtualchip0", 0},
{"virtualchip0", 1},
{"virtualchip0", 2},
{"virtualchip0", 3}
]- More tests now that a first draft is written
- Configurability : it would be great to be able to configure the available chips & outputs in test/dev environments, to be able to test code written for the board.
- Sample project showing behavior on a host & on a board