{-# LANGUAGE OverloadedStrings, ForeignFunctionInterface #-}

-- |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.

module System.RaspberryPi.GPIO (
    -- *Data types

    Pin(..),
    PinMode(..),
    LogicLevel,
    Address,
    SPIBitOrder(..),
    SPIPin(..),
    CPOL,
    CPHA,
    -- *General functions

    withGPIO,
    -- *GPIO specific functions

    setPinFunction,
    readPin,
    writePin,
    -- *I2C specific functions

    withI2C,
    setI2cClockDivider,
    setI2cBaudRate,
    writeI2C,
    readI2C,
    writeReadRSI2C,
    -- *SPI specific functions

    withAUXSPI,
    withSPI,
    chipSelectSPI,
    setBitOrderSPI,
    setChipSelectPolaritySPI,
    setClockDividerAUXSPI,
    setClockDividerSPI,
    setDataModeSPI,
    transferAUXSPI,
    transferSPI,
    transferManySPI,
    -- *PWM specific functions

    -- |Allows control of 2 independent PWM channels. A limited subset of GPIO

    -- pins can be connected to one of these 2 channels, allowing PWM control

    -- of GPIO pins. You have to set the desired pin into a particular Alt Fun

    -- to PWM output.

    setClockPWM,
    setModePWM,
    setRangePWM,
    setDataPWM
    ) where

-- FFI wrapper over the I2C portions of the BCM2835 library by Mike McCauley, also some utility functions to

-- make reading and writing simpler


import Control.Applicative ((<$>))
import Control.Exception
import Foreign
import Foreign.C
import Foreign.C.String
import qualified Data.ByteString as BS
import Data.Maybe
import Data.Tuple
import GHC.IO.Exception

------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------- Data types ---------------------------------------------------------------------------

-- |This describes the pins on the Raspberry Pi boards. Since the BCM2835 SOC internally uses different numbers (and these numbers 

-- differ between versions, the library internally translates this pin number to the correct number.

data Pin =  -- |Pins for the P1 connector of the V2 revision of the Raspberry Pi

            Pin03|Pin05|Pin07|Pin08|Pin10|Pin11|Pin12|Pin13|Pin15|Pin16|Pin18|Pin19|Pin21|Pin22|Pin23|Pin24|Pin26|Pin32|Pin33|Pin35|Pin36|
            -- |Pins for the P5 connector of the V2 revision of the Raspberry Pi

            PinP5_03|PinP5_04|PinP5_05|PinP5_06|
            -- |Pins for the P1 connector of the V1 revision of the Raspberry Pi

            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
            deriving (Pin -> Pin -> LogicLevel
(Pin -> Pin -> LogicLevel) -> (Pin -> Pin -> LogicLevel) -> Eq Pin
forall a. (a -> a -> LogicLevel) -> (a -> a -> LogicLevel) -> Eq a
$c== :: Pin -> Pin -> LogicLevel
== :: Pin -> Pin -> LogicLevel
$c/= :: Pin -> Pin -> LogicLevel
/= :: Pin -> Pin -> LogicLevel
Eq,Int -> Pin -> ShowS
[Pin] -> ShowS
Pin -> String
(Int -> Pin -> ShowS)
-> (Pin -> String) -> ([Pin] -> ShowS) -> Show Pin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Pin -> ShowS
showsPrec :: Int -> Pin -> ShowS
$cshow :: Pin -> String
show :: Pin -> String
$cshowList :: [Pin] -> ShowS
showList :: [Pin] -> ShowS
Show)

-- |A GPIO pin can be either set to input mode, output mode or an alternative mode.

data PinMode = Input | Output | Alt0 | Alt1 | Alt2 | Alt3 | Alt4 | Alt5 deriving (PinMode -> PinMode -> LogicLevel
(PinMode -> PinMode -> LogicLevel)
-> (PinMode -> PinMode -> LogicLevel) -> Eq PinMode
forall a. (a -> a -> LogicLevel) -> (a -> a -> LogicLevel) -> Eq a
$c== :: PinMode -> PinMode -> LogicLevel
== :: PinMode -> PinMode -> LogicLevel
$c/= :: PinMode -> PinMode -> LogicLevel
/= :: PinMode -> PinMode -> LogicLevel
Eq,Int -> PinMode -> ShowS
[PinMode] -> ShowS
PinMode -> String
(Int -> PinMode -> ShowS)
-> (PinMode -> String) -> ([PinMode] -> ShowS) -> Show PinMode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PinMode -> ShowS
showsPrec :: Int -> PinMode -> ShowS
$cshow :: PinMode -> String
show :: PinMode -> String
$cshowList :: [PinMode] -> ShowS
showList :: [PinMode] -> ShowS
Show)

instance Enum PinMode where -- bit strange, but just deriving Enum doesn't work because the numbers don't monotonically ascend

    fromEnum :: PinMode -> Int
fromEnum = Maybe Int -> Int
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe Int -> Int) -> (PinMode -> Maybe Int) -> PinMode -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PinMode -> [(PinMode, Int)] -> Maybe Int)
-> [(PinMode, Int)] -> PinMode -> Maybe Int
forall a b c. (a -> b -> c) -> b -> a -> c
flip PinMode -> [(PinMode, Int)] -> Maybe Int
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup [(PinMode, Int)]
table
    toEnum :: Int -> PinMode
