summaryrefslogtreecommitdiff
path: root/Reactive/Banana/Automation.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Reactive/Banana/Automation.hs')
-rw-r--r--Reactive/Banana/Automation.hs25
1 files changed, 20 insertions, 5 deletions
diff --git a/Reactive/Banana/Automation.hs b/Reactive/Banana/Automation.hs
index fbc58eb..24e02b5 100644
--- a/Reactive/Banana/Automation.hs
+++ b/Reactive/Banana/Automation.hs
@@ -34,7 +34,9 @@ module Reactive.Banana.Automation (
(=:),
sensorUnavailable,
sensedEventBehavior,
+ -- * Combinators
automationStepper,
+ automationChanges,
-- * Time
Timestamped(..),
Timestamp(..),
@@ -48,6 +50,7 @@ module Reactive.Banana.Automation (
-- * Actuators
PowerChange(..),
actuateEvent,
+ actuateFutureEvent,
actuateBehavior,
actuateBehaviorMaybe,
-- * Ranges
@@ -254,6 +257,10 @@ sensedEventBehavior = automationStepper SensorUnavailable
automationStepper :: a -> Event a -> Automation sensors actuators (Behavior a)
automationStepper a e = Automation $ lift $ stepper a e
+-- | `changes` lifted into `Automation`
+automationChanges :: Behavior a -> Automation sensors actuators (Event (Future a))
+automationChanges = Automation . lift . changes
+
-- | Call when a sensor has sensed a value.
--
-- > getFridgeTemperature >>= sensed (fridgeTemperature sensors)
@@ -375,7 +382,7 @@ clockSignalBehavior getsensor = Automation $ do
-- | For controlling relays and other things that can have
-- their power turned on and off.
data PowerChange = PowerOff | PowerOn
- deriving (Show)
+ deriving (Show, Eq, Ord)
-- | Makes an Event drive an actuator.
actuateEvent :: Event a -> (a -> actuators) -> Automation sensors actuators ()
@@ -383,15 +390,23 @@ actuateEvent e getactuator = Automation $ do
actuators <- snd <$> ask
lift $ reactimate $ fmap (actuators . getactuator) e
+-- | Like `actuateEvent` but with a Future, as produced by
+-- `automationChanges`
+actuateFutureEvent :: Event (Future a) -> (a -> actuators) -> Automation sensors actuators ()
+actuateFutureEvent e getactuator = Automation $ actuateFutureEvent' e getactuator
+
+actuateFutureEvent' :: Event (Future a) -> (a -> actuators) -> ReaderT (sensors, actuators -> IO ()) MomentIO ()
+actuateFutureEvent' e getactuator = do
+ actuators <- snd <$> ask
+ lift $ reactimate' $ fmap (actuators . getactuator) <$> e
+
-- | Makes a Behavior drive an actuator. This will happen when the
-- Behavior's value changes, but possibly more often as well, depending on
-- how the Behavior is constructed.
actuateBehavior :: Behavior a -> (a -> actuators) -> Automation sensors actuators ()
actuateBehavior b getactuator = Automation $ do
- actuators <- snd <$> ask
- c <- lift $ changes b
- lift $ reactimate' $
- fmap (actuators . getactuator) <$> c
+ e <- lift $ changes b
+ actuateFutureEvent' e getactuator
-- | Variant of `actuateBehavior` that does nothing when a behavior
-- is Nothing.