{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Copilot.Arduino (
arduino,
Sketch,
Pin,
Arduino,
Behavior,
TypedBehavior(..),
Event,
(@:),
Input,
input,
input',
pullup,
millis,
micros,
Output,
led,
(=:),
pwm,
delay,
ADC,
MilliSeconds(..),
MicroSeconds(..),
ClockMillis,
ClockMicros,
IsDigitalIOPin,
IsAnalogInputPin,
IsPWMPin,
blinking,
firstIteration,
frequency,
sketchSpec,
liftB,
liftB2,
whenB,
scheduleB,
ifThenElse,
IfThenElse,
Stream,
module X,
) where
import Language.Copilot as X hiding (Stream, ifThenElse, (=:))
import Language.Copilot (Stream)
import Sketch.FRP.Copilot
import Copilot.Arduino.Internals
import Copilot.Arduino.Main
import Control.Monad.Writer
import Data.Proxy
import qualified Data.Map as M
import qualified Data.Set as S
millis :: ClockMillis
millis :: ClockMillis
millis = ClockMillis
ClockMillis
micros :: ClockMicros
micros :: ClockMicros
micros = ClockMicros
ClockMicros
data ClockMillis = ClockMillis
data ClockMicros = ClockMicros
instance Input Arduino ClockMillis Word32 where
input' :: ClockMillis -> [Word32] -> GenSketch Arduino (Behavior Word32)
input' ClockMillis
ClockMillis = String -> [Word32] -> GenSketch Arduino (Behavior Word32)
inputClock String
"millis"
instance Input Arduino ClockMicros Word32 where
input' :: ClockMicros -> [Word32] -> GenSketch Arduino (Behavior Word32)
input' ClockMicros
ClockMicros = String -> [Word32] -> GenSketch Arduino (Behavior Word32)
inputClock String
"micros"
inputClock :: [Char] -> [Word32] -> Sketch (Behavior Word32)
inputClock :: String -> [Word32] -> GenSketch Arduino (Behavior Word32)
inputClock String
src [Word32]
interpretvalues = MkInputSource Arduino Word32 -> GenSketch Arduino (Behavior Word32)
forall ctx t. MkInputSource ctx t -> GenSketch ctx (Behavior t)
mkInput (MkInputSource Arduino Word32
-> GenSketch Arduino (Behavior Word32))
-> MkInputSource Arduino Word32
-> GenSketch Arduino (Behavior Word32)
forall a b. (a -> b) -> a -> b
$ InputSource
{ setupInput :: [CChunk]
setupInput = []
, defineVar :: [CChunk]
defineVar = [CLine] -> [CChunk]
mkCChunk
[String -> CLine
CLine (String -> CLine) -> String -> CLine
forall a b. (a -> b) -> a -> b
$ Proxy Word32 -> String
forall {k} (t :: k). ShowCType t => Proxy t -> String
showCType (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @Word32) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
varname String -> String -> String
forall a. Semigroup a => a -> a -> a
<>String
";"]
, inputPinmode :: Map Arduino PinMode
inputPinmode = Map Arduino PinMode
forall a. Monoid a => a
mempty
, readInput :: [CChunk]
readInput = [CLine] -> [CChunk]
mkCChunk
[String -> CLine
CLine (String -> CLine) -> String -> CLine
forall a b. (a -> b) -> a -> b
$ String
varname String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
" = " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
src String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"();"]
, inputStream :: Behavior Word32
inputStream = String -> Maybe [Word32] -> Behavior Word32
forall a. Typed a => String -> Maybe [a] -> Stream a
extern String
varname Maybe [Word32]
interpretvalues'
}
where
varname :: String
varname = String
"clock_" String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
src
interpretvalues' :: Maybe [Word32]
interpretvalues'
| [Word32] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Word32]
interpretvalues = Maybe [Word32]
forall a. Maybe a
Nothing
| Bool
otherwise = [Word32] -> Maybe [Word32]
forall a. a -> Maybe a
Just [Word32]
interpretvalues
pwm :: Behavior Word8 -> TypedBehavior 'PWM Word8
pwm :: Behavior Word8 -> TypedBehavior 'PWM Word8
pwm = Behavior Word8 -> TypedBehavior 'PWM Word8
forall {k} (p :: k) t. Behavior t -> TypedBehavior p t
TypedBehavior
led :: Pin '[ 'DigitalIO ]
led :: Pin '[ 'DigitalIO]
led = Arduino -> Pin '[ 'DigitalIO]
forall {k} (t :: k). Arduino -> Pin t
Pin (Int16 -> Arduino
Arduino Int16
13)
pullup :: IsDigitalIOPin t => Pin t -> Sketch ()
pullup :: forall (t :: [PinCapabilities]).
IsDigitalIOPin t =>
Pin t -> Sketch ()
pullup (Pin Arduino
p) = [(TriggerLimit -> WriterT [SpecItem] Identity (),
TriggerLimit -> GenFramework Arduino)]
-> Sketch ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [(\TriggerLimit
_ -> () -> WriterT [SpecItem] Identity ()
forall a. a -> WriterT [SpecItem] Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (), \TriggerLimit
_ -> GenFramework Arduino
f)]
where
f :: GenFramework Arduino
f = (forall ctx. Context ctx => GenFramework ctx
emptyFramework @Arduino)
{ pinmodes = M.singleton p (S.singleton InputPullupMode)
}