-- | 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 Arduino RandomSeed (Event () (Stream Word8)) where
	RandomSeed
RandomSeed =: :: RandomSeed -> Event () (Stream Word8) -> GenSketch Arduino ()
=: Event () (Stream Word8)
e = Event () (Stream Word8) -> GenSketch Arduino ()
forall a p. Typed a => Event p (Stream a) -> GenSketch Arduino ()
randomSeedWith Event () (Stream Word8)
e

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

randomSeedWith :: Typed a => Event p (Stream a) -> Sketch ()
randomSeedWith :: forall a p. Typed a => Event p (Stream a) -> GenSketch Arduino ()
randomSeedWith (Event Stream a
n Stream Bool
c) = do
		(f, triggername) <- String
-> GenFramework Arduino
-> GenSketch Arduino (GenFramework Arduino, String)
forall ctx.
String
-> GenFramework ctx -> GenSketch ctx (GenFramework ctx, String)
defineTriggerAlias String
"randomSeed" GenFramework Arduino
forall a. Monoid a => a
mempty
		tell [(go triggername, \TriggerLimit
_ -> GenFramework Arduino
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 :: forall (t :: [PinCapabilities]).
IsAnalogInputPin t =>
Pin t -> GenSketch Arduino ()
randomSeedPin Pin t
p = do
	seed <- Pin t -> GenSketch Arduino (Stream ADC)
forall ctx o t. Input ctx o t => o -> GenSketch ctx (Behavior t)
input Pin t
p :: Sketch (Behavior ADC)
	randomSeed =: 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 Arduino RandomInput Word32 where
	input' :: RandomInput -> [Word32] -> GenSketch Arduino (Behavior Word32)
input' (RandomInput Word32
lo Word32
hi) [Word32]
interpretvalues = do
		i <- String -> GenSketch Arduino UniqueId
forall ctx. String -> GenSketch ctx UniqueId
getUniqueId String
"random"
		let varname = String -> UniqueId -> String
uniqueName String
"randomval" UniqueId
i
		let word32 = 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)
		mkInput $ InputSource
			{ setupInput = []
			, defineVar = mkCChunk
				[ CLine $ word32 <> " " <> varname <> ";"
				]
			, inputPinmode = mempty
			, readInput = mkCChunk
				[ CLine $ varname <> " = random"
					<> "("
						<> Prelude.show lo
						<> ", " 
						<> Prelude.show hi
					<> ");"]
			, inputStream = extern varname interpretvalues'
			}
  	  where
		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