1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
{-# 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 :: (Log :> es, Readline :> es) => S.Stream (Eff es) Command
commandStream =
S.mapMaybeM (parse . fromJust) $ S.takeWhile isJust $ S.repeatM readline
where
parse string =
case readEither string of
Left err -> logAttention_ (pack err) >> return Nothing
Right command -> return $ Just command
|