toEnum = Maybe PinMode -> PinMode
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe PinMode -> PinMode)
-> (Int -> Maybe PinMode) -> Int -> PinMode
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> [(Int, PinMode)] -> Maybe PinMode)
-> [(Int, PinMode)] -> Int -> Maybe PinMode
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> [(Int, PinMode)] -> Maybe PinMode
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup (((PinMode, Int) -> (Int, PinMode))
-> [(PinMode, Int)] -> [(Int, PinMode)]
forall a b. (a -> b) -> [a] -> [b]
map (PinMode, Int) -> (Int, PinMode)
forall a b. (a, b) -> (b, a)
swap [(PinMode, Int)]
table)
table :: [(PinMode, Int)]
table = [(PinMode
Input, Int
0), (PinMode
Output, Int
1), (PinMode
Alt0, Int
4), (PinMode
Alt1, Int
5), (PinMode
Alt2, Int
6), (PinMode
Alt3, Int
7), (PinMode
Alt4, Int
3), (PinMode
Alt5, Int
2)]

-- |This describes the address of an I2C slave.

type Address = Word8 --adress of an I2C slave


-- |Either high or low.

type LogicLevel = Bool

-- |Specifies the SPI data bit ordering.

data SPIBitOrder = LSBFirst | MSBFirst

-- |This describes which Chip Select pins are asserted (used in SPI communications).

data SPIPin = CS0 | CS1 | CS2 | CSNONE deriving (SPIPin -> SPIPin -> LogicLevel
(SPIPin -> SPIPin -> LogicLevel)
-> (SPIPin -> SPIPin -> LogicLevel) -> Eq SPIPin
forall a. (a -> a -> LogicLevel) -> (a -> a -> LogicLevel) -> Eq a
$c== :: SPIPin -> SPIPin -> LogicLevel
== :: SPIPin -> SPIPin -> LogicLevel
$c/= :: SPIPin -> SPIPin -> LogicLevel
/= :: SPIPin -> SPIPin -> LogicLevel
Eq, Int -> SPIPin -> ShowS
[SPIPin] -> ShowS
SPIPin -> String
(Int -> SPIPin -> ShowS)
-> (SPIPin -> String) -> ([SPIPin] -> ShowS) -> Show SPIPin
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SPIPin -> ShowS
showsPrec :: Int -> SPIPin -> ShowS
$cshow :: SPIPin -> String
show :: SPIPin -> String
$cshowList :: [SPIPin] -> ShowS
showList :: [SPIPin] -> ShowS
Show, Int -> SPIPin
SPIPin -> Int
SPIPin -> [SPIPin]
SPIPin -> SPIPin
SPIPin -> SPIPin -> [SPIPin]
SPIPin -> SPIPin -> SPIPin -> [SPIPin]
(SPIPin -> SPIPin)
-> (SPIPin -> SPIPin)
-> (Int -> SPIPin)
-> (SPIPin -> Int)
-> (SPIPin -> [SPIPin])
-> (SPIPin -> SPIPin -> [SPIPin])
-> (SPIPin -> SPIPin -> [SPIPin])
-> (SPIPin -> SPIPin -> SPIPin -> [SPIPin])
-> Enum SPIPin
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: SPIPin -> SPIPin
succ :: SPIPin -> SPIPin
$cpred :: SPIPin -> SPIPin
pred :: SPIPin -> SPIPin
$ctoEnum :: Int -> SPIPin
toEnum :: Int -> SPIPin
$cfromEnum :: SPIPin -> Int
fromEnum :: SPIPin -> Int
$cenumFrom :: SPIPin -> [SPIPin]
enumFrom :: SPIPin -> [SPIPin]
$cenumFromThen :: SPIPin -> SPIPin -> [SPIPin]
enumFromThen :: SPIPin -> SPIPin -> [SPIPin]
$cenumFromTo :: SPIPin -> SPIPin -> [SPIPin]
enumFromTo :: SPIPin -> SPIPin -> [SPIPin]
$cenumFromThenTo :: SPIPin -> SPIPin -> SPIPin -> [SPIPin]
enumFromThenTo :: SPIPin -> SPIPin -> SPIPin -> [SPIPin]
Enum)

