Copyright | © Patrick Pelletier 2017 |
---|---|
License | BSD3 |
Maintainer | code@funwithsoftware.org |
Safe Haskell | Safe |
Language | Haskell2010 |
This module lets you control an MCP23017 port expander (datasheet), such as the one used in the Adafruit LCD+Keypad Kit.
- data PortExpander
- mkPortExpander :: ReadFunc -> WriteFunc -> IO PortExpander
- type ReadFunc = Word8 -> Int -> IO [Word8]
- type WriteFunc = Word8 -> [Word8] -> IO ()
- writeIoDir :: PortExpander -> Word16 -> Word16 -> IO ()
- writeIPol :: PortExpander -> Word16 -> Word16 -> IO ()
- writeGpPu :: PortExpander -> Word16 -> Word16 -> IO ()
- writeGpio :: PortExpander -> Word16 -> Word16 -> IO ()
- readGpio :: PortExpander -> IO Word16
- readGpioA :: PortExpander -> IO Word8
- readGpioB :: PortExpander -> IO Word8
Creating a Port Expander
Note: the PortExpander
caches register values, so it
assumes no one else is going to be writing to the MCP23017
during the lifetime of the PortExpander
.
data PortExpander Source #
Opaque type representing an MCP23017 port expander.
mkPortExpander :: ReadFunc -> WriteFunc -> IO PortExpander Source #
Given a ReadFunc
and a WriteFunc
, makes a PortExpander
.
Read and write functions
These functions are used for reading and writing the
MCP23017's registers. Generally, you would want to specify
the i2cReadReg
and i2cWriteReg
functions from the
System.Hardware.PiLcd.I2c
module, partially applied to
the I2cHandle
and the chip's address on the I²C bus.
However, by supplying SPI read and write functions instead,
you could probably interface with an MCP23S17, although
this has not been tested.
type ReadFunc = Word8 -> Int -> IO [Word8] Source #
Specifies a register number, and the number of consecutive bytes to read.
type WriteFunc = Word8 -> [Word8] -> IO () Source #
Specifies the register number, and the bytes to write.
Writing to registers
Each pair of 8-bit registers ("A" and "B") is treated as a 16-bit register, with "A" in the most significant 8 bits, and "B" in the least significant 8 bits. Each write function takes a mask operand, which specifies which bits to change. Bits which are "0" in the mask are left unchanged.
:: PortExpander | |
-> Word16 | The value to write. A '1' bit indicates an input, and a '0' bit indicates an output. |
-> Word16 | Mask of bits to write. |
-> IO () |
Write to I/O Direction Register. (§ 1.6.1 of datasheet)
:: PortExpander | |
-> Word16 | The value to write. A '1' bit means the input will be inverted. |
-> Word16 | Mask of bits to write. |
-> IO () |
Write to Input Polarity Register. (§ 1.6.2 of datasheet)
:: PortExpander | |
-> Word16 | The value to write. A '1' bit means the input will be pulled up with a 100 kΩ resistor. |
-> Word16 | Mask of bits to write. |
-> IO () |
Write to Pull-Up Resistor Configuration Register. (§ 1.6.7 of datasheet)
:: PortExpander | |
-> Word16 | The value to write. This controls the value of the output pins. |
-> Word16 | Mask of bits to write. |
-> IO () |
Write to Output Latch Register. (§ 1.6.11 of datasheet)