From cc639b06c7126fac7b445d8f778455620d7f8f50 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Sun, 29 Dec 2024 17:05:34 +0000 Subject: Initial --- hsm-command/Hsm/Command/Command.hs | 52 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 hsm-command/Hsm/Command/Command.hs (limited to 'hsm-command/Hsm/Command/Command.hs') 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 -- cgit v1.2.1