aboutsummaryrefslogtreecommitdiff
path: root/hsm-ina226/Hsm
diff options
context:
space:
mode:
authorPaul Oliver <contact@pauloliver.dev>2026-01-02 11:37:35 +0000
committerPaul Oliver <contact@pauloliver.dev>2026-01-02 11:57:54 +0000
commitf7f11acafe0a404fa218c13832e32fce574ae0f6 (patch)
treef3a531050b4be2d708eb6aac53ce4474f798886b /hsm-ina226/Hsm
parent43af089b3588b6fd29a1b09be9874054e0291c5e (diff)
Adds battery monitoring to frontend
Diffstat (limited to 'hsm-ina226/Hsm')
-rw-r--r--hsm-ina226/Hsm/INA226.hs19
1 files changed, 12 insertions, 7 deletions
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