From 307cb1b1094c73fd15eab378c27ac0073696b739 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Tue, 19 Aug 2025 03:56:40 +0000 Subject: Improves formatting --- hsm-gpio/Hsm/GPIO.hs | 9 ++-- hsm-gpio/Hsm/GPIO/FFI.hs | 118 ++++++++++++++++++++++++++++++++++++++++++++++ hsm-gpio/Hsm/GPIO/FFI.hsc | 116 --------------------------------------------- 3 files changed, 121 insertions(+), 122 deletions(-) create mode 100644 hsm-gpio/Hsm/GPIO/FFI.hs delete mode 100644 hsm-gpio/Hsm/GPIO/FFI.hsc (limited to 'hsm-gpio/Hsm') diff --git a/hsm-gpio/Hsm/GPIO.hs b/hsm-gpio/Hsm/GPIO.hs index 2bcf3ed..7cc2c36 100644 --- a/hsm-gpio/Hsm/GPIO.hs +++ b/hsm-gpio/Hsm/GPIO.hs @@ -73,8 +73,7 @@ setPins :: (GPIO :> es, Log "gpio" :> es) => [GPIOPin] -> LineValue -> Eff es () setPins pins lineValue = do GPIO lineRequest <- getStaticRep logMsg Trace $ "Setting pin(s) " <> show pins <> " to " <> show lineValue - forM_ pins $ \pin -> - unsafeEff_ $ lineRequestSetValue lineRequest (pinLine pin) lineValue + forM_ pins $ \pin -> unsafeEff_ $ lineRequestSetValue lineRequest (pinLine pin) lineValue setAllPins :: (GPIO :> es, Log "gpio" :> es) => LineValue -> Eff es () setAllPins lineValue = do @@ -122,8 +121,7 @@ runGPIO consumer action = do lineSettingsDealloc lineSettings = do logMsg Info "Freeing line settings" liftIO $ lineSettingsFree lineSettings - lineConfigBracket lineSettings = - allocateEff lineConfigAlloc lineConfigDealloc + lineConfigBracket lineSettings = allocateEff lineConfigAlloc lineConfigDealloc where lineConfigAlloc = do logMsg Info "Allocating line config" @@ -150,8 +148,7 @@ runGPIO consumer action = do requestConfigDealloc requestConfig = do logMsg Info "Freeing request config" liftIO $ requestConfigFree requestConfig - lineRequestBracket chip requestConfig lineConfig = - allocateEff lineRequestAlloc lineRequestDealloc + lineRequestBracket chip requestConfig lineConfig = allocateEff lineRequestAlloc lineRequestDealloc where lineRequestAlloc = do logMsg Info "Allocating line request" diff --git a/hsm-gpio/Hsm/GPIO/FFI.hs b/hsm-gpio/Hsm/GPIO/FFI.hs new file mode 100644 index 0000000..c1bb9e8 --- /dev/null +++ b/hsm-gpio/Hsm/GPIO/FFI.hs @@ -0,0 +1,118 @@ +{-# LANGUAGE CApiFFI #-} + +-- FFI bindings to `libgpiod` for direct GPIO hardware access. +-- +-- Exposes only the minimal required subset of `libgpiod` functionality used by +-- this project. The bindings are suitable for low-level hardware control. +-- +-- Future work could expand this into a comprehensive `gpiod` binding package. +module Hsm.GPIO.FFI + ( chipOpen + , chipClose + , input + , output + , LineValue + , active + , inactive + , lineSettingsNew + , lineSettingsFree + , lineSettingsSetDirection + , lineSettingsSetOutputValue + , lineConfigNew + , lineConfigFree + , lineConfigAddLineSettings + , requestConfigNew + , requestConfigFree + , requestConfigSetConsumer + , LineRequest + , chipRequestLines + , lineRequestRelease + , lineRequestSetValue + , lineRequestSetValues + ) +where + +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 capi unsafe "gpiod.h gpiod_chip_open" + chipOpen :: CString -> IO (Ptr Chip) + +foreign import capi unsafe "gpiod.h gpiod_chip_close" + chipClose :: Ptr Chip -> IO () + +data LineSettings + +newtype LineDirection + = LineDirection CInt + deriving Show + +foreign import capi safe "gpiod.h value GPIOD_LINE_DIRECTION_INPUT" + input :: LineDirection + +foreign import capi safe "gpiod.h value GPIOD_LINE_DIRECTION_OUTPUT" + output :: LineDirection + +newtype LineValue + = LineValue CInt + deriving (Show, Storable) + +foreign import capi safe "gpiod.h value GPIOD_LINE_VALUE_ACTIVE" + active :: LineValue + +foreign import capi safe "gpiod.h value GPIOD_LINE_VALUE_INACTIVE" + inactive :: LineValue + +foreign import capi unsafe "gpiod.h gpiod_line_settings_new" + lineSettingsNew :: IO (Ptr LineSettings) + +foreign import capi unsafe "gpiod.h gpiod_line_settings_free" + lineSettingsFree :: Ptr LineSettings -> IO () + +foreign import capi unsafe "gpiod.h gpiod_line_settings_set_direction" + lineSettingsSetDirection :: Ptr LineSettings -> LineDirection -> IO CInt + +foreign import capi unsafe "gpiod.h gpiod_line_settings_set_output_value" + lineSettingsSetOutputValue :: Ptr LineSettings -> LineValue -> IO CInt + +data LineConfig + +foreign import capi unsafe "gpiod.h gpiod_line_config_new" + lineConfigNew :: IO (Ptr LineConfig) + +foreign import capi unsafe "gpiod.h gpiod_line_config_free" + lineConfigFree :: Ptr LineConfig -> IO () + +foreign import capi unsafe "gpiod.h gpiod_line_config_add_line_settings" + lineConfigAddLineSettings + :: Ptr LineConfig -> Ptr CUInt -> CSize -> Ptr LineSettings -> IO CInt + +data RequestConfig + +foreign import capi unsafe "gpiod.h gpiod_request_config_new" + requestConfigNew :: IO (Ptr RequestConfig) + +foreign import capi unsafe "gpiod.h gpiod_request_config_free" + requestConfigFree :: Ptr RequestConfig -> IO () + +foreign import capi unsafe "gpiod.h gpiod_request_config_set_consumer" + requestConfigSetConsumer :: Ptr RequestConfig -> CString -> IO () + +data LineRequest + +foreign import capi unsafe "gpiod.h gpiod_chip_request_lines" + chipRequestLines + :: Ptr Chip -> Ptr RequestConfig -> Ptr LineConfig -> IO (Ptr LineRequest) + +foreign import capi unsafe "gpiod.h gpiod_line_request_release" + lineRequestRelease :: Ptr LineRequest -> IO () + +foreign import capi unsafe "gpiod.h gpiod_line_request_set_value" + lineRequestSetValue :: Ptr LineRequest -> CUInt -> LineValue -> IO CInt + +foreign import capi unsafe "gpiod.h gpiod_line_request_set_values" + lineRequestSetValues :: Ptr LineRequest -> Ptr LineValue -> IO CInt diff --git a/hsm-gpio/Hsm/GPIO/FFI.hsc b/hsm-gpio/Hsm/GPIO/FFI.hsc deleted file mode 100644 index f0f5737..0000000 --- a/hsm-gpio/Hsm/GPIO/FFI.hsc +++ /dev/null @@ -1,116 +0,0 @@ -{-# LANGUAGE CApiFFI #-} - --- FFI bindings to `libgpiod` for direct GPIO hardware access. --- --- Exposes only the minimal required subset of `libgpiod` functionality used by --- this project. The bindings are suitable for low-level hardware control. --- --- Future work could expand this into a comprehensive `gpiod` binding package. -module Hsm.GPIO.FFI - ( chipOpen - , chipClose - , input - , output - , LineValue - , active - , inactive - , lineSettingsNew - , lineSettingsFree - , lineSettingsSetDirection - , lineSettingsSetOutputValue - , lineConfigNew - , lineConfigFree - , lineConfigAddLineSettings - , requestConfigNew - , requestConfigFree - , requestConfigSetConsumer - , LineRequest - , chipRequestLines - , lineRequestRelease - , lineRequestSetValue - , lineRequestSetValues - ) -where - -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 capi unsafe "gpiod.h gpiod_chip_open" - chipOpen :: CString -> IO (Ptr Chip) - -foreign import capi unsafe "gpiod.h gpiod_chip_close" - chipClose :: Ptr Chip -> IO () - -data LineSettings - -newtype LineDirection - = LineDirection CInt - deriving Show - -foreign import capi "gpiod.h value GPIOD_LINE_DIRECTION_INPUT" - input :: LineDirection - -foreign import capi "gpiod.h value GPIOD_LINE_DIRECTION_OUTPUT" - output :: LineDirection - -newtype LineValue - = LineValue CInt - deriving (Show, Storable) - -foreign import capi "gpiod.h value GPIOD_LINE_VALUE_ACTIVE" - active :: LineValue - -foreign import capi "gpiod.h value GPIOD_LINE_VALUE_INACTIVE" - inactive :: LineValue - -foreign import capi unsafe "gpiod.h gpiod_line_settings_new" - lineSettingsNew :: IO (Ptr LineSettings) - -foreign import capi unsafe "gpiod.h gpiod_line_settings_free" - lineSettingsFree :: Ptr LineSettings -> IO () - -foreign import capi unsafe "gpiod.h gpiod_line_settings_set_direction" - lineSettingsSetDirection :: Ptr LineSettings -> LineDirection -> IO CInt - -foreign import capi unsafe "gpiod.h gpiod_line_settings_set_output_value" - lineSettingsSetOutputValue :: Ptr LineSettings -> LineValue -> IO CInt - -data LineConfig - -foreign import capi unsafe "gpiod.h gpiod_line_config_new" - lineConfigNew :: IO (Ptr LineConfig) - -foreign import capi unsafe "gpiod.h gpiod_line_config_free" - lineConfigFree :: Ptr LineConfig -> IO () - -foreign import capi unsafe "gpiod.h gpiod_line_config_add_line_settings" - lineConfigAddLineSettings :: Ptr LineConfig -> Ptr CUInt -> CSize -> Ptr LineSettings -> IO CInt - -data RequestConfig - -foreign import capi unsafe "gpiod.h gpiod_request_config_new" - requestConfigNew :: IO (Ptr RequestConfig) - -foreign import capi unsafe "gpiod.h gpiod_request_config_free" - requestConfigFree :: Ptr RequestConfig -> IO () - -foreign import capi unsafe "gpiod.h gpiod_request_config_set_consumer" - requestConfigSetConsumer :: Ptr RequestConfig -> CString -> IO () - -data LineRequest - -foreign import capi unsafe "gpiod.h gpiod_chip_request_lines" - chipRequestLines :: Ptr Chip -> Ptr RequestConfig -> Ptr LineConfig -> IO (Ptr LineRequest) - -foreign import capi unsafe "gpiod.h gpiod_line_request_release" - lineRequestRelease :: Ptr LineRequest -> IO () - -foreign import capi unsafe "gpiod.h gpiod_line_request_set_value" - lineRequestSetValue :: Ptr LineRequest -> CUInt -> LineValue -> IO CInt - -foreign import capi unsafe "gpiod.h gpiod_line_request_set_values" - lineRequestSetValues :: Ptr LineRequest -> Ptr LineValue -> IO CInt -- cgit v1.2.1