-- | Serial port library for arduino-copilot.
--
-- This module is designed to be imported qualified as Serial

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Copilot.Arduino.Library.Serial (
        baud,
        output,
        char,
        str,
        show,
        showFormatted,
        byte,
        input,
        input',
        noInput,
        ShowableType,
        FormatableType,
        Base(..),
) where

import Copilot.Arduino hiding (show)
import Copilot.Arduino.Library.Serial.Device
import Prelude ()

dev :: SerialDeviceName
dev = SerialDeviceName "Serial"

-- | Configure the baud rate of the serial port.
--
-- This must be included in your sketch if it uses the serial port.
baud :: Int -> Sketch ()
baud = baudD dev

-- | Output to the serial port.
--
-- Note that this can only be used once in a Sketch.
--
-- > main = arduino $ do
-- > 	Serial.baud 9600
-- > 	b <- readfrom pin3
-- > 	n <- readvoltage a1
-- > 	Serial.output true
-- > 		[ Serial.str "pin3:"
-- > 		, Serial.show b
-- > 		, Serial.str " a1:"
-- > 		, Serial.show n
-- > 		, Serial.char '\n'
-- > 		]
output
        :: Stream Bool
        -- ^ This Stream controls when output is sent to the serial port.
        -> [FormatOutput]
        -> Sketch ()
output = outputD dev

-- | Input from the serial port.
--
-- Reads one byte on each iteration of the sketch. When there is no
-- serial input available, reads `noInput`.
--
-- > userinput <- Serial.input
input :: Input Int8
input = inputD dev

-- | The list is used to simulate serial input when interpreting the program.
input' :: [Int8] -> Input Int8
input' = input'D dev