Skip to content
Snippets Groups Projects
Verified Commit e52b8935 authored by Legy (Beini)'s avatar Legy (Beini)
Browse files

Fix battery charging time estimate becoming negative in rare cases

parent 22c9d47d
No related branches found
No related tags found
1 merge request!4Resolve "Battery block shows negative time estimate"
......@@ -158,8 +158,8 @@ batteryIsDischarging = any (singleBatteryIsDischarging . _status)
batteryEstimate :: [BatteryState] -> Maybe Int
batteryEstimate batteryStates
| batteryPowerNow == 0 = Nothing
| isCharging, not isDischarging = Just $ div ((batteryEnergyFull - batteryEnergyNow) * 3600) batteryPowerNow
| isDischarging, not isCharging = Just $ div (batteryEnergyNow * 3600) batteryPowerNow
| isCharging, not isDischarging = ensure (>0) batteryEstimateCharging
| isDischarging, not isCharging = ensure (>0) batteryEstimateDischarging
| otherwise = Nothing
where
isCharging :: Bool
......@@ -176,3 +176,9 @@ batteryEstimate batteryStates
batteryEnergyFull :: Int
batteryEnergyFull = sum . map _energyFull $ batteryStates
batteryEstimateCharging :: Int
batteryEstimateCharging = div ((batteryEnergyFull - batteryEnergyNow) * 3600) batteryPowerNow
batteryEstimateDischarging :: Int
batteryEstimateDischarging = div (batteryEnergyNow * 3600) batteryPowerNow
......@@ -14,6 +14,11 @@ formatFloatN = formatFloatN' 0
formatFloatN' :: RealFloat a => Int -> Int -> a -> T.Text
formatFloatN' padWithZeros decimalPlaces f = T.justifyRight (fromIntegral padWithZeros) '0' . T.pack $ showFFloat (Just decimalPlaces) f ""
ensure :: (a -> Bool) -> a -> Maybe a
ensure f a
| f a = Just a
| otherwise = Nothing
tryMaybe :: IO a -> IO (Maybe a)
tryMaybe a = tryMaybe' (Just <$> a)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment