aboutsummaryrefslogtreecommitdiff
path: root/hsm-status
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2025-01-27 21:43:49 +0000
committerPaul Oliver <contact@pauloliver.dev>2025-01-29 04:13:02 +0000
commite876094f54aa3d4fc57b0ee3455ba5653facce67 (patch)
tree0d963599ece6591eca59b26e993d882a7caecf35 /hsm-status
parent194800033037f25f4c9fdae365f63f6abe0e110c (diff)
Adds C bindings to interface with libgpiod
Diffstat (limited to 'hsm-status')
-rw-r--r--hsm-status/Main.hs21
-rw-r--r--hsm-status/hsm-status.cabal2
2 files changed, 13 insertions, 10 deletions
diff --git a/hsm-status/Main.hs b/hsm-status/Main.hs
index 594661b..8d02d76 100644
--- a/hsm-status/Main.hs
+++ b/hsm-status/Main.hs
@@ -7,8 +7,9 @@ import Data.Function ((&))
import Data.Set (Set, singleton)
import Data.Text (Text, pack)
import Effectful (Eff, (:>), runEff)
+import Effectful.Concurrent (Concurrent, runConcurrent, threadDelay)
import Effectful.Log (Log, LogLevel(LogAttention), runLog)
-import Effectful.Reader.Static (Reader, ask, runReader)
+import Effectful.Reader.Static (Reader, asks, runReader)
import Effectful.Resource (runResource)
import Effectful.State.Static.Local (evalState)
import Hsm.Core.App (launch)
@@ -16,7 +17,7 @@ import Hsm.Core.Env (deriveFromYaml)
import Hsm.Core.Fsm qualified as F
import Hsm.Core.Message (body)
import Hsm.Core.Zmq.Client (poll, runClient)
-import Hsm.GPIO (GPIO, GPIOEffect, runGPIO, toggle)
+import Hsm.GPIO qualified as G
import Hsm.Status.Error (Error(Error))
import Streamly.Data.Fold qualified as S (drain)
import Streamly.Data.Stream qualified as S (Stream, fold, mapM)
@@ -24,8 +25,8 @@ import System.IO.Echo (withoutInputEcho)
data Env = Env
{ name :: Text
- , gpioOk :: GPIO
- , gpioError :: GPIO
+ , gpioOk :: G.GPIO
+ , gpioError :: G.GPIO
, period :: Word
, subEps :: [Text]
, topics :: [Text]
@@ -60,16 +61,17 @@ stateError :: F.FsmState [ByteString] Bool Env ()
stateError = F.FsmState "error" $ \errs _ _ -> result False stateError errs
handle ::
- (GPIOEffect Bool :> es, Log :> es, Reader Env :> es)
+ (Concurrent :> es, G.GPIOEffect Bool :> es, Log :> es, Reader Env :> es)
=> S.Stream (Eff es) Bool
-> Eff es ()
handle = S.fold S.drain . S.mapM handler
where
+ sleep = asks period >>= threadDelay . fromIntegral
handler sta = do
- env <- ask @Env
- toggle False sta [env.period, env.period, 0]
+ G.setPins G.active sta >> sleep
+ G.setPins G.inactive sta >> sleep
-mapper :: Env -> Bool -> Set GPIO
+mapper :: Env -> Bool -> Set G.GPIO
mapper env True = singleton env.gpioOk
mapper env False = singleton env.gpioError
@@ -80,7 +82,8 @@ main =
launch "status" withoutInputEcho $ \env logger level ->
(poll & F.fsm @_ @_ @Env @() & handle)
& runClient @Env
- & runGPIO (mapper env)
+ & G.runGPIO @Env (mapper env)
+ & runConcurrent
& evalState ()
& evalState stateOk
& runLog env.name logger level
diff --git a/hsm-status/hsm-status.cabal b/hsm-status/hsm-status.cabal
index feeff93..108ce45 100644
--- a/hsm-status/hsm-status.cabal
+++ b/hsm-status/hsm-status.cabal
@@ -22,7 +22,7 @@ executable status
, bytestring
, containers
, echo
- , effectful-core
+ , effectful
, hsm-core
, hsm-gpio
, log-effectful