-- |Clock polarity (CPOL) for SPI transmissions.

type CPOL = Bool

-- |Clock phase (CPHA) for SPI transmissions.

type CPHA = Bool

------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------ Foreign imports -------------------------------------------------------------------------


----------------------------------------- Library functions ------------------------------------------------------------------------

--initialises /dev/mem and obtaining the proper pointers to device registers. Don't use any other functions if this fails!

foreign import ccall unsafe "bcm2835.h bcm2835_init" initBCM2835 :: IO Int
--deallocates any memory, closes /dev/mem and stops the library in general

foreign import ccall unsafe "bcm2835.h bcm2835_close" stopBCM2835 :: IO Int
--sets debug level

foreign import ccall unsafe "bcm2835.h bcm2835_set_debug" setDebugBCM2835 :: CUChar -> IO ()

---------------------------------------- Basic GPIO functions ----------------------------------------------------------------------

-- setFunction (input/output)

foreign import ccall unsafe "bcm2835.h bcm2835_gpio_fsel" c_setPinFunction    :: CUChar -> CUChar -> IO ()
-- setPin (zet een outputpin hoog/laag)

foreign import ccall unsafe "bcm2835.h bcm2835_gpio_write" c_writePin         :: CUChar -> CUChar -> IO ()
-- readPin (geeft weer of een pin hoog/laag is)

foreign import ccall unsafe "bcm2835.h bcm2835_gpio_lev" c_readPin            :: CUChar -> IO CUChar

------------------------------------------- I2C functions --------------------------------------------------------------------------

--inits the i2c pins

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_begin" initI2C   :: IO ()
--resets the i2c pins

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_end"   stopI2C   :: IO ()

--sets the slave address used

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_setSlaveAddress" c_setSlaveAddressI2C ::   CUChar -> IO ()
--sets the I2C bus clock divider (and thus, the speed)

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_setClockDivider" c_setClockDividerI2C ::   CUShort -> IO ()
--sets the I2C bus baud rate (100000 for the default 100khz)

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_set_baudrate"    c_setBaudRateI2C ::       CUInt -> IO ()

-- writes some bytes to the bus

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_write" c_writeI2C :: CString -> CUInt -> IO CUChar
--read some bytes from the bus

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_read" c_readI2C :: CString -> CUShort -> IO CUChar
--reads a certain register with the repeated start method

foreign import ccall unsafe "bcm2835.h bcm2835_i2c_write_read_rs" c_writeReadRSI2C :: CString -> CUInt -> CString -> CUInt -> IO CUChar

------------------------------------------- SPI functions --------------------------------------------------------------------------

--inits the SPI pins

foreign import ccall unsafe "bcm2835.h bcm2835_spi_begin" initSPI   :: IO ()
--resets the SPI pins

foreign import ccall unsafe "bcm2835.h bcm2835_spi_end"   stopSPI   :: IO ()

--Transfers one byte to and from the currently selected SPI slave

foreign import ccall unsafe "bcm2835.h bcm2835_spi_transfer"        c_transferSPI :: CUChar -> IO CUChar
--Transfers multiple bytes to and from the currently selected SPI slave

foreign import ccall unsafe "bcm2835.h bcm2835_spi_transfern"      c_transferManySPI :: CString -> CUInt -> IO ()
--Changes the chip select pins

foreign import ccall unsafe "bcm2835.h bcm2835_spi_chipSelect"      c_chipSelectSPI :: CUChar -> IO ()

--Set the bit order to be used for transmit and receive.

foreign import ccall unsafe "bcm2835.h bcm2835_spi_setBitOrder"           c_setBitOrder :: CUChar -> IO ()
--Sets whether SPI Chip Select pulls pins high or low.

foreign import ccall unsafe "bcm2835.h bcm2835_spi_setChipSelectPolarity" c_setChipSelectPolarity :: CUChar -> CUChar -> IO ()
--Sets the SPI clock divider and therefore the SPI clock speed.

foreign import ccall unsafe "bcm2835.h bcm2835_spi_setClockDivider"       c_setClockDividerSPI :: CUShort -> IO ()
--Sets the data mode used (phase/polarity)

foreign import ccall unsafe "bcm2835.h bcm2835_spi_setDataMode"     c_setDataModeSPI :: CUChar -> IO ()

----------------------------------------- AUX SPI functions ------------------------------------------------------------------------

--inits the AUX SPI pins

foreign import ccall unsafe "bcm2835.h bcm2835_aux_spi_begin" initAUXSPI   :: IO Int
--resets the AUX SPI pins

foreign import ccall unsafe "bcm2835.h bcm2835_aux_spi_end"   stopAUXSPI   :: IO ()

--Transfers one byte to and from the currently selected SPI slave

