aboutsummaryrefslogtreecommitdiff
path: root/hsm-gpio/Hsm/GPIO/Lib.hsc
blob: 6716f3acbd01d7a33cf0889e28943ebc5f293330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}

-- This module provides C bindings to the `gpiod` library for direct GPIO pin
-- control. It includes bindings only for the C functions that are currently
-- used. In the future, creating a complete set of bindings for the entire
-- `gpiod` library as an external package would be a valuable contribution to
-- Hackage.

module Hsm.GPIO.Lib
  ( chipOpen
  , chipClose
  , input
  , output
  , LineValue
  , active
  , inactive
  , lineSettingsNew
  , lineSettingsFree
  , lineSettingsSetDirection
  , lineSettingsSetOutputValue
  , lineConfigNew
  , lineConfigFree
  , lineConfigAddLineSettings
  , requestConfigNew
  , requestConfigFree
  , requestConfigSetConsumer
  , requestLines
  , LineRequest
  , lineRequestRelease
  , lineRequestSetValue
  , lineRequestSetValues
  ) where

#include <gpiod.h>

import Foreign.C.String (CString)
import Foreign.C.Types (CInt(CInt), CSize(CSize), CUInt(CUInt))
import Foreign.Ptr (Ptr)
import Foreign.Storable (Storable)

data Chip

foreign import ccall unsafe "gpiod.h gpiod_chip_open"
  chipOpen :: CString -> IO (Ptr Chip)

foreign import ccall unsafe "gpiod.h gpiod_chip_close"
  chipClose :: Ptr Chip -> IO ()

data LineSettings

newtype LineDirection =
  LineDirection CInt
  deriving (Show)

#{enum LineDirection, LineDirection
  , input = GPIOD_LINE_DIRECTION_INPUT
  , output = GPIOD_LINE_DIRECTION_OUTPUT
}

newtype LineValue =
  LineValue CInt
  deriving (Show, Storable)

#{enum LineValue, LineValue
  , active = GPIOD_LINE_VALUE_ACTIVE
  , inactive = GPIOD_LINE_VALUE_INACTIVE
}

foreign import ccall unsafe "gpiod.h gpiod_line_settings_new"
  lineSettingsNew :: IO (Ptr LineSettings)

foreign import ccall unsafe "gpiod.h gpiod_line_settings_free"
  lineSettingsFree :: Ptr LineSettings -> IO ()

foreign import ccall unsafe "gpiod.h gpiod_line_settings_set_direction"
  lineSettingsSetDirection :: Ptr LineSettings -> LineDirection -> IO CInt

foreign import ccall unsafe "gpiod.h gpiod_line_settings_set_output_value"
  lineSettingsSetOutputValue :: Ptr LineSettings -> LineValue -> IO CInt

data LineConfig

foreign import ccall unsafe "gpiod.h gpiod_line_config_new"
  lineConfigNew :: IO (Ptr LineConfig)

foreign import ccall unsafe "gpiod.h gpiod_line_config_free"
  lineConfigFree :: Ptr LineConfig -> IO ()

foreign import ccall unsafe "gpiod.d gpiod_line_config_add_line_settings"
  lineConfigAddLineSettings :: Ptr LineConfig -> Ptr CUInt -> CSize -> Ptr LineSettings -> IO CInt

data RequestConfig

foreign import ccall unsafe "gpiod.h gpiod_request_config_new"
  requestConfigNew :: IO (Ptr RequestConfig)

foreign import ccall unsafe "gpiod.h gpiod_request_config_free"
  requestConfigFree :: Ptr RequestConfig -> IO ()

foreign import ccall unsafe "gpiod.h gpiod_request_config_set_consumer"
  requestConfigSetConsumer :: Ptr RequestConfig -> CString -> IO ()

data LineRequest

foreign import ccall unsafe "gpiod.h gpiod_chip_request_lines"
  requestLines :: Ptr Chip -> Ptr RequestConfig -> Ptr LineConfig -> IO (Ptr LineRequest)

foreign import ccall unsafe "gpiod.h gpiod_line_request_release"
  lineRequestRelease :: Ptr LineRequest -> IO ()

foreign import ccall unsafe "gpiod.h gpiod_line_request_set_value"
  lineRequestSetValue :: Ptr LineRequest -> CUInt -> LineValue -> IO CInt

foreign import ccall unsafe "gpiod.h gpiod_line_request_set_values"
  lineRequestSetValues :: Ptr LineRequest -> Ptr LineValue -> IO CInt