module Data.GCode.Generate where
import Data.GCode.Types
import Data.GCode.RS274
import Data.GCode.Utils
g :: Code
g = cls G emptyCode
m :: Code
m = cls M emptyCode
s :: Code
s = emptyCode & cls SStandalone
(<#>) :: Code -> Int -> Code
(<#>) a n = num n a
feed :: Double -> Code -> Code
feed = param F
x :: Double -> Code -> Code
x = axis X
y :: Double -> Code -> Code
y = axis Y
z :: Double -> Code -> Code
z = axis Z
xy :: Double -> Double -> Code -> Code
xy xVal yVal = x xVal . y yVal
xyz :: Double -> Double -> Double -> Code -> Code
xyz xVal yVal zVal = x xVal . y yVal . z zVal
movexy :: Double -> Double -> Code
movexy xVal yVal = move & xy xVal yVal
ij :: Double -> Double -> Code -> Code
ij iVal jVal = param I iVal . param J jVal
arc :: Code
arc = arcCW
rectangle :: (Num a, Num b) => a -> b -> [(a, b)]
rectangle xv yv = [(0, 0), (xv, 0), (xv, yv), (0, yv), (0,0)]
rot :: Floating b => b -> b -> b -> (b, b)
rot by xv yv = (xv * (cos by) - yv * (sin by), yv * (cos by) + xv * (sin by))
circle :: (Floating b, Enum b) => b -> b -> [(b, b)]
circle r steps = map (\step -> rot (step * 2*pi / steps) (r/2) 0) [1..steps]
circle' :: (Floating b, Enum b) => b -> b -> b -> [(b, b)]
circle' rin r steps = map (\step -> rot (rin + step * 2*pi / steps) (r/2) 0) [1..steps]
closedCircle :: (Floating a, Enum a) => a -> a -> [(a, a)]
closedCircle r steps = map (\step -> rot (step * 2*pi / steps) (r/2) 0) [1..(steps+1)]
travelCat :: Code -> Code -> [GCode] -> [Code]
travelCat up down (block:rest) = (travel up down block) ++ (travelCat up down rest)
travelCat _ _ [] = []
travelCatDrill :: Code -> [GCode] -> [Code]
travelCatDrill up (block:rest) = (travelDrills up block) ++ (travelCatDrill up rest)
travelCatDrill _ [] = []
travel :: Code -> Code -> GCode -> GCode
travel up down (c:rest) = [up, asRapidXY c, down, c] ++ rest
travel _ _ [] = []
travelDrills :: Code -> GCode -> GCode
travelDrills up block = travel up emptyCode block
asRapidXY :: Code -> Code
asRapidXY c@Code{} =
case getAxes [X,Y] c of
[Just xv, Just yv] -> rapid & xy xv yv
_ -> c
asRapidXY c = c