From c27c55fad9cd1fbf0d27ebcd919b78f59c4cbbb0 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Mon, 12 Aug 2024 14:11:40 +0200 Subject: feat: critical battery blinks --- app/Sensor.hs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'app/Sensor.hs') diff --git a/app/Sensor.hs b/app/Sensor.hs index 0c05519..9ef38d0 100644 --- a/app/Sensor.hs +++ b/app/Sensor.hs @@ -207,17 +207,27 @@ instance (S.MonadSensor m) => S.Aggregate m BatStat Float where <*> (readFile (fp "energy_full") (pure . read)) ] -bat :: (S.MonadSensor m) => S.Sensor m () String -bat = do - value <- S.sensor BatStat - return - ( if - | 0.8 < value -> "\xf240" - | 0.6 < value -> "\xf241" - | 0.4 < value -> "\xf242" - | 0.2 < value -> "\xf243" - | otherwise -> "\xf244" - ) +bat :: (S.MonadSensor m) => S.Sensor m () P.Doc +bat = + S.sensor BatStat >>= \value -> + if + | value >= 0.8 -> pure (P.pretty "\xf240 ") + | value >= 0.6 -> pure (P.pretty "\xf241 ") + | value >= 0.4 -> pure (P.pretty "\xf242 ") + | value >= 0.2 -> pure (P.pretty "\xf243 ") + | otherwise -> + (\b -> (if b then id else (P.color P.Red)) (P.pretty "\xf244 ")) + <$> blink + +data Blink = Blink deriving (Show) + +blink :: (S.MonadSensor m) => S.Sensor m () Bool +blink = S.sensor Blink + +instance (S.MonadSensor m) => S.Aggregate m Blink Bool where + aggregate _ = loop True + where + loop b = S.yield b >> sleep >> loop (not b) choice :: a -> [IO a] -> IO a choice def [] = pure def -- cgit v1.2.3