{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Data.GCode.Types (
Class(..)
, AxisDesignator(..)
, ParamDesignator(..)
, allClasses
, allAxisDesignators
, zeroAxes
, allParamDesignators
, asChars
, Axes
, Params
, Limits
, ParamLimits
, Code(..)
, GCode
, toCodeClass
, toAxis
, toParam
, (&)
, cls
, axis
, param
, num
, sub
, axes
, params
, comment
, emptyCode
, defaultPrec
, Style(..)
, defaultStyle
) where
import Data.ByteString (ByteString)
import Data.Map (Map)
import qualified Data.Char
import qualified Data.Map
data Class =
G
| M
| T
| PStandalone
| FStandalone
| SStandalone
deriving (Show, Enum, Eq, Ord)
allClasses :: [Class]
allClasses = [G, M, T, PStandalone, FStandalone, SStandalone]
data AxisDesignator =
X
| Y
| Z
| A
| B
| C
| U
| V
| W
| E
| L
deriving (Show, Enum, Eq, Ord)
allAxisDesignators :: [AxisDesignator]
allAxisDesignators = [X, Y, Z, A, B, C, U, V, W, E, L]
zeroAxes :: Axes
zeroAxes = Data.Map.fromList $ map (\a -> (a, 0)) allAxisDesignators
data ParamDesignator =
S
| P
| F
| H
| R
| I
| J
| K
deriving (Show, Enum, Eq, Ord)
allParamDesignators :: [ParamDesignator]
allParamDesignators = [S, P, F, R, I, J, K]
asChars :: Show a => [a] -> [Char]
asChars types = map ((!! 0) . show) types
fromChar :: Show a => Char -> [a] -> Maybe a
fromChar c types = Data.Map.lookup (Data.Char.toUpper c)
$ Data.Map.fromList (zip (asChars types) types)
toCodeClass :: Char -> Maybe Class
toCodeClass c = fromChar c allClasses
toAxis :: Char -> Maybe AxisDesignator
toAxis c = fromChar c allAxisDesignators
toParam :: Char -> Maybe ParamDesignator
toParam c = fromChar c allParamDesignators
type Axes = Map AxisDesignator Double
type Limits = Map AxisDesignator (Double, Double)
type Params = Map ParamDesignator Double
type ParamLimits = Map ParamDesignator (Double, Double)
type GCode = [Code]
data Code =
Code {
codeCls :: Maybe Class
, codeNum :: Maybe Int
, codeSub :: Maybe Int
, codeAxes :: Axes
, codeParams :: Params
, codeComment :: ByteString
}
| Comment ByteString
| Empty
| Other ByteString
deriving (Show, Eq, Ord)
cls :: Class -> Code -> Code
cls x c = c { codeCls = Just x}
num :: Int -> Code -> Code
num x c = c { codeNum = Just x}
sub :: Int -> Code -> Code
sub x c = c { codeSub = Just x}
axes :: Axes -> Code -> Code
axes x c = c { codeAxes = x}
axis :: AxisDesignator -> Double -> Code -> Code
axis des val c = c { codeAxes = Data.Map.insert des val $ codeAxes c }
params :: Params -> Code -> Code
params x c = c { codeParams = x}
param :: ParamDesignator -> Double -> Code -> Code
param des val c = c { codeParams = Data.Map.insert des val $ codeParams c }
comment :: ByteString -> Code -> Code
comment x c = c { codeComment = x}
(&) :: a -> (a -> c) -> c
(&) = flip ($)
emptyCode :: Code
emptyCode = Code {
codeCls = Nothing
, codeNum = Nothing
, codeSub = Nothing
, codeAxes = mempty
, codeParams = mempty
, codeComment = mempty
}
data Style =
Style {
stylePrecision :: Int
, styleColorful :: Bool
} deriving (Show)
defaultPrec :: Int
defaultPrec = 6
defaultStyle :: Style
defaultStyle = Style defaultPrec False