From f7f11acafe0a404fa218c13832e32fce574ae0f6 Mon Sep 17 00:00:00 2001 From: Paul Oliver Date: Fri, 2 Jan 2026 11:37:35 +0000 Subject: Adds battery monitoring to frontend --- hsm-ina226/Hsm/INA226.hs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'hsm-ina226/Hsm/INA226.hs') diff --git a/hsm-ina226/Hsm/INA226.hs b/hsm-ina226/Hsm/INA226.hs index 8862689..ed9cc23 100644 --- a/hsm-ina226/Hsm/INA226.hs +++ b/hsm-ina226/Hsm/INA226.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE TypeFamilies #-} @@ -6,14 +7,17 @@ module Hsm.INA226 , INA226Reading (..) , INA226Path , INA226Addr + , I2CINA226 , readINA226State , runINA226 ) where +import Data.Aeson (ToJSON) import Data.Word (Word16, Word8) import Effectful (Dispatch (Static), DispatchOf, Eff, IOE, (:>)) import Effectful.Dispatch.Static (SideEffects (WithSideEffects), StaticRep, evalStaticRep) +import GHC.Generics (Generic) import Hsm.Core.Show (showHex) import Hsm.I2C (I2C, readInt16, readWord16, writeWord16) import Hsm.Log (Logs, Severity (Info, Trace), logMsg) @@ -30,13 +34,15 @@ data INA226Reading = INA226Reading , current :: Float , power :: Float } - deriving Show + deriving (Generic, ToJSON, Show) -- INA226 I2C device path and address type INA226Path = "/dev/i2c-0" type INA226Addr = 64 +type I2CINA226 = I2C INA226Path INA226Addr + -- INA226 registers configurationReg :: Word8 configurationReg = 0x00 @@ -89,16 +95,15 @@ busVoltageLSB :: Float busVoltageLSB = 0.00125 -- Read INA226 voltage/current/power registers -readINA226State :: (I2C INA226Path INA226Addr :> es, INA226 :> es, Logs '["i2c", "ina226"] es) => Eff es INA226Reading +readINA226State :: (I2CINA226 :> es, INA226 :> es, Logs '["i2c", "ina226"] es) => Eff es INA226Reading readINA226State = do logMsg @"ina226" Trace "Reading INA226 state registers" - voltage <- (* busVoltageLSB) . fromIntegral <$> readWord16 busVoltageReg - current <- (* currentLSB) . fromIntegral <$> readInt16 currentReg - power <- (* powerLSB) . fromIntegral <$> readInt16 powerReg + voltage <- (* busVoltageLSB) . abs . fromIntegral <$> readWord16 busVoltageReg + current <- (* currentLSB) . abs . fromIntegral <$> readInt16 currentReg + power <- (* powerLSB) . abs . fromIntegral <$> readInt16 powerReg return INA226Reading{..} -runINA226 - :: (I2C INA226Path INA226Addr :> es, IOE :> es, Logs '["i2c", "ina226"] es) => Eff (INA226 : es) a -> Eff es a +runINA226 :: (I2CINA226 :> es, IOE :> es, Logs '["i2c", "ina226"] es) => Eff (INA226 : es) a -> Eff es a runINA226 action = do -- Prepare chip writeWord16 configurationReg reset -- cgit v1.2.1