module Language.Atom.Common.Threshold
( boolThreshold
, doubleThreshold
) where
import Language.Atom.Expressions
import Language.Atom.Language
import Data.Int (Int32)
boolThreshold :: Name -> Int32 -> Bool -> E Bool -> Atom (E Bool)
boolThreshold name_ num init_ input = atom name_ $ do
state <- bool "state" init_
count <- int32 "count" (if init_ then num else 0)
atom "update" $ do
cond $ value count >. Const 0 &&. value count <. Const num
count <== value count + mux input (Const 1) (Const (1))
atom "low" $ do
cond $ value count ==. Const 0
state <== false
atom "high" $ do
cond $ value count ==. Const num
state <== true
return $ value state
doubleThreshold :: Name -> Double -> E Double -> Atom (E Bool)
doubleThreshold name_ lim input = atom name_ $ do
state <- bool "state" False
sum_ <- double "sum" 0
atom "update" $ do
sum_ <== value sum_ + input
atom "clear" $ do
cond $ value sum_ <=. 0
state <== false
sum_ <== 0
atom "set" $ do
cond $ value sum_ >=. Const lim
state <== true
sum_ <== Const lim
return $ value state