on master help sign in
Tweaks and doc cleanups
c44307fTom McLaughlin
auto-update/Control/Debounce.hs+4−15
@@ -11,7 +11,7 @@
11-- printString <- 'mkDebounce' 'defaultDebounceSettings'
11-- printString <- 'mkDebounce' 'defaultDebounceSettings'
12-- { 'debounceAction' = putStrLn "Running action"
12-- { 'debounceAction' = putStrLn "Running action"
13-- , 'debounceFreq' = 5000000 -- 5 seconds
13-- , 'debounceFreq' = 5000000 -- 5 seconds
14−-- , 'debounceEdge' = 'trailingEdge' -- Trigger on the trailing edge
14+-- , 'debounceEdge' = 'DI.trailingEdge' -- Trigger on the trailing edge
15-- }
15-- }
16-- @
16-- @
17--
17--
@@ -32,8 +32,8 @@ module Control.Debounce
32 , DI.debounceFreq
32 , DI.debounceFreq
33 , DI.debounceAction
33 , DI.debounceAction
34 , DI.debounceEdge
34 , DI.debounceEdge
35− , leadingEdge
35+ , DI.leadingEdge
36− , trailingEdge
36+ , DI.trailingEdge
37 -- * Creation
37 -- * Creation
38 , mkDebounce
38 , mkDebounce
39 ) where
39 ) where
@@ -48,7 +48,7 @@ defaultDebounceSettings :: DI.DebounceSettings
48defaultDebounceSettings = DI.DebounceSettings
48defaultDebounceSettings = DI.DebounceSettings
49 { DI.debounceFreq = 1000000
49 { DI.debounceFreq = 1000000
50 , DI.debounceAction = return ()
50 , DI.debounceAction = return ()
51− , DI.debounceEdge = leadingEdge
51+ , DI.debounceEdge = DI.leadingEdge
52 }
52 }
53
53
54-- | Generate an action which will trigger the debounced action to be performed.
54-- | Generate an action which will trigger the debounced action to be performed.
@@ -58,14 +58,3 @@ mkDebounce :: DI.DebounceSettings -> IO (IO ())
58mkDebounce settings = do
58mkDebounce settings = do
59 baton <- newEmptyMVar
59 baton <- newEmptyMVar
60 DI.mkDebounceInternal baton threadDelay settings
60 DI.mkDebounceInternal baton threadDelay settings
61−
62−-- | Perform the action immediately, and then begin a cooldown period.
63−-- If the trigger happens again during the cooldown, wait until the end of the cooldown
64−-- and then perform the action again, then enter a new cooldown period.
65−leadingEdge :: DI.DebounceEdge
66−leadingEdge = DI.Leading
67−
68−-- | Start a cooldown period and perform the action when the period ends. If another trigger
69−-- happens during the cooldown, it has no effect.
70−trailingEdge :: DI.DebounceEdge
71−trailingEdge = DI.Trailing
auto-update/Control/Debounce/Internal.hs+20−3
@@ -4,14 +4,15 @@
4module Control.Debounce.Internal (
4module Control.Debounce.Internal (
5 DebounceSettings(..)
5 DebounceSettings(..)
6 , DebounceEdge(..)
6 , DebounceEdge(..)
7+ , leadingEdge
8+ , trailingEdge
7 , mkDebounceInternal
9 , mkDebounceInternal
8 ) where
10 ) where
9
11
10import Control.Concurrent (forkIO)
12import Control.Concurrent (forkIO)
11import Control.Concurrent.MVar (takeMVar, tryPutMVar, tryTakeMVar, MVar)
13import Control.Concurrent.MVar (takeMVar, tryPutMVar, tryTakeMVar, MVar)
12import Control.Exception (SomeException, handle, mask_)
14import Control.Exception (SomeException, handle, mask_)
13−import Control.Monad (forever, void, when)
15+import Control.Monad (forever, void)
14−import Data.Maybe (isJust)
15
16
16-- | Settings to control how debouncing should work.
17-- | Settings to control how debouncing should work.
17--
18--
@@ -42,7 +43,7 @@ data DebounceSettings = DebounceSettings
42 -- ^ Whether to perform the action on the leading edge or trailing edge of
43 -- ^ Whether to perform the action on the leading edge or trailing edge of
43 -- the timeout.
44 -- the timeout.
44 --
45 --
45− -- Default: trailingEdge.
46+ -- Default: 'trailingEdge'.
46 --
47 --
47 -- @since 0.1.6
48 -- @since 0.1.6
48 }
49 }
@@ -61,6 +62,22 @@ data DebounceEdge =
61 -- happens during the cooldown, it has no effect.
62 -- happens during the cooldown, it has no effect.
62 deriving (Show, Eq)
63 deriving (Show, Eq)
63
64
65+
66+-- | Perform the action immediately, and then begin a cooldown period.
67+-- If the trigger happens again during the cooldown, wait until the end of the cooldown
68+-- and then perform the action again, then enter a new cooldown period.
69+--
70+-- @since 0.1.6
71+leadingEdge :: DebounceEdge
72+leadingEdge = Leading
73+
74+-- | Start a cooldown period and perform the action when the period ends. If another trigger
75+-- happens during the cooldown, it has no effect.
76+--
77+-- @since 0.1.6
78+trailingEdge :: DebounceEdge
79+trailingEdge = Trailing
80+
64mkDebounceInternal :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
81mkDebounceInternal :: MVar () -> (Int -> IO ()) -> DebounceSettings -> IO (IO ())
65mkDebounceInternal baton delayFn (DebounceSettings freq action edge) = do
82mkDebounceInternal baton delayFn (DebounceSettings freq action edge) = do
66 mask_ $ void $ forkIO $ forever $ do
83 mask_ $ void $ forkIO $ forever $ do