foreign import ccall unsafe "bcm2835.h bcm2835_aux_spi_transfer"       c_transferAUXSPI :: CUChar -> IO CUChar

--Sets the SPI clock divider and therefore the SPI clock speed.

foreign import ccall unsafe "bcm2835.h bcm2835_aux_spi_setClockDivider"     c_setClockDividerAUXSPI :: CUShort -> IO ()

------------------------------------------- PWM functions --------------------------------------------------------------------------

-- sets the PWM clock

foreign import ccall unsafe "bcm2835.h bcm2835_pwm_set_clock" c_setClockPWM :: CUInt -> IO ()

-- sets the PWM pulse ratio to emit to DATA/RANGE

foreign import ccall unsafe "bcm2835.h bcm2835_pwm_set_data" c_setDataPWM :: CUChar -> CUInt -> IO ()

-- sets the mode of the given PWM channel

foreign import ccall unsafe "bcm2835.h bcm2835_pwm_set_mode" c_setModePWM :: CUChar -> CUChar -> CUChar -> IO ()

-- sets the maximum range of the PWM output

foreign import ccall unsafe "bcm2835.h bcm2835_pwm_set_range" c_setRangePWM :: CUChar -> CUInt -> IO ()

------------------------------------------------------------------------------------------------------------------------------------

------------------------------------------ Exportable functions --------------------------------------------------------------------


------------------------------------------- Utility functions ----------------------------------------------------------------------

-- |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 /dev/mem and makes sure everything is safely deallocated if an exception occurs. The behavior

-- when accessing the GPIO outside of this function is undefined.

withGPIO :: IO a -> IO a
withGPIO :: forall a. IO a -> IO a
withGPIO IO a
f = IO Int -> (Int -> IO Int) -> (Int -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket    IO Int
initBCM2835
                        (IO Int -> Int -> IO Int
forall a b. a -> b -> a
const IO Int
stopBCM2835) --const because you don't care about the output of initBCM2835

                        (\Int
a -> if Int
aInt -> Int -> LogicLevel
forall a. Eq a => a -> a -> LogicLevel
==Int
0 then IOException -> IO a
forall e a. Exception e => e -> IO a
throwIO IOException
ioe else IO a
f) -- init returning 0 is not good

                            where ioe :: IOException
ioe = Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"GPIO: " String
"Unable to start GPIO." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing

-- |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.

withI2C :: IO a -> IO a
withI2C :: forall a. IO a -> IO a
withI2C IO a
f = IO () -> IO () -> IO a -> IO a
forall a b c. IO a -> IO b -> IO c -> IO c
bracket_    IO ()
initI2C
                        IO ()
stopI2C
                        IO a
f

-- |Any IO computation that uses the SPI functionality using this library should be wrapped with this function; ie @withSPI $ do foo@.

-- It prepares the relevant pins for use with the SPI protocol and makes sure everything is safely returned to normal if an exception

-- occurs. If you only use the GPIO pins for SPI, you can do @withGPIO . withSPI $ do foo@ and it will work as expected. WARNING: 

-- after this function returns, the SPI pins will be set to Input, so use 'setPinFunction' if you want to use them for output.

withSPI :: IO a -> IO a
withSPI :: forall a. IO a -> IO a
withSPI IO a
f = IO () -> IO () -> IO a -> IO a
forall a b c. IO a -> IO b -> IO c -> IO c
bracket_    IO ()
initSPI
                        IO ()
stopSPI
                        IO a
f
                        
-- Possible error results for I2C functions. (not exported)

actOnResult :: CUChar -> CStringLen -> IO BS.ByteString
actOnResult :: CUChar -> CStringLen -> IO ByteString
actOnResult CUChar
rr CStringLen
buf = case CUChar
rr of
    CUChar
0x01 -> IOException -> IO ByteString
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ByteString) -> IOException -> IO ByteString
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"I2C: " String
"Received an unexpected NACK." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
    CUChar
0x02 -> IOException -> IO ByteString
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ByteString) -> IOException -> IO ByteString
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"I2C: " String
"Received Clock Stretch Timeout." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing 
    CUChar
0x04 -> IOException -> IO ByteString
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ByteString) -> IOException -> IO ByteString
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"I2C: " String
"Not all data was read." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
    CUChar
0x00 -> CStringLen -> IO ByteString
BS.packCStringLen CStringLen
buf --convert C buffer to a bytestring



-- Mapping raspberry pi pin number to internal bmc2835 pin number, ugly solution, but meh. Also, the existence of mutiple versions

-- of the pin layout makes this the most elegant solution without resorting to state monads (which I don't want to do because that

-- would hamper the simplicity of having writePin and readPin be simple IO actions). As this function isn't exported anyway, the

-- user should never be troubled by all this.

getHwPin :: Pin -> CUChar
--P1 connector on V1 boards

getHwPin :: Pin -> CUChar
getHwPin Pin
PinV1_03 = CUChar
0
getHwPin Pin
PinV1_05 = CUChar
1
getHwPin Pin
PinV1_07 = CUChar
4
getHwPin Pin
PinV1_08 = CUChar
14
getHwPin Pin
PinV1_10 = CUChar
15
getHwPin Pin
PinV1_11 = CUChar
17
getHwPin Pin
PinV1_12 = CUChar
18
getHwPin Pin
PinV1_13 = CUChar
21
getHwPin Pin
PinV1_15 = CUChar
22
getHwPin Pin
PinV1_16 = CUChar
23
getHwPin Pin
PinV1_18 = CUChar
24
getHwPin Pin
PinV1_19 = CUChar
10
getHwPin Pin
PinV1_21 = CUChar
9
getHwPin Pin
PinV1_22 = CUChar
25
getHwPin Pin
PinV1_23 = CUChar
11
getHwPin Pin
PinV1_24 = CUChar
8
getHwPin Pin
PinV1_26 = CUChar
7
--P1 connector on V2 boards

getHwPin Pin
Pin03 = CUChar
2
getHwPin Pin
Pin05 = CUChar
3
getHwPin Pin
Pin07 = CUChar
4
getHwPin Pin
Pin08 = CUChar
14
getHwPin Pin
Pin10 = CUChar
15
getHwPin Pin
Pin11 = CUChar
17
getHwPin Pin
Pin12 = CUChar
18
getHwPin Pin
Pin13 = CUChar
27
getHwPin Pin
Pin15 = CUChar
22
getHwPin Pin
Pin16 = CUChar
23
getHwPin Pin
Pin18 = CUChar
24
getHwPin Pin
Pin19 = CUChar
10
getHwPin Pin
Pin21 = CUChar
9
getHwPin Pin
Pin22 = CUChar
25
getHwPin Pin
Pin23 = CUChar
11
getHwPin Pin
Pin24 = CUChar
8
getHwPin Pin
Pin26 = CUChar
7
getHwPin Pin
Pin32 = CUChar
12
getHwPin Pin
Pin33 = CUChar
13
getHwPin Pin
Pin35 = CUChar
19
getHwPin Pin
Pin36 = CUChar
16
--for the P5 connector on V2 boards

getHwPin Pin
PinP5_03 = CUChar
28
getHwPin Pin
PinP5_04 = CUChar
29
getHwPin Pin
PinP5_05 = CUChar
30
getHwPin Pin
PinP5_06 = CUChar
31

------------------------------------------- GPIO functions -------------------------------------------------------------------------

-- |Sets the pin to either 'Input' or 'Output' mode.

setPinFunction :: Pin -> PinMode -> IO ()
setPinFunction :: Pin -> PinMode -> IO ()
setPinFunction Pin
pin PinMode
mode = CUChar -> CUChar -> IO ()
c_setPinFunction (Pin -> CUChar
getHwPin Pin
pin) (Int -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUChar) -> Int -> CUChar
forall a b. (a -> b) -> a -> b
$ PinMode -> Int
forall a. Enum a => a -> Int
fromEnum PinMode
mode)

-- |Sets the specified pin to either 'True' or 'False'.

writePin :: Pin -> LogicLevel -> IO () --wat gebeurt er als het geen output pin is?

writePin :: Pin -> LogicLevel -> IO ()
writePin Pin
pin LogicLevel
level = CUChar -> CUChar -> IO ()
c_writePin (Pin -> CUChar
getHwPin Pin
pin) (Int -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUChar) -> Int -> CUChar
forall a b. (a -> b) -> a -> b
$ LogicLevel -> Int
forall a. Enum a => a -> Int
fromEnum LogicLevel
level)

-- |Returns the current state of the specified pin.

readPin :: Pin -> IO LogicLevel
readPin :: Pin -> IO LogicLevel
readPin Pin
pin = (Int -> LogicLevel
forall a. Enum a => Int -> a
toEnum (Int -> LogicLevel) -> (CUChar -> Int) -> CUChar -> LogicLevel
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CUChar -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (CUChar -> LogicLevel) -> IO CUChar -> IO LogicLevel
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CUChar -> IO CUChar
c_readPin (Pin -> CUChar
getHwPin Pin
pin)

-------------------------------------------- I2C functions -------------------------------------------------------------------------

--not exported, only used internally

setI2cAddress :: Address -> IO ()
setI2cAddress :: Word8 -> IO ()
setI2cAddress Word8
a = CUChar -> IO ()
c_setSlaveAddressI2C (CUChar -> IO ()) -> CUChar -> IO ()
forall a b. (a -> b) -> a -> b
$ Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
a

-- |Sets the clock divider for (and hence the speed of) the I2C bus.

setI2cClockDivider :: Word16 -> IO ()
setI2cClockDivider :: Word16 -> IO ()
setI2cClockDivider Word16
a = CUShort -> IO ()
c_setClockDividerI2C (CUShort -> IO ()) -> CUShort -> IO ()
forall a b. (a -> b) -> a -> b
$ Word16 -> CUShort
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
a

-- |Sets the baud rate of the I2C bus.

setI2cBaudRate :: Word32 -> IO ()
setI2cBaudRate :: Word32 -> IO ()
setI2cBaudRate Word32
a = CUInt -> IO ()
c_setBaudRateI2C (CUInt -> IO ()) -> CUInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Word32 -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
a

-- |Writes the data in the 'ByteString' to the specified I2C 'Address'. Throws an IOException if an error occurs.

writeI2C :: Address -> BS.ByteString -> IO () --writes a bytestring to the specified address

writeI2C :: Word8 -> ByteString -> IO ()
writeI2C Word8
address ByteString
by = ByteString -> (CStringLen -> IO ()) -> IO ()
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
by ((CStringLen -> IO ()) -> IO ()) -> (CStringLen -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
bs,Int
len) -> do
    Word8 -> IO ()
setI2cAddress Word8
address
    CUChar
readresult <- Ptr CChar -> CUInt -> IO CUChar
c_writeI2C Ptr CChar
bs (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
    case CUChar
readresult of
        CUChar
0x01 -> IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ()) -> IOException -> IO ()
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"I2C: " String
"Received an unexpected NACK." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
        CUChar
0x02 -> IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ()) -> IOException -> IO ()
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"I2C: " String
"Received Clock Stretch Timeout." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing 
        CUChar
0x04 -> IOException -> IO ()
forall e a. Exception e => e -> IO a
throwIO (IOException -> IO ()) -> IOException -> IO ()
forall a b. (a -> b) -> a -> b
$ Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"I2C: " String
"Not all data was read." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing
        CUChar
0x00 -> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()

-- |Reads num bytes from the specified 'Address'. Throws an IOException if an error occurs.

readI2C :: Address -> Int -> IO BS.ByteString --reads num bytes from the specified address

readI2C :: Word8 -> Int -> IO ByteString
readI2C Word8
address Int
num = Int -> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes (Int
numInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) ((Ptr CChar -> IO ByteString) -> IO ByteString)
-> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
buf -> do --is the +1 necessary??

    Word8 -> IO ()
setI2cAddress Word8
address
    CUChar
readresult <- Ptr CChar -> CUShort -> IO CUChar
c_readI2C Ptr CChar
buf (Int -> CUShort
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
num)
    CUChar -> CStringLen -> IO ByteString
actOnResult CUChar
readresult (Ptr CChar
buf, Int
num)

-- |Writes the data in the 'ByteString' to the specified 'Address', then issues a "repeated start" (with no prior stop) and then 

-- reads num bytes from the same 'Address'. Necessary for devices that require such behavior, such as the MLX90620.

writeReadRSI2C :: Address -> BS.ByteString -> Int -> IO BS.ByteString
writeReadRSI2C :: Word8 -> ByteString -> Int -> IO ByteString
writeReadRSI2C Word8
address ByteString
by Int
num = ByteString -> (CStringLen -> IO ByteString) -> IO ByteString
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ByteString
by ((CStringLen -> IO ByteString) -> IO ByteString)
-> (CStringLen -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
bs,Int
len) -> do --marshall the register-containing bytestring

    Int -> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
num ((Ptr CChar -> IO ByteString) -> IO ByteString)
-> (Ptr CChar -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr CChar
buf -> do --allocate a buffer for the response

        Word8 -> IO ()
setI2cAddress Word8
address
        CUChar
readresult <- Ptr CChar -> CUInt -> Ptr CChar -> CUInt -> IO CUChar
c_writeReadRSI2C Ptr CChar
bs (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len) Ptr CChar
buf (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
num)
        CUChar -> CStringLen -> IO ByteString
actOnResult CUChar
readresult (Ptr CChar
buf, Int
num)
        
-------------------------------------------- SPI functions -------------------------------------------------------------------------

-- |Sets the chip select pin(s). When a transfer is made with 'transferSPI' or 'transferManySPI', the selected pin(s) will be 

-- asserted during the transfer. 

chipSelectSPI :: SPIPin -> IO ()
chipSelectSPI :: SPIPin -> IO ()
chipSelectSPI SPIPin
pin = CUChar -> IO ()
c_chipSelectSPI (Int -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUChar) -> (SPIPin -> Int) -> SPIPin -> CUChar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SPIPin -> Int
forall a. Enum a => a -> Int
fromEnum (SPIPin -> CUChar) -> SPIPin -> CUChar
forall a b. (a -> b) -> a -> b
$ SPIPin
pin)

