-- | Random value library for arduino-copilot.
--
-- Here's an example that flashes the LED randomly.
--
-- > main :: IO ()
-- > main = arduino $ do
-- >	if firstIteration
-- > 		then randomSeedPin a0
-- > 		else do
-- > 			n <- input (random 10) :: Sketch (Behavior Word32)
-- > 			led =: (n >= 5)
--
-- The use of `firstIteration` makes the RNG be seeded in the first
-- iteration, and then random numbers are generated in subsequent
-- iterations. That's a typical pattern when using this module.

{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RebindableSyntax #-}
{-# LANGUAGE TypeApplications #-}

module Copilot.Arduino.Library.Random (
	randomSeed,
	randomSeedPin,
	RandomSeed,
	RandomInput,
	random,
	randomR,
) where

import Copilot.Arduino hiding (show)
import Copilot.Arduino.Internals
import Control.Monad.Writer
import Data.Proxy
import Prelude ()
import qualified Prelude

-- | Use this to seed the RNG.
--
-- > randomSeed =: constant (1 :: Word8)
randomSeed :: RandomSeed
randomSeed :: RandomSeed
randomSeed = RandomSeed
RandomSeed

data RandomSeed = RandomSeed

instance Output RandomSeed (Event () (Stream Word8)) where
	RandomSeed
RandomSeed =: :: RandomSeed -> Event () (Stream Word8) -> Sketch ()
=: Event () (Stream Word8)
e = Event () (Stream Word8) -> Sketch ()
forall a p. Typed a => Event p (Stream a) -> Sketch ()
randomSeedWith Event () (Stream Word8)
e

-- Seeds with the first 8 bits read from the ADC, discarding the rest.
instance Output RandomSeed (Event () (Stream ADC)) where
	RandomSeed
RandomSeed =: :: RandomSeed -> Event () (Stream ADC) -> Sketch ()
=: Event () (Stream ADC)
e = Event () (Stream ADC) -> Sketch ()
forall a p. Typed a => Event p (Stream a) -> Sketch ()
randomSeedWith Event () (Stream ADC)
e

randomSeedWith :: Typed a => Event p (Stream a) -> Sketch ()
randomSeedWith :: Event p (Stream a) -> Sketch ()
randomSeedWith (Event Stream a
n Stream Bool
c) = do
		(Framework
f, String
triggername) <- String -> Framework -> Sketch (Framework, String)
defineTriggerAlias String
"randomSeed" Framework
forall a. Monoid a => a
mempty
		[(TriggerLimit -> Spec, TriggerLimit -> Framework)] -> Sketch ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell [(String -> TriggerLimit -> Spec
go String
triggername, \TriggerLimit
_ -> Framework
f)]
	  where
		go :: String -> TriggerLimit -> Spec
go String
triggername TriggerLimit
tl = 
			let c' :: Stream Bool
c' = TriggerLimit -> Stream Bool -> Stream Bool
addTriggerLimit TriggerLimit
tl Stream Bool
c
			in String -> Stream Bool -> [Arg] -> Spec
trigger String
triggername Stream Bool
c' [Stream a -> Arg
forall a. Typed a => Stream a -> Arg
arg Stream a
n]

-- | Seed the RNG by reading from an analog input pin.
--
-- If the pin is left unconnected, noise will be read from it.
randomSeedPin :: IsAnalogInputPin t => Pin t -> Sketch ()
randomSeedPin :: Pin t -> Sketch ()
randomSeedPin Pin t
p = do
	Stream ADC
seed <- Pin t -> Sketch (Stream ADC)
forall o t. Input o t => o -> Sketch (Behavior t)
input Pin t
p :: Sketch (Behavior ADC)
	RandomSeed
randomSeed RandomSeed -> Stream ADC -> Sketch ()
forall o t. Output o t => o -> t -> Sketch ()
=: Stream ADC
seed

data RandomInput = RandomInput Word32 Word32

-- | Generate a random number, up to but exclusive of an upper bound.
--
-- > n <- input (random 10)
random :: Word32 -> RandomInput
random :: Word32 -> RandomInput
random Word32
hi = Word32 -> Word32 -> RandomInput
RandomInput Word32
0 Word32
hi

-- | Generate a random number in the range (lo, hi). The number will be 
-- @>= lo@ and @< hi@
--
-- > n <- input (randomR 5 10) :: Sketch (Behavior Word32)
randomR :: (Word32, Word32) -> RandomInput
randomR :: (Word32, Word32) -> RandomInput
randomR (Word32
lo, Word32
hi) = Word32 -> Word32 -> RandomInput
RandomInput Word32
lo Word32
hi

instance Input RandomInput Word32 where
	input' :: RandomInput -> [Word32] -> Sketch (Behavior Word32)
input' (RandomInput Word32
lo Word32
hi) [Word32]
interpretvalues = do
		UniqueId
i <- String -> Sketch UniqueId
getUniqueId String
"random"
		let varname :: String
varname = String -> UniqueId -> String
uniqueName String
"randomval" UniqueId
i
		let word32 :: String
word32 = Proxy Word32 -> String
forall k (t :: k). ShowCType t => Proxy t -> String
showCType (Proxy Word32
forall k (t :: k). Proxy t
Proxy @Word32)
		InputSource Word32 -> Sketch (Behavior Word32)
forall t. InputSource t -> Sketch (Behavior t)
mkInput (InputSource Word32 -> Sketch (Behavior Word32))
-> InputSource Word32 -> Sketch (Behavior Word32)
forall a b. (a -> b) -> a -> b
$ InputSource :: forall t.
[CChunk]
-> [CChunk]
-> Map PinId PinMode
-> [CChunk]
-> Stream t
-> InputSource t
InputSource
			{ setupInput :: [CChunk]
setupInput = []
			, defineVar :: [CChunk]
defineVar = [CLine] -> [CChunk]
mkCChunk
				[ String -> CLine
CLine (String -> CLine) -> String -> CLine
forall a b. (a -> b) -> a -> b
$ String
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 PinId PinMode
inputPinmode = Map PinId 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
" = random"
					String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
"("
						String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Word32 -> String
forall a. Show a => a -> String
Prelude.show Word32
lo
						String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", " 
						String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Word32 -> String
forall a. Show a => a -> String
Prelude.show Word32
hi
					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
		interpretvalues' :: Maybe [Word32]
interpretvalues'
			| [Word32] -> 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