/**
 * @file hv_drv_OsdEffect.c
 * @brief Osd Driver Effect c file.
 * @details This file provides the following functions: \n
 *          (1) font Blink effect functions \n
 *          (2) font fade in/out functions \n
 *          (3) font shutter in/out functions \n
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-31
 * @copyright Copyright(c),2022-8, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author         <th>Date        <th>Change Description
 * <tr><td>OSD Group      <td>2022-08-31   <td>create
 * </table>
*/

#include "hv_drv_Osd.h"
#include "hv_cal_Osd.h"

VOID Hv_Drv_OsdEffect_SetBlinkPeriod(OsdWindowPhyId enId, USHORT16 usMs)
{
    USHORT16 usFrames = 0;
    UCHAR8 ucFrameSet = 0;

    usFrames = (usMs * PANEL_FRAME_RATE_TYP) / 1000;
    HV_ASSERT_TRUE_VOID(usFrames != 0);

    if (usFrames >= 15)
    {
        ucFrameSet = (usFrames - 7) >> 3;
    }

    OSD_DRV_LOG_DEBUG("Osd Blink Period set to %u frames",
            (ucFrameSet << 3) + 7);

    Hv_Drv_OsdWindow_SetCurrentWindow(enId);
    Hv_Cal_OsdWindow_SetFontBlinkRate(ucFrameSet);
}

VOID Hv_Drv_OsdEffect_SetBlinkDuration(OsdWindowPhyId enId, OsdBlinkDuty enDuty)
{
    OSD_DRV_LOG_DEBUG("Osd Blink duration set to %d", enDuty);

    Hv_Drv_OsdWindow_SetCurrentWindow(enId);
    Hv_Cal_OsdWindow_SetFontBlinkDuty(enDuty);
}

VOID Hv_Drv_OsdEffect_SetBlinkEnable(OsdWindowPhyId enId, USHORT16 usY, BOOL bEnable)
{
    OsdRowInfo *pstRowInfo = NULL;
    OsdIndexRow *pstRow = NULL;

    Hv_Drv_OsdWindow_SetCurrentWindow(enId);
    pstRowInfo = Hv_Drv_OsdFont_GetRowInfoByYaxis(usY, HV_FALSE);
    if (NULL == pstRowInfo)
    {
        OSD_DRV_LOG_ERROR("could not find Row @ %u", usY);
        return;
    }
    pstRow = Hv_Drv_OsdIndex_GetRow(pstRowInfo->ucRowIndex);
    if (NULL == pstRow)
    {
        OSD_DRV_LOG_ERROR("could not find RowIndex @ %u", usY);
        return;
    }

    Hv_Cal_OsdIndex_SetRowBlinkEnable(pstRow, bEnable);

    Hv_Drv_OsdIndex_SetChangeRange(OSD_SRAM_CHANGE_PARTIAL,
            (UINT32)pstRowInfo->pucColumnStart,
            (UINT32)(pstRowInfo->pucColumnStart
                + OSD_INDEX_COLUMN_SIZE * pstRowInfo->ucRowLen));
}

VOID Hv_Drv_OsdEffect_SetFadeInOutSpeed(OsdWindowPhyId enId, USHORT16 usMs)
{
    USHORT16 usFrames = 0;
    UCHAR8 ucFrameInterval = 0;
    UCHAR8 ucDelta = 0;

    if (usMs < 1000)
    {
        OSD_DRV_LOG_WARN("Warning: Osd set fade in/out time less than 1 second");
    }

    usFrames = (usMs * PANEL_FRAME_RATE_TYP) / 1000;
    HV_ASSERT_TRUE_VOID(usFrames != 0);

    ucFrameInterval = usFrames >= 32 ? 31: (usFrames - 1);
    ucDelta = 256 / (ucFrameInterval + 1);

    Hv_Drv_OsdWindow_SetCurrentWindow(enId);
    Hv_Cal_OsdWindow_SetFontTransSpeed(ucDelta);
    Hv_Cal_OsdWindow_SetFontTransFrame(ucFrameInterval);

    OSD_DRV_LOG_DEBUG("osd fade in/out: change %u every %u frame",
            ucDelta, ucFrameInterval);
}

VOID Hv_Drv_OsdEffect_StartFadeInOut(OsdWindowPhyId enId, BOOL bIsShowing)
{
    OsdTransDirection enDirection = OSD_TRANS_DI_V_H;

    if (!bIsShowing)
    {
        enDirection = OSD_TRANS_DI_H;
    }

    Hv_Drv_OsdWindow_SetCurrentWindow(enId);
    Hv_Cal_OsdWindow_SetFontTransDirection(enDirection);
    Hv_Cal_OsdWindow_SetFontTransMode(OSD_TRANS_MODE_FADE);

    Hv_Cal_OsdWindow_SetFontEnable(bIsShowing);

    OSD_DRV_LOG_DEBUG("osd start fade %s", bIsShowing ? "in" : "out");
}

VOID Hv_Drv_OsdEffect_SetShutterSpeed(OsdWindowPhyId enId, USHORT16 usMs)
{
    USHORT16 usFrames = 0;
    UCHAR8 ucSteps = 0;
    UCHAR8 ucFrameInterval = 0;

    usFrames = (usMs * PANEL_FRAME_RATE_TYP) / 1000;
    HV_ASSERT_TRUE_VOID(usFrames != 0);

    ucSteps = OSD_SHUTTER_SEGMENT / OSD_SHUTTER_STEP;
    ucFrameInterval = usFrames / ucSteps;
    ucFrameInterval = ucFrameInterval == 0 ? 1 : ucFrameInterval;

    Hv_Drv_OsdWindow_SetCurrentWindow(enId);
    Hv_Cal_OsdWindow_SetFontTransSpeed(OSD_SHUTTER_STEP);
    Hv_Cal_OsdWindow_SetFontTransFrame(ucFrameInterval);
    Hv_Cal_OsdWindow_SetFontTransStartPos(OSD_SHUTTER_SEGMENT, OSD_SHUTTER_SEGMENT);

    OSD_DRV_LOG_DEBUG("osd shutter in/out: segment %u change %u every %u frame",
            OSD_SHUTTER_SEGMENT, OSD_SHUTTER_STEP, ucFrameInterval);
}

VOID Hv_Drv_OsdEffect_StartShutterInOut(OsdWindowPhyId enId, BOOL bIsShowing, OsdTransDirection enDirection)
{
    Hv_Drv_OsdWindow_SetCurrentWindow(enId);

    Hv_Cal_OsdWindow_SetFontTransDirection(enDirection);
    Hv_Cal_OsdWindow_SetFontTransMode(OSD_TRANS_MODE_SHUTTER);

    Hv_Cal_OsdWindow_SetFontEnable(bIsShowing);

    OSD_DRV_LOG_DEBUG("osd start shutter %s, direction %d",
            bIsShowing ? "in" : "out", enDirection);
}

VOID Hv_Drv_OsdEffect_SetShutter2(OsdWindowPhyId enId, UCHAR8 ucSpeed, OsdTransDirection enDirection)
{
    USHORT16 usWidth = 0;
    USHORT16 usHeight = 0;

    Hv_Drv_OsdWindow_SetCurrentWindow(enId);

    Hv_Cal_OsdWindow_SetFontTransSpeed(ucSpeed);
    Hv_Cal_OsdWindow_SetFontTransFrame(1);

    Hv_Cal_OsdWindow_GetSize(&usWidth, &usHeight);
    Hv_Cal_OsdWindow_SetFontTransStartPos(0, 0);
    Hv_Cal_OsdWindow_SetFontTransCen(usWidth >> 3, usHeight >> 3);

    Hv_Cal_OsdWindow_SetFontTransDirection(enDirection);
    Hv_Cal_OsdWindow_SetFontTransMode(OSD_TRANS_MODE_SHUTTER2);
}

VOID Hv_Drv_OsdEffect_DisableTranMode(OsdWindowPhyId enId)
{
    Hv_Drv_OsdWindow_SetCurrentWindow(enId);
    Hv_Cal_OsdWindow_SetFontTransMode(OSD_TRANS_MODE_NONE);
}