-- |Sets the SPI clock divider and therefore the SPI clock speed.

setClockDividerSPI :: Word16 -> IO ()
setClockDividerSPI :: Word16 -> IO ()
setClockDividerSPI Word16
a = CUShort -> IO ()
c_setClockDividerSPI (CUShort -> IO ()) -> CUShort -> IO ()
forall a b. (a -> b) -> a -> b
$ Word16 -> CUShort
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
a

-- |Set the bit order to be used for transmit and receive. The bcm2835 SPI0 only supports MSBFirst,

-- so if you select LSBFirst, the bytes will be reversed in software.

-- The library defaults to MSBFirst.

setBitOrderSPI :: SPIBitOrder -> IO ()
setBitOrderSPI :: SPIBitOrder -> IO ()
setBitOrderSPI SPIBitOrder
LSBFirst = CUChar -> IO ()
c_setBitOrder CUChar
0
setBitOrderSPI SPIBitOrder
MSBFirst = CUChar -> IO ()
c_setBitOrder CUChar
1

-- |Sets the chip select pin polarity for a given pin(s). When a transfer is made with 'transferSPI' or 'transferManySPI', the 

-- currently selected chip select pin(s) will be asserted to the LogicLevel supplied. When transfers are not happening, the chip 

-- select pin(s) return to the complement (inactive) value. 

setChipSelectPolaritySPI :: SPIPin -> LogicLevel -> IO ()
setChipSelectPolaritySPI :: SPIPin -> LogicLevel -> IO ()
setChipSelectPolaritySPI SPIPin
pin LogicLevel
level = CUChar -> CUChar -> IO ()
c_setChipSelectPolarity (Int -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUChar) -> (SPIPin -> Int) -> SPIPin -> CUChar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SPIPin -> Int
forall a. Enum a => a -> Int
fromEnum (SPIPin -> CUChar) -> SPIPin -> CUChar
forall a b. (a -> b) -> a -> b
$ SPIPin
pin) (Int -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUChar) -> (LogicLevel -> Int) -> LogicLevel -> CUChar
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LogicLevel -> Int
forall a. Enum a => a -> Int
fromEnum (LogicLevel -> CUChar) -> LogicLevel -> CUChar
forall a b. (a -> b) -> a -> b
$ LogicLevel
level)

-- |Sets the SPI clock polarity and phase (ie, CPOL and CPHA)

setDataModeSPI :: (CPOL,CPHA) -> IO ()
setDataModeSPI :: (LogicLevel, LogicLevel) -> IO ()
setDataModeSPI (LogicLevel
False,LogicLevel
False) = CUChar -> IO ()
c_setDataModeSPI CUChar
0
setDataModeSPI (LogicLevel
False,LogicLevel
True)  = CUChar -> IO ()
c_setDataModeSPI CUChar
1
setDataModeSPI (LogicLevel
True,LogicLevel
False)  = CUChar -> IO ()
c_setDataModeSPI CUChar
2
setDataModeSPI (LogicLevel
True,LogicLevel
True)   = CUChar -> IO ()
c_setDataModeSPI CUChar
3

-- |Transfers one byte to and from the currently selected SPI slave. Asserts the currently selected CS pins (as previously set by 

-- 'chipSelectSPI') during the transfer. Clocks the 8 bit value out on MOSI, and simultaneously clocks in data from MISO. Returns the 

-- read data byte from the slave.

transferSPI :: Word8 -> IO Word8
transferSPI :: Word8 -> IO Word8
transferSPI Word8
input = CUChar -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUChar -> Word8) -> IO CUChar -> IO Word8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CUChar -> IO CUChar
c_transferSPI (Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
input)

-- |Transfers any number of bytes to and from the currently selected SPI slave, one byte at a time. Asserts the currently selected 

-- CS pins (as previously set by 'chipSelectSPI') during the transfer. Clocks 8 bit bytes out on MOSI, and simultaneously clocks in 

-- data from MISO.

transferManySPI :: [Word8] -> IO [Word8]
transferManySPI :: [Word8] -> IO [Word8]
transferManySPI [Word8]
input = ByteString -> (CStringLen -> IO [Word8]) -> IO [Word8]
forall a. ByteString -> (CStringLen -> IO a) -> IO a
BS.useAsCStringLen ([Word8] -> ByteString
BS.pack [Word8]
input) ((CStringLen -> IO [Word8]) -> IO [Word8])
-> (CStringLen -> IO [Word8]) -> IO [Word8]
forall a b. (a -> b) -> a -> b
$ \(Ptr CChar
buf,Int
len) -> do --convert input list to bytestring and from there to CString

    --returns the read bytes in buf. Uses CStringLen because the responses might have zero bytes and this will influence the result if a

    --normal CString is used

    Ptr CChar -> CUInt -> IO ()
