Safe Haskell | Safe-Infered |
---|
Library for controlling the GPIO pins on a Raspberry Pi (or any system using the Broadcom 2835 SOC). It is constructed as a FFI wrapper over the BCM2835 library by Mike McCauley.
- data Pin
- = Pin03
- | Pin05
- | Pin07
- | Pin08
- | Pin10
- | Pin11
- | Pin12
- | Pin13
- | Pin15
- | Pin16
- | Pin18
- | Pin19
- | Pin21
- | Pin22
- | Pin23
- | Pin24
- | Pin26
- | PinP5_03
- | PinP5_04
- | PinP5_05
- | PinP5_06
- | PinV1_03
- | PinV1_05
- | PinV1_07
- | PinV1_08
- | PinV1_10
- | PinV1_11
- | PinV1_12
- | PinV1_13
- | PinV1_15
- | PinV1_16
- | PinV1_18
- | PinV1_19
- | PinV1_21
- | PinV1_22
- | PinV1_23
- | PinV1_24
- | PinV1_26
- data PinMode
- type LogicLevel = Bool
- type Address = Word8
- withGPIO :: IO a -> IO a
- setPinFunction :: Pin -> PinMode -> IO ()
- readPin :: Pin -> IO LogicLevel
- writePin :: Pin -> LogicLevel -> IO ()
- withI2C :: IO a -> IO a
- setI2cClockDivider :: Word16 -> IO ()
- setI2cBaudRate :: Word32 -> IO ()
- writeI2C :: Address -> ByteString -> IO ()
- readI2C :: Address -> Int -> IO ByteString
- writeReadI2C :: Address -> ByteString -> Int -> IO ByteString
Data types
Pin03 | Pins for the P1 connector of the V2 revision of the Raspberry Pi |
Pin05 | |
Pin07 | |
Pin08 | |
Pin10 | |
Pin11 | |
Pin12 | |
Pin13 | |
Pin15 | |
Pin16 | |
Pin18 | |
Pin19 | |
Pin21 | |
Pin22 | |
Pin23 | |
Pin24 | |
Pin26 | |
PinP5_03 | Pins for the P5 connector of the V2 revision of the Raspberry Pi |
PinP5_04 | |
PinP5_05 | |
PinP5_06 | |
PinV1_03 | Pins for the P1 connector of the V1 revision of the Raspberry Pi |
PinV1_05 | |
PinV1_07 | |
PinV1_08 | |
PinV1_10 | |
PinV1_11 | |
PinV1_12 | |
PinV1_13 | |
PinV1_15 | |
PinV1_16 | |
PinV1_18 | |
PinV1_19 | |
PinV1_21 | |
PinV1_22 | |
PinV1_23 | |
PinV1_24 | |
PinV1_26 |
A GPIO pin can be either set to input mode, output mode or an alternative mode.
type LogicLevel = BoolSource
Either high or low.
General functions
withGPIO :: IO a -> IO aSource
Any IO computation that accesses the GPIO pins using this library should be wrapped with this function; ie withGPIO $ do foo
.
It prepares the file descriptors to devmem and makes sure everything is safely deallocated if an exception occurs. The behavior
when accessing the GPIO outside of this function is undefined.
GPIO specific functions
readPin :: Pin -> IO LogicLevelSource
Returns the current state of the specified pin.
writePin :: Pin -> LogicLevel -> IO ()Source
Sets the specified pin to either True
or False
.
I2C specific functions
Any IO computation that uses the I2C bus using this library should be wrapped with this function; ie withI2C $ do foo
.
It prepares the relevant pins for use with the I2C protocol and makes sure everything is safely returned to normal if an exception
occurs. If you only use the GPIO pins for I2C, you can do withGPIO . withI2C $ do foo
and it will work as expected. WARNING:
after this function returns, the I2C pins will be set to Input, so use setPinFunction
if you want to use them for output.
setI2cClockDivider :: Word16 -> IO ()Source
Sets the clock divider for (and hence the speed of) the I2C bus.
setI2cBaudRate :: Word32 -> IO ()Source
Sets the baud rate of the I2C bus.
writeI2C :: Address -> ByteString -> IO ()Source
Writes the data in the ByteString
to the specified adress. Throws an IOException if an error occurs.
readI2C :: Address -> Int -> IO ByteStringSource
Reads num bytes from the specified address. Throws an IOException if an error occurs.
writeReadI2C :: Address -> ByteString -> Int -> IO ByteStringSource
Writes a ByteString
containing a register address to the specified address, then reads num bytes from
it, using the "repeated start" I2C method. Throws an IOException if an error occurs.