aboutsummaryrefslogtreecommitdiff
path: root/hsm-command/Hsm/Command
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2024-12-29 17:05:34 +0000
committerPaul Oliver <contact@pauloliver.dev>2025-01-16 18:30:09 -0800
commitcc639b06c7126fac7b445d8f778455620d7f8f50 (patch)
treea4c5c7c0b0a9cdb5bea0891e198003035065e57d /hsm-command/Hsm/Command
Initial
Diffstat (limited to 'hsm-command/Hsm/Command')
-rw-r--r--hsm-command/Hsm/Command/Command.hs52
-rw-r--r--hsm-command/Hsm/Command/Readline.hs51
2 files changed, 103 insertions, 0 deletions
diff --git a/hsm-command/Hsm/Command/Command.hs b/hsm-command/Hsm/Command/Command.hs
new file mode 100644
index 0000000..3b53287
--- /dev/null
+++ b/hsm-command/Hsm/Command/Command.hs
@@ -0,0 +1,52 @@
+{-# LANGUAGE DeriveAnyClass #-}
+
+module Hsm.Command.Command
+ ( Direction(X, Z)
+ , Angle(CW, CCW)
+ , Speed(Slow, Mid, Fast)
+ , Command(Move, Rotate)
+ , commandStream
+ ) where
+
+import Data.Binary (Binary)
+import Data.Maybe (fromJust, isJust)
+import Data.Text (pack)
+import Effectful (Eff, (:>))
+import Effectful.Log (Log, logAttention_)
+import GHC.Generics (Generic)
+import Hsm.Command.Readline (Readline, readline)
+import Streamly.Data.Stream qualified as S
+import Text.Read (readEither)
+
+data Direction
+ = X
+ | Z
+ deriving (Binary, Generic, Read, Show)
+
+data Angle
+ = CW
+ | CCW
+ deriving (Binary, Generic, Read, Show)
+
+data Speed
+ = Slow
+ | Mid
+ | Fast
+ deriving (Binary, Generic, Read, Show)
+
+data Command
+ = Move Direction Speed Int
+ | Rotate Angle Speed Int
+ deriving (Binary, Generic, Read, Show)
+
+commandStream ::
+ forall es. (Log :> es, Readline :> es)
+ => S.Stream (Eff es) Command
+commandStream =
+ S.mapMaybeM (parse . fromJust) $ S.takeWhile isJust $ S.repeatM readline
+ where
+ parse :: String -> Eff es (Maybe Command)
+ parse string =
+ case readEither string of
+ Left err -> logAttention_ (pack err) >> return Nothing
+ Right command -> return $ Just command
diff --git a/hsm-command/Hsm/Command/Readline.hs b/hsm-command/Hsm/Command/Readline.hs
new file mode 100644
index 0000000..3c56453
--- /dev/null
+++ b/hsm-command/Hsm/Command/Readline.hs
@@ -0,0 +1,51 @@
+{-# LANGUAGE DataKinds #-}
+{-# LANGUAGE TypeFamilies #-}
+
+module Hsm.Command.Readline
+ ( Readline
+ , readline
+ , runReadline
+ ) where
+
+import Effectful (Dispatch(Static), DispatchOf, Eff, IOE, (:>))
+import Effectful.Dispatch.Static qualified as S
+import Effectful.Log (Log, getLoggerEnv, leLogger, waitForLogger)
+import Effectful.Resource (Resource, allocate)
+import System.Console.Haskeline qualified as H
+import System.Console.Haskeline.IO qualified as H
+
+data Readline a b
+
+type instance DispatchOf Readline = Static S.WithSideEffects
+
+newtype instance S.StaticRep Readline =
+ Readline H.InputState
+
+readline ::
+ forall es. (Log :> es, Readline :> es)
+ => Eff es (Maybe String)
+readline = do
+ flushLogger
+ Readline hdl <- S.getStaticRep
+ S.unsafeEff_ $ nextLine hdl
+ where
+ flushLogger :: Eff es ()
+ flushLogger = getLoggerEnv >>= S.unsafeEff_ . waitForLogger . leLogger
+ --
+ nextLine :: H.InputState -> IO (Maybe String)
+ nextLine hdl =
+ H.queryInput hdl
+ $ H.handleInterrupt (return Nothing)
+ $ H.withInterrupt
+ $ H.getInputLine "% "
+
+runReadline :: (IOE :> es, Resource :> es) => Eff (Readline : es) a -> Eff es a
+runReadline action = do
+ handle <- snd <$> allocate istate H.cancelInput
+ S.evalStaticRep (Readline handle) action
+ where
+ settings :: H.Settings IO
+ settings = H.defaultSettings {H.historyFile = Just ".hsm_command_history"}
+ --
+ istate :: IO H.InputState
+ istate = H.initializeInput settings