diff options
author | Paul Oliver <contact@pauloliver.dev> | 2025-08-22 04:06:44 +0000 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-08-23 22:59:12 +0000 |
commit | 5a78bc1885ad7d6fd7ad63d6ef900188ab38a80c (patch) | |
tree | 38f30d67e924a5ed67d7a35cd1b71b2cff7f131e | |
parent | 82b02509150b615360118ca381ad8c9fd39d2f29 (diff) |
Improves formatting again
-rw-r--r-- | hsm-core/Hsm/Core/Serial.hs | 27 | ||||
-rw-r--r-- | hsm-gpio/Hsm/GPIO.hs | 39 | ||||
-rw-r--r-- | hsm-gpio/Hsm/GPIO/FFI.hs | 77 | ||||
-rw-r--r-- | hsm-log/Hsm/Log.hs | 44 | ||||
-rw-r--r-- | hsm-pwm/Hsm/PWM.hs | 29 | ||||
-rw-r--r-- | hsm-repl/Hsm/Repl.hs | 67 | ||||
-rw-r--r-- | hsm-repl/Test/Repl.hs | 9 |
7 files changed, 90 insertions, 202 deletions
diff --git a/hsm-core/Hsm/Core/Serial.hs b/hsm-core/Hsm/Core/Serial.hs index 0fc89e8..a0efca3 100644 --- a/hsm-core/Hsm/Core/Serial.hs +++ b/hsm-core/Hsm/Core/Serial.hs @@ -2,22 +2,21 @@ module Hsm.Core.Serial ( makeSerial - ) -where + ) where import GHC.Num (integerFromInt) import Language.Haskell.TH - ( Body (NormalB) - , Clause (Clause) - , Con (NormalC) - , Dec (DataD, FunD, SigD) - , DerivClause (DerivClause) - , Exp (LitE) - , Lit (IntegerL) + ( Body(NormalB) + , Clause(Clause) + , Con(NormalC) + , Dec(DataD, FunD, SigD) + , DerivClause(DerivClause) + , Exp(LitE) + , Lit(IntegerL) , Name - , Pat (ConP) + , Pat(ConP) , Q - , Type (AppT, ArrowT, ConT) + , Type(AppT, ArrowT, ConT) , mkName ) @@ -38,11 +37,7 @@ import Language.Haskell.TH -- and a function `pinLine :: GPIOPin -> Int`. makeSerial :: String -> String -> String -> Name -> [Int] -> Q [Dec] makeSerial name suffix mapFun mapType idxs = - return - [ DataD [] dtName [] Nothing (idxCons <$> idxs) [derivClause] - , SigD mapFunName . AppT (AppT ArrowT $ ConT dtName) $ ConT mapType - , FunD mapFunName $ mapFunClause <$> idxs - ] + return [DataD [] dtName [] Nothing (idxCons <$> idxs) [derivClause], SigD mapFunName . AppT (AppT ArrowT $ ConT dtName) $ ConT mapType, FunD mapFunName $ mapFunClause <$> idxs] where dtName = mkName $ name <> suffix idxName idx = mkName $ name <> show idx diff --git a/hsm-gpio/Hsm/GPIO.hs b/hsm-gpio/Hsm/GPIO.hs index 0e4e2e5..4786379 100644 --- a/hsm-gpio/Hsm/GPIO.hs +++ b/hsm-gpio/Hsm/GPIO.hs @@ -3,28 +3,21 @@ {-# LANGUAGE TypeFamilies #-} module Hsm.GPIO - ( GPIOPin (..) + ( GPIOPin(..) , GPIO , setPins , setAllPins , runGPIO - ) -where + ) where import Control.Monad (forM_, void) import Data.Vector.Storable (fromList, replicate, unsafeWith) -import Effectful (Dispatch (Static), DispatchOf, Eff, IOE, liftIO, (:>)) -import Effectful.Dispatch.Static - ( SideEffects (WithSideEffects) - , StaticRep - , evalStaticRep - , getStaticRep - , unsafeEff_ - ) +import Effectful (Dispatch(Static), DispatchOf, Eff, IOE, (:>), liftIO) +import Effectful.Dispatch.Static (SideEffects(WithSideEffects), StaticRep, evalStaticRep, getStaticRep, unsafeEff_) import Effectful.Exception (bracket) import Effectful.Resource (Resource, allocateEff, releaseEff) import Foreign.C.String (withCString) -import Foreign.C.Types (CSize (CSize), CUInt) +import Foreign.C.Types (CSize(CSize), CUInt) import Foreign.Ptr (Ptr) import Hsm.Core.Serial (makeSerial) import Hsm.GPIO.FFI @@ -49,7 +42,7 @@ import Hsm.GPIO.FFI , requestConfigNew , requestConfigSetConsumer ) -import Hsm.Log (Log, Severity (Info, Trace), logMsg) +import Hsm.Log (Log, Severity(Info, Trace), logMsg) import Prelude hiding (replicate) $(makeSerial "GPIO" "Pin" "pinLine" ''CUInt $ [2 .. 17] <> [20 .. 27]) @@ -67,8 +60,8 @@ data GPIO (a :: * -> *) (b :: *) type instance DispatchOf GPIO = Static WithSideEffects -newtype instance StaticRep GPIO - = GPIO (Ptr LineRequest) +newtype instance StaticRep GPIO = + GPIO (Ptr LineRequest) setPins :: (GPIO :> es, Log "gpio" :> es) => [GPIOPin] -> LineValue -> Eff es () setPins pins lineValue = do @@ -80,14 +73,9 @@ setAllPins :: (GPIO :> es, Log "gpio" :> es) => LineValue -> Eff es () setAllPins lineValue = do GPIO lineRequest <- getStaticRep logMsg Trace $ "Setting all pins " <> show allPins <> " to " <> show lineValue - unsafeEff_ . unsafeWith (replicate pinCount lineValue) $ - void . lineRequestSetValues lineRequest + unsafeEff_ . unsafeWith (replicate pinCount lineValue) $ void . lineRequestSetValues lineRequest -runGPIO - :: (IOE :> es, Log "gpio" :> es, Resource :> es) - => String - -> Eff (GPIO : es) a - -> Eff es a +runGPIO :: (IOE :> es, Log "gpio" :> es, Resource :> es) => String -> Eff (GPIO : es) a -> Eff es a runGPIO consumer action = do (chipKey, chip) <- chipBracket (lineSettingsKey, lineSettings) <- lineSettingsBracket @@ -128,12 +116,7 @@ runGPIO consumer action = do logMsg Info "Allocating line config" logMsg Info $ "With GPIO pins " <> show allPins lineConfig <- liftIO lineConfigNew - liftIO . void . unsafeWith (fromList allLines) $ \pinsVector -> - lineConfigAddLineSettings - lineConfig - pinsVector - (CSize $ fromIntegral pinCount) - lineSettings + liftIO . void . unsafeWith (fromList allLines) $ \pinsVector -> lineConfigAddLineSettings lineConfig pinsVector (CSize $ fromIntegral pinCount) lineSettings return lineConfig lineConfigDealloc lineConfig = do logMsg Info "Freeing line config" diff --git a/hsm-gpio/Hsm/GPIO/FFI.hs b/hsm-gpio/Hsm/GPIO/FFI.hs index a4eae5c..e0d6d07 100644 --- a/hsm-gpio/Hsm/GPIO/FFI.hs +++ b/hsm-gpio/Hsm/GPIO/FFI.hs @@ -29,90 +29,67 @@ module Hsm.GPIO.FFI , lineRequestRelease , lineRequestSetValue , lineRequestSetValues - ) -where + ) where import Foreign.C.String (CString) -import Foreign.C.Types (CInt (CInt), CSize (CSize), CUInt (CUInt)) +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_open" chipOpen :: CString -> IO (Ptr Chip) -foreign import ccall unsafe "gpiod.h gpiod_chip_close" - chipClose :: Ptr Chip -> IO () +foreign import ccall unsafe "gpiod.h gpiod_chip_close" chipClose :: Ptr Chip -> IO () data LineSettings -newtype LineDirection - = LineDirection CInt - deriving Show +newtype LineDirection = + LineDirection CInt + deriving (Show) -foreign import capi unsafe "gpiod.h value GPIOD_LINE_DIRECTION_INPUT" - input :: LineDirection +foreign import capi unsafe "gpiod.h value GPIOD_LINE_DIRECTION_INPUT" input :: LineDirection -foreign import capi unsafe "gpiod.h value GPIOD_LINE_DIRECTION_OUTPUT" - output :: LineDirection +foreign import capi unsafe "gpiod.h value GPIOD_LINE_DIRECTION_OUTPUT" output :: LineDirection -newtype LineValue - = LineValue CInt +newtype LineValue = + LineValue CInt deriving (Show, Storable) -foreign import capi unsafe "gpiod.h value GPIOD_LINE_VALUE_ACTIVE" - active :: LineValue +foreign import capi unsafe "gpiod.h value GPIOD_LINE_VALUE_ACTIVE" active :: LineValue -foreign import capi unsafe "gpiod.h value GPIOD_LINE_VALUE_INACTIVE" - inactive :: LineValue +foreign import capi unsafe "gpiod.h value GPIOD_LINE_VALUE_INACTIVE" inactive :: LineValue -foreign import ccall unsafe "gpiod.h gpiod_line_settings_new" - lineSettingsNew :: IO (Ptr LineSettings) +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_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_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 +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_new" lineConfigNew :: IO (Ptr LineConfig) -foreign import ccall unsafe "gpiod.h gpiod_line_config_free" - lineConfigFree :: Ptr LineConfig -> IO () +foreign import ccall unsafe "gpiod.h gpiod_line_config_free" lineConfigFree :: Ptr LineConfig -> IO () -foreign import ccall unsafe "gpiod.h gpiod_line_config_add_line_settings" - lineConfigAddLineSettings - :: Ptr LineConfig -> Ptr CUInt -> CSize -> Ptr LineSettings -> IO CInt +foreign import ccall unsafe "gpiod.h 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_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_free" requestConfigFree :: Ptr RequestConfig -> IO () -foreign import ccall unsafe "gpiod.h gpiod_request_config_set_consumer" - requestConfigSetConsumer :: Ptr RequestConfig -> CString -> 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" - chipRequestLines - :: Ptr Chip -> Ptr RequestConfig -> Ptr LineConfig -> IO (Ptr LineRequest) +foreign import ccall unsafe "gpiod.h gpiod_chip_request_lines" chipRequestLines :: 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_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_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 +foreign import ccall unsafe "gpiod.h gpiod_line_request_set_values" lineRequestSetValues :: Ptr LineRequest -> Ptr LineValue -> IO CInt diff --git a/hsm-log/Hsm/Log.hs b/hsm-log/Hsm/Log.hs index 73da4bd..3c25501 100644 --- a/hsm-log/Hsm/Log.hs +++ b/hsm-log/Hsm/Log.hs @@ -3,27 +3,20 @@ {-# LANGUAGE UndecidableInstances #-} module Hsm.Log - ( Severity (Attention, Info, Trace) + ( Severity(Attention, Info, Trace) , Log , getLoggerIO , logMsg , runLog - ) -where + ) where import Control.Monad (when) import Data.Function (applyWhen) -import Data.Proxy (Proxy (Proxy)) +import Data.Proxy (Proxy(Proxy)) import Data.Time.Clock (getCurrentTime) import Data.Time.ISO8601 (formatISO8601Millis) -import Effectful (Dispatch (Static), DispatchOf, Eff, Effect, IOE, (:>)) -import Effectful.Dispatch.Static - ( SideEffects (WithSideEffects) - , StaticRep - , evalStaticRep - , getStaticRep - , unsafeEff_ - ) +import Effectful (Dispatch(Static), DispatchOf, Eff, Effect, IOE, (:>)) +import Effectful.Dispatch.Static (SideEffects(WithSideEffects), StaticRep, evalStaticRep, getStaticRep, unsafeEff_) import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) import GHC.TypeLits.Printf (printf) import String.ANSI (red) @@ -38,32 +31,28 @@ data Log (d :: Symbol) (a :: * -> *) (b :: *) type instance DispatchOf (Log d) = Static WithSideEffects -newtype instance StaticRep (Log d) - = Log Severity +newtype instance StaticRep (Log d) = + Log Severity -getLoggerIO - :: forall d es - . (KnownSymbol d, Log d :> es) +getLoggerIO :: + forall d es. (KnownSymbol d, Log d :> es) => Eff es (Severity -> String -> IO ()) getLoggerIO = do Log level <- getStaticRep return $ \severity message -> when (severity <= level) $ do time <- formatISO8601Millis <$> getCurrentTime - putStrLn . applyWhen (severity == Attention) red $ - printf "%s %s [%s] %s" time (symbolVal $ Proxy @d) (show severity) message + putStrLn . applyWhen (severity == Attention) red $ printf "%s %s [%s] %s" time (symbolVal $ Proxy @d) (show severity) message -logMsg - :: forall d es - . (KnownSymbol d, Log d :> es) +logMsg :: + forall d es. (KnownSymbol d, Log d :> es) => Severity -> String -> Eff es () logMsg severity message = getLoggerIO >>= \loggerIO -> unsafeEff_ $ loggerIO severity message -runLog - :: forall d es a - . IOE :> es +runLog :: + forall d es a. IOE :> es => Severity -> Eff (Log d : es) a -> Eff es a @@ -77,9 +66,6 @@ instance Logs ('[] :: [Symbol]) (es :: [Effect]) where type Insert '[] es = es runLogs _ = id -instance - (IOE :> Insert ds es, KnownSymbol d, Logs ds es) - => Logs (d : ds :: [Symbol]) (es :: [Effect]) - where +instance (IOE :> Insert ds es, KnownSymbol d, Logs ds es) => Logs (d : ds :: [Symbol]) (es :: [Effect]) where type Insert (d : ds) es = Log d : Insert ds es runLogs level = runLogs @ds level . runLog @d level diff --git a/hsm-pwm/Hsm/PWM.hs b/hsm-pwm/Hsm/PWM.hs index 9a4fe5c..2fd5955 100644 --- a/hsm-pwm/Hsm/PWM.hs +++ b/hsm-pwm/Hsm/PWM.hs @@ -12,26 +12,20 @@ -- - SysFS Reference: https://forums.raspberrypi.com/viewtopic.php?t=359251 -- - UDEV Setup: https://forums.raspberrypi.com/viewtopic.php?t=316514 module Hsm.PWM - ( PWMChannel (..) + ( PWMChannel(..) , PWM , setCycleDuration , runPWM - ) -where + ) where import Control.Concurrent (threadDelay) import Control.Monad (forM_) import Control.Monad.Loops (untilM_) -import Effectful (Dispatch (Static), DispatchOf, Eff, IOE, liftIO, (:>)) -import Effectful.Dispatch.Static - ( SideEffects (WithSideEffects) - , StaticRep - , evalStaticRep - , unsafeEff_ - ) +import Effectful (Dispatch(Static), DispatchOf, Eff, IOE, (:>), liftIO) +import Effectful.Dispatch.Static (SideEffects(WithSideEffects), StaticRep, evalStaticRep, unsafeEff_) import Effectful.Exception (bracket_) import Hsm.Core.Serial (makeSerial) -import Hsm.Log (Log, Severity (Info, Trace), logMsg) +import Hsm.Log (Log, Severity(Info, Trace), logMsg) import System.FilePath ((</>)) import System.Posix.Files (fileAccess) @@ -41,8 +35,8 @@ data PWM (a :: * -> *) (b :: *) type instance DispatchOf PWM = Static WithSideEffects -newtype instance StaticRep PWM - = PWM () +newtype instance StaticRep PWM = + PWM () chipPath :: FilePath chipPath = "/sys/class/pwm/pwmchip0" @@ -83,17 +77,12 @@ setDutyCycle channel dutyCycle = do -- 2. Update period -- 3. Set default 50% duty cycle -- 4. Re-enable output -setCycleDuration - :: (Log "pwm" :> es, PWM :> es) => PWMChannel -> Int -> Eff es () +setCycleDuration :: (Log "pwm" :> es, PWM :> es) => PWMChannel -> Int -> Eff es () setCycleDuration channel 0 = do logMsg Trace $ "Halting PWM signals on channel " <> show channel setEnable channel False setCycleDuration channel cycleDuration = do - logMsg Trace $ - "Setting cycle duration on channel " - <> show channel - <> " to " - <> show cycleDuration + logMsg Trace $ "Setting cycle duration on channel " <> show channel <> " to " <> show cycleDuration setEnable channel False setDutyCycle channel 0 setPeriod channel cycleDuration diff --git a/hsm-repl/Hsm/Repl.hs b/hsm-repl/Hsm/Repl.hs index 1da7493..dacc76a 100644 --- a/hsm-repl/Hsm/Repl.hs +++ b/hsm-repl/Hsm/Repl.hs @@ -4,72 +4,36 @@ module Hsm.Repl ( Repl , repl , runRepl - ) -where + ) where import Control.Monad (forM_) -import Data.Typeable (Proxy (Proxy), Typeable, typeRep) -import Effectful (Dispatch (Static), DispatchOf, Eff, IOE, liftIO, (:>)) -import Effectful.Dispatch.Static - ( SideEffects (WithSideEffects) - , StaticRep - , evalStaticRep - , getStaticRep - , unsafeEff_ - ) +import Data.Typeable (Proxy(Proxy), Typeable, typeRep) +import Effectful (Dispatch(Static), DispatchOf, Eff, IOE, (:>), liftIO) +import Effectful.Dispatch.Static (SideEffects(WithSideEffects), StaticRep, evalStaticRep, getStaticRep, unsafeEff_) import Effectful.Exception (bracket) import GHC.TypeLits (KnownSymbol, Symbol, symbolVal) import Generic.Data.Function.Common.Generic.Meta (KnownSymbols, symbolVals) -import Hsm.Log (Log, Severity (Attention, Info, Trace), logMsg) -import Language.Haskell.Interpreter - ( GhcError (errMsg) - , InterpreterError (WontCompile) - , as - , interpret - , runInterpreter - , setImports - ) -import System.Console.Haskeline - ( defaultSettings - , getInputLine - , handleInterrupt - , withInterrupt - ) -import System.Console.Haskeline.IO - ( InputState - , cancelInput - , initializeInput - , queryInput - ) +import Hsm.Log (Log, Severity(Attention, Info, Trace), logMsg) +import Language.Haskell.Interpreter (GhcError(errMsg), InterpreterError(WontCompile), as, interpret, runInterpreter, setImports) +import System.Console.Haskeline (defaultSettings, getInputLine, handleInterrupt, withInterrupt) +import System.Console.Haskeline.IO (InputState, cancelInput, initializeInput, queryInput) data Repl (p :: Symbol) (ms :: [Symbol]) (t :: *) (a :: * -> *) (b :: *) type instance DispatchOf (Repl p ms t) = Static WithSideEffects -newtype instance StaticRep (Repl p ms t) - = Repl InputState +newtype instance StaticRep (Repl p ms t) = + Repl InputState -repl - :: forall p ms t es - . ( KnownSymbol p - , KnownSymbols ms - , Log "repl" :> es - , Repl p ms t :> es - , Show t - , Typeable t - ) +repl :: + forall p ms t es. (KnownSymbol p, KnownSymbols ms, Log "repl" :> es, Repl p ms t :> es, Show t, Typeable t) => Eff es (Maybe t) repl = query >>= maybe (return Nothing) parse where query = do Repl inputState <- getStaticRep logMsg Trace $ "Expecting a value of type: " <> show (typeRep $ Proxy @t) - unsafeEff_ - . queryInput inputState - . handleInterrupt (return Nothing) - . withInterrupt - . getInputLine $ - symbolVal (Proxy @p) + unsafeEff_ . queryInput inputState . handleInterrupt (return Nothing) . withInterrupt . getInputLine $ symbolVal (Proxy @p) parse string = do logMsg Trace $ "Parsing string: " <> string eitherValue <- @@ -87,9 +51,8 @@ repl = query >>= maybe (return Nothing) parse logMsg Attention $ show err repl -runRepl - :: forall p ms t es a - . (IOE :> es, Log "repl" :> es) +runRepl :: + forall p ms t es a. (IOE :> es, Log "repl" :> es) => Eff (Repl p ms t : es) a -> Eff es a runRepl action = bracket inputStateAlloc inputStateDealloc $ \inputState -> evalStaticRep (Repl inputState) action diff --git a/hsm-repl/Test/Repl.hs b/hsm-repl/Test/Repl.hs index 3b0e534..2d299b8 100644 --- a/hsm-repl/Test/Repl.hs +++ b/hsm-repl/Test/Repl.hs @@ -1,13 +1,8 @@ -import Control.Monad (void) import Control.Monad.Loops (whileJust_) import Data.Function ((&)) import Effectful (runEff) -import Hsm.Log (Severity (Trace), runLog) +import Hsm.Log (Severity(Trace), runLog) import Hsm.Repl (repl, runRepl) main :: IO () -main = - void (whileJust_ repl return) - & runRepl @"exec-repl λ " @'["Prelude"] @[Bool] - & runLog @"repl" Trace - & runEff +main = whileJust_ repl return & runRepl @"exec-repl λ " @'[ "Prelude"] @[Bool] & runLog @"repl" Trace & runEff |