MCP300X
Library for working the MCP300X Analog-to-Digitial Converters.
Road map
This library is under active development and supports single channel reads using the MCP3008 ADC. So, here is the goals of this library:
- [ ] Support MCP3002
- [ ] Support MCP3004
- [ ] Support Differential Reads
- [ ] Support MSBF
Basic usage
Assuming that you have a potentiometer to the 0
channel on the MCP3008
you could call
MCP300X.read_channel("spidev0.0", MCP300X.MCP3008, 0)
This will proivde the raw reading from the ADC. To get volts you can pass in the MCP300X.to_volts/1
function:
MCP300X.read_channel("spidev0.0", MCP300X.MCP3008, 0, convert_func: &MCP300X.to_volts/1)`
The convert_func
option is a function that will run on the output of the ADC. By
default this is the MCP300X.id/1
function, which is just a pass through of the raw
value.
Also, there is MCP300X.Server
that you can run in your superivsion tree that
will keep the SPI connection open for faster reads.
children = [
{MCP300X.Server, ["spidev0.0", MCP300X.MCP3008, [name: MyApp.MCP3008]]}
]
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
Supervisor.start_link(children, opts)
# Calling
MCP300X.Server.read_channel(MyApp.MCP3008, 0)
Installation
If available in Hex, the package can be installed
by adding mcp300x
to your list of dependencies in mix.exs
:
def deps do
[
{:mcp300x, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/mcp300x.