c_transferManySPI Ptr CChar
buf (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len) --

    (CStringLen -> IO ByteString
BS.packCStringLen (Ptr CChar
buf,Int
len)) IO ByteString -> (ByteString -> IO [Word8]) -> IO [Word8]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [Word8] -> IO [Word8]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Word8] -> IO [Word8])
-> (ByteString -> [Word8]) -> ByteString -> IO [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [Word8]
BS.unpack -- translate back from a buffer to a bytestring to a [Word8]

        
------------------------------------------ AUX SPI functions -----------------------------------------------------------------------


-- |Any IO computation that uses the AUX SPI functionality using this library should be wrapped with this function; ie @withAUXSPI $ do foo@.

-- It prepares the relevant pins for use with the SPI protocol and makes sure everything is safely returned to normal if an exception

-- occurs. If you only use the GPIO pins for SPI, you can do @withGPIO . withAUXSPI $ do foo@ and it will work as expected. WARNING: 

-- after this function returns, the SPI pins will be set to Input, so use 'setPinFunction' if you want to use them for output.

withAUXSPI :: IO a -> IO a
withAUXSPI :: forall a. IO a -> IO a
withAUXSPI IO a
f = IO Int -> (Int -> IO ()) -> (Int -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO Int
initAUXSPI
                       (IO () -> Int -> IO ()
forall a b. a -> b -> a
const IO ()
stopAUXSPI)
                       (\Int
r -> if Int
rInt -> Int -> LogicLevel
forall a. Eq a => a -> a -> LogicLevel
==Int
0 then IOException -> IO a
forall e a. Exception e => e -> IO a
throwIO IOException
ioe else IO a
f)
                            where ioe :: IOException
ioe = Maybe Handle
-> IOErrorType
-> String
-> String
-> Maybe CInt
-> Maybe String
-> IOException
IOError Maybe Handle
forall a. Maybe a
Nothing IOErrorType
IllegalOperation String
"AUXAPI: " String
"Unable to start AUXAPI." Maybe CInt
forall a. Maybe a
Nothing Maybe String
forall a. Maybe a
Nothing

-- |Sets the AUX SPI clock divider and therefore the SPI clock speed.

setClockDividerAUXSPI :: Word16 -> IO ()
setClockDividerAUXSPI :: Word16 -> IO ()
setClockDividerAUXSPI Word16
a = CUShort -> IO ()
c_setClockDividerAUXSPI (CUShort -> IO ()) -> CUShort -> IO ()
forall a b. (a -> b) -> a -> b
$ Word16 -> CUShort
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
a

-- |Transfers one byte to and from the SPI slave. Asserts the CS2 pin during the transfer. Clocks the 8 bit value out

-- on MOSI, and simultaneously clocks in data from MISO. Returns the read data byte from the slave.

transferAUXSPI :: Word8 -> IO Word8
transferAUXSPI :: Word8 -> IO Word8
transferAUXSPI Word8
input = CUChar -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CUChar -> Word8) -> IO CUChar -> IO Word8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CUChar -> IO CUChar
c_transferAUXSPI (Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
input)

-------------------------------------------- PWM functions -------------------------------------------------------------------------


-- |Sets the PWM clock divisor, to control the basic PWM pulse widths. 

setClockPWM :: Word32 -> IO ()
setClockPWM :: Word32 -> IO ()
setClockPWM Word32
a = CUInt -> IO ()
c_setClockPWM (CUInt -> IO ()) -> CUInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Word32 -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
a

-- |Sets the mode of the given PWM channel, allowing you to control the PWM mode and enable/disable that channel 

setModePWM :: Word8 -> Word8 -> Word8 -> IO ()
setModePWM :: Word8 -> Word8 -> Word8 -> IO ()
setModePWM Word8
a Word8
b Word8
c = CUChar -> CUChar -> CUChar -> IO ()
c_setModePWM (Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
a) (Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
b) (Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
c)

-- |Sets the maximum range of the PWM output. The data value can vary between 0 and this range to control PWM output 

setRangePWM :: Word8 -> Word32 -> IO ()
setRangePWM :: Word8 -> Word32 -> IO ()
setRangePWM Word8
a Word32
b = CUChar -> CUInt -> IO ()
c_setRangePWM (Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
a) (Word32 -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
b)

-- |Sets the PWM pulse ratio to emit to DATA/RANGE, where RANGE is set by 'setRangePWM'.

setDataPWM :: Word8 -> Word32 -> IO ()
setDataPWM :: Word8 -> Word32 -> IO ()
setDataPWM Word8
a Word32
b = CUChar -> CUInt -> IO ()
c_setDataPWM (Word8 -> CUChar
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
a) (Word32 -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
b)