diff options
author | Paul Oliver <contact@pauloliver.dev> | 2025-08-31 21:45:59 +0000 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-09-02 21:10:34 +0000 |
commit | 13654ca1df8bd4b6de6b7867e66dedc5f9f9780d (patch) | |
tree | ea7b29c256971ad05f74d1f021d58ff619848171 | |
parent | 765794c37ca7130e23dc460d7e507d43c8df2470 (diff) |
Polishes `hsm-cam` even more
-rw-r--r-- | hsm-cam/FFI/Cam.cpp | 2 | ||||
-rw-r--r-- | hsm-cam/Hsm/Cam.hs | 33 | ||||
-rw-r--r-- | hsm-cam/hsm-cam.cabal | 2 |
3 files changed, 25 insertions, 12 deletions
diff --git a/hsm-cam/FFI/Cam.cpp b/hsm-cam/FFI/Cam.cpp index bfdecf6..4c21e7f 100644 --- a/hsm-cam/FFI/Cam.cpp +++ b/hsm-cam/FFI/Cam.cpp @@ -83,7 +83,7 @@ allocate_frame_buffer() g_config = g_camera->generateConfiguration({ StreamRole::StillCapture }); g_config->at(0).size.width = FRAME_WIDTH; g_config->at(0).size.height = FRAME_HEIGHT; - g_config->at(0).pixelFormat = formats::RGB888; + g_config->at(0).pixelFormat = formats::BGR888; logMsg(Info, "Generated camera configuration: {}", g_config->at(0).toString()); g_camera->configure(g_config.get()); diff --git a/hsm-cam/Hsm/Cam.hs b/hsm-cam/Hsm/Cam.hs index fe17057..e5b30c2 100644 --- a/hsm-cam/Hsm/Cam.hs +++ b/hsm-cam/Hsm/Cam.hs @@ -12,8 +12,8 @@ import Codec.Picture (Image(Image), encodePng) import Codec.Picture.Types (PixelRGB8) import Control.Concurrent (MVar, forkIO, newEmptyMVar, putMVar, takeMVar) import Control.Exception (mask_) -import Control.Monad (forever) -import Control.Monad.Loops (whileM_) +import Control.Monad.Extra (whenM) +import Control.Monad.Loops (iterateM_) import Data.Bits ((.|.)) import Data.ByteString.Lazy (ByteString) import Data.List ((!?)) @@ -106,14 +106,18 @@ data LibCameraSeverity deriving (Read, Show) toLibCameraSeverity :: Severity -> LibCameraSeverity -toLibCameraSeverity Trace = DEBUG -toLibCameraSeverity Info = INFO -toLibCameraSeverity Attention = WARN +toLibCameraSeverity = + \case + Trace -> DEBUG + Info -> INFO + Attention -> WARN fromLibCameraSeverity :: LibCameraSeverity -> Severity -fromLibCameraSeverity DEBUG = Trace -fromLibCameraSeverity INFO = Info -fromLibCameraSeverity _ = Attention +fromLibCameraSeverity = + \case + DEBUG -> Trace + INFO -> Info + _ -> Attention runCam :: (IOE :> es, Log "cam" :> es, Log "libcamera" :> es) => Eff (Cam : es) a -> Eff es a runCam action = do @@ -152,11 +156,10 @@ runCam action = do -- A dedicated thread reads from the FIFO, parses log severity levels, and -- forwards messages to the application's logger with proper level mapping. logCaptureFifo = "/tmp/hsm-cam-libcamera.fifo" - logCaptureClear = liftIO . whileM_ (doesFileExist logCaptureFifo) $ removeFile logCaptureFifo + logCaptureClear = liftIO . whenM (doesFileExist logCaptureFifo) $ removeFile logCaptureFifo logCaptureSetEnvVar key value = do logMsg @"cam" Info $ "Setting env variable: " <> key <> "=" <> value liftIO $ setEnv key value - logCaptureLineSeverity logLine = maybe Trace fromLibCameraSeverity $ words logLine !? 2 >>= readMaybe logCaptureAlloc = do logCaptureClear logMsg @"cam" Info $ "Creating libcamera log capture FIFO at: " <> logCaptureFifo @@ -166,7 +169,15 @@ runCam action = do logCaptureSetEnvVar "LIBCAMERA_LOG_LEVELS" $ "*:" <> show libCameraSeverity loggerIO <- makeLoggerIO @"libcamera" logMsg @"cam" Info "Starting libcamera log capture" - liftIO . forkIO . withFile logCaptureFifo ReadWriteMode $ \handle -> forever $ hGetLine handle >>= \logLine -> loggerIO (logCaptureLineSeverity logLine) logLine + -- Thread handles multiline logs by maintaining severity state between lines. + -- When a new line doesn't contain a parsable severity level, the previous + -- line's level is reused to ensure continuous log context. + liftIO . forkIO . withFile logCaptureFifo ReadWriteMode $ \handle -> + flip iterateM_ DEBUG $ \previousSeverity -> do + logLine <- hGetLine handle + flip (maybe $ return previousSeverity) (words logLine !? 2 >>= readMaybe) $ \severity -> do + loggerIO (fromLibCameraSeverity severity) logLine + return severity logCaptureDealloc = do logMsg @"cam" Info "Removing libcamera log capture FIFO" logCaptureClear diff --git a/hsm-cam/hsm-cam.cabal b/hsm-cam/hsm-cam.cabal index 892a5fe..7dd0dab 100644 --- a/hsm-cam/hsm-cam.cabal +++ b/hsm-cam/hsm-cam.cabal @@ -13,6 +13,7 @@ library , directory , effectful-core , effectful-plugin + , extra , hsm-core , hsm-log , JuicyPixels @@ -46,6 +47,7 @@ executable test-cam , effectful , effectful-core , effectful-plugin + , extra , hsm-core , hsm-log , JuicyPixels |