
{-# LANGUAGE Trustworthy #-}

module Pipes.Serial
    ( -- * Serial Ports
      fromSerial
    , toSerial

      -- * Re-exports
      -- $reexports
    , module System.Hardware.Serialport
    ) where

import Control.Monad.IO.Class
import Control.Monad (void)
import qualified Data.ByteString                as BS
import qualified Data.ByteString.Lazy.Internal  as I
import System.Hardware.Serialport
import Pipes


fromSerial :: MonadIO m => SerialPort -> Producer' BS.ByteString m r
fromSerial h = liftIO (recv h I.defaultChunkSize) >~ cat
{-# INLINEABLE fromSerial #-}

{-# RULES
    "fromSerial h >-> p" forall h p .
        fromSerial h >-> p = liftIO (recv h I.defaultChunkSize) >~ p
  #-}

toSerial :: MonadIO m => SerialPort -> Consumer' BS.ByteString m r
toSerial h = for cat $ \bs -> liftIO (void $ send h bs)
{-# INLINEABLE toSerial #-}

{-# RULES
    "p >-> toSerial" forall p h .
        p >-> toSerial h = for p (\bs -> liftIO (void $ send h bs))
  #-}


-- $